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