Commit b56f5315 authored by Roger Barton's avatar Roger Barton
Browse files

Added saving of events/jobs between sessions

parent 0bdaf281
......@@ -98,9 +98,6 @@ public class ListFragment extends Fragment {
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
if(!(MainActivity.instance instanceof MainActivity))
return;
if(pagePosition == PageType.EVENTS)
Requests.FetchEventList(MainActivity.instance, onEventsListUpdatedCallback, cancelRefreshCallback, "");
else if (pagePosition == PageType.JOBS)
......@@ -111,9 +108,6 @@ public class ListFragment extends Fragment {
swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
if(!(MainActivity.instance instanceof MainActivity))
return;
if(pagePosition == PageType.EVENTS) {
SetRefreshUI(true);
Requests.FetchEventList(MainActivity.instance, onEventsListUpdatedCallback, cancelRefreshCallback, "");
......@@ -136,9 +130,9 @@ public class ListFragment extends Fragment {
// specify an adapter (see also next example)
if(pagePosition == PageType.EVENTS)
recyclerAdapter = new EventsListAdapter(((Activity) MainActivity.instance));
recyclerAdapter = new EventsListAdapter((MainActivity.instance));
else if (pagePosition == PageType.JOBS)
recyclerAdapter = new JobListAdapter((Activity) MainActivity.instance);
recyclerAdapter = new JobListAdapter(MainActivity.instance);
if(recyclerAdapter != null) {
recyclerView.setLayoutAnimation(AnimationUtils.loadLayoutAnimation(MainActivity.instance, R.anim.layout_anim_falldown));
......@@ -157,8 +151,9 @@ public class ListFragment extends Fragment {
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { }
});
}
RefreshList(true);
}
public void RefreshList(boolean animate)
......
......@@ -97,6 +97,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
bottomNavigation = findViewById(R.id.bottomNav);
bottomNavigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
PersistentStorage.LoadEvents(getApplicationContext());
PersistentStorage.LoadJobs(getApplicationContext());
InitialisePageView();
new Settings(getApplicationContext()); //creates the settings instance, so we can store/retrieve shared preferences
......
......@@ -98,7 +98,7 @@ public final class Requests {
public void run() {
try {
if(eventId.isEmpty())
Events.UpdateEventInfos(json.getJSONArray("_items"));
Events.UpdateEventInfos(context, json.getJSONArray("_items"));
else
Events.UpdateSingleEvent(json, eventId);
if(callback != null)
......@@ -263,7 +263,7 @@ public final class Requests {
public void run() {
try {
if(jobId.isEmpty())
Jobs.UpdateJobInfos(json.getJSONArray("_items"));
Jobs.UpdateJobInfos(context, json.getJSONArray("_items"));
else
Jobs.UpdateSingleJob(json, jobId);
if(callback != null)
......
package ch.amiv.android_app.events;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
......@@ -25,6 +26,8 @@ import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -407,6 +410,16 @@ public class EventDetailActivity extends AppCompatActivity {
params.put("email", UserInfo.current.email);
else
params.put("user", UserInfo.current._id);
//encode signup form values
/* Add when fully tested XXX
SharedPreferences prefs = getSharedPreferences(Settings.SHARED_PREFS_KEY, MODE_PRIVATE);
Gson gson = new Gson();
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("food_preference", prefs.getString(getResources().getString(R.string.pref_food_key), ""));
jsonObject.addProperty("sbb_abo", prefs.getString(getResources().getString(R.string.pref_sbb_key), ""));
params.put("additional_fields", jsonObject.getAsString());
*/
return params;
}
};
......
package ch.amiv.android_app.events;
import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
......@@ -14,6 +15,8 @@ import java.util.Comparator;
import java.util.Date;
import java.util.List;
import ch.amiv.android_app.util.PersistentStorage;
/**
* This is the central place for storing information about the events, events + signups.
*/
......@@ -38,17 +41,9 @@ public final class Events {
* Update the event infos with the data received from the api. This is just for updating information about the event NOT the signup
* @param json json array of the events.
*/
public static void UpdateEventInfos(JSONArray json)
public static void UpdateEventInfos(Context context, JSONArray json)
{
boolean isInitialising = eventInfos.size() == 0;
if(isInitialising){
for (int k = 0; k < EventGroup.SIZE; k++)
sortedEvents.add(new ArrayList<EventInfo>());
}
else {
for (int k = 0; k < sortedEvents.size(); k++)
sortedEvents.get(k).clear();
}
for (int i = 0; i < json.length(); i++)
{
......@@ -65,6 +60,22 @@ public final class Events {
}
}
GenerateSortedLists(isInitialising);
PersistentStorage.SaveEvents(context);
}
public static void GenerateSortedLists(boolean isInitialising)
{
if(isInitialising){
for (int k = 0; k < EventGroup.SIZE; k++)
sortedEvents.add(new ArrayList<EventInfo>());
}
else {
for (int k = 0; k < sortedEvents.size(); k++)
sortedEvents.get(k).clear();
}
//Sort list and update sorted list
if(!eventInfos.isEmpty()){
//sort so first elem has an advertising start date furthest in the future
......
package ch.amiv.android_app.jobs;
import android.content.Context;
import android.support.annotation.NonNull;
import org.json.JSONArray;
import org.json.JSONException;
......@@ -11,6 +12,8 @@ import java.util.Comparator;
import java.util.Date;
import java.util.List;
import ch.amiv.android_app.util.PersistentStorage;
/**
* This holds all the data about the job offers similar to the events class, for more explanations see the Events class
*/
......@@ -34,18 +37,10 @@ public class Jobs {
* Update the list of job offers with a json from the api
* @param json json array of the events.
*/
public static void UpdateJobInfos(JSONArray json)
public static void UpdateJobInfos(Context context, JSONArray json)
{
//initialise lists first or clear them
boolean isInitialising = jobInfos.size() == 0;
if(isInitialising){
for (int k = 0; k < JobGroup.SIZE; k++)
sortedJobs.add(new ArrayList<JobInfo>());
}
else {
for (int k = 0; k < sortedJobs.size(); k++)
sortedJobs.get(k).clear();
}
for (int i = 0; i < json.length(); i++)
{
......@@ -62,6 +57,22 @@ public class Jobs {
}
}
GenerateSortedLists(isInitialising);
PersistentStorage.SaveJobs(context);
}
public static void GenerateSortedLists(boolean isInitialising)
{
if(isInitialising){
for (int k = 0; k < JobGroup.SIZE; k++)
sortedJobs.add(new ArrayList<JobInfo>());
}
else {
for (int k = 0; k < sortedJobs.size(); k++)
sortedJobs.get(k).clear();
}
//Sort list
if(!jobInfos.isEmpty()){
//sort so first elem has an end date furthest in the future
......@@ -92,12 +103,12 @@ public class Jobs {
/**
* Will update a given event with the id
* @param json
* @param eventId
* @param jobId
* @return true if the event was found and updated
*/
public static boolean UpdateSingleJob(JSONObject json, @NonNull String eventId){
public static boolean UpdateSingleJob(JSONObject json, @NonNull String jobId){
for (int i = 0; i < jobInfos.size(); i++){
if(jobInfos.get(i)._id.equalsIgnoreCase(eventId)) {
if(jobInfos.get(i)._id.equalsIgnoreCase(jobId)) {
jobInfos.get(i).UpdateJob(json);
return true;
}
......
......@@ -8,8 +8,11 @@ import android.support.v4.content.ContextCompat;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
......@@ -19,10 +22,16 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Vector;
import ch.amiv.android_app.core.Settings;
import ch.amiv.android_app.core.UserInfo;
import ch.amiv.android_app.events.EventInfo;
import ch.amiv.android_app.events.Events;
import ch.amiv.android_app.jobs.JobInfo;
import ch.amiv.android_app.jobs.Jobs;
import static android.content.Context.MODE_PRIVATE;
......@@ -31,7 +40,11 @@ import static android.content.Context.MODE_PRIVATE;
*/
public final class PersistentStorage {
private static SharedPreferences prefs;
private static final String userKey= "user";
private static final String userKey = "user";
private static final String eventsKey = "events";
private static final Type eventListType = new TypeToken<List<EventInfo>>() {}.getType();
private static final String jobsKey = "jobs";
private static final Type jobListType = new TypeToken<List<JobInfo>>() {}.getType();
private static void Init(Context context){
if(prefs == null)
......@@ -69,11 +82,101 @@ public final class PersistentStorage {
return true;
}
/**
* Clears the stored userinfo, done in async
* @param context
*/
public static void ClearUser(Context context)
{
Init(context);
SharedPreferences.Editor prefsEditor = prefs.edit();
prefsEditor.putString(userKey, "");
prefsEditor.apply(); //use commit() to run in serial
}
//region - Events
public static void SaveEvents(Context context)
{
Init(context);
SharedPreferences.Editor prefsEditor = prefs.edit();
Gson gson = new Gson();
String json = gson.toJson(Events.eventInfos, eventListType);
prefsEditor.putString(eventsKey, json);
prefsEditor.apply();
}
public static boolean LoadEvents (Context context)
{
Init(context);
if(!prefs.contains(eventsKey))
return false;
String json = prefs.getString(eventsKey, "");
if(json.isEmpty())
return false;
try {
Gson gson = new Gson();
Events.eventInfos = gson.fromJson(json, eventListType);
Events.GenerateSortedLists(true);
}
catch (Exception e){ //This may happen if the userinfo class changes
e.printStackTrace();
return false;
}
return true;
}
public static void ClearEvents(Context context)
{
Init(context);
SharedPreferences.Editor prefsEditor = prefs.edit();
prefsEditor.putString(eventsKey, "");
prefsEditor.apply();
}
//endregion
//region - Jobs
public static void SaveJobs(Context context)
{
Init(context);
SharedPreferences.Editor prefsEditor = prefs.edit();
Gson gson = new Gson();
String json = gson.toJson(Jobs.jobInfos, jobListType);
prefsEditor.putString(jobsKey, json);
prefsEditor.apply();
}
public static boolean LoadJobs (Context context)
{
Init(context);
if(!prefs.contains(jobsKey))
return false;
String json = prefs.getString(jobsKey, "");
if(json.isEmpty())
return false;
try {
Gson gson = new Gson();
Jobs.jobInfos = gson.fromJson(json, jobListType);
Jobs.GenerateSortedLists(true);
}
catch (Exception e){ //This may happen if the userinfo class changes
e.printStackTrace();
return false;
}
return true;
}
public static void ClearJobs(Context context)
{
Init(context);
SharedPreferences.Editor prefsEditor = prefs.edit();
prefsEditor.putString(jobsKey, "");
prefsEditor.apply();
}
//endregion
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment