From be78c20c07ea7a9e724720e675f9078757fa06b9 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Sat, 5 Nov 2022 02:12:03 +1100 Subject: [PATCH] Load more posts in ViewPostDetailActivity for post feed in HistoryPostFragment. There are bugs in HistoryPostPagingSource so duplicate posts will be fetched and more posts could not be fetched. Will be fixed. --- .../activities/ViewPostDetailActivity.java | 47 ++++++++++++++++++- .../fragments/HistoryPostFragment.java | 2 +- .../readpost/ReadPostDao.java | 5 +- 3 files changed, 51 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java index 0f3b02a9..c759d06a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java @@ -49,6 +49,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashSet; +import java.util.List; import java.util.Map; import java.util.concurrent.Executor; @@ -77,6 +78,7 @@ import ml.docilealligator.infinityforreddit.post.ParsePost; import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.post.PostPagingSource; import ml.docilealligator.infinityforreddit.postfilter.PostFilter; +import ml.docilealligator.infinityforreddit.readpost.ReadPost; import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import retrofit2.Call; @@ -660,7 +662,50 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele isFetchingMorePosts = false; }); } else { - + mExecutor.execute((Runnable) () -> { + long lastItem = posts.isEmpty() ? System.currentTimeMillis() : posts.get(posts.size() - 1).getPostTimeMillis(); + List readPosts = mRedditDataRoomDatabase.readPostDao().getAllReadPosts(username, lastItem); + StringBuilder ids = new StringBuilder(); + for (ReadPost readPost : readPosts) { + ids.append("t3_").append(readPost.getId()).append(","); + } + if (ids.length() > 0) { + ids.deleteCharAt(ids.length() - 1); + } + + Call historyPosts; + if (mAccessToken != null && !mAccessToken.isEmpty()) { + historyPosts = mOauthRetrofit.create(RedditAPI.class).getInfoOauth(ids.toString(), APIUtils.getOAuthHeader(mAccessToken)); + } else { + historyPosts = mRetrofit.create(RedditAPI.class).getInfo(ids.toString()); + } + + try { + Response response = historyPosts.execute(); + if (response.isSuccessful()) { + String responseString = response.body(); + LinkedHashSet newPosts = ParsePost.parsePostsSync(responseString, -1, postFilter, null); + if (newPosts == null || newPosts.isEmpty()) { + noMorePosts = true; + } else { + LinkedHashSet postLinkedHashSet = new LinkedHashSet<>(posts); + int currentPostsSize = postLinkedHashSet.size(); + postLinkedHashSet.addAll(newPosts); + if (currentPostsSize == postLinkedHashSet.size()) { + noMorePosts = true; + } else { + posts = new ArrayList<>(postLinkedHashSet); + handler.post(() -> sectionsPagerAdapter.notifyItemRangeInserted(currentPostsSize, postLinkedHashSet.size() - currentPostsSize)); + } + } + } else { + fetchMorePostsFailed = true; + } + } catch (IOException e) { + e.printStackTrace(); + fetchMorePostsFailed = true; + } + }); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/HistoryPostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/HistoryPostFragment.java index 596c2c79..917b9f5f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/HistoryPostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/HistoryPostFragment.java @@ -1265,7 +1265,7 @@ public class HistoryPostFragment extends Fragment implements FragmentCommunicato public void onNeedForPostListFromPostRecyclerViewAdapterEvent(NeedForPostListFromPostFragmentEvent event) { if (historyPostFragmentId == event.postFragmentTimeId && mAdapter != null) { EventBus.getDefault().post(new ProvidePostListToViewPostDetailActivityEvent(historyPostFragmentId, - new ArrayList<>(mAdapter.snapshot()), postType, null, null, null, + new ArrayList<>(mAdapter.snapshot()), HistoryPostPagingSource.TYPE_READ_POSTS, null, null, null, null, null, null, postFilter, null, null)); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/readpost/ReadPostDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/readpost/ReadPostDao.java index f446811d..3e2f975a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/readpost/ReadPostDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/readpost/ReadPostDao.java @@ -14,9 +14,12 @@ public interface ReadPostDao { @Insert(onConflict = OnConflictStrategy.REPLACE) void insert(ReadPost readPost); - @Query("SELECT * FROM read_posts WHERE username = :username AND time < :before ORDER BY :before LIMIT 25") + @Query("SELECT * FROM read_posts WHERE username = :username AND time < :before ORDER BY time LIMIT 25") ListenableFuture> getAllReadPostsListenableFuture(String username, long before); + @Query("SELECT * FROM read_posts WHERE username = :username AND time < :before ORDER BY time LIMIT 25") + List getAllReadPosts(String username, long before); + @Query("SELECT * FROM read_posts WHERE username = :username") List getAllReadPosts(String username);