Skip to content

Commit

Permalink
Allow for refresh to skip disk-based network cache on story comment v…
Browse files Browse the repository at this point in the history
…iew.
  • Loading branch information
Tukajo committed Apr 29, 2024
1 parent a1ac0e8 commit c448f62
Showing 1 changed file with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public WindowInsetsCompat onApplyWindowInsets(@NonNull View v, @NonNull WindowIn
queue = NetworkComponent.getRequestQueueInstance(requireContext());
String cachedResponse = Utils.loadCachedStory(getContext(), story.id);

loadStoryAndComments(story.id, cachedResponse);
loadStoryAndComments(story.id, cachedResponse, false);

// if this isn't here, the addition of the text appears to scroll the recyclerview down a little
recyclerView.scrollToPosition(0);
Expand Down Expand Up @@ -1097,16 +1097,17 @@ public void onViewDetachedFromWindow(@NonNull View v) {

public void refreshComments() {
swipeRefreshLayout.setRefreshing(true);
loadStoryAndComments(adapter.story.id, null);
loadStoryAndComments(adapter.story.id, null, true);
}

private void loadStoryAndComments(final int id, final String oldCachedResponse) {
private void loadStoryAndComments(final int id, final String oldCachedResponse, boolean forceInvalidateNetworkCache) {
String url = "https://hn.algolia.com/api/v1/items/" + id;

lastLoaded = System.currentTimeMillis();

StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
StringRequest getStoryAndComments = new StringRequest(Request.Method.GET, url,
response -> {
if (TextUtils.isEmpty(oldCachedResponse) || !oldCachedResponse.equals(response)) {
if (TextUtils.isEmpty(oldCachedResponse) || !response.equals(oldCachedResponse)) {
handleJsonResponse(id, response, true, oldCachedResponse == null, false);
}
swipeRefreshLayout.setRefreshing(false);
Expand Down Expand Up @@ -1177,13 +1178,17 @@ public void onFailure(String reason) {
});
}

stringRequest.setTag(requestTag);
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
getStoryAndComments.setTag(requestTag);
getStoryAndComments.setRetryPolicy(new DefaultRetryPolicy(
15000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

queue.add(stringRequest);
if(forceInvalidateNetworkCache) {
// In order to re-invoke the network request, we must invalidate the disk cache.
this.queue.getCache().invalidate(url, true);
}
queue.add(getStoryAndComments);
}

private void loadPollOptions() {
Expand Down Expand Up @@ -1224,7 +1229,7 @@ private void loadPollOptions() {
}
}

private void handleJsonResponse(final int id, final String response, final boolean cache, final boolean forceHeaderRefresh, boolean restoreScroll) {
private void handleJsonResponse(final int id, final String response, final boolean shouldCacheResult, final boolean forceHeaderRefresh, boolean restoreScroll) {
int oldCommentCount = comments.size();

// This is what we get if the Algolia API has not indexed the post,
Expand Down Expand Up @@ -1283,7 +1288,7 @@ private void handleJsonResponse(final int id, final String response, final boole
adapter.loadingFailedServerError = false;

// Seems like loading went well, lets cache the result
if (cache) {
if (shouldCacheResult) {
Utils.cacheStory(getContext(), id, response);
} else if (restoreScroll) {
// If we're not caching the result, this means we just loaded an old cache.
Expand Down

0 comments on commit c448f62

Please sign in to comment.