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.

This commit is contained in:
Docile-Alligator 2022-11-05 02:12:03 +11:00
parent 0a286cd9ba
commit be78c20c07
3 changed files with 51 additions and 3 deletions

View File

@ -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;
}
});
}
}

View File

@ -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));
}
}

View File

@ -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);