Commit e7aa0434 authored by Varun Patil's avatar Varun Patil

Prevent multiple calls to notifications API when changing fragments

Since this is a very expensive call, the data to be loaded in the
fragment has sluggish response when made in parallel to this call.
parent b200717f
...@@ -87,10 +87,10 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On ...@@ -87,10 +87,10 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
SessionManager session; SessionManager session;
FeedFragment feedFragment; FeedFragment feedFragment;
private User currentUser; private User currentUser;
private boolean showNotifications = false;
private BackHandledFragment selectedFragment; private BackHandledFragment selectedFragment;
private Menu menu; private Menu menu;
private RetrofitInterface retrofitInterface; private RetrofitInterface retrofitInterface;
private List<Notification> notifications = null;
/** which menu item should be checked on activity start */ /** which menu item should be checked on activity start */
private int initMenuChecked = R.id.nav_feed; private int initMenuChecked = R.id.nav_feed;
...@@ -150,22 +150,35 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On ...@@ -150,22 +150,35 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
checkLatestVersion(); checkLatestVersion();
} }
/** Get the notifications from memory cache or network */
private void fetchNotifications() { private void fetchNotifications() {
// Try memory cache
if (notifications != null) {
showNotifications();
return;
}
// Get from network
RetrofitInterface retrofitInterface = getRetrofitInterface(); RetrofitInterface retrofitInterface = getRetrofitInterface();
retrofitInterface.getNotifications(getSessionIDHeader()).enqueue(new EmptyCallback<List<Notification>>() { retrofitInterface.getNotifications(getSessionIDHeader()).enqueue(new EmptyCallback<List<Notification>>() {
@Override @Override
public void onResponse(Call<List<Notification>> call, Response<List<Notification>> response) { public void onResponse(Call<List<Notification>> call, Response<List<Notification>> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
List<Notification> notifications = response.body(); notifications = response.body();
showNotifications();
}
}
});
}
/** Show the right notification icon */
private void showNotifications() {
if (notifications != null && !notifications.isEmpty()) { if (notifications != null && !notifications.isEmpty()) {
menu.findItem(R.id.action_notifications).setIcon(R.drawable.baseline_notifications_active_white_24); menu.findItem(R.id.action_notifications).setIcon(R.drawable.baseline_notifications_active_white_24);
} else { } else {
menu.findItem(R.id.action_notifications).setIcon(R.drawable.ic_notifications_white_24dp); menu.findItem(R.id.action_notifications).setIcon(R.drawable.ic_notifications_white_24dp);
} }
} }
}
});
}
/** Get version code we are currently on */ /** Get version code we are currently on */
private int getCurrentVersion() { private int getCurrentVersion() {
...@@ -469,7 +482,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On ...@@ -469,7 +482,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
int id = item.getItemId(); int id = item.getItemId();
if (id == R.id.action_notifications) { if (id == R.id.action_notifications) {
showNotifications = true;
NotificationsFragment notificationsFragment = new NotificationsFragment(); NotificationsFragment notificationsFragment = new NotificationsFragment();
updateFragment(notificationsFragment); updateFragment(notificationsFragment);
return true; return true;
......
Markdown is supported
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