diff options
author | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2022-11-04 15:12:03 +0000 |
---|---|---|
committer | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2022-11-04 15:12:03 +0000 |
commit | be78c20c07ea7a9e724720e675f9078757fa06b9 (patch) | |
tree | 5f5ed544a5ca77fce6713f74ce758b36faf018b8 /app | |
parent | 0a286cd9baab6cf0275d4850f6c2ddb4d2113288 (diff) | |
download | infinity-for-reddit-be78c20c07ea7a9e724720e675f9078757fa06b9.tar infinity-for-reddit-be78c20c07ea7a9e724720e675f9078757fa06b9.tar.gz infinity-for-reddit-be78c20c07ea7a9e724720e675f9078757fa06b9.tar.bz2 infinity-for-reddit-be78c20c07ea7a9e724720e675f9078757fa06b9.tar.lz infinity-for-reddit-be78c20c07ea7a9e724720e675f9078757fa06b9.tar.xz infinity-for-reddit-be78c20c07ea7a9e724720e675f9078757fa06b9.tar.zst infinity-for-reddit-be78c20c07ea7a9e724720e675f9078757fa06b9.zip |
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.
Diffstat (limited to 'app')
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<ReadPost> 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<String> 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<String> response = historyPosts.execute(); + if (response.isSuccessful()) { + String responseString = response.body(); + LinkedHashSet<Post> newPosts = ParsePost.parsePostsSync(responseString, -1, postFilter, null); + if (newPosts == null || newPosts.isEmpty()) { + noMorePosts = true; + } else { + LinkedHashSet<Post> 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<List<ReadPost>> getAllReadPostsListenableFuture(String username, long before); + @Query("SELECT * FROM read_posts WHERE username = :username AND time < :before ORDER BY time LIMIT 25") + List<ReadPost> getAllReadPosts(String username, long before); + @Query("SELECT * FROM read_posts WHERE username = :username") List<ReadPost> getAllReadPosts(String username); |