Commit 71db08b4 authored by Varun Patil's avatar Varun Patil

Use memory cache for body

parent 3b675d07
...@@ -40,6 +40,7 @@ import app.insti.fragment.UserFragment; ...@@ -40,6 +40,7 @@ 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; public static UpdatableList<Notification> notificationCache = null;
public static UpdatableList<Body> bodyCache = new UpdatableList<>();
private static String sessionId; private static String sessionId;
private static RetrofitInterface retrofitInterface; private static RetrofitInterface retrofitInterface;
......
...@@ -5,6 +5,7 @@ import android.support.annotation.NonNull; ...@@ -5,6 +5,7 @@ import android.support.annotation.NonNull;
import com.google.gson.annotations.SerializedName; import com.google.gson.annotations.SerializedName;
import java.util.List; import java.util.List;
import java.util.Objects;
import app.insti.interfaces.CardInterface; import app.insti.interfaces.CardInterface;
...@@ -199,4 +200,12 @@ public class Body implements CardInterface { ...@@ -199,4 +200,12 @@ public class Body implements CardInterface {
public String getAvatarUrl() { public String getAvatarUrl() {
return getBodyImageURL(); return getBodyImageURL();
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Body body = (Body) o;
return Objects.equals(bodyID, body.getBodyID());
}
} }
\ No newline at end of file
...@@ -136,10 +136,16 @@ public class BodyFragmentNew extends BackHandledFragment implements TransitionTa ...@@ -136,10 +136,16 @@ public class BodyFragmentNew extends BackHandledFragment implements TransitionTa
/* Initialize */ /* Initialize */
bodyDisplayed = false; bodyDisplayed = false;
body = min_body;
displayBody();
int index = Utils.bodyCache.indexOf(min_body);
if (index < 0) {
body = min_body;
updateBody(); updateBody();
} else {
body = Utils.bodyCache.get(index);
}
displayBody();
bodySwipeRefreshLayout = getActivity().findViewById(R.id.body_swipe_refresh_layout); bodySwipeRefreshLayout = getActivity().findViewById(R.id.body_swipe_refresh_layout);
bodySwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { bodySwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
...@@ -166,6 +172,7 @@ public class BodyFragmentNew extends BackHandledFragment implements TransitionTa ...@@ -166,6 +172,7 @@ public class BodyFragmentNew extends BackHandledFragment implements TransitionTa
public void onResponse(Call<Body> call, Response<Body> response) { public void onResponse(Call<Body> call, Response<Body> response) {
if (response.isSuccessful()) { if (response.isSuccessful()) {
Body bodyResponse = response.body(); Body bodyResponse = response.body();
Utils.bodyCache.updateCache(response.body());
if (!bodyDisplayed) { if (!bodyDisplayed) {
body = bodyResponse; body = bodyResponse;
...@@ -185,7 +192,7 @@ public class BodyFragmentNew extends BackHandledFragment implements TransitionTa ...@@ -185,7 +192,7 @@ public class BodyFragmentNew extends BackHandledFragment implements TransitionTa
private void displayBody() { private void displayBody() {
/* Skip if we're already destroyed */ /* Skip if we're already destroyed */
if (getActivity() == null || getView() == null) return; if (getActivity() == null || getView() == null) return;
if (!body.equals(min_body)) bodyDisplayed = true; if (!(body == min_body)) bodyDisplayed = true;
bodyPicture = (ImageView) getActivity().findViewById(R.id.body_picture); bodyPicture = (ImageView) getActivity().findViewById(R.id.body_picture);
...@@ -197,7 +204,7 @@ public class BodyFragmentNew extends BackHandledFragment implements TransitionTa ...@@ -197,7 +204,7 @@ public class BodyFragmentNew extends BackHandledFragment implements TransitionTa
} }
/* Skip for min body */ /* Skip for min body */
if (body.equals(min_body)) { if (body == min_body) {
return; return;
} }
......
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