Fix history posts problems.

This commit is contained in:
Docile-Alligator 2022-11-05 14:31:58 +11:00
parent be78c20c07
commit c86408dbe5
3 changed files with 8 additions and 8 deletions

View File

@ -373,7 +373,7 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele
viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() { viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
@Override @Override
public void onPageSelected(int position) { public void onPageSelected(int position) {
if (position > posts.size() - 5) { if (posts != null && position > posts.size() - 5) {
fetchMorePosts(); fetchMorePosts();
} }
} }
@ -663,7 +663,7 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele
}); });
} else { } else {
mExecutor.execute((Runnable) () -> { mExecutor.execute((Runnable) () -> {
long lastItem = posts.isEmpty() ? System.currentTimeMillis() : posts.get(posts.size() - 1).getPostTimeMillis(); long lastItem = posts.isEmpty() ? 0 : posts.get(posts.size() - 1).getPostTimeMillis();
List<ReadPost> readPosts = mRedditDataRoomDatabase.readPostDao().getAllReadPosts(username, lastItem); List<ReadPost> readPosts = mRedditDataRoomDatabase.readPostDao().getAllReadPosts(username, lastItem);
StringBuilder ids = new StringBuilder(); StringBuilder ids = new StringBuilder();
for (ReadPost readPost : readPosts) { for (ReadPost readPost : readPosts) {

View File

@ -111,8 +111,8 @@ public class HistoryPostPagingSource extends ListenableFuturePagingSource<String
} }
private ListenableFuture<LoadResult<String, Post>> loadHomePosts(@NonNull LoadParams<String> loadParams, RedditDataRoomDatabase redditDataRoomDatabase) { private ListenableFuture<LoadResult<String, Post>> loadHomePosts(@NonNull LoadParams<String> loadParams, RedditDataRoomDatabase redditDataRoomDatabase) {
String before = loadParams.getKey(); String after = loadParams.getKey();
ListenableFuture<List<ReadPost>> readPosts = redditDataRoomDatabase.readPostDao().getAllReadPostsListenableFuture(username, Long.parseLong(before == null ? Long.toString(System.currentTimeMillis()) : before)); ListenableFuture<List<ReadPost>> readPosts = redditDataRoomDatabase.readPostDao().getAllReadPostsListenableFuture(username, Long.parseLong(after == null ? "0" : after));
ListenableFuture<LoadResult<String, Post>> pageFuture = Futures.transform(readPosts, this::transformData, executor); ListenableFuture<LoadResult<String, Post>> pageFuture = Futures.transform(readPosts, this::transformData, executor);

View File

@ -14,11 +14,11 @@ public interface ReadPostDao {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
void insert(ReadPost readPost); void insert(ReadPost readPost);
@Query("SELECT * FROM read_posts WHERE username = :username AND time < :before ORDER BY time LIMIT 25") @Query("SELECT * FROM read_posts WHERE username = :username AND time > :after ORDER BY time LIMIT 25")
ListenableFuture<List<ReadPost>> getAllReadPostsListenableFuture(String username, long before); ListenableFuture<List<ReadPost>> getAllReadPostsListenableFuture(String username, long after);
@Query("SELECT * FROM read_posts WHERE username = :username AND time < :before ORDER BY time LIMIT 25") @Query("SELECT * FROM read_posts WHERE username = :username AND time > :after ORDER BY time LIMIT 25")
List<ReadPost> getAllReadPosts(String username, long before); List<ReadPost> getAllReadPosts(String username, long after);
@Query("SELECT * FROM read_posts WHERE username = :username") @Query("SELECT * FROM read_posts WHERE username = :username")
List<ReadPost> getAllReadPosts(String username); List<ReadPost> getAllReadPosts(String username);