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
SessionManager session;
FeedFragment feedFragment;
private User currentUser;
private boolean showNotifications = false;
private BackHandledFragment selectedFragment;
private Menu menu;
private RetrofitInterface retrofitInterface;
private List<Notification> notifications = null;
/** which menu item should be checked on activity start */
private int initMenuChecked = R.id.nav_feed;
......@@ -150,22 +150,35 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
checkLatestVersion();
}
/** Get the notifications from memory cache or network */
private void fetchNotifications() {
// Try memory cache
if (notifications != null) {
showNotifications();
return;
}
// Get from network
RetrofitInterface retrofitInterface = getRetrofitInterface();
retrofitInterface.getNotifications(getSessionIDHeader()).enqueue(new EmptyCallback<List<Notification>>() {
@Override
public void onResponse(Call<List<Notification>> call, Response<List<Notification>> response) {
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()) {
menu.findItem(R.id.action_notifications).setIcon(R.drawable.baseline_notifications_active_white_24);
} else {
menu.findItem(R.id.action_notifications).setIcon(R.drawable.ic_notifications_white_24dp);
}
}
}
});
}
/** Get version code we are currently on */
private int getCurrentVersion() {
......@@ -469,7 +482,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
int id = item.getItemId();
if (id == R.id.action_notifications) {
showNotifications = true;
NotificationsFragment notificationsFragment = new NotificationsFragment();
updateFragment(notificationsFragment);
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