Commit 745dc639 authored by Sajal Narang's avatar Sajal Narang Committed by GitHub

Merge pull request #216 from pulsejet/patch26

Prevent multiple calls to notifications API when changing fragments
parents 54ed0f93 92783298
......@@ -89,9 +89,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;
......@@ -147,22 +148,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 = Utils.getRetrofitInterface();
retrofitInterface.getNotifications(Utils.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() {
......@@ -458,7 +472,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