From 76feb81c6baf68415dcd56eb122229134c6ed54b Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sun, 26 Sep 2021 21:37:54 +0800 Subject: Load anonymous multireddit posts. --- ...lterReadPostsAndConcatenatedSubredditNames.java | 23 ++++++ .../activities/ViewMultiRedditDetailActivity.java | 2 +- .../infinityforreddit/fragments/PostFragment.java | 96 ++++++++++++++++++++++ .../AnonymousMultiredditSubredditDao.java | 3 +- .../multireddit/FavoriteMultiReddit.java | 8 -- .../infinityforreddit/post/PostPagingSource.java | 2 +- app/src/main/res/values/strings.xml | 1 + 7 files changed, 123 insertions(+), 12 deletions(-) (limited to 'app/src/main') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchPostFilterReadPostsAndConcatenatedSubredditNames.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchPostFilterReadPostsAndConcatenatedSubredditNames.java index 468a1ce2..76d5457a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchPostFilterReadPostsAndConcatenatedSubredditNames.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchPostFilterReadPostsAndConcatenatedSubredditNames.java @@ -6,6 +6,7 @@ import java.util.ArrayList; import java.util.List; import java.util.concurrent.Executor; +import ml.docilealligator.infinityforreddit.multireddit.AnonymousMultiredditSubreddit; import ml.docilealligator.infinityforreddit.postfilter.PostFilter; import ml.docilealligator.infinityforreddit.readpost.ReadPost; import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditData; @@ -59,4 +60,26 @@ public class FetchPostFilterReadPostsAndConcatenatedSubredditNames { } }); } + + public static void fetchPostFilterAndConcatenatedSubredditNames(RedditDataRoomDatabase redditDataRoomDatabase, Executor executor, + Handler handler, String multipath, int postFilterUsage, String nameOfUsage, + FetchPostFilterAndConcatenatecSubredditNamesListener fetchPostFilterAndConcatenatecSubredditNamesListener) { + executor.execute(() -> { + List postFilters = redditDataRoomDatabase.postFilterDao().getValidPostFilters(postFilterUsage, nameOfUsage); + PostFilter mergedPostFilter = PostFilter.mergePostFilter(postFilters); + List anonymousMultiredditSubreddits = redditDataRoomDatabase.anonymousMultiredditSubredditDao().getAllAnonymousMultiRedditSubreddits(multipath); + if (anonymousMultiredditSubreddits != null && !anonymousMultiredditSubreddits.isEmpty()) { + StringBuilder stringBuilder = new StringBuilder(); + for (AnonymousMultiredditSubreddit s : anonymousMultiredditSubreddits) { + stringBuilder.append(s.getSubredditName()).append("+"); + } + if (stringBuilder.length() > 0) { + stringBuilder.deleteCharAt(stringBuilder.length() - 1); + } + handler.post(() -> fetchPostFilterAndConcatenatecSubredditNamesListener.success(mergedPostFilter, stringBuilder.toString())); + } else { + handler.post(() -> fetchPostFilterAndConcatenatecSubredditNamesListener.success(mergedPostFilter, null)); + } + }); + } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java index c88822a2..d6c96495 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ViewMultiRedditDetailActivity.java @@ -186,7 +186,7 @@ public class ViewMultiRedditDetailActivity extends BaseActivity implements SortT mFragment = new PostFragment(); Bundle bundle = new Bundle(); bundle.putString(PostFragment.EXTRA_NAME, multiPath); - bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostPagingSource.TYPE_MULTI_REDDIT); + bundle.putInt(PostFragment.EXTRA_POST_TYPE, mAccessToken == null ? PostPagingSource.TYPE_ANONYMOUS_MULTIREDDIT : PostPagingSource.TYPE_MULTI_REDDIT); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); bundle.putString(PostFragment.EXTRA_ACCOUNT_NAME, mAccountName); mFragment.setArguments(bundle); 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 9eac7f5f..8098dba8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -721,6 +721,66 @@ public class PostFragment extends Fragment implements FragmentCommunicator { postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST, defaultPostLayout); + mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, + mRedgifsRetrofit, mCustomThemeWrapper, locale, + windowWidth, accessToken, accountName, postType, postLayout, true, + mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, + mExoCreator, new PostRecyclerViewAdapter.Callback() { + @Override + public void typeChipClicked(int filter) { + Intent intent = new Intent(activity, FilteredPostsActivity.class); + intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, postType); + intent.putExtra(FilteredPostsActivity.EXTRA_FILTER, filter); + startActivity(intent); + } + + @Override + public void flairChipClicked(String flair) { + Intent intent = new Intent(activity, FilteredPostsActivity.class); + intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, postType); + intent.putExtra(FilteredPostsActivity.EXTRA_CONTAIN_FLAIR, flair); + startActivity(intent); + } + + @Override + public void nsfwChipClicked() { + Intent intent = new Intent(activity, FilteredPostsActivity.class); + intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, postType); + intent.putExtra(FilteredPostsActivity.EXTRA_FILTER, Post.NSFW_TYPE); + startActivity(intent); + } + + @Override + public void currentlyBindItem(int position) { + if (maxPosition < position) { + maxPosition = position; + } + } + + @Override + public void delayTransition() { + TransitionManager.beginDelayedTransition(mPostRecyclerView, new AutoTransition()); + } + }); + } else if (postType == PostPagingSource.TYPE_ANONYMOUS_MULTIREDDIT) { + multiRedditPath = getArguments().getString(EXTRA_NAME); + if (savedInstanceState == null) { + postFragmentId += multiRedditPath.hashCode(); + } + + usage = PostFilterUsage.HOME_TYPE; + nameOfUsage = PostFilterUsage.NO_USAGE; + + String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_MULTI_REDDIT_POST_BASE + multiRedditPath, SortType.Type.BEST.name()); + if (sort.equals(SortType.Type.CONTROVERSIAL.name()) || sort.equals(SortType.Type.TOP.name())) { + String sortTime = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TIME_MULTI_REDDIT_POST_BASE + multiRedditPath, SortType.Time.ALL.name()); + sortType = new SortType(SortType.Type.valueOf(sort), SortType.Time.valueOf(sortTime)); + } else { + sortType = new SortType(SortType.Type.valueOf(sort)); + } + + postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST, defaultPostLayout); + mAdapter = new PostRecyclerViewAdapter(activity, this, mExecutor, mOauthRetrofit, mGfycatRetrofit, mRedgifsRetrofit, mCustomThemeWrapper, locale, windowWidth, accessToken, accountName, postType, postLayout, true, @@ -901,6 +961,24 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } else { initializeAndBindPostViewModelForAnonymous(concatenatedSubredditNames); } + } else if (postType == PostPagingSource.TYPE_ANONYMOUS_MULTIREDDIT) { + if (concatenatedSubredditNames == null) { + FetchPostFilterReadPostsAndConcatenatedSubredditNames.fetchPostFilterAndConcatenatedSubredditNames(mRedditDataRoomDatabase, mExecutor, new Handler(), multiRedditPath, usage, nameOfUsage, + (postFilter, concatenatedSubredditNames) -> { + if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) { + this.postFilter = postFilter; + postFilter.allowNSFW = !mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_NSFW_FOREVER, false) && mNsfwAndSpoilerSharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_BASE, false); + this.concatenatedSubredditNames = concatenatedSubredditNames; + if (concatenatedSubredditNames == null) { + showErrorView(R.string.anonymous_multireddit_no_subreddit); + } else { + initializeAndBindPostViewModelForAnonymous(concatenatedSubredditNames); + } + } + }); + } else { + initializeAndBindPostViewModelForAnonymous(concatenatedSubredditNames); + } } else { FetchPostFilterReadPostsAndConcatenatedSubredditNames.fetchPostFilterAndReadPosts(mRedditDataRoomDatabase, mExecutor, new Handler(), null, usage, nameOfUsage, (postFilter, readPostList) -> { @@ -930,6 +1008,24 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } else { initializeAndBindPostViewModelForAnonymous(concatenatedSubredditNames); } + } else if (postType == PostPagingSource.TYPE_ANONYMOUS_MULTIREDDIT) { + if (concatenatedSubredditNames == null) { + FetchPostFilterReadPostsAndConcatenatedSubredditNames.fetchPostFilterAndConcatenatedSubredditNames(mRedditDataRoomDatabase, mExecutor, new Handler(), multiRedditPath, usage, nameOfUsage, + (postFilter, concatenatedSubredditNames) -> { + if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) { + this.postFilter = postFilter; + postFilter.allowNSFW = !mSharedPreferences.getBoolean(SharedPreferencesUtils.DISABLE_NSFW_FOREVER, false) && mNsfwAndSpoilerSharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_BASE, false); + this.concatenatedSubredditNames = concatenatedSubredditNames; + if (concatenatedSubredditNames == null) { + showErrorView(R.string.anonymous_multireddit_no_subreddit); + } else { + initializeAndBindPostViewModelForAnonymous(concatenatedSubredditNames); + } + } + }); + } else { + initializeAndBindPostViewModelForAnonymous(concatenatedSubredditNames); + } } else { initializeAndBindPostViewModelForAnonymous(null); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/AnonymousMultiredditSubredditDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/AnonymousMultiredditSubredditDao.java index 0117ca46..dfc7a7ed 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/AnonymousMultiredditSubredditDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/AnonymousMultiredditSubredditDao.java @@ -1,6 +1,5 @@ package ml.docilealligator.infinityforreddit.multireddit; -import androidx.lifecycle.LiveData; import androidx.room.Dao; import androidx.room.Insert; import androidx.room.OnConflictStrategy; @@ -17,5 +16,5 @@ public interface AnonymousMultiredditSubredditDao { void insertAll(List anonymousMultiredditSubreddits); @Query("SELECT * FROM anonymous_multireddit_subreddits WHERE path = :path ORDER BY subreddit_name COLLATE NOCASE ASC") - LiveData> getAllAnonymousMultiRedditSubreddits(String path); + List getAllAnonymousMultiRedditSubreddits(String path); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/FavoriteMultiReddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/FavoriteMultiReddit.java index 53e1a980..227b7665 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/FavoriteMultiReddit.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/multireddit/FavoriteMultiReddit.java @@ -50,12 +50,4 @@ public class FavoriteMultiReddit { } }); } - - public static void anonymousFavoriteMultiReddit(Executor executor, Handler handler, - RedditDataRoomDatabase redditDataRoomDatabase, - MultiReddit multiReddit, - FavoriteMultiRedditListener favoriteMultiRedditListener) { - InsertMultireddit.insertMultireddit(executor, handler, redditDataRoomDatabase, multiReddit, - favoriteMultiRedditListener::success); - } } 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 fe19a962..fe5b1d3e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPagingSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostPagingSource.java @@ -20,7 +20,6 @@ import java.util.concurrent.Executor; import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.apis.RedditAPI; 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.HttpException; @@ -34,6 +33,7 @@ public class PostPagingSource extends ListenableFuturePagingSource public static final int TYPE_SEARCH = 3; public static final int TYPE_MULTI_REDDIT = 4; public static final int TYPE_ANONYMOUS_FRONT_PAGE = 5; + public static final int TYPE_ANONYMOUS_MULTIREDDIT = 6; public static final String USER_WHERE_SUBMITTED = "submitted"; public static final String USER_WHERE_UPVOTED = "upvoted"; diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7b44a036..294a075a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1102,6 +1102,7 @@ Vote Start by joining a subreddit! + This multireddit does not have a subreddit! Successfully exported settings to the destination directory. The password of the generated zip file is 123321. Please don\'t modify the zip file. Could not create backup zip in the destination directory -- cgit v1.2.3