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

Added selective refreshing of event/signup

parent 682a300f
Pipeline #4513 failed with stages
in 8 seconds
......@@ -78,6 +78,23 @@ public class EventDetailActivity extends AppCompatActivity {
}
};
public Requests.OnDataReceivedCallback onEventsListUpdatedCallback = new Requests.OnDataReceivedCallback() {
@Override
public void OnDataReceived() {
SetUIDirty(true, false);
Requests.FetchEventSignups(getApplicationContext(), onSignupsUpdatedCallback, cancelRefreshCallback, event()._id);
LoadEventImage(true);
}
};
private Requests.OnDataReceivedCallback onSignupsUpdatedCallback = new Requests.OnDataReceivedCallback() {
@Override
public void OnDataReceived() {
SetUIDirty(true, true);
swipeRefreshLayout.setRefreshing(false);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -99,7 +116,7 @@ public class EventDetailActivity extends AppCompatActivity {
public void OnDataReceived() {
UpdateRegisterButton();
}
}, "");
}, null, "");
responseIntent.putExtra("login_success", refreshLogin);
ScrollToBottom(null);
RegisterForEvent(null);
......@@ -148,8 +165,6 @@ public class EventDetailActivity extends AppCompatActivity {
}
//Link up variables with UI elements from the layout xml
((TextView) findViewById(R.id.eventTitle)).setText(event().GetTitle(getResources()));
((TextView)findViewById(R.id.eventDetail)).setText(event().GetDescription(getResources()));
scrollView = findViewById(R.id.scrollView_event);
posterProgress = findViewById(R.id.progressBar);
posterImage = findViewById(R.id.eventPoster);
......@@ -161,14 +176,25 @@ public class EventDetailActivity extends AppCompatActivity {
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { //This sets what function is called when we swipe down to refresh
@Override
public void onRefresh() {
LoadEventImage(true); //XXX fetch this specific event from the server again
if(!hasEvent())
return;
Requests.FetchEventList(getApplicationContext(), onEventsListUpdatedCallback, cancelRefreshCallback, event()._id);
}
});
AddRegisterInfos();
UpdateRegisterButton();
SetUIDirty(false, false);
}
LoadEventImage(false);
public void SetUIDirty(boolean isRefreshing, boolean signupUpdated)
{
if(!signupUpdated) {
((TextView) findViewById(R.id.eventTitle)).setText(event().GetTitle(getResources()));
((TextView) findViewById(R.id.eventDetail)).setText(event().GetDescription(getResources()));
LoadEventImage(isRefreshing);
AddRegisterInfos();
}
UpdateRegisterButton();
}
/**
......@@ -191,7 +217,6 @@ public class EventDetailActivity extends AppCompatActivity {
else
{
if(!isRefreshing || posterImage.getDrawable() == null) {
swipeRefreshLayout.setRefreshing(false);
posterProgress.setVisibility(View.VISIBLE);
posterImage.setVisibility(View.VISIBLE);
posterBg.setVisibility(View.VISIBLE);
......@@ -241,7 +266,6 @@ public class EventDetailActivity extends AppCompatActivity {
posterMask.setVisibility(View.VISIBLE);
posterProgress.setVisibility(View.GONE);
swipeRefreshLayout.setRefreshing(false);
}
});
}
......@@ -250,18 +274,14 @@ public class EventDetailActivity extends AppCompatActivity {
public void onErrorResponse(VolleyError error) {
Snackbar.make(posterImage, R.string.error_image_load, Snackbar.LENGTH_SHORT).show();
posterProgress.setVisibility(View.GONE);
swipeRefreshLayout.setRefreshing(false);
}
});
if(!Requests.SendRequest(posterRequest, getApplicationContext())){
//Only enter here if the request was not sent, usually because of missing internet
posterProgress.setVisibility(View.GONE);
swipeRefreshLayout.setRefreshing(false);
}
}
swipeRefreshLayout.setRefreshing(false);
}
/**
......@@ -325,7 +345,7 @@ public class EventDetailActivity extends AppCompatActivity {
public void OnDataReceived() {
UpdateRegisterButton();
}
}, event()._id);
}, null, event()._id);
//Interpret notification to show from the signup
int notification = 0;
......
......@@ -64,7 +64,15 @@ public class EventInfo {
public CheckinState checked_in = CheckinState.none;
//endregion
public EventInfo(JSONObject json)
public EventInfo(JSONObject json) {
UpdateEvent(json);
}
/**
* Overwrite the current data
* @param json
*/
public void UpdateEvent(JSONObject json)
{
_id = json.optString("_id");
title_en = json.optString("title_en");
......@@ -162,7 +170,7 @@ public class EventInfo {
*/
public ArrayList<String[]> GetInfos(Resources r){
if(infos != null && infos.size() > 0)
return infos;
infos.clear();
DateFormat dateFormat = new SimpleDateFormat("dd - MMM - yyyy HH:mm", r.getConfiguration().locale);
if(time_start != null) infos.add(new String[]{ r.getString(R.string.start_time), dateFormat.format(time_start)});
......
package ch.amiv.android_app.core;
import android.support.annotation.NonNull;
import android.util.Log;
import org.json.JSONArray;
......@@ -82,6 +83,18 @@ public final class Events {
}
}
/**
* Do not use this to create a single event, will update a given event with the id
* @param json
* @param eventId
*/
public static void UpdateSingleEvent(JSONObject json, @NonNull String eventId){
for (int i = 0; i < eventInfos.size(); i++){
if(eventInfos.get(i)._id.equalsIgnoreCase(eventId))
eventInfos.get(i).UpdateEvent(json);
}
}
/**
* Add signups to their events, use this when the data is received from the api in a jsonArray
* @param json
......
......@@ -63,7 +63,7 @@ public class ListFragment extends Fragment {
@Override
public void onRefresh() {
if(pagePosition == 0 && getActivity() instanceof MainActivity)
Requests.FetchEventList(getContext(), ((MainActivity)getActivity()).onEventsListUpdatedCallback, cancelRefreshCallback);
Requests.FetchEventList(getContext(), ((MainActivity)getActivity()).onEventsListUpdatedCallback, cancelRefreshCallback, "");
}
});
//refresh on activity start
......@@ -72,7 +72,7 @@ public class ListFragment extends Fragment {
public void run() {
if(pagePosition == 0 && getActivity() instanceof MainActivity) {
swipeRefreshLayout.setRefreshing(true);
Requests.FetchEventList(getContext(), ((MainActivity)getActivity()).onEventsListUpdatedCallback, cancelRefreshCallback);
Requests.FetchEventList(getContext(), ((MainActivity)getActivity()).onEventsListUpdatedCallback, cancelRefreshCallback, "");
}
}
});
......
......@@ -37,7 +37,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
public Requests.OnDataReceivedCallback onEventsListUpdatedCallback = new Requests.OnDataReceivedCallback() {
@Override
public void OnDataReceived() {
Requests.FetchEventSignups(getApplicationContext(), onSignupsUpdatedCallback, "");
Requests.FetchEventSignups(getApplicationContext(), onSignupsUpdatedCallback, null, "");
pagerAdapter.RefreshCurrentList(true);
}
};
......@@ -161,7 +161,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
SetLoginUIDirty();
System.gc();//run garbage collector explicitly to clean up user data
Requests.FetchEventList(getApplicationContext(), onEventsListUpdatedCallback, null);
Requests.FetchEventList(getApplicationContext(), onEventsListUpdatedCallback, null, "");
}
/**
......@@ -243,9 +243,9 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
SetLoginUIDirty();
//Update events and signups with the new userinfo
if(Events.eventInfos.size() > 0)
Requests.FetchEventSignups(getApplicationContext(), onSignupsUpdatedCallback, "");
Requests.FetchEventSignups(getApplicationContext(), onSignupsUpdatedCallback, null, "");
else
Requests.FetchEventList(getApplicationContext(), onEventsListUpdatedCallback, null);
Requests.FetchEventList(getApplicationContext(), onEventsListUpdatedCallback, null, "");
}
});
......
......@@ -63,16 +63,19 @@ public final class Requests {
/**
* Will fetch the list of events from the server, note does not require an access token.
* @param errorCallback Use this to know when an error occured to stop loading animations etc
* @param eventId to only fetch for a specific event id add this, else set as emoty
* @return True if the request was sent.
*/
public static void FetchEventList(final Context context, final OnDataReceivedCallback callback, final OnDataReceivedCallback errorCallback)
public static void FetchEventList(final Context context, final OnDataReceivedCallback callback, final OnDataReceivedCallback errorCallback, @NonNull final String eventId)
{
if(!CheckConnection(context)) {
RunCallback(errorCallback);
return;
}
String url = Settings.API_URL + "events";
String url = Settings.API_URL + "events" + (eventId.isEmpty() ? "" : "/" + eventId);
Log.e("request", "url: " + url);
StringRequest request = new StringRequest(Request.Method.GET, url,null, null)
{
@Override
......@@ -81,21 +84,28 @@ public final class Requests {
Log.e("request", "fetch events status Code: " + response.statusCode);
try {
final JSONArray eventArrayJson = new JSONObject(new String(response.data)).getJSONArray("_items");
final JSONObject json = new JSONObject(new String(response.data));
//Update events on main thread
if(callback != null) {
Runnable runnable = new Runnable() {
@Override
public void run() {
Events.UpdateEventInfos(eventArrayJson);
try {
if(eventId.isEmpty())
Events.UpdateEventInfos(json.getJSONArray("_items"));
else
Events.UpdateSingleEvent(json, eventId);
} catch (JSONException e) {
e.printStackTrace();
}
callback.OnDataReceived();
}
};
callbackHandler.post(runnable);
}
Log.e("request", eventArrayJson.toString());
Log.e("request", new JSONObject(new String(response.data)).toString());
} catch (JSONException e) {
RunCallback(errorCallback);
e.printStackTrace();
......@@ -144,10 +154,12 @@ public final class Requests {
* Will fetch the event signups for the current user and save them in the eventInfos list
* @param eventId to only fetch for a specific event id add this, else set as emoty
*/
public static void FetchEventSignups(final Context context, final OnDataReceivedCallback callback, @NonNull String eventId)
public static void FetchEventSignups(final Context context, final OnDataReceivedCallback callback, final OnDataReceivedCallback errorCallback, @NonNull String eventId)
{
if(!Settings.IsLoggedIn(context) || UserInfo.current == null || UserInfo.current._id.isEmpty())
if(!Settings.IsLoggedIn(context) || UserInfo.current == null || UserInfo.current._id.isEmpty()) {
RunCallback(errorCallback);
return;
}
String url = Settings.API_URL + "eventsignups?where={\"user\":\"" + UserInfo.current._id + "\"" + (eventId.isEmpty() ? "" : ",\"event\":\"" + eventId + "\"") + "}";
Log.e("request", "url: " + url);
......@@ -178,11 +190,14 @@ public final class Requests {
//Log.e("request", json.toString());
} catch (JSONException e) {
RunCallback(errorCallback);
e.printStackTrace();
}
}
else
else {
RunCallback(errorCallback);
Log.e("request", "Request returned null response: fetch event signups");
}
return super.parseNetworkResponse(response);
}
......@@ -193,6 +208,7 @@ public final class Requests {
else
Log.e("request", "Request returned null response: fetch event signups");
RunCallback(errorCallback);
return super.parseNetworkError(volleyError);
}
......@@ -209,7 +225,8 @@ public final class Requests {
}
};
boolean hasSent = Requests.SendRequest(request, context);
if(!Requests.SendRequest(request, context))
RunCallback(errorCallback);
}
/**
......
......@@ -15,8 +15,8 @@ import java.security.PublicKey;
public class Settings {
public static Settings instance;
public static final String API_URL = "http://192.168.1.105:5000/";
//public static final String API_URL = "https://api-dev.amiv.ethz.ch/";
//public static final String API_URL = "http://192.168.1.105:5000/";
public static final String API_URL = "https://api-dev.amiv.ethz.ch/";
//Vars for saving/reading the url from shared prefs, to allow saving between sessions. For each variable, have a key to access it and a default value
private static SharedPreferences sharedPrefs;
......
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