New home cache strategy

This commit is contained in:
Thomas 2022-05-07 14:08:53 +02:00
parent 2ad650ba2f
commit 239d748553
2 changed files with 26 additions and 5 deletions

View File

@ -213,9 +213,9 @@ public class StatusCache {
if (max_id == null && min_id != null) { if (max_id == null && min_id != null) {
selection += "AND " + Sqlite.COL_STATUS_ID + " >= '" + min_id + "'"; selection += "AND " + Sqlite.COL_STATUS_ID + " >= '" + min_id + "'";
} else if (max_id != null && min_id == null) { } else if (max_id != null && min_id == null) {
selection += "AND " + Sqlite.COL_STATUS_ID + " <= '" + max_id + "'"; selection += "AND " + Sqlite.COL_STATUS_ID + " < '" + max_id + "'";
} else if (max_id != null) { } else if (max_id != null) {
selection += "AND " + Sqlite.COL_STATUS_ID + " >= '" + min_id + "' AND " + Sqlite.COL_STATUS_ID + " <= '" + max_id + "'"; selection += "AND " + Sqlite.COL_STATUS_ID + " >= '" + min_id + "' AND " + Sqlite.COL_STATUS_ID + " < '" + max_id + "'";
limit = null; limit = null;
} }
try { try {

View File

@ -576,6 +576,8 @@ public class FragmentMastodonTimeline extends Fragment {
Marker.MarkerContent markerContent = marker.home; Marker.MarkerContent markerContent = marker.home;
max_id = markerContent.last_read_id; max_id = markerContent.last_read_id;
min_id = markerContent.last_read_id; min_id = markerContent.last_read_id;
} else {
max_id = null;
} }
timelinesVM.getHome(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, max_id, null, null, MastodonHelper.statusesPerCall(requireActivity()), false) timelinesVM.getHome(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, max_id, null, null, MastodonHelper.statusesPerCall(requireActivity()), false)
.observe(getViewLifecycleOwner(), this::initializeStatusesCommonView); .observe(getViewLifecycleOwner(), this::initializeStatusesCommonView);
@ -596,15 +598,34 @@ public class FragmentMastodonTimeline extends Fragment {
} }
} else if (direction == DIRECTION.BOTTOM) { } else if (direction == DIRECTION.BOTTOM) {
if (networkAvailable == BaseMainActivity.status.CONNECTED) { if (networkAvailable == BaseMainActivity.status.CONNECTED) {
timelinesVM.getHome(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, max_id, null, null, MastodonHelper.statusesPerCall(requireActivity()), false) //We first if we get results from cache
.observe(getViewLifecycleOwner(), statusesBottom -> dealWithPagination(statusesBottom, DIRECTION.BOTTOM)); timelinesVM.getHomeCache(BaseMainActivity.currentInstance, BaseMainActivity.currentUserID, max_id, null)
.observe(getViewLifecycleOwner(), statusesBottomCache -> {
if (statusesBottomCache != null && statusesBottomCache.statuses != null && statusesBottomCache.statuses.size() > 0) {
dealWithPagination(statusesBottomCache, DIRECTION.BOTTOM);
} else { // If not, we fetch remotely
timelinesVM.getHome(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, max_id, null, null, MastodonHelper.statusesPerCall(requireActivity()), false)
.observe(getViewLifecycleOwner(), statusesBottom -> dealWithPagination(statusesBottom, DIRECTION.BOTTOM));
}
});
} else { } else {
timelinesVM.getHomeCache(BaseMainActivity.currentInstance, BaseMainActivity.currentUserID, max_id, null) timelinesVM.getHomeCache(BaseMainActivity.currentInstance, BaseMainActivity.currentUserID, max_id, null)
.observe(getViewLifecycleOwner(), statusesBottom -> dealWithPagination(statusesBottom, DIRECTION.BOTTOM)); .observe(getViewLifecycleOwner(), statusesBottom -> dealWithPagination(statusesBottom, DIRECTION.BOTTOM));
} }
} else if (direction == DIRECTION.TOP) { } else if (direction == DIRECTION.TOP) {
if (networkAvailable == BaseMainActivity.status.CONNECTED) { if (networkAvailable == BaseMainActivity.status.CONNECTED) {
timelinesVM.getHome(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, null, null, min_id, MastodonHelper.statusesPerCall(requireActivity()), false) timelinesVM.getHomeCache(BaseMainActivity.currentInstance, BaseMainActivity.currentUserID, null, min_id)
.observe(getViewLifecycleOwner(), statusesTopCache -> {
if (statusesTopCache != null && statusesTopCache.statuses != null && statusesTopCache.statuses.size() > 0) {
dealWithPagination(statusesTopCache, DIRECTION.TOP);
} else {
timelinesVM.getHome(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, null, null, min_id, MastodonHelper.statusesPerCall(requireActivity()), false)
.observe(getViewLifecycleOwner(), statusesTop -> dealWithPagination(statusesTop, DIRECTION.TOP));
}
});
} else {
timelinesVM.getHomeCache(BaseMainActivity.currentInstance, BaseMainActivity.currentUserID, null, min_id)
.observe(getViewLifecycleOwner(), statusesTop -> dealWithPagination(statusesTop, DIRECTION.TOP)); .observe(getViewLifecycleOwner(), statusesTop -> dealWithPagination(statusesTop, DIRECTION.TOP));
} }
} }