diff options
author | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2022-11-05 13:01:18 +0000 |
---|---|---|
committer | Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> | 2022-11-05 13:01:18 +0000 |
commit | cf3595f22dd35e0bd15d1344c11408f73f64c144 (patch) | |
tree | b497c5cfd627605a8c8b41d7eaa5cb6c5ab59376 /app/src/main | |
parent | fa472a3ad81f752c9568ee8430db0d74764afd53 (diff) | |
download | infinity-for-reddit-cf3595f22dd35e0bd15d1344c11408f73f64c144.tar infinity-for-reddit-cf3595f22dd35e0bd15d1344c11408f73f64c144.tar.gz infinity-for-reddit-cf3595f22dd35e0bd15d1344c11408f73f64c144.tar.bz2 infinity-for-reddit-cf3595f22dd35e0bd15d1344c11408f73f64c144.tar.lz infinity-for-reddit-cf3595f22dd35e0bd15d1344c11408f73f64c144.tar.xz infinity-for-reddit-cf3595f22dd35e0bd15d1344c11408f73f64c144.tar.zst infinity-for-reddit-cf3595f22dd35e0bd15d1344c11408f73f64c144.zip |
Fix problems when fetching more history posts in ViewPostDetailActivity.
Diffstat (limited to '')
4 files changed, 69 insertions, 55 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/LoadingMorePostsStatus.java b/app/src/main/java/ml/docilealligator/infinityforreddit/LoadingMorePostsStatus.java new file mode 100644 index 00000000..8df6016b --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/LoadingMorePostsStatus.java @@ -0,0 +1,16 @@ +package ml.docilealligator.infinityforreddit; + +import androidx.annotation.IntDef; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@IntDef({LoadingMorePostsStatus.LOADING, LoadingMorePostsStatus.FAILED, LoadingMorePostsStatus.NO_MORE_POSTS, + LoadingMorePostsStatus.NOT_LOADING}) +@Retention(RetentionPolicy.SOURCE) +public @interface LoadingMorePostsStatus { + int LOADING = 0; + int FAILED = 1; + int NO_MORE_POSTS = 2; + int NOT_LOADING = 3; +} 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 44fe148c..034d1e26 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java @@ -61,6 +61,7 @@ import butterknife.BindView; import butterknife.ButterKnife; import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.LoadingMorePostsStatus; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.SaveThing; @@ -171,11 +172,8 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele @State Post post; @State - boolean isFetchingMorePosts; - @State - boolean noMorePosts; - @State - boolean fetchMorePostsFailed; + @LoadingMorePostsStatus + int loadingMorePostsStatus = LoadingMorePostsStatus.NOT_LOADING; public Map<String, String> authorIcons = new HashMap<>(); private FragmentManager fragmentManager; private SlidrInterface mSlidrInterface; @@ -511,12 +509,11 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele } public void fetchMorePosts() { - if (isFetchingMorePosts || noMorePosts) { + if (loadingMorePostsStatus == LoadingMorePostsStatus.LOADING || loadingMorePostsStatus == LoadingMorePostsStatus.NO_MORE_POSTS) { return; } - isFetchingMorePosts = true; - fetchMorePostsFailed = false; + loadingMorePostsStatus = LoadingMorePostsStatus.LOADING; Handler handler = new Handler(Looper.getMainLooper()); @@ -642,10 +639,10 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele LinkedHashSet<Post> newPosts = ParsePost.parsePostsSync(responseString, -1, postFilter, readPostList); if (newPosts == null) { handler.post(() -> { - noMorePosts = true; + loadingMorePostsStatus = LoadingMorePostsStatus.NO_MORE_POSTS; MorePostsInfoFragment fragment = sectionsPagerAdapter.getMorePostsInfoFragment(); if (fragment != null) { - fragment.setStatus(MorePostsInfoFragment.Status.NO_MORE_POSTS); + fragment.setStatus(LoadingMorePostsStatus.NO_MORE_POSTS); } }); } else { @@ -654,45 +651,51 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele postLinkedHashSet.addAll(newPosts); if (currentPostsSize == postLinkedHashSet.size()) { handler.post(() -> { - noMorePosts = true; + loadingMorePostsStatus = LoadingMorePostsStatus.NO_MORE_POSTS; MorePostsInfoFragment fragment = sectionsPagerAdapter.getMorePostsInfoFragment(); if (fragment != null) { - fragment.setStatus(MorePostsInfoFragment.Status.NO_MORE_POSTS); + fragment.setStatus(LoadingMorePostsStatus.NO_MORE_POSTS); } }); } else { posts = new ArrayList<>(postLinkedHashSet); - handler.post(() -> sectionsPagerAdapter.notifyItemRangeInserted(currentPostsSize, postLinkedHashSet.size() - currentPostsSize)); + handler.post(() -> { + sectionsPagerAdapter.notifyItemRangeInserted(currentPostsSize, postLinkedHashSet.size() - currentPostsSize); + loadingMorePostsStatus = LoadingMorePostsStatus.NOT_LOADING; + MorePostsInfoFragment fragment = sectionsPagerAdapter.getMorePostsInfoFragment(); + if (fragment != null) { + fragment.setStatus(LoadingMorePostsStatus.NOT_LOADING); + } + }); } } } else { handler.post(() -> { - fetchMorePostsFailed = true; + loadingMorePostsStatus = LoadingMorePostsStatus.FAILED; MorePostsInfoFragment fragment = sectionsPagerAdapter.getMorePostsInfoFragment(); if (fragment != null) { - fragment.setStatus(MorePostsInfoFragment.Status.FAILED); + fragment.setStatus(LoadingMorePostsStatus.FAILED); } }); } } catch (IOException e) { e.printStackTrace(); handler.post(() -> { - fetchMorePostsFailed = true; + loadingMorePostsStatus = LoadingMorePostsStatus.FAILED; MorePostsInfoFragment fragment = sectionsPagerAdapter.getMorePostsInfoFragment(); if (fragment != null) { - fragment.setStatus(MorePostsInfoFragment.Status.FAILED); + fragment.setStatus(LoadingMorePostsStatus.FAILED); } }); } - - handler.post(() -> { - isFetchingMorePosts = false; - }); }); } else { mExecutor.execute((Runnable) () -> { - long lastItem = posts.isEmpty() ? 0 : posts.get(posts.size() - 1).getPostTimeMillis(); - List<ReadPost> readPosts = mRedditDataRoomDatabase.readPostDao().getAllReadPosts(username, lastItem); + long lastItem = 0; + if (!posts.isEmpty()) { + lastItem = mRedditDataRoomDatabase.readPostDao().getReadPost(posts.get(posts.size() - 1).getId()).getTime(); + } + List<ReadPost> readPosts = mRedditDataRoomDatabase.readPostDao().getAllReadPosts(mAccountName, lastItem); StringBuilder ids = new StringBuilder(); for (ReadPost readPost : readPosts) { ids.append("t3_").append(readPost.getId()).append(","); @@ -715,10 +718,10 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele LinkedHashSet<Post> newPosts = ParsePost.parsePostsSync(responseString, -1, postFilter, null); if (newPosts == null || newPosts.isEmpty()) { handler.post(() -> { - noMorePosts = true; + loadingMorePostsStatus = LoadingMorePostsStatus.NO_MORE_POSTS; MorePostsInfoFragment fragment = sectionsPagerAdapter.getMorePostsInfoFragment(); if (fragment != null) { - fragment.setStatus(MorePostsInfoFragment.Status.NO_MORE_POSTS); + fragment.setStatus(LoadingMorePostsStatus.NO_MORE_POSTS); } }); } else { @@ -727,40 +730,43 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele postLinkedHashSet.addAll(newPosts); if (currentPostsSize == postLinkedHashSet.size()) { handler.post(() -> { - noMorePosts = true; + loadingMorePostsStatus = LoadingMorePostsStatus.NO_MORE_POSTS; MorePostsInfoFragment fragment = sectionsPagerAdapter.getMorePostsInfoFragment(); if (fragment != null) { - fragment.setStatus(MorePostsInfoFragment.Status.NO_MORE_POSTS); + fragment.setStatus(LoadingMorePostsStatus.NO_MORE_POSTS); } }); } else { posts = new ArrayList<>(postLinkedHashSet); - handler.post(() -> sectionsPagerAdapter.notifyItemRangeInserted(currentPostsSize, postLinkedHashSet.size() - currentPostsSize)); + handler.post(() -> { + sectionsPagerAdapter.notifyItemRangeInserted(currentPostsSize, postLinkedHashSet.size() - currentPostsSize); + loadingMorePostsStatus = LoadingMorePostsStatus.NOT_LOADING; + MorePostsInfoFragment fragment = sectionsPagerAdapter.getMorePostsInfoFragment(); + if (fragment != null) { + fragment.setStatus(LoadingMorePostsStatus.NOT_LOADING); + } + }); } } } else { handler.post(() -> { - fetchMorePostsFailed = true; + loadingMorePostsStatus = LoadingMorePostsStatus.FAILED; MorePostsInfoFragment fragment = sectionsPagerAdapter.getMorePostsInfoFragment(); if (fragment != null) { - fragment.setStatus(MorePostsInfoFragment.Status.NO_MORE_POSTS); + fragment.setStatus(LoadingMorePostsStatus.FAILED); } }); } } catch (IOException e) { e.printStackTrace(); handler.post(() -> { - fetchMorePostsFailed = true; + loadingMorePostsStatus = LoadingMorePostsStatus.FAILED; MorePostsInfoFragment fragment = sectionsPagerAdapter.getMorePostsInfoFragment(); if (fragment != null) { - fragment.setStatus(MorePostsInfoFragment.Status.NO_MORE_POSTS); + fragment.setStatus(LoadingMorePostsStatus.FAILED); } }); } - - handler.post(() -> { - isFetchingMorePosts = false; - }); }); } } @@ -936,7 +942,7 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele if (position >= posts.size()) { MorePostsInfoFragment morePostsInfoFragment = new MorePostsInfoFragment(); Bundle moreBundle = new Bundle(); - moreBundle.putInt(MorePostsInfoFragment.EXTRA_STATUS, MorePostsInfoFragment.Status.LOADING); + moreBundle.putInt(MorePostsInfoFragment.EXTRA_STATUS, loadingMorePostsStatus); morePostsInfoFragment.setArguments(moreBundle); return morePostsInfoFragment; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/MorePostsInfoFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/MorePostsInfoFragment.java index 499c30ff..430be57d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/MorePostsInfoFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/MorePostsInfoFragment.java @@ -6,16 +6,13 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import androidx.annotation.IntDef; import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; - import javax.inject.Inject; import ml.docilealligator.infinityforreddit.Infinity; +import ml.docilealligator.infinityforreddit.LoadingMorePostsStatus; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.activities.BaseActivity; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; @@ -29,7 +26,7 @@ public class MorePostsInfoFragment extends Fragment { CustomThemeWrapper mCustomThemeWrapper; private FragmentMorePostsInfoBinding binding; private BaseActivity mActivity; - @Status + @LoadingMorePostsStatus int status; public MorePostsInfoFragment() { @@ -45,21 +42,21 @@ public class MorePostsInfoFragment extends Fragment { applyTheme(); - setStatus(getArguments().getInt(EXTRA_STATUS, Status.LOADING)); + setStatus(getArguments().getInt(EXTRA_STATUS, LoadingMorePostsStatus.LOADING)); return binding.getRoot(); } - public void setStatus(@Status int status) { + public void setStatus(@LoadingMorePostsStatus int status) { this.status = status; switch (status) { - case Status.LOADING: + case LoadingMorePostsStatus.LOADING: binding.infoTextViewMorePostsInfoFragment.setText(R.string.loading); break; - case Status.FAILED: + case LoadingMorePostsStatus.FAILED: binding.infoTextViewMorePostsInfoFragment.setText(R.string.load_more_posts_failed); break; - case Status.NO_MORE_POSTS: + case LoadingMorePostsStatus.NO_MORE_POSTS: binding.infoTextViewMorePostsInfoFragment.setText(R.string.no_more_posts); } } @@ -73,12 +70,4 @@ public class MorePostsInfoFragment extends Fragment { super.onAttach(context); mActivity = (BaseActivity) context; } - - @IntDef({Status.LOADING, Status.FAILED, Status.NO_MORE_POSTS}) - @Retention(RetentionPolicy.SOURCE) - public @interface Status { - int LOADING = 0; - int FAILED = 1; - int NO_MORE_POSTS = 2; - } }
\ No newline at end of file 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 832b491a..b7752647 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/readpost/ReadPostDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/readpost/ReadPostDao.java @@ -23,6 +23,9 @@ public interface ReadPostDao { @Query("SELECT * FROM read_posts WHERE username = :username") List<ReadPost> getAllReadPosts(String username); + @Query("SELECT * FROM read_posts WHERE id = :id LIMIT 1") + ReadPost getReadPost(String id); + @Query("SELECT COUNT(id) FROM read_posts") int getReadPostsCount(); |