diff options
author | Alex Ning <chineseperson5@gmail.com> | 2021-03-04 09:28:43 +0000 |
---|---|---|
committer | Alex Ning <chineseperson5@gmail.com> | 2021-03-04 09:28:43 +0000 |
commit | e8cb0d329cd358e4ac2e8ee8c4e808e0d0389e50 (patch) | |
tree | 71d8b5ebf00650b98ffa37f2179f701b873fd54d | |
parent | 7bd60c907f232515561c951cad9145390274fad9 (diff) | |
download | infinity-for-reddit-e8cb0d329cd358e4ac2e8ee8c4e808e0d0389e50.tar infinity-for-reddit-e8cb0d329cd358e4ac2e8ee8c4e808e0d0389e50.tar.gz infinity-for-reddit-e8cb0d329cd358e4ac2e8ee8c4e808e0d0389e50.tar.bz2 infinity-for-reddit-e8cb0d329cd358e4ac2e8ee8c4e808e0d0389e50.tar.lz infinity-for-reddit-e8cb0d329cd358e4ac2e8ee8c4e808e0d0389e50.tar.xz infinity-for-reddit-e8cb0d329cd358e4ac2e8ee8c4e808e0d0389e50.tar.zst infinity-for-reddit-e8cb0d329cd358e4ac2e8ee8c4e808e0d0389e50.zip |
Start adding anonymous subscriptions.
10 files changed, 431 insertions, 145 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchPostFilterAndReadPosts.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchPostFilterAndReadPosts.java deleted file mode 100644 index fc9e54f5..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchPostFilterAndReadPosts.java +++ /dev/null @@ -1,31 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.os.Handler; - -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.Executor; - -import ml.docilealligator.infinityforreddit.postfilter.PostFilter; -import ml.docilealligator.infinityforreddit.readpost.ReadPost; - -public class FetchPostFilterAndReadPosts { - public interface FetchPostFilterAndReadPostsListener { - void success(PostFilter postFilter, ArrayList<ReadPost> readPostList); - } - - public static void fetchPostFilterAndReadPosts(RedditDataRoomDatabase redditDataRoomDatabase, Executor executor, - Handler handler, String accountName, int postFilterUsage, - String nameOfUsage, FetchPostFilterAndReadPostsListener fetchPostFilterAndReadPostsListener) { - executor.execute(() -> { - List<PostFilter> postFilters = redditDataRoomDatabase.postFilterDao().getValidPostFilters(postFilterUsage, nameOfUsage); - PostFilter mergedPostFilter = PostFilter.mergePostFilter(postFilters); - if (accountName != null) { - ArrayList<ReadPost> readPosts = (ArrayList<ReadPost>) redditDataRoomDatabase.readPostDao().getAllReadPosts(accountName); - handler.post(() -> fetchPostFilterAndReadPostsListener.success(mergedPostFilter, readPosts)); - } else { - handler.post(() -> fetchPostFilterAndReadPostsListener.success(mergedPostFilter, null)); - } - }); - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchPostFilterReadPostsAndConcatenatedSubredditNames.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchPostFilterReadPostsAndConcatenatedSubredditNames.java new file mode 100644 index 00000000..e594b504 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchPostFilterReadPostsAndConcatenatedSubredditNames.java @@ -0,0 +1,58 @@ +package ml.docilealligator.infinityforreddit; + +import android.os.Handler; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Executor; + +import ml.docilealligator.infinityforreddit.postfilter.PostFilter; +import ml.docilealligator.infinityforreddit.readpost.ReadPost; +import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditData; + +public class FetchPostFilterReadPostsAndConcatenatedSubredditNames { + public interface FetchPostFilterAndReadPostsListener { + void success(PostFilter postFilter, ArrayList<ReadPost> readPostList); + } + + public interface FetchPostFilterAndConcatenatecSubredditNamesListener { + void success(PostFilter postFilter, String concatenatedSubredditNames); + } + + public static void fetchPostFilterAndReadPosts(RedditDataRoomDatabase redditDataRoomDatabase, Executor executor, + Handler handler, String accountName, int postFilterUsage, + String nameOfUsage, FetchPostFilterAndReadPostsListener fetchPostFilterAndReadPostsListener) { + executor.execute(() -> { + List<PostFilter> postFilters = redditDataRoomDatabase.postFilterDao().getValidPostFilters(postFilterUsage, nameOfUsage); + PostFilter mergedPostFilter = PostFilter.mergePostFilter(postFilters); + if (accountName != null) { + ArrayList<ReadPost> readPosts = (ArrayList<ReadPost>) redditDataRoomDatabase.readPostDao().getAllReadPosts(accountName); + handler.post(() -> fetchPostFilterAndReadPostsListener.success(mergedPostFilter, readPosts)); + } else { + handler.post(() -> fetchPostFilterAndReadPostsListener.success(mergedPostFilter, null)); + } + }); + } + + public static void fetchPostFilterAndConcatenatedSubredditNames(RedditDataRoomDatabase redditDataRoomDatabase, Executor executor, + Handler handler, int postFilterUsage, String nameOfUsage, + FetchPostFilterAndConcatenatecSubredditNamesListener fetchPostFilterAndConcatenatecSubredditNamesListener) { + executor.execute(() -> { + List<PostFilter> postFilters = redditDataRoomDatabase.postFilterDao().getValidPostFilters(postFilterUsage, nameOfUsage); + PostFilter mergedPostFilter = PostFilter.mergePostFilter(postFilters); + List<SubscribedSubredditData> anonymousSubscribedSubreddits = redditDataRoomDatabase.subscribedSubredditDao().getAllSubscribedSubredditsList("-"); + if (anonymousSubscribedSubreddits != null && !anonymousSubscribedSubreddits.isEmpty()) { + StringBuilder stringBuilder = new StringBuilder(); + for (SubscribedSubredditData s : anonymousSubscribedSubreddits) { + stringBuilder.append(s).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/RedditDataRoomDatabase.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java index 3ebfcfc2..49f706e9 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java @@ -35,7 +35,7 @@ import ml.docilealligator.infinityforreddit.user.UserData; @Database(entities = {Account.class, SubredditData.class, SubscribedSubredditData.class, UserData.class, SubscribedUserData.class, MultiReddit.class, CustomTheme.class, RecentSearchQuery.class, - ReadPost.class, PostFilter.class, PostFilterUsage.class}, version = 18) + ReadPost.class, PostFilter.class, PostFilterUsage.class}, version = 19) public abstract class RedditDataRoomDatabase extends RoomDatabase { private static RedditDataRoomDatabase INSTANCE; @@ -49,7 +49,7 @@ public abstract class RedditDataRoomDatabase extends RoomDatabase { MIGRATION_5_6, MIGRATION_6_7, MIGRATION_7_8, MIGRATION_8_9, MIGRATION_9_10, MIGRATION_10_11, MIGRATION_11_12, MIGRATION_12_13, MIGRATION_13_14, MIGRATION_14_15, MIGRATION_15_16, MIGRATION_16_17, - MIGRATION_17_18) + MIGRATION_17_18, MIGRATION_18_19) .build(); } } @@ -325,4 +325,11 @@ public abstract class RedditDataRoomDatabase extends RoomDatabase { database.execSQL("ALTER TABLE custom_themes ADD COLUMN upvote_ratio_icon_tint INTEGER DEFAULT " + Color.parseColor("#0256EE") + " NOT NULL"); } }; + + private static final Migration MIGRATION_18_19 = new Migration(18, 19) { + @Override + public void migrate(@NonNull SupportSQLiteDatabase database) { + database.execSQL("INSERT INTO accounts(username, karma, is_current_user) VALUES (\"-\", 0, false)"); + } + }; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java index 0bcb8d4e..d2895479 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/account/AccountDao.java @@ -13,19 +13,19 @@ public interface AccountDao { @Insert(onConflict = OnConflictStrategy.REPLACE) void insert(Account account); - @Query("SELECT * FROM accounts") + @Query("SELECT * FROM accounts WHERE username != '-'") List<Account> getAllAccounts(); - @Query("SELECT * FROM accounts WHERE is_current_user = 0") + @Query("SELECT * FROM accounts WHERE is_current_user = 0 AND username != '-'") List<Account> getAllNonCurrentAccounts(); - @Query("UPDATE accounts SET is_current_user = 0 WHERE is_current_user = 1") + @Query("UPDATE accounts SET is_current_user = 0 WHERE is_current_user = 1 AND username != '-'") void markAllAccountsNonCurrent(); - @Query("DELETE FROM accounts WHERE is_current_user = 1") + @Query("DELETE FROM accounts WHERE is_current_user = 1 AND username != '-'") void deleteCurrentAccount(); - @Query("DELETE FROM accounts") + @Query("DELETE FROM accounts WHERE username != '-'") void deleteAllAccounts(); @Query("SELECT * FROM accounts WHERE username = :username COLLATE NOCASE LIMIT 1") @@ -34,17 +34,17 @@ public interface AccountDao { @Query("SELECT * FROM accounts WHERE username = :username COLLATE NOCASE LIMIT 1") Account getAccountData(String username); - @Query("SELECT * FROM accounts WHERE is_current_user = 1 LIMIT 1") + @Query("SELECT * FROM accounts WHERE is_current_user = 1 AND username != '-' LIMIT 1") Account getCurrentAccount(); - @Query("SELECT * FROM accounts WHERE is_current_user = 1 LIMIT 1") + @Query("SELECT * FROM accounts WHERE is_current_user = 1 AND username != '-' LIMIT 1") LiveData<Account> getCurrentAccountLiveData(); @Query("UPDATE accounts SET profile_image_url = :profileImageUrl, banner_image_url = :bannerImageUrl, " + "karma = :karma WHERE username = :username") void updateAccountInfo(String username, String profileImageUrl, String bannerImageUrl, int karma); - @Query("SELECT * FROM accounts WHERE is_current_user = 0 ORDER BY username COLLATE NOCASE ASC") + @Query("SELECT * FROM accounts WHERE is_current_user = 0 AND username != '-' ORDER BY username COLLATE NOCASE ASC") LiveData<List<Account>> getAccountsExceptCurrentAccountLiveData(); @Query("UPDATE accounts SET is_current_user = 1 WHERE username = :username") diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java index 98e2306b..ac4308ca 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/MainActivity.java @@ -792,9 +792,12 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb if (mAccessToken == null) { switch (position) { case 0: - tab.setText(R.string.popular); + tab.setText(R.string.home); break; case 1: + tab.setText(R.string.popular); + break; + case 2: tab.setText(R.string.all); break; } @@ -1354,6 +1357,12 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb if (position == 0) { PostFragment fragment = new PostFragment(); Bundle bundle = new Bundle(); + bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_ANONYMOUS_FRONT_PAGE); + fragment.setArguments(bundle); + return fragment; + } else if (position == 1) { + PostFragment fragment = new PostFragment(); + Bundle bundle = new Bundle(); bundle.putInt(PostFragment.EXTRA_POST_TYPE, PostDataSource.TYPE_SUBREDDIT); bundle.putString(PostFragment.EXTRA_NAME, "popular"); bundle.putString(PostFragment.EXTRA_ACCESS_TOKEN, mAccessToken); @@ -1499,7 +1508,7 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb @Override public int getItemCount() { if (mAccessToken == null) { - return 2; + return 3; } return tabCount + favoriteSubscribedSubreddits.size() + subscribedSubreddits.size(); } 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 44e4e613..e4fe58c0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -65,7 +65,7 @@ import im.ene.toro.exoplayer.ExoCreator; import im.ene.toro.media.PlaybackInfo; import im.ene.toro.media.VolumeInfo; import ml.docilealligator.infinityforreddit.ActivityToolbarInterface; -import ml.docilealligator.infinityforreddit.FetchPostFilterAndReadPosts; +import ml.docilealligator.infinityforreddit.FetchPostFilterReadPostsAndConcatenatedSubredditNames; import ml.docilealligator.infinityforreddit.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.NetworkState; @@ -146,6 +146,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private static final String READ_POST_LIST_STATE = "RPLS"; private static final String HIDE_READ_POSTS_INDEX_STATE = "HRPIS"; private static final String POST_FILTER_STATE = "PFS"; + private static final String CONCATENATED_SUBREDDIT_NAMES_STATE = "CSNS"; private static final String POST_FRAGMENT_ID_STATE = "PFIS"; @BindView(R.id.swipe_refresh_layout_post_fragment) @@ -221,6 +222,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private String query; private String where; private String multiRedditPath; + private String concatenatedSubredditNames; private int maxPosition = -1; private int postLayout; private SortType sortType; @@ -381,6 +383,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { readPosts = savedInstanceState.getParcelableArrayList(READ_POST_LIST_STATE); hideReadPostsIndex = savedInstanceState.getInt(HIDE_READ_POSTS_INDEX_STATE, 0); postFilter = savedInstanceState.getParcelable(POST_FILTER_STATE); + concatenatedSubredditNames = savedInstanceState.getString(CONCATENATED_SUBREDDIT_NAMES_STATE); postFragmentId = savedInstanceState.getLong(POST_FRAGMENT_ID_STATE); } else { postFilter = getArguments().getParcelable(EXTRA_FILTER); @@ -678,6 +681,60 @@ public class PostFragment extends Fragment implements FragmentCommunicator { TransitionManager.beginDelayedTransition(mPostRecyclerView, new AutoTransition()); } }); + } else if (postType == PostDataSource.TYPE_ANONYMOUS_FRONT_PAGE) { + usage = PostFilterUsage.HOME_TYPE; + nameOfUsage = PostFilterUsage.NO_USAGE; + + String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_BEST_POST, SortType.Type.BEST.name()); + if (sort.equals(SortType.Type.CONTROVERSIAL.name()) || sort.equals(SortType.Type.TOP.name())) { + String sortTime = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TIME_BEST_POST, 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, mRetrofit, mGfycatRetrofit, + mRedgifsRetrofit, mRedditDataRoomDatabase, mCustomThemeWrapper, locale, + windowWidth, accessToken, accountName, postType, postLayout, true, + mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, + mExoCreator, new PostRecyclerViewAdapter.Callback() { + @Override + public void retryLoadingMore() { + mPostViewModel.retryLoadingMore(); + } + + @Override + public void typeChipClicked(int filter) { + Intent intent = new Intent(activity, FilteredPostsActivity.class); + intent.putExtra(FilteredPostsActivity.EXTRA_NAME, activity.getString(R.string.best)); + intent.putExtra(FilteredPostsActivity.EXTRA_POST_TYPE, postType); + intent.putExtra(FilteredPostsActivity.EXTRA_FILTER, filter); + startActivity(intent); + } + + @Override + public void nsfwChipClicked() { + Intent intent = new Intent(activity, FilteredPostsActivity.class); + intent.putExtra(FilteredPostsActivity.EXTRA_NAME, activity.getString(R.string.best)); + 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 { usage = PostFilterUsage.HOME_TYPE; nameOfUsage = PostFilterUsage.NO_USAGE; @@ -752,15 +809,15 @@ public class PostFragment extends Fragment implements FragmentCommunicator { ((ActivityToolbarInterface) activity).displaySortType(); } - if (accountName != null && !accountName.equals("")) { + if (accessToken != null && !accessToken.equals("")) { if (mPostHistorySharedPreferences.getBoolean(accountName + SharedPreferencesUtils.MARK_POSTS_AS_READ_BASE, false) && readPosts == null) { if (getArguments().getBoolean(EXTRA_DISABLE_READ_POSTS, false)) { if (postFilter == null) { - FetchPostFilterAndReadPosts.fetchPostFilterAndReadPosts(mRedditDataRoomDatabase, mExecutor, + FetchPostFilterReadPostsAndConcatenatedSubredditNames.fetchPostFilterAndReadPosts(mRedditDataRoomDatabase, mExecutor, new Handler(), null, usage, nameOfUsage, (postFilter, readPostList) -> { if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) { this.postFilter = postFilter; - postFilter.allowNSFW = mNsfwAndSpoilerSharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.NSFW_BASE, false); + postFilter.allowNSFW = mNsfwAndSpoilerSharedPreferences.getBoolean(accountName + SharedPreferencesUtils.NSFW_BASE, false); initializeAndBindPostViewModel(accessToken); } }); @@ -768,12 +825,12 @@ public class PostFragment extends Fragment implements FragmentCommunicator { initializeAndBindPostViewModel(accessToken); } } else { - FetchPostFilterAndReadPosts.fetchPostFilterAndReadPosts(mRedditDataRoomDatabase, mExecutor, + FetchPostFilterReadPostsAndConcatenatedSubredditNames.fetchPostFilterAndReadPosts(mRedditDataRoomDatabase, mExecutor, new Handler(), accountName, usage, nameOfUsage, (postFilter, readPostList) -> { if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) { if (this.postFilter == null) { this.postFilter = postFilter; - postFilter.allowNSFW = mNsfwAndSpoilerSharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.NSFW_BASE, false); + postFilter.allowNSFW = mNsfwAndSpoilerSharedPreferences.getBoolean(accountName + SharedPreferencesUtils.NSFW_BASE, false); } this.readPosts = readPostList; initializeAndBindPostViewModel(accessToken); @@ -782,11 +839,11 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } else { if (postFilter == null) { - FetchPostFilterAndReadPosts.fetchPostFilterAndReadPosts(mRedditDataRoomDatabase, mExecutor, + FetchPostFilterReadPostsAndConcatenatedSubredditNames.fetchPostFilterAndReadPosts(mRedditDataRoomDatabase, mExecutor, new Handler(), null, usage, nameOfUsage, (postFilter, readPostList) -> { if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) { this.postFilter = postFilter; - postFilter.allowNSFW = mNsfwAndSpoilerSharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.NSFW_BASE, false); + postFilter.allowNSFW = mNsfwAndSpoilerSharedPreferences.getBoolean(accountName + SharedPreferencesUtils.NSFW_BASE, false); initializeAndBindPostViewModel(accessToken); } }); @@ -796,16 +853,56 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } } else { if (postFilter == null) { - FetchPostFilterAndReadPosts.fetchPostFilterAndReadPosts(mRedditDataRoomDatabase, mExecutor, - new Handler(), null, usage, nameOfUsage, (postFilter, readPostList) -> { - if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) { - this.postFilter = postFilter; - postFilter.allowNSFW = mNsfwAndSpoilerSharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.NSFW_BASE, false); - initializeAndBindPostViewModelForAnonymous(accessToken); - } - }); + if (postType == PostDataSource.TYPE_ANONYMOUS_FRONT_PAGE) { + if (concatenatedSubredditNames == null) { + FetchPostFilterReadPostsAndConcatenatedSubredditNames.fetchPostFilterAndConcatenatedSubredditNames(mRedditDataRoomDatabase, mExecutor, new Handler(), usage, nameOfUsage, + (postFilter, concatenatedSubredditNames) -> { + if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) { + this.postFilter = postFilter; + postFilter.allowNSFW = mNsfwAndSpoilerSharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_BASE, false); + this.concatenatedSubredditNames = concatenatedSubredditNames; + if (concatenatedSubredditNames == null) { + showErrorView(R.string.anonymous_front_page_no_subscriptions); + } else { + initializeAndBindPostViewModelForAnonymous(concatenatedSubredditNames); + } + } + }); + } else { + initializeAndBindPostViewModelForAnonymous(concatenatedSubredditNames); + } + } else { + FetchPostFilterReadPostsAndConcatenatedSubredditNames.fetchPostFilterAndReadPosts(mRedditDataRoomDatabase, mExecutor, + new Handler(), null, usage, nameOfUsage, (postFilter, readPostList) -> { + if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) { + this.postFilter = postFilter; + postFilter.allowNSFW = mNsfwAndSpoilerSharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_BASE, false); + initializeAndBindPostViewModelForAnonymous(null); + } + }); + } } else { - initializeAndBindPostViewModelForAnonymous(accessToken); + if (postType == PostDataSource.TYPE_ANONYMOUS_FRONT_PAGE) { + if (concatenatedSubredditNames == null) { + FetchPostFilterReadPostsAndConcatenatedSubredditNames.fetchPostFilterAndConcatenatedSubredditNames(mRedditDataRoomDatabase, mExecutor, new Handler(), usage, nameOfUsage, + (postFilter, concatenatedSubredditNames) -> { + if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) { + this.postFilter = postFilter; + postFilter.allowNSFW = mNsfwAndSpoilerSharedPreferences.getBoolean(SharedPreferencesUtils.NSFW_BASE, false); + this.concatenatedSubredditNames = concatenatedSubredditNames; + if (concatenatedSubredditNames == null) { + showErrorView(R.string.anonymous_front_page_no_subscriptions); + } else { + initializeAndBindPostViewModelForAnonymous(concatenatedSubredditNames); + } + } + }); + } else { + initializeAndBindPostViewModelForAnonymous(concatenatedSubredditNames); + } + } else { + initializeAndBindPostViewModelForAnonymous(null); + } } } @@ -970,32 +1067,32 @@ public class PostFragment extends Fragment implements FragmentCommunicator { bindPostViewModel(); } - private void initializeAndBindPostViewModelForAnonymous(String accessToken) { + private void initializeAndBindPostViewModelForAnonymous(String concatenatedSubredditNames) { //For anonymous user if (postType == PostDataSource.TYPE_SEARCH) { - mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mRetrofit, null, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, postFilter, readPosts)).get(PostViewModel.class); } else if (postType == PostDataSource.TYPE_SUBREDDIT) { - mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, + mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(mRetrofit, null, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, postFilter, readPosts)).get(PostViewModel.class); } else if (postType == PostDataSource.TYPE_MULTI_REDDIT) { - mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mRetrofit, null, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, multiRedditPath, postType, sortType, postFilter, readPosts)).get(PostViewModel.class); } else if (postType == PostDataSource.TYPE_USER) { - mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mRetrofit, null, accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, username, postType, sortType, postFilter, where, readPosts)).get(PostViewModel.class); } else { - mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mOauthRetrofit, accessToken, - accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, - postType, sortType, postFilter, readPosts)).get(PostViewModel.class); + //Anonymous Front Page + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mRetrofit, + mSharedPreferences, concatenatedSubredditNames, postType, sortType, postFilter)).get(PostViewModel.class); } bindPostViewModel(); @@ -1014,8 +1111,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { stopLazyMode(); } - mFetchPostInfoLinearLayout.setOnClickListener(view -> { - }); + mFetchPostInfoLinearLayout.setOnClickListener(null); showErrorView(R.string.no_posts); } }); @@ -1036,51 +1132,53 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } public void changeSortType(SortType sortType) { - switch (postType) { - case PostDataSource.TYPE_FRONT_PAGE: - mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_BEST_POST, sortType.getType().name()).apply(); - if (sortType.getTime() != null) { - mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TIME_BEST_POST, sortType.getTime().name()).apply(); - } - break; - case PostDataSource.TYPE_SUBREDDIT: - mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_SUBREDDIT_POST_BASE + subredditName, sortType.getType().name()).apply(); - if (sortType.getTime() != null) { - mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TIME_SUBREDDIT_POST_BASE + subredditName, sortType.getTime().name()).apply(); - } - break; - case PostDataSource.TYPE_USER: - mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_USER_POST_BASE + username, sortType.getType().name()).apply(); - if (sortType.getTime() != null) { - mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TIME_USER_POST_BASE + username, sortType.getTime().name()).apply(); - } - break; - case PostDataSource.TYPE_SEARCH: - mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_SEARCH_POST, sortType.getType().name()).apply(); - if (sortType.getTime() != null) { - mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TIME_SEARCH_POST, sortType.getTime().name()).apply(); - } - break; - case PostDataSource.TYPE_MULTI_REDDIT: - mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_MULTI_REDDIT_POST_BASE + multiRedditPath, - sortType.getType().name()).apply(); - if (sortType.getTime() != null) { - mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TIME_MULTI_REDDIT_POST_BASE + multiRedditPath, - sortType.getTime().name()).apply(); - } - break; - } - if (mFetchPostInfoLinearLayout.getVisibility() != View.GONE) { - mFetchPostInfoLinearLayout.setVisibility(View.GONE); - mGlide.clear(mFetchPostInfoImageView); - } - mAdapter.removeFooter(); - hasPost = false; - if (isInLazyMode) { - stopLazyMode(); + if (mPostViewModel != null) { + switch (postType) { + case PostDataSource.TYPE_FRONT_PAGE: + mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_BEST_POST, sortType.getType().name()).apply(); + if (sortType.getTime() != null) { + mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TIME_BEST_POST, sortType.getTime().name()).apply(); + } + break; + case PostDataSource.TYPE_SUBREDDIT: + mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_SUBREDDIT_POST_BASE + subredditName, sortType.getType().name()).apply(); + if (sortType.getTime() != null) { + mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TIME_SUBREDDIT_POST_BASE + subredditName, sortType.getTime().name()).apply(); + } + break; + case PostDataSource.TYPE_USER: + mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_USER_POST_BASE + username, sortType.getType().name()).apply(); + if (sortType.getTime() != null) { + mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TIME_USER_POST_BASE + username, sortType.getTime().name()).apply(); + } + break; + case PostDataSource.TYPE_SEARCH: + mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_SEARCH_POST, sortType.getType().name()).apply(); + if (sortType.getTime() != null) { + mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TIME_SEARCH_POST, sortType.getTime().name()).apply(); + } + break; + case PostDataSource.TYPE_MULTI_REDDIT: + mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TYPE_MULTI_REDDIT_POST_BASE + multiRedditPath, + sortType.getType().name()).apply(); + if (sortType.getTime() != null) { + mSortTypeSharedPreferences.edit().putString(SharedPreferencesUtils.SORT_TIME_MULTI_REDDIT_POST_BASE + multiRedditPath, + sortType.getTime().name()).apply(); + } + break; + } + if (mFetchPostInfoLinearLayout.getVisibility() != View.GONE) { + mFetchPostInfoLinearLayout.setVisibility(View.GONE); + mGlide.clear(mFetchPostInfoImageView); + } + mAdapter.removeFooter(); + hasPost = false; + if (isInLazyMode) { + stopLazyMode(); + } + this.sortType = sortType; + mPostViewModel.changeSortType(sortType); } - this.sortType = sortType; - mPostViewModel.changeSortType(sortType); } private void initializeSwipeActionDrawable() { @@ -1127,6 +1225,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { mStaggeredGridLayoutManager.findFirstVisibleItemPositions(into)[0]); } outState.putParcelable(POST_FILTER_STATE, postFilter); + outState.putString(CONCATENATED_SUBREDDIT_NAMES_STATE, concatenatedSubredditNames); outState.putLong(POST_FRAGMENT_ID_STATE, postFragmentId); } @@ -1150,14 +1249,18 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Override public void refresh() { - mAdapter.removeFooter(); - mFetchPostInfoLinearLayout.setVisibility(View.GONE); - hasPost = false; - if (isInLazyMode) { - stopLazyMode(); + if (mPostViewModel != null) { + mAdapter.removeFooter(); + mFetchPostInfoLinearLayout.setVisibility(View.GONE); + hasPost = false; + if (isInLazyMode) { + stopLazyMode(); + } + saveCache(); + mPostViewModel.refresh(); + } else { + mSwipeRefreshLayout.setRefreshing(false); } - saveCache(); - mPostViewModel.refresh(); } private void showErrorView(int stringResId) { @@ -1321,7 +1424,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Override public void changePostFilter(PostFilter postFilter) { this.postFilter = postFilter; - postFilter.allowNSFW = mNsfwAndSpoilerSharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.NSFW_BASE, false); + postFilter.allowNSFW = mNsfwAndSpoilerSharedPreferences.getBoolean((accountName == null || accountName.equals("-") ? "" : accountName) + SharedPreferencesUtils.NSFW_BASE, false); if (mPostViewModel != null) { mPostViewModel.changePostFilter(postFilter); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSource.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSource.java index ceeccb94..e6991a7b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSource.java @@ -28,6 +28,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { public static final int TYPE_USER = 2; 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 String USER_WHERE_SUBMITTED = "submitted"; public static final String USER_WHERE_UPVOTED = "upvoted"; @@ -86,7 +87,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { this.accountName = accountName; this.sharedPreferences = sharedPreferences; this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - if (postType == TYPE_SUBREDDIT) { + if (postType == TYPE_SUBREDDIT || postType == TYPE_ANONYMOUS_FRONT_PAGE) { this.subredditOrUserName = path; } else { if (sortType != null) { @@ -196,6 +197,8 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { case TYPE_MULTI_REDDIT: loadMultiRedditPostsInitial(callback, null); break; + case TYPE_ANONYMOUS_FRONT_PAGE: + break; } } @@ -231,6 +234,8 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { case TYPE_MULTI_REDDIT: loadMultiRedditPostsAfter(params, callback, null); break; + case TYPE_ANONYMOUS_FRONT_PAGE: + break; } } @@ -918,6 +923,120 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { }); } + private void loadAnonymousFrontPagePostsInitial(@NonNull final LoadInitialCallback<String, Post> callback, String lastItem) { + RedditAPI api = retrofit.create(RedditAPI.class); + + Call<String> getPost; + if (sortType.getTime() != null) { + getPost = api.getSubredditBestPosts(subredditOrUserName, sortType.getType().value, sortType.getTime().value, lastItem); + } else { + getPost = api.getSubredditBestPosts(subredditOrUserName, sortType.getType().value, lastItem); + } + getPost.enqueue(new Callback<String>() { + @Override + public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { + if (response.isSuccessful()) { + ParsePost.parsePosts(response.body(), -1, postFilter, null, + new ParsePost.ParsePostsListingListener() { + @Override + public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { + String nextPageKey; + if (lastItem == null || lastItem.equals("") || lastItem.equals("null")) { + nextPageKey = null; + } else { + nextPageKey = lastItem; + } + + if (newPosts.size() != 0) { + postLinkedHashSet.addAll(newPosts); + callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); + hasPostLiveData.postValue(true); + } else if (nextPageKey != null) { + loadAnonymousFrontPagePostsInitial(callback, nextPageKey); + return; + } else { + postLinkedHashSet.addAll(newPosts); + callback.onResult(new ArrayList<>(newPosts), null, nextPageKey); + hasPostLiveData.postValue(false); + } + + initialLoadStateLiveData.postValue(NetworkState.LOADED); + } + + @Override + public void onParsePostsListingFail() { + initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing posts")); + } + }); + } else { + initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, + "code: " + response + " message: " + response.message())); + } + } + + @Override + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + String errorMessage = t.getMessage(); + initialLoadStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, + errorMessage + " " + call.request().url().toString())); + } + }); + } + + private void loadAnonymousFrontPagePostsAfter(@NonNull LoadParams<String> params, @NonNull final LoadCallback<String, Post> callback, String lastItem) { + String after = lastItem == null ? params.key : lastItem; + + RedditAPI api = retrofit.create(RedditAPI.class); + + Call<String> getPost; + if (sortType.getTime() != null) { + getPost = api.getSubredditBestPosts(subredditOrUserName, sortType.getType().value, + sortType.getTime().value, after); + } else { + getPost = api.getSubredditBestPosts(subredditOrUserName, sortType.getType().value, after); + } + + getPost.enqueue(new Callback<String>() { + @Override + public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { + if (response.isSuccessful()) { + ParsePost.parsePosts(response.body(), -1, postFilter, null, + new ParsePost.ParsePostsListingListener() { + @Override + public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { + if (newPosts.size() == 0 && lastItem != null && !lastItem.equals("") && !lastItem.equals("null")) { + loadAnonymousFrontPagePostsAfter(params, callback, lastItem); + } else { + int currentPostsSize = postLinkedHashSet.size(); + postLinkedHashSet.addAll(newPosts); + if (currentPostsSize == postLinkedHashSet.size()) { + loadAnonymousFrontPagePostsAfter(params, callback, lastItem); + } else { + List<Post> newPostsList = new ArrayList<>(postLinkedHashSet).subList(currentPostsSize, postLinkedHashSet.size()); + callback.onResult(newPostsList, lastItem); + } + paginationNetworkStateLiveData.postValue(NetworkState.LOADED); + } + } + + @Override + public void onParsePostsListingFail() { + paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, "Error parsing data")); + } + }); + } else { + paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, response.message())); + } + } + + @Override + public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { + String errorMessage = t.getMessage(); + paginationNetworkStateLiveData.postValue(new NetworkState(NetworkState.Status.FAILED, errorMessage)); + } + }); + } + void retryLoadingMore() { loadAfter(params, callback); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSourceFactory.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSourceFactory.java index 37da54fd..da99a508 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSourceFactory.java @@ -20,7 +20,7 @@ class PostDataSourceFactory extends DataSource.Factory { private String accountName; private SharedPreferences sharedPreferences; private SharedPreferences postFeedScrolledPositionSharedPreferences; - private String subredditName; + private String name; private String query; private int postType; private SortType sortType; @@ -49,14 +49,14 @@ class PostDataSourceFactory extends DataSource.Factory { PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String subredditName, int postType, SortType sortType, PostFilter postFilter, + String name, int postType, SortType sortType, PostFilter postFilter, List<ReadPost> readPostList) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.sharedPreferences = sharedPreferences; this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - this.subredditName = subredditName; + this.name = name; postDataSourceLiveData = new MutableLiveData<>(); this.postType = postType; this.sortType = sortType; @@ -66,14 +66,14 @@ class PostDataSourceFactory extends DataSource.Factory { PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String subredditName, int postType, SortType sortType, PostFilter postFilter, + String name, int postType, SortType sortType, PostFilter postFilter, String where, List<ReadPost> readPostList) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.sharedPreferences = sharedPreferences; this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - this.subredditName = subredditName; + this.name = name; postDataSourceLiveData = new MutableLiveData<>(); this.postType = postType; this.sortType = sortType; @@ -84,14 +84,14 @@ class PostDataSourceFactory extends DataSource.Factory { PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, - String subredditName, String query, int postType, SortType sortType, PostFilter postFilter, + String name, String query, int postType, SortType sortType, PostFilter postFilter, List<ReadPost> readPostList) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.sharedPreferences = sharedPreferences; this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - this.subredditName = subredditName; + this.name = name; this.query = query; postDataSourceLiveData = new MutableLiveData<>(); this.postType = postType; @@ -109,16 +109,20 @@ class PostDataSourceFactory extends DataSource.Factory { postFilter, readPostList); } else if (postType == PostDataSource.TYPE_SEARCH) { postDataSource = new PostDataSource(retrofit, accessToken, accountName, - sharedPreferences, postFeedScrolledPositionSharedPreferences, subredditName, query, + sharedPreferences, postFeedScrolledPositionSharedPreferences, name, query, postType, sortType, postFilter, readPostList); } else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) { Log.i("asdasfd", "s5 " + (postFilter == null)); postDataSource = new PostDataSource(retrofit, accessToken, accountName, - sharedPreferences, postFeedScrolledPositionSharedPreferences, subredditName, postType, + sharedPreferences, postFeedScrolledPositionSharedPreferences, name, postType, sortType, postFilter, readPostList); + } else if (postType == PostDataSource.TYPE_ANONYMOUS_FRONT_PAGE) { + postDataSource = new PostDataSource(retrofit, null, null, + sharedPreferences, null, name, postType, + sortType, postFilter, null); } else { postDataSource = new PostDataSource(retrofit, accessToken, accountName, - sharedPreferences, postFeedScrolledPositionSharedPreferences, subredditName, postType, + sharedPreferences, postFeedScrolledPositionSharedPreferences, name, postType, sortType, postFilter, userWhere, readPostList); } 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 2fc1ef33..45001fbf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java @@ -100,11 +100,11 @@ public class PostViewModel extends ViewModel { } public PostViewModel(Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences cache, String subredditName, + SharedPreferences sharedPreferences, SharedPreferences cache, String username, int postType, SortType sortType, PostFilter postFilter, String where, List<ReadPost> readPostList) { postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, - sharedPreferences, cache, subredditName, postType, sortType, postFilter, where, readPostList); + sharedPreferences, cache, username, postType, sortType, postFilter, where, readPostList); initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::getInitialLoadStateLiveData); @@ -205,7 +205,7 @@ public class PostViewModel extends ViewModel { private String accountName; private SharedPreferences sharedPreferences; private SharedPreferences postFeedScrolledPositionSharedPreferences; - private String subredditName; + private String name; private String query; private int postType; private SortType sortType; @@ -229,14 +229,14 @@ public class PostViewModel extends ViewModel { public Factory(Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, - SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditName, + SharedPreferences postFeedScrolledPositionSharedPreferences, String name, int postType, SortType sortType, PostFilter postFilter, List<ReadPost> readPostList) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.sharedPreferences = sharedPreferences; this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - this.subredditName = subredditName; + this.name = name; this.postType = postType; this.sortType = sortType; this.postFilter = postFilter; @@ -245,14 +245,14 @@ public class PostViewModel extends ViewModel { //User posts public Factory(Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditName, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String username, int postType, SortType sortType, PostFilter postFilter, String where, List<ReadPost> readPostList) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.sharedPreferences = sharedPreferences; this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - this.subredditName = subredditName; + this.name = username; this.postType = postType; this.sortType = sortType; this.postFilter = postFilter; @@ -261,14 +261,14 @@ public class PostViewModel extends ViewModel { } public Factory(Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditName, + SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String name, String query, int postType, SortType sortType, PostFilter postFilter, List<ReadPost> readPostList) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; this.sharedPreferences = sharedPreferences; this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - this.subredditName = subredditName; + this.name = name; this.query = query; this.postType = postType; this.sortType = sortType; @@ -276,6 +276,17 @@ public class PostViewModel extends ViewModel { this.readPostList = readPostList; } + //Anonymous Front Page + public Factory(Retrofit retrofit, SharedPreferences sharedPreferences, String concatenatedSubredditNames, + int postType, SortType sortType, PostFilter postFilter) { + this.retrofit = retrofit; + this.sharedPreferences = sharedPreferences; + this.name = concatenatedSubredditNames; + this.postType = postType; + this.sortType = sortType; + this.postFilter = postFilter; + } + @NonNull @Override public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { @@ -284,15 +295,19 @@ public class PostViewModel extends ViewModel { postFeedScrolledPositionSharedPreferences, postType, sortType, postFilter, readPostList); } else if (postType == PostDataSource.TYPE_SEARCH) { return (T) new PostViewModel(retrofit, accessToken, accountName, sharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, + postFeedScrolledPositionSharedPreferences, name, query, postType, sortType, postFilter, readPostList); } else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) { return (T) new PostViewModel(retrofit, accessToken, accountName, sharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, + postFeedScrolledPositionSharedPreferences, name, postType, sortType, postFilter, readPostList); + } else if (postType == PostDataSource.TYPE_ANONYMOUS_FRONT_PAGE) { + return (T) new PostViewModel(retrofit, null, null, sharedPreferences, + null, name, postType, sortType, + postFilter, null); } else { return (T) new PostViewModel(retrofit, accessToken, accountName, sharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, + postFeedScrolledPositionSharedPreferences, name, postType, sortType, postFilter, userWhere, readPostList); } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 81cc184b..93adbf0b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1035,4 +1035,6 @@ <string name="vote">Vote</string> + <string name="anonymous_front_page_no_subscriptions">Start by joining a subreddit!</string> + </resources> |