Commit c09b5ecd authored by Varun Patil's avatar Varun Patil

Update feed events global cache on changing going/interested

parent 9b5e97d6
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
package app.insti;
import android.os.Handler;
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;
}
/** Update existing or add */
public void updateCache(T t) {
for (int i = 0; i < cache.size(); i++) {
T cachedEvent = cache.get(i);
if (cachedEvent.equals(t)) {
cache.set(i, t);
return;
}
}
cache.add(t);
}
}
......@@ -5,9 +5,11 @@ import android.widget.ImageView;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import java.time.Instant;
import app.insti.api.model.Event;
public final class Utils {
public static UpdatableList<Event> eventCache = new UpdatableList<>();
public static final void loadImageWithPlaceholder(final ImageView imageView, final String url) {
Picasso.get()
.load(resizeImageUrl(url))
......
......@@ -7,6 +7,7 @@ import com.google.gson.annotations.SerializedName;
import java.sql.Timestamp;
import java.util.List;
import java.util.Objects;
public class Event {
@NonNull()
......@@ -223,4 +224,17 @@ public class Event {
public void setEventBigImage(boolean eventBigImage) {
this.eventBigImage = eventBigImage;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Event event = (Event) o;
return Objects.equals(eventID, event.eventID);
}
@Override
public int hashCode() {
return Objects.hash(eventID);
}
}
......@@ -40,9 +40,9 @@ import java.util.Date;
import java.util.List;
import app.insti.Constants;
import app.insti.Utils;
import app.insti.R;
import app.insti.ShareURLMaker;
import app.insti.Utils;
import app.insti.activity.MainActivity;
import app.insti.adapter.BodyAdapter;
import app.insti.api.RetrofitInterface;
......@@ -306,6 +306,9 @@ public class EventFragment extends BackHandledFragment {
event.setEventUserUes(endStatus);
setFollowButtonColors(endStatus);
// Update global memory cache
Utils.eventCache.updateCache(event);
}
}
......
......@@ -5,8 +5,6 @@ import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
......@@ -15,19 +13,16 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.gson.Gson;
import java.util.List;
import app.insti.ActivityBuffer;
import app.insti.Constants;
import app.insti.interfaces.ItemClickListener;
import app.insti.R;
import app.insti.Utils;
import app.insti.activity.MainActivity;
import app.insti.adapter.FeedAdapter;
import app.insti.api.RetrofitInterface;
import app.insti.api.response.NewsFeedResponse;
import app.insti.api.model.Event;
import app.insti.api.response.NewsFeedResponse;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
......@@ -42,7 +37,6 @@ public class FeedFragment extends BaseFragment {
private FloatingActionButton fab;
LinearLayoutManager mLayoutManager;
public static int index = -1, top = -1;
public static List<Event> eventList = null;
private FeedAdapter feedAdapter = null;
public FeedFragment() {
......@@ -78,10 +72,10 @@ public class FeedFragment extends BaseFragment {
fab = getView().findViewById(R.id.fab);
// Initialize the feed
if (eventList == null) {
if (Utils.eventCache.getCache() == null || Utils.eventCache.getCache().size() == 0) {
updateFeed();
} else {
displayEvents(eventList);
displayEvents(Utils.eventCache.getCache());
}
}
......@@ -111,8 +105,8 @@ public class FeedFragment extends BaseFragment {
@Override
public void onResponse(Call<NewsFeedResponse> call, Response<NewsFeedResponse> response) {
if (response.isSuccessful()) {
eventList = response.body().getEvents();
displayEvents(eventList);
Utils.eventCache.setCache(response.body().getEvents());
displayEvents(Utils.eventCache.getCache());
}
//Server Error
feedSwipeRefreshLayout.setRefreshing(false);
......
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