Commit cf0ba177 authored by Varun Patil's avatar Varun Patil

Cache the list of notifications

parent d4a42fd9
...@@ -4,25 +4,21 @@ import java.util.ArrayList; ...@@ -4,25 +4,21 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public class UpdatableList<T> extends ArrayList<T> { public class UpdatableList<T> extends ArrayList<T> {
private List<T> cache = new ArrayList<>(); /** Convert a list to updatable list */
public void setList(List<T> list) {
public List<T> getCache() { this.clear();
return cache; this.addAll(list);
}
public void setCache(List<T> mCache) {
cache = mCache;
} }
/** Update existing or add */ /** Update existing or add */
public void updateCache(T t) { public void updateCache(T t) {
for (int i = 0; i < cache.size(); i++) { for (int i = 0; i < this.size(); i++) {
T cachedT = cache.get(i); T cachedT = this.get(i);
if (cachedT.equals(t)) { if (cachedT.equals(t)) {
cache.set(i, t); this.set(i, t);
return; return;
} }
} }
cache.add(t); this.add(t);
} }
} }
...@@ -18,6 +18,7 @@ import app.insti.activity.MainActivity; ...@@ -18,6 +18,7 @@ import app.insti.activity.MainActivity;
import app.insti.api.RetrofitInterface; import app.insti.api.RetrofitInterface;
import app.insti.api.model.Body; import app.insti.api.model.Body;
import app.insti.api.model.Event; import app.insti.api.model.Event;
import app.insti.api.model.Notification;
import app.insti.api.model.User; import app.insti.api.model.User;
import app.insti.fragment.BodyFragment; import app.insti.fragment.BodyFragment;
import app.insti.fragment.EventFragment; import app.insti.fragment.EventFragment;
...@@ -25,6 +26,8 @@ import app.insti.fragment.UserFragment; ...@@ -25,6 +26,8 @@ import app.insti.fragment.UserFragment;
public final class Utils { public final class Utils {
public static UpdatableList<Event> eventCache = new UpdatableList<>(); public static UpdatableList<Event> eventCache = new UpdatableList<>();
public static UpdatableList<Notification> notificationCache = null;
private static String sessionId; private static String sessionId;
private static RetrofitInterface retrofitInterface; private static RetrofitInterface retrofitInterface;
public static Gson gson; public static Gson gson;
......
...@@ -47,6 +47,7 @@ import java.util.List; ...@@ -47,6 +47,7 @@ import java.util.List;
import app.insti.Constants; import app.insti.Constants;
import app.insti.R; import app.insti.R;
import app.insti.SessionManager; import app.insti.SessionManager;
import app.insti.UpdatableList;
import app.insti.Utils; import app.insti.Utils;
import app.insti.api.EmptyCallback; import app.insti.api.EmptyCallback;
import app.insti.api.RetrofitInterface; import app.insti.api.RetrofitInterface;
...@@ -72,6 +73,7 @@ import app.insti.fragment.QuickLinksFragment; ...@@ -72,6 +73,7 @@ import app.insti.fragment.QuickLinksFragment;
import app.insti.fragment.SettingsFragment; import app.insti.fragment.SettingsFragment;
import app.insti.fragment.TrainingBlogFragment; import app.insti.fragment.TrainingBlogFragment;
import app.insti.fragment.UserFragment; import app.insti.fragment.UserFragment;
import okhttp3.internal.Util;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Response; import retrofit2.Response;
...@@ -95,8 +97,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On ...@@ -95,8 +97,6 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
private User currentUser; private User currentUser;
private BackHandledFragment selectedFragment; private BackHandledFragment selectedFragment;
private Menu menu; private Menu menu;
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
...@@ -186,7 +186,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On ...@@ -186,7 +186,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
*/ */
private void fetchNotifications() { private void fetchNotifications() {
// Try memory cache // Try memory cache
if (notifications != null) { if (Utils.notificationCache != null) {
showNotifications(); showNotifications();
return; return;
} }
...@@ -197,7 +197,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On ...@@ -197,7 +197,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
@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()) {
notifications = response.body(); Utils.notificationCache = new UpdatableList<>();
Utils.notificationCache.setList(response.body());
showNotifications(); showNotifications();
} }
} }
...@@ -208,7 +209,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On ...@@ -208,7 +209,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
* Show the right notification icon * Show the right notification icon
*/ */
private void showNotifications() { private void showNotifications() {
if (notifications != null && !notifications.isEmpty()) { if (Utils.notificationCache != null && !Utils.notificationCache.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);
......
...@@ -17,6 +17,7 @@ import java.util.List; ...@@ -17,6 +17,7 @@ import java.util.List;
import app.insti.ActivityBuffer; import app.insti.ActivityBuffer;
import app.insti.R; import app.insti.R;
import app.insti.UpdatableList;
import app.insti.Utils; import app.insti.Utils;
import app.insti.activity.MainActivity; import app.insti.activity.MainActivity;
import app.insti.adapter.FeedAdapter; import app.insti.adapter.FeedAdapter;
...@@ -72,10 +73,10 @@ public class FeedFragment extends BaseFragment { ...@@ -72,10 +73,10 @@ public class FeedFragment extends BaseFragment {
fab = getView().findViewById(R.id.fab); fab = getView().findViewById(R.id.fab);
// Initialize the feed // Initialize the feed
if (Utils.eventCache.getCache() == null || Utils.eventCache.getCache().size() == 0) { if (Utils.eventCache == null || Utils.eventCache.isEmpty()) {
updateFeed(); updateFeed();
} else { } else {
displayEvents(Utils.eventCache.getCache()); displayEvents(Utils.eventCache);
} }
} }
...@@ -105,8 +106,8 @@ public class FeedFragment extends BaseFragment { ...@@ -105,8 +106,8 @@ public class FeedFragment extends BaseFragment {
@Override @Override
public void onResponse(Call<NewsFeedResponse> call, Response<NewsFeedResponse> response) { public void onResponse(Call<NewsFeedResponse> call, Response<NewsFeedResponse> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
Utils.eventCache.setCache(response.body().getEvents()); Utils.eventCache.setList(response.body().getEvents());
displayEvents(Utils.eventCache.getCache()); displayEvents(Utils.eventCache);
} }
//Server Error //Server Error
feedSwipeRefreshLayout.setRefreshing(false); feedSwipeRefreshLayout.setRefreshing(false);
......
...@@ -14,10 +14,13 @@ import android.view.ViewGroup; ...@@ -14,10 +14,13 @@ import android.view.ViewGroup;
import java.util.List; import java.util.List;
import app.insti.R; import app.insti.R;
import app.insti.UpdatableList;
import app.insti.Utils; import app.insti.Utils;
import app.insti.adapter.NotificationsAdapter; import app.insti.adapter.NotificationsAdapter;
import app.insti.api.EmptyCallback;
import app.insti.api.RetrofitInterface; import app.insti.api.RetrofitInterface;
import app.insti.api.model.Notification; import app.insti.api.model.Notification;
import okhttp3.internal.Util;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; import retrofit2.Callback;
import retrofit2.Response; import retrofit2.Response;
...@@ -28,8 +31,7 @@ import retrofit2.Response; ...@@ -28,8 +31,7 @@ import retrofit2.Response;
public class NotificationsFragment extends BottomSheetDialogFragment { public class NotificationsFragment extends BottomSheetDialogFragment {
RecyclerView notificationsRecyclerView; RecyclerView notificationsRecyclerView;
NotificationsAdapter notificationsAdapter = null;
List<Notification> notifications;
public NotificationsFragment() { public NotificationsFragment() {
// Required empty public constructor // Required empty public constructor
...@@ -47,19 +49,23 @@ public class NotificationsFragment extends BottomSheetDialogFragment { ...@@ -47,19 +49,23 @@ public class NotificationsFragment extends BottomSheetDialogFragment {
public void onStart() { public void onStart() {
super.onStart(); super.onStart();
/* Show cached notifications */
if (Utils.notificationCache != null) {
showNotifications(Utils.notificationCache);
} else {
Utils.notificationCache = new UpdatableList<>();
}
/* Update notifications */
RetrofitInterface retrofitInterface = Utils.getRetrofitInterface(); RetrofitInterface retrofitInterface = Utils.getRetrofitInterface();
retrofitInterface.getNotifications(Utils.getSessionIDHeader()).enqueue(new Callback<List<Notification>>() { retrofitInterface.getNotifications(Utils.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()) {
notifications = response.body(); Utils.notificationCache.setList(response.body());
showNotifications(notifications); showNotifications(Utils.notificationCache);
} }
} }
@Override
public void onFailure(Call<List<Notification>> call, Throwable t) {
}
}); });
} }
...@@ -70,9 +76,15 @@ public class NotificationsFragment extends BottomSheetDialogFragment { ...@@ -70,9 +76,15 @@ public class NotificationsFragment extends BottomSheetDialogFragment {
/* Hide loader */ /* Hide loader */
getView().findViewById(R.id.loadingPanel).setVisibility(View.GONE); getView().findViewById(R.id.loadingPanel).setVisibility(View.GONE);
NotificationsAdapter notificationsAdapter = new NotificationsAdapter(notifications, this); /* Initialize */
if (notificationsAdapter == null) {
notificationsAdapter = new NotificationsAdapter(notifications, this);
notificationsRecyclerView = (RecyclerView) getView().findViewById(R.id.notifications_recycler_view); notificationsRecyclerView = (RecyclerView) getView().findViewById(R.id.notifications_recycler_view);
notificationsRecyclerView.setAdapter(notificationsAdapter); notificationsRecyclerView.setAdapter(notificationsAdapter);
notificationsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); notificationsRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
} else {
notificationsAdapter.setList(notifications);
notificationsAdapter.notifyDataSetChanged();
}
} }
} }
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