From 8503e78a8e42f8e29b57f0017dafcfc615512d69 Mon Sep 17 00:00:00 2001 From: Docile-Alligator <25734209+Docile-Alligator@users.noreply.github.com> Date: Wed, 17 Apr 2024 13:17:26 -0400 Subject: Fix issues related to PostPagingSource.TYPE_ANONYMOUS_MULTIREDDIT. Fix crashes when loading anonymous front page or anonymous multireddit on ViewPostDetailActivity when swipe between posts is enabled. --- .../activities/ViewPostDetailActivity.java | 7 ++++-- .../adapters/CommentsRecyclerViewAdapter.java | 25 +++++++++++----------- ...ovidePostListToViewPostDetailActivityEvent.java | 8 +++++-- .../fragments/HistoryPostFragment.java | 3 ++- .../infinityforreddit/fragments/PostFragment.java | 12 ++++------- .../infinityforreddit/post/PostPagingSource.java | 2 +- .../infinityforreddit/post/PostViewModel.java | 3 ++- 7 files changed, 32 insertions(+), 28 deletions(-) (limited to 'app/src/main') 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 a34ea1b0..59cab228 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewPostDetailActivity.java @@ -151,6 +151,8 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele @State String subredditName; @State + String concatenatedSubredditNames; + @State String username; @State String userWhere; @@ -559,8 +561,8 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele } break; case PostPagingSource.TYPE_ANONYMOUS_FRONT_PAGE: - //case PostPagingSource.TYPE_ANONYMOUS_MULTIREDDIT - call = api.getSubredditBestPosts(subredditName, sortType, sortTime, afterKey); + case PostPagingSource.TYPE_ANONYMOUS_MULTIREDDIT: + call = api.getSubredditBestPosts(concatenatedSubredditNames, sortType, sortTime, afterKey); break; default: call = api.getBestPosts(sortType, sortTime, afterKey, @@ -725,6 +727,7 @@ public class ViewPostDetailActivity extends BaseActivity implements SortTypeSele this.posts = event.posts; this.postType = event.postType; this.subredditName = event.subredditName; + this.concatenatedSubredditNames = event.concatenatedSubredditNames; this.username = event.username; this.userWhere = event.userWhere; this.multiPath = event.multiPath; diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java index 2c4c21a8..5254c6db 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CommentsRecyclerViewAdapter.java @@ -323,7 +323,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter comments, ArrayList newList) { - if (comments != null && comments.size() > 0) { + if (comments != null && !comments.isEmpty()) { for (Comment comment : comments) { newList.add(comment); expandChildren(comment.getChildren(), newList); @@ -1165,7 +1165,7 @@ public class CommentsRecyclerViewAdapter extends RecyclerView.Adapter { - getItemCount(); Comment comment = getCurrentComment(this); if (comment != null) { Bundle bundle = new Bundle(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/events/ProvidePostListToViewPostDetailActivityEvent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/events/ProvidePostListToViewPostDetailActivityEvent.java index 15c37ff1..ffe27c0d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/events/ProvidePostListToViewPostDetailActivityEvent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/events/ProvidePostListToViewPostDetailActivityEvent.java @@ -11,6 +11,7 @@ public class ProvidePostListToViewPostDetailActivityEvent { public ArrayList posts; public int postType; public String subredditName; + public String concatenatedSubredditNames; public String username; public String userWhere; public String multiPath; @@ -21,13 +22,16 @@ public class ProvidePostListToViewPostDetailActivityEvent { public ArrayList readPostList; public ProvidePostListToViewPostDetailActivityEvent(long postFragmentId, ArrayList posts, int postType, - String subredditName, String username, String userWhere, + String subredditName, String concatenatedSubredditNames, + String username, String userWhere, String multiPath, String query, String trendingSource, - PostFilter postFilter, SortType sortType, ArrayList readPostList) { + PostFilter postFilter, SortType sortType, + ArrayList readPostList) { this.postFragmentId = postFragmentId; this.posts = posts; this.postType = postType; this.subredditName = subredditName; + this.concatenatedSubredditNames = concatenatedSubredditNames; this.username = username; this.userWhere = userWhere; this.multiPath = multiPath; 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 616b1759..e88e549f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/HistoryPostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/HistoryPostFragment.java @@ -1280,7 +1280,8 @@ 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()), HistoryPostPagingSource.TYPE_READ_POSTS, null, null, null, + new ArrayList<>(mAdapter.snapshot()), HistoryPostPagingSource.TYPE_READ_POSTS, + null, null, null, null, null, null, null, postFilter, null, null)); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java index 3dfd5828..f73e3db8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -1238,18 +1238,13 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mRetrofit, null, activity.accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, null, subredditName, postType, sortType, postFilter, readPosts)).get(PostViewModel.class); - } else if (postType == PostPagingSource.TYPE_MULTI_REDDIT) { - mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, - mRetrofit, null, activity.accountName, mSharedPreferences, - mPostFeedScrolledPositionSharedPreferences, null, multiRedditPath, - postType, sortType, postFilter, readPosts)).get(PostViewModel.class); } else if (postType == PostPagingSource.TYPE_USER) { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, mRetrofit, null, activity.accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, null, username, postType, sortType, postFilter, where, readPosts)).get(PostViewModel.class); } else { - //Anonymous Front Page + //Anonymous front page or multireddit mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mExecutor, mRetrofit, mSharedPreferences, concatenatedSubredditNames, postType, sortType, postFilter)) .get(PostViewModel.class); @@ -2051,8 +2046,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { public void onNeedForPostListFromPostRecyclerViewAdapterEvent(NeedForPostListFromPostFragmentEvent event) { if (postFragmentId == event.postFragmentTimeId && mAdapter != null) { EventBus.getDefault().post(new ProvidePostListToViewPostDetailActivityEvent(postFragmentId, - new ArrayList<>(mAdapter.snapshot()), postType, subredditName, username, where, - multiRedditPath, query, trendingSource, postFilter, sortType, readPosts)); + new ArrayList<>(mAdapter.snapshot()), postType, subredditName, + concatenatedSubredditNames, username, where, multiRedditPath, query, trendingSource, + postFilter, sortType, readPosts)); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPagingSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPagingSource.java index cc2519cf..2d5191a4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPagingSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPagingSource.java @@ -86,7 +86,7 @@ public class PostPagingSource extends ListenableFuturePagingSource this.accountName = accountName; this.sharedPreferences = sharedPreferences; this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - if (postType == TYPE_SUBREDDIT || postType == TYPE_ANONYMOUS_FRONT_PAGE) { + if (postType == TYPE_SUBREDDIT || postType == TYPE_ANONYMOUS_FRONT_PAGE || postType == TYPE_ANONYMOUS_MULTIREDDIT) { this.subredditOrUserName = path; } else { if (sortType != null) { diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java index 65f8314c..aad51bad 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java @@ -235,6 +235,7 @@ public class PostViewModel extends ViewModel { case PostPagingSource.TYPE_SUBREDDIT: case PostPagingSource.TYPE_MULTI_REDDIT: case PostPagingSource.TYPE_ANONYMOUS_FRONT_PAGE: + case PostPagingSource.TYPE_ANONYMOUS_MULTIREDDIT: paging3PagingSource = new PostPagingSource(executor, retrofit, accessToken, accountName, sharedPreferences, postFeedScrolledPositionSharedPreferences, name, postType, sortType, postFilter, readPostList); @@ -386,7 +387,7 @@ public class PostViewModel extends ViewModel { return (T) new PostViewModel(executor, retrofit, accessToken, accountName, sharedPreferences, postFeedScrolledPositionSharedPreferences, postHistorySharedPreferences, name, postType, sortType, postFilter, readPostList); - } else if (postType == PostPagingSource.TYPE_ANONYMOUS_FRONT_PAGE) { + } else if (postType == PostPagingSource.TYPE_ANONYMOUS_FRONT_PAGE || postType == PostPagingSource.TYPE_ANONYMOUS_MULTIREDDIT) { return (T) new PostViewModel(executor, retrofit, null, null, sharedPreferences, null, null, name, postType, sortType, postFilter, null); -- cgit v1.2.3