Load anonymous multireddit posts.

This commit is contained in:
Alex Ning 2021-09-26 21:37:54 +08:00
parent 39835c587a
commit 76feb81c6b
7 changed files with 123 additions and 12 deletions

View File

@ -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<PostFilter> postFilters = redditDataRoomDatabase.postFilterDao().getValidPostFilters(postFilterUsage, nameOfUsage);
PostFilter mergedPostFilter = PostFilter.mergePostFilter(postFilters);
List<AnonymousMultiredditSubreddit> 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));
}
});
}
}

View File

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

View File

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

View File

@ -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<AnonymousMultiredditSubreddit> anonymousMultiredditSubreddits);
@Query("SELECT * FROM anonymous_multireddit_subreddits WHERE path = :path ORDER BY subreddit_name COLLATE NOCASE ASC")
LiveData<List<AnonymousMultiredditSubreddit>> getAllAnonymousMultiRedditSubreddits(String path);
List<AnonymousMultiredditSubreddit> getAllAnonymousMultiRedditSubreddits(String path);
}

View File

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

View File

@ -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<String, Post>
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";

View File

@ -1102,6 +1102,7 @@
<string name="vote">Vote</string>
<string name="anonymous_front_page_no_subscriptions">Start by joining a subreddit!</string>
<string name="anonymous_multireddit_no_subreddit">This multireddit does not have a subreddit!</string>
<string name="backup_settings_success">Successfully exported settings to the destination directory. The password of the generated zip file is 123321. Please don\'t modify the zip file.</string>
<string name="create_zip_in_destination_directory_failed">Could not create backup zip in the destination directory</string>