diff options
Diffstat (limited to '')
21 files changed, 227 insertions, 705 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index 38b477e8..45b0eba0 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -38,7 +38,6 @@ import ml.docilealligator.infinityforreddit.activities.SelectedSubredditsActivit import ml.docilealligator.infinityforreddit.activities.SendPrivateMessageActivity; import ml.docilealligator.infinityforreddit.activities.SettingsActivity; import ml.docilealligator.infinityforreddit.activities.SubmitCrosspostActivity; -import ml.docilealligator.infinityforreddit.activities.SubredditFilterPopularAndAllActivity; import ml.docilealligator.infinityforreddit.activities.SubredditMultiselectionActivity; import ml.docilealligator.infinityforreddit.activities.SubredditSelectionActivity; import ml.docilealligator.infinityforreddit.activities.SubscribedThingListingActivity; @@ -230,8 +229,6 @@ public interface AppComponent { void inject(MiscellaneousPreferenceFragment miscellaneousPreferenceFragment); - void inject(SubredditFilterPopularAndAllActivity subredditFilterPopularAndAllActivity); - void inject(CustomizePostFilterActivity customizePostFilterActivity); void inject(PostHistoryFragment postHistoryFragment); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchPostFilterAndReadPosts.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchPostFilterAndReadPosts.java new file mode 100644 index 00000000..fc9e54f5 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchPostFilterAndReadPosts.java @@ -0,0 +1,31 @@ +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/RedditDataRoomDatabase.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java index f90c814c..a385ff4a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditDataRoomDatabase.java @@ -26,8 +26,6 @@ import ml.docilealligator.infinityforreddit.recentsearchquery.RecentSearchQuery; import ml.docilealligator.infinityforreddit.recentsearchquery.RecentSearchQueryDao; import ml.docilealligator.infinityforreddit.subreddit.SubredditDao; import ml.docilealligator.infinityforreddit.subreddit.SubredditData; -import ml.docilealligator.infinityforreddit.subredditfilter.SubredditFilter; -import ml.docilealligator.infinityforreddit.subredditfilter.SubredditFilterDao; import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditDao; import ml.docilealligator.infinityforreddit.subscribedsubreddit.SubscribedSubredditData; import ml.docilealligator.infinityforreddit.subscribeduser.SubscribedUserDao; @@ -37,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, - SubredditFilter.class, ReadPost.class, PostFilter.class, PostFilterUsage.class}, version = 15) + ReadPost.class, PostFilter.class, PostFilterUsage.class}, version = 16) public abstract class RedditDataRoomDatabase extends RoomDatabase { private static RedditDataRoomDatabase INSTANCE; @@ -50,7 +48,7 @@ public abstract class RedditDataRoomDatabase extends RoomDatabase { .addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4, MIGRATION_4_5, 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_13_14, MIGRATION_14_15, MIGRATION_15_16) .build(); } } @@ -74,8 +72,6 @@ public abstract class RedditDataRoomDatabase extends RoomDatabase { public abstract RecentSearchQueryDao recentSearchQueryDao(); - public abstract SubredditFilterDao subredditFilterDao(); - public abstract ReadPostDao readPostDao(); public abstract PostFilterDao postFilterDao(); @@ -306,4 +302,11 @@ public abstract class RedditDataRoomDatabase extends RoomDatabase { "name_of_usage TEXT NOT NULL, PRIMARY KEY(name, usage, name_of_usage), FOREIGN KEY(name) REFERENCES post_filter(name) ON DELETE CASCADE)"); } }; + + private static final Migration MIGRATION_15_16 = new Migration(15, 16) { + @Override + public void migrate(@NonNull SupportSQLiteDatabase database) { + database.execSQL("DROP TABLE subreddit_filter"); + } + }; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditFilterPopularAndAllActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditFilterPopularAndAllActivity.java deleted file mode 100644 index 292e65be..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SubredditFilterPopularAndAllActivity.java +++ /dev/null @@ -1,168 +0,0 @@ -package ml.docilealligator.infinityforreddit.activities; - -import android.content.Context; -import android.content.Intent; -import android.content.SharedPreferences; -import android.os.Bundle; -import android.view.MenuItem; -import android.view.inputmethod.InputMethodManager; -import android.widget.EditText; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.appcompat.widget.Toolbar; -import androidx.coordinatorlayout.widget.CoordinatorLayout; -import androidx.lifecycle.ViewModelProvider; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; - -import com.google.android.material.appbar.AppBarLayout; -import com.google.android.material.dialog.MaterialAlertDialogBuilder; -import com.google.android.material.floatingactionbutton.FloatingActionButton; - -import javax.inject.Inject; -import javax.inject.Named; - -import butterknife.BindView; -import butterknife.ButterKnife; -import ml.docilealligator.infinityforreddit.adapters.SubredditFilterRecyclerViewAdapter; -import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; -import ml.docilealligator.infinityforreddit.Infinity; -import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.subredditfilter.DeleteSubredditFilter; -import ml.docilealligator.infinityforreddit.subredditfilter.InsertSubredditFilter; -import ml.docilealligator.infinityforreddit.subredditfilter.SubredditFilter; -import ml.docilealligator.infinityforreddit.subredditfilter.SubredditFilterViewModel; - -public class SubredditFilterPopularAndAllActivity extends BaseActivity { - - private static final int SUBREDDIT_SEARCH_REQUEST_CODE = 1; - @BindView(R.id.coordinator_layout_subreddit_filter_popular_and_all_activity) - CoordinatorLayout coordinatorLayout; - @BindView(R.id.appbar_layout_subreddit_filter_popular_and_all_activity) - AppBarLayout appBarLayout; - @BindView(R.id.toolbar_subreddit_filter_popular_and_all_activity) - Toolbar toolbar; - @BindView(R.id.recycler_view_subreddit_filter_popular_and_all_activity) - RecyclerView recyclerView; - @BindView(R.id.fab_subreddit_filter_popular_and_all_activity) - FloatingActionButton fab; - @Inject - @Named("default") - SharedPreferences sharedPreferences; - @Inject - RedditDataRoomDatabase redditDataRoomDatabase; - @Inject - CustomThemeWrapper customThemeWrapper; - SubredditFilterViewModel subredditFilterViewModel; - private SubredditFilterRecyclerViewAdapter adapter; - - @Override - protected void onCreate(Bundle savedInstanceState) { - ((Infinity) getApplication()).getAppComponent().inject(this); - - setImmersiveModeNotApplicable(); - - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_subreddit_filter_popular_and_all); - - ButterKnife.bind(this); - - applyCustomTheme(); - - setSupportActionBar(toolbar); - getSupportActionBar().setDisplayHomeAsUpEnabled(true); - - adapter = new SubredditFilterRecyclerViewAdapter(subredditFilter -> DeleteSubredditFilter.deleteSubredditFilter(redditDataRoomDatabase, subredditFilter, () -> {})); - recyclerView.setLayoutManager(new LinearLayoutManager(this)); - recyclerView.setAdapter(adapter); - recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { - @Override - public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) { - if (dy > 0) { - fab.hide(); - } else if (dy < 0) { - fab.show(); - } - } - }); - - subredditFilterViewModel = new ViewModelProvider(this, - new SubredditFilterViewModel.Factory(redditDataRoomDatabase)) - .get(SubredditFilterViewModel.class); - subredditFilterViewModel.getSubredditFilterLiveData().observe(this, subredditFilters -> adapter.updateSubredditsName(subredditFilters)); - - fab.setOnClickListener(view -> { - EditText thingEditText = (EditText) getLayoutInflater().inflate(R.layout.dialog_go_to_thing_edit_text, null); - thingEditText.requestFocus(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); - } - new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) - .setTitle(R.string.choose_a_subreddit) - .setView(thingEditText) - .setPositiveButton(R.string.ok, (dialogInterface, i) - -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } - SubredditFilter subredditFilter = new SubredditFilter(thingEditText.getText().toString(), SubredditFilter.TYPE_POPULAR_AND_ALL); - InsertSubredditFilter.insertSubredditFilter(redditDataRoomDatabase, subredditFilter, - () -> {}); - }) - .setNegativeButton(R.string.cancel, null) - .setNeutralButton(R.string.search, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } - - Intent intent = new Intent(this, SearchActivity.class); - intent.putExtra(SearchActivity.EXTRA_SEARCH_ONLY_SUBREDDITS, true); - startActivityForResult(intent, SUBREDDIT_SEARCH_REQUEST_CODE); - }) - .setOnDismissListener(dialogInterface -> { - if (imm != null) { - imm.hideSoftInputFromWindow(thingEditText.getWindowToken(), 0); - } - }) - .show(); - }); - } - - @Override - public boolean onOptionsItemSelected(@NonNull MenuItem item) { - if (item.getItemId() == android.R.id.home) { - finish(); - return true; - } - - return false; - } - - @Override - protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { - super.onActivityResult(requestCode, resultCode, data); - if (requestCode == SUBREDDIT_SEARCH_REQUEST_CODE && resultCode == RESULT_OK && data != null) { - SubredditFilter subredditFilter = new SubredditFilter(data.getStringExtra(SearchActivity.EXTRA_RETURN_SUBREDDIT_NAME), SubredditFilter.TYPE_POPULAR_AND_ALL); - InsertSubredditFilter.insertSubredditFilter(redditDataRoomDatabase, subredditFilter, - () -> {}); - } - } - - @Override - protected SharedPreferences getDefaultSharedPreferences() { - return sharedPreferences; - } - - @Override - protected CustomThemeWrapper getCustomThemeWrapper() { - return customThemeWrapper; - } - - @Override - protected void applyCustomTheme() { - applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar); - } -}
\ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java index fd76f4f7..4abf2e55 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostRecyclerViewAdapter.java @@ -1950,7 +1950,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView } void markPostRead(Post post) { - if (mAccessToken != null && !post.isRead()) { + if (mAccessToken != null && !post.isRead() && mMarkPostsAsRead) { post.markAsRead(); cardView.setBackgroundTintList(ColorStateList.valueOf(mReadPostCardViewBackgroundColor)); titleTextView.setTextColor(mReadPostTitleColor); @@ -2077,7 +2077,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView } Post post = getItem(position); if (post != null) { - ((PostBaseViewHolder) this).markPostRead(post); + markPostRead(post); Intent intent = new Intent(mActivity, ViewVideoActivity.class); if (post.isGfycat()) { intent.putExtra(ViewVideoActivity.EXTRA_VIDEO_TYPE, ViewVideoActivity.VIDEO_TYPE_GFYCAT); @@ -3009,7 +3009,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView } void markPostRead(Post post) { - if (mAccessToken != null && !post.isRead()) { + if (mAccessToken != null && !post.isRead() && mMarkPostsAsRead) { post.markAsRead(); itemView.setBackgroundColor(mReadPostCardViewBackgroundColor); titleTextView.setTextColor(mReadPostTitleColor); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditFilterRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditFilterRecyclerViewAdapter.java deleted file mode 100644 index 330eab39..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/SubredditFilterRecyclerViewAdapter.java +++ /dev/null @@ -1,72 +0,0 @@ -package ml.docilealligator.infinityforreddit.adapters; - -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ImageView; -import android.widget.TextView; - -import androidx.annotation.NonNull; -import androidx.recyclerview.widget.RecyclerView; - -import java.util.List; - -import butterknife.BindView; -import butterknife.ButterKnife; -import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.subredditfilter.SubredditFilter; - -public class SubredditFilterRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> { - private List<SubredditFilter> subredditFilters; - private ItemClickListener itemClickListener; - - public SubredditFilterRecyclerViewAdapter(ItemClickListener itemClickListener) { - this.itemClickListener = itemClickListener; - } - - public interface ItemClickListener { - void onItemDelete(SubredditFilter subredditFilter); - } - - @NonNull - @Override - public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { - return new SubredditNameViewHolder(LayoutInflater.from(parent.getContext()) - .inflate(R.layout.item_selected_subreddit, parent, false)); - } - - @Override - public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { - if (holder instanceof SubredditNameViewHolder) { - ((SubredditNameViewHolder) holder).subredditRedditNameTextView.setText(subredditFilters.get(position).getSubredditName()); - } - } - - @Override - public int getItemCount() { - return subredditFilters == null ? 0 : subredditFilters.size(); - } - - public void updateSubredditsName(List<SubredditFilter> subredditFilters) { - this.subredditFilters = subredditFilters; - notifyDataSetChanged(); - } - - class SubredditNameViewHolder extends RecyclerView.ViewHolder { - @BindView(R.id.subreddit_name_item_selected_subreddit) - TextView subredditRedditNameTextView; - @BindView(R.id.delete_image_view_item_selected_subreddit) - ImageView deleteImageView; - - public SubredditNameViewHolder(@NonNull View itemView) { - super(itemView); - - ButterKnife.bind(this, itemView); - - deleteImageView.setOnClickListener(view -> { - itemClickListener.onItemDelete(subredditFilters.get(getAdapterPosition())); - }); - } - } - -} 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 0c4a0e4e..f8a5a9d7 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/PostFragment.java @@ -52,6 +52,7 @@ import org.greenrobot.eventbus.Subscribe; import java.util.ArrayList; import java.util.Locale; +import java.util.concurrent.Executor; import javax.inject.Inject; import javax.inject.Named; @@ -63,10 +64,10 @@ 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.FragmentCommunicator; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.NetworkState; -import ml.docilealligator.infinityforreddit.postfilter.PostFilter; import ml.docilealligator.infinityforreddit.PostFragmentContentScrollingInterface; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; @@ -105,10 +106,9 @@ import ml.docilealligator.infinityforreddit.events.ShowThumbnailOnTheRightInComp import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.post.PostDataSource; import ml.docilealligator.infinityforreddit.post.PostViewModel; -import ml.docilealligator.infinityforreddit.readpost.FetchReadPosts; +import ml.docilealligator.infinityforreddit.postfilter.PostFilter; +import ml.docilealligator.infinityforreddit.postfilter.PostFilterUsage; import ml.docilealligator.infinityforreddit.readpost.ReadPost; -import ml.docilealligator.infinityforreddit.subredditfilter.FetchSubredditFilters; -import ml.docilealligator.infinityforreddit.subredditfilter.SubredditFilter; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.utils.Utils; import retrofit2.Retrofit; @@ -135,7 +135,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private static final String IS_IN_LAZY_MODE_STATE = "IILMS"; private static final String RECYCLER_VIEW_POSITION_STATE = "RVPS"; private static final String READ_POST_LIST_STATE = "RPLS"; - private static final String SUBREDDIT_FILTER_LIST_STATE = "SFLS"; private static final String HIDE_READ_POSTS_INDEX_STATE = "HRPIS"; private static final String POST_FILTER_STATE = "PFS"; @@ -180,12 +179,14 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Named("post_history") SharedPreferences mPostHistorySharedPreferences; @Inject - CustomThemeWrapper customThemeWrapper; + @Named("post_feed_scrolled_position_cache") + SharedPreferences mPostFeedScrolledPositionSharedPreferences; + @Inject + CustomThemeWrapper mCustomThemeWrapper; @Inject - ExoCreator exoCreator; + ExoCreator mExoCreator; @Inject - @Named("post_feed_scrolled_position_cache") - SharedPreferences postFeedScrolledPositionSharedPreferences; + Executor mExecutor; private RequestManager mGlide; private AppCompatActivity activity; private LinearLayoutManager mLinearLayoutManager; @@ -222,7 +223,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private boolean vibrateWhenActionTriggered; private float swipeActionThreshold; private ItemTouchHelper touchHelper; - private ArrayList<SubredditFilter> subredditFilterList; private ArrayList<ReadPost> readPosts; private Unbinder unbinder; @@ -387,15 +387,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator { isInLazyMode = savedInstanceState.getBoolean(IS_IN_LAZY_MODE_STATE); readPosts = savedInstanceState.getParcelableArrayList(READ_POST_LIST_STATE); - subredditFilterList = savedInstanceState.getParcelableArrayList(SUBREDDIT_FILTER_LIST_STATE); hideReadPostsIndex = savedInstanceState.getInt(HIDE_READ_POSTS_INDEX_STATE, 0); postFilter = savedInstanceState.getParcelable(POST_FILTER_STATE); } else { - //TODO: Initialize PostFilter postFilter = getArguments().getParcelable(EXTRA_FILTER); - if (postFilter == null) { - postFilter = new PostFilter(); - } } mPostRecyclerView.setOnTouchListener((view, motionEvent) -> { @@ -422,25 +417,30 @@ public class PostFragment extends Fragment implements FragmentCommunicator { String accessToken = getArguments().getString(EXTRA_ACCESS_TOKEN); accountName = getArguments().getString(EXTRA_ACCOUNT_NAME); - postFilter.allowNSFW = mNsfwAndSpoilerSharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.NSFW_BASE, false); int defaultPostLayout = Integer.parseInt(mSharedPreferences.getString(SharedPreferencesUtils.DEFAULT_POST_LAYOUT_KEY, "0")); savePostFeedScrolledPosition = mSharedPreferences.getBoolean(SharedPreferencesUtils.SAVE_FRONT_PAGE_SCROLLED_POSITION, false); Locale locale = getResources().getConfiguration().locale; + int usage; + String nameOfUsage; + if (postType == PostDataSource.TYPE_SEARCH) { subredditName = getArguments().getString(EXTRA_NAME); query = getArguments().getString(EXTRA_QUERY); + usage = PostFilterUsage.SEARCH_TYPE; + nameOfUsage = PostFilterUsage.NO_USAGE; + String sort = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TYPE_SEARCH_POST, SortType.Type.RELEVANCE.name()); String sortTime = mSortTypeSharedPreferences.getString(SharedPreferencesUtils.SORT_TIME_SEARCH_POST, SortType.Time.ALL.name()); sortType = new SortType(SortType.Type.valueOf(sort), SortType.Time.valueOf(sortTime)); postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_SEARCH_POST, defaultPostLayout); mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mGfycatRetrofit, - mRedgifsRetrofit, mRedditDataRoomDatabase, customThemeWrapper, locale, + mRedgifsRetrofit, mRedditDataRoomDatabase, mCustomThemeWrapper, locale, windowWidth, accessToken, accountName, postType, postLayout, true, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, - exoCreator, new PostRecyclerViewAdapter.Callback() { + mExoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { mPostViewModel.retryLoadingMore(); @@ -480,6 +480,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator { }); } else if (postType == PostDataSource.TYPE_SUBREDDIT) { subredditName = getArguments().getString(EXTRA_NAME); + + usage = PostFilterUsage.SUBREDDIT_TYPE; + nameOfUsage = subredditName; + String sort; String sortTime = null; @@ -497,10 +501,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mGfycatRetrofit, - mRedgifsRetrofit, mRedditDataRoomDatabase, customThemeWrapper, locale, + mRedgifsRetrofit, mRedditDataRoomDatabase, mCustomThemeWrapper, locale, windowWidth, accessToken, accountName, postType, postLayout, displaySubredditName, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, - exoCreator, new PostRecyclerViewAdapter.Callback() { + mExoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { mPostViewModel.retryLoadingMore(); @@ -538,6 +542,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator { }); } else if (postType == PostDataSource.TYPE_MULTI_REDDIT) { multiRedditPath = getArguments().getString(EXTRA_NAME); + + usage = PostFilterUsage.MULTIREDDIT_TYPE; + nameOfUsage = multiRedditPath; + String sort; String sortTime = null; @@ -557,10 +565,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mGfycatRetrofit, - mRedgifsRetrofit, mRedditDataRoomDatabase, customThemeWrapper, locale, + mRedgifsRetrofit, mRedditDataRoomDatabase, mCustomThemeWrapper, locale, windowWidth, accessToken, accountName, postType, postLayout, true, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, - exoCreator, new PostRecyclerViewAdapter.Callback() { + mExoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { mPostViewModel.retryLoadingMore(); @@ -599,6 +607,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } else if (postType == PostDataSource.TYPE_USER) { username = getArguments().getString(EXTRA_USER_NAME); where = getArguments().getString(EXTRA_USER_WHERE); + + usage = PostFilterUsage.USER_TYPE; + nameOfUsage = username; + if (where != null && where.equals(PostDataSource.USER_WHERE_SUBMITTED)) { CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) mFetchPostInfoLinearLayout.getLayoutParams(); params.height = ViewGroup.LayoutParams.WRAP_CONTENT; @@ -615,10 +627,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator { postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_USER_POST_BASE + username, defaultPostLayout); mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mGfycatRetrofit, - mRedgifsRetrofit, mRedditDataRoomDatabase, customThemeWrapper, locale, + mRedgifsRetrofit, mRedditDataRoomDatabase, mCustomThemeWrapper, locale, windowWidth, accessToken, accountName, postType, postLayout, true, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, - exoCreator, new PostRecyclerViewAdapter.Callback() { + mExoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { mPostViewModel.retryLoadingMore(); @@ -657,6 +669,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } }); } else { + 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()); @@ -667,10 +682,10 @@ public class PostFragment extends Fragment implements FragmentCommunicator { postLayout = mPostLayoutSharedPreferences.getInt(SharedPreferencesUtils.POST_LAYOUT_FRONT_PAGE_POST, defaultPostLayout); mAdapter = new PostRecyclerViewAdapter(activity, mOauthRetrofit, mRetrofit, mGfycatRetrofit, - mRedgifsRetrofit, mRedditDataRoomDatabase, customThemeWrapper, locale, + mRedgifsRetrofit, mRedditDataRoomDatabase, mCustomThemeWrapper, locale, windowWidth, accessToken, accountName, postType, postLayout, true, mSharedPreferences, mNsfwAndSpoilerSharedPreferences, mPostHistorySharedPreferences, - exoCreator, new PostRecyclerViewAdapter.Callback() { + mExoCreator, new PostRecyclerViewAdapter.Callback() { @Override public void retryLoadingMore() { mPostViewModel.retryLoadingMore(); @@ -715,25 +730,60 @@ public class PostFragment extends Fragment implements FragmentCommunicator { } if (accountName != null && !accountName.equals("")) { - if (mPostHistorySharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.MARK_POSTS_AS_READ_BASE, false) && readPosts == null) { + if (mPostHistorySharedPreferences.getBoolean(accountName + SharedPreferencesUtils.MARK_POSTS_AS_READ_BASE, false) && readPosts == null) { if (getArguments().getBoolean(EXTRA_DISABLE_READ_POSTS, false)) { - initializeAndBindPostViewModel(accessToken); + 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); + initializeAndBindPostViewModel(accessToken); + } + }); + } else { + initializeAndBindPostViewModel(accessToken); + } } else { - FetchReadPosts.fetchReadPosts(mRedditDataRoomDatabase, accountName, - postType == PostDataSource.TYPE_SUBREDDIT && subredditName != null && (subredditName.equals("all") || subredditName.equals("popular")), - (readPosts, subredditFilters) -> { + FetchPostFilterAndReadPosts.fetchPostFilterAndReadPosts(mRedditDataRoomDatabase, mExecutor, + new Handler(), accountName, usage, nameOfUsage, (postFilter, readPostList) -> { if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) { - this.readPosts = readPosts; - this.subredditFilterList = subredditFilters; + if (this.postFilter == null) { + this.postFilter = postFilter; + postFilter.allowNSFW = mNsfwAndSpoilerSharedPreferences.getBoolean((accountName == null ? "" : accountName) + SharedPreferencesUtils.NSFW_BASE, false); + } + this.readPosts = readPostList; initializeAndBindPostViewModel(accessToken); } }); } } else { - initializeAndBindPostViewModel(accessToken); + 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); + initializeAndBindPostViewModel(accessToken); + } + }); + } else { + initializeAndBindPostViewModel(accessToken); + } } } else { - initializeAndBindPostViewModelForAnonymous(accessToken); + 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); + } + }); + } else { + initializeAndBindPostViewModelForAnonymous(accessToken); + } } vibrateWhenActionTriggered = mSharedPreferences.getBoolean(SharedPreferencesUtils.VIBRATE_WHEN_ACTION_TRIGGERED, true); @@ -849,33 +899,26 @@ public class PostFragment extends Fragment implements FragmentCommunicator { if (postType == PostDataSource.TYPE_SEARCH) { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, accountName, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, + mPostFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, postFilter, readPosts)).get(PostViewModel.class); } else if (postType == PostDataSource.TYPE_SUBREDDIT) { - if (subredditName.equals("all") || subredditName.equals("popular")) { - mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, - accountName, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, - postFilter, readPosts, subredditFilterList)).get(PostViewModel.class); - } else { - mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, - accountName, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, - postFilter, readPosts)).get(PostViewModel.class); - } + mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, + 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, accountName, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, multiRedditPath, postType, sortType, + 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, accountName, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, username, postType, sortType, postFilter, + mPostFeedScrolledPositionSharedPreferences, username, postType, sortType, postFilter, where, readPosts)).get(PostViewModel.class); } else { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mOauthRetrofit, accessToken, - accountName, mSharedPreferences, postFeedScrolledPositionSharedPreferences, + accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, postType, sortType, postFilter, readPosts)).get(PostViewModel.class); } @@ -887,53 +930,30 @@ public class PostFragment extends Fragment implements FragmentCommunicator { if (postType == PostDataSource.TYPE_SEARCH) { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, accountName, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, + mPostFeedScrolledPositionSharedPreferences, subredditName, query, postType, sortType, postFilter, readPosts)).get(PostViewModel.class); } else if (postType == PostDataSource.TYPE_SUBREDDIT) { - if (subredditName.equals("all") || subredditName.equals("popular")) { - if (subredditFilterList != null) { - mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, - accountName, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, - postFilter, readPosts, subredditFilterList)).get(PostViewModel.class); - } else { - FetchSubredditFilters.fetchSubredditFilters(mRedditDataRoomDatabase, subredditFilters -> { - if (activity != null && !activity.isFinishing() && !activity.isDestroyed()) { - subredditFilterList = subredditFilters; - mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, - accountName, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, postType, - sortType, postFilter, readPosts, subredditFilterList)).get(PostViewModel.class); - - bindPostViewModel(); - } - }); - } - } else { - mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, - accountName, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, postFilter, - readPosts)).get(PostViewModel.class); - } + mPostViewModel = new ViewModelProvider(this, new PostViewModel.Factory(accessToken == null ? mRetrofit : mOauthRetrofit, accessToken, + 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, accountName, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, multiRedditPath, postType, sortType, postFilter, + 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, accountName, mSharedPreferences, - postFeedScrolledPositionSharedPreferences, username, postType, sortType, postFilter, + mPostFeedScrolledPositionSharedPreferences, username, postType, sortType, postFilter, where, readPosts)).get(PostViewModel.class); } else { mPostViewModel = new ViewModelProvider(PostFragment.this, new PostViewModel.Factory(mOauthRetrofit, accessToken, - accountName, mSharedPreferences, postFeedScrolledPositionSharedPreferences, + accountName, mSharedPreferences, mPostFeedScrolledPositionSharedPreferences, postType, sortType, postFilter, readPosts)).get(PostViewModel.class); } - if (mPostViewModel != null) { - bindPostViewModel(); - } + bindPostViewModel(); } private void bindPostViewModel() { @@ -1020,18 +1040,18 @@ public class PostFragment extends Fragment implements FragmentCommunicator { private void initializeSwipeActionDrawable() { if (swipeRightAction == SharedPreferencesUtils.SWIPE_ACITON_DOWNVOTE) { - backgroundSwipeRight = new ColorDrawable(customThemeWrapper.getDownvoted()); + backgroundSwipeRight = new ColorDrawable(mCustomThemeWrapper.getDownvoted()); drawableSwipeRight = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_downward_black_24dp, null); } else { - backgroundSwipeRight = new ColorDrawable(customThemeWrapper.getUpvoted()); + backgroundSwipeRight = new ColorDrawable(mCustomThemeWrapper.getUpvoted()); drawableSwipeRight = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_upward_black_24dp, null); } if (swipeLeftAction == SharedPreferencesUtils.SWIPE_ACITON_UPVOTE) { - backgroundSwipeLeft = new ColorDrawable(customThemeWrapper.getUpvoted()); + backgroundSwipeLeft = new ColorDrawable(mCustomThemeWrapper.getUpvoted()); drawableSwipeLeft = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_upward_black_24dp, null); } else { - backgroundSwipeLeft = new ColorDrawable(customThemeWrapper.getDownvoted()); + backgroundSwipeLeft = new ColorDrawable(mCustomThemeWrapper.getDownvoted()); drawableSwipeLeft = ResourcesCompat.getDrawable(activity.getResources(), R.drawable.ic_arrow_downward_black_24dp, null); } } @@ -1047,7 +1067,6 @@ public class PostFragment extends Fragment implements FragmentCommunicator { super.onSaveInstanceState(outState); outState.putBoolean(IS_IN_LAZY_MODE_STATE, isInLazyMode); outState.putParcelableArrayList(READ_POST_LIST_STATE, readPosts); - outState.putParcelableArrayList(SUBREDDIT_FILTER_LIST_STATE, subredditFilterList); if (mAdapter != null) { outState.putInt(HIDE_READ_POSTS_INDEX_STATE, mAdapter.getHideReadPostsIndex()); } @@ -1074,7 +1093,7 @@ public class PostFragment extends Fragment implements FragmentCommunicator { String accountNameForCache = accountName == null ? SharedPreferencesUtils.FRONT_PAGE_SCROLLED_POSITION_ANONYMOUS : accountName; String key = accountNameForCache + SharedPreferencesUtils.FRONT_PAGE_SCROLLED_POSITION_FRONT_PAGE_BASE; String value = currentPost.getFullName(); - postFeedScrolledPositionSharedPreferences.edit().putString(key, value).apply(); + mPostFeedScrolledPositionSharedPreferences.edit().putString(key, value).apply(); } } } @@ -1215,9 +1234,9 @@ public class PostFragment extends Fragment implements FragmentCommunicator { @Override public void applyTheme() { - mSwipeRefreshLayout.setProgressBackgroundColorSchemeColor(customThemeWrapper.getCircularProgressBarBackground()); - mSwipeRefreshLayout.setColorSchemeColors(customThemeWrapper.getColorAccent()); - mFetchPostInfoTextView.setTextColor(customThemeWrapper.getSecondaryTextColor()); + mSwipeRefreshLayout.setProgressBackgroundColorSchemeColor(mCustomThemeWrapper.getCircularProgressBarBackground()); + mSwipeRefreshLayout.setColorSchemeColors(mCustomThemeWrapper.getColorAccent()); + mFetchPostInfoTextView.setTextColor(mCustomThemeWrapper.getSecondaryTextColor()); } @Override @@ -1231,6 +1250,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); if (mPostViewModel != null) { mPostViewModel.changePostFilter(postFilter); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java index 6791dbdb..c5aa3990 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java @@ -16,7 +16,6 @@ import java.util.List; import ml.docilealligator.infinityforreddit.postfilter.PostFilter; import ml.docilealligator.infinityforreddit.readpost.ReadPost; -import ml.docilealligator.infinityforreddit.subredditfilter.SubredditFilter; import ml.docilealligator.infinityforreddit.utils.JSONUtils; import ml.docilealligator.infinityforreddit.utils.Utils; @@ -30,11 +29,6 @@ public class ParsePost { new ParsePostDataAsyncTask(response, nPosts, postFilter, readPostList, parsePostsListingListener).execute(); } - public static void parsePosts(String response, int nPosts, PostFilter postFilter, List<ReadPost> readPostList, - List<SubredditFilter> subredditFilterList, ParsePostsListingListener parsePostsListingListener) { - new ParsePostDataAsyncTask(response, nPosts, postFilter, readPostList, subredditFilterList, parsePostsListingListener).execute(); - } - public static void parsePost(String response, ParsePostListener parsePostListener) { PostFilter postFilter = new PostFilter(); postFilter.allowNSFW = true; @@ -498,7 +492,6 @@ public class ParsePost { private int nPosts; private PostFilter postFilter; private List<ReadPost> readPostList; - private List<SubredditFilter> subredditFilterList; private ParsePostsListingListener parsePostsListingListener; private ParsePostListener parsePostListener; private LinkedHashSet<Post> newPosts; @@ -524,12 +517,6 @@ public class ParsePost { } } - ParsePostDataAsyncTask(String response, int nPosts, PostFilter postFilter, List<ReadPost> readPostList, - List<SubredditFilter> subredditFilterList, ParsePostsListingListener parsePostsListingListener) { - this(response, nPosts, postFilter, readPostList, parsePostsListingListener); - this.subredditFilterList = subredditFilterList; - } - ParsePostDataAsyncTask(String response, PostFilter postFilter, ParsePostListener parsePostListener) { this.parsePostListener = parsePostListener; @@ -581,19 +568,10 @@ public class ParsePost { if (allData.getJSONObject(i).getString(JSONUtils.KIND_KEY).equals("t3")) { JSONObject data = allData.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY); Post post = parseBasicData(data); - boolean availablePost = true; if (readPostHashSet != null && readPostHashSet.contains(ReadPost.convertPost(post))) { post.markAsRead(); } - if (subredditFilterList != null) { - for (SubredditFilter subredditFilter : subredditFilterList) { - if (subredditFilter.getSubredditName().equals(post.getSubredditName())) { - availablePost = false; - break; - } - } - } - if (availablePost && PostFilter.isPostAllowed(post, postFilter)) { + if (PostFilter.isPostAllowed(post, postFilter)) { newPosts.add(post); } } 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 615df8f7..ceeccb94 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSource.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSource.java @@ -11,11 +11,10 @@ import java.util.LinkedHashSet; import java.util.List; import ml.docilealligator.infinityforreddit.NetworkState; -import ml.docilealligator.infinityforreddit.postfilter.PostFilter; 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.subredditfilter.SubredditFilter; import ml.docilealligator.infinityforreddit.utils.APIUtils; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import retrofit2.Call; @@ -48,7 +47,6 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { private SortType sortType; private PostFilter postFilter; private List<ReadPost> readPostList; - private List<SubredditFilter> subredditFilterList; private String userWhere; private String multiRedditPath; private LinkedHashSet<Post> postLinkedHashSet; @@ -82,7 +80,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { PostDataSource(Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String path, int postType, SortType sortType, PostFilter postFilter, - List<ReadPost> readPostList, List<SubredditFilter> subredditFilterList) { + List<ReadPost> readPostList) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -116,7 +114,6 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { } this.postFilter = postFilter; this.readPostList = readPostList; - this.subredditFilterList = subredditFilterList; postLinkedHashSet = new LinkedHashSet<>(); } @@ -373,7 +370,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, postFilter, readPostList, subredditFilterList, + ParsePost.parsePosts(response.body(), -1, postFilter, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { @@ -447,7 +444,7 @@ public class PostDataSource extends PageKeyedDataSource<String, Post> { @Override public void onResponse(@NonNull Call<String> call, @NonNull retrofit2.Response<String> response) { if (response.isSuccessful()) { - ParsePost.parsePosts(response.body(), -1, postFilter, readPostList, subredditFilterList, + ParsePost.parsePosts(response.body(), -1, postFilter, readPostList, new ParsePost.ParsePostsListingListener() { @Override public void onParsePostsListingSuccess(LinkedHashSet<Post> newPosts, String lastItem) { 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 777fb8ae..37da54fd 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSourceFactory.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostDataSourceFactory.java @@ -12,7 +12,6 @@ import java.util.List; import ml.docilealligator.infinityforreddit.postfilter.PostFilter; import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.readpost.ReadPost; -import ml.docilealligator.infinityforreddit.subredditfilter.SubredditFilter; import retrofit2.Retrofit; class PostDataSourceFactory extends DataSource.Factory { @@ -28,7 +27,6 @@ class PostDataSourceFactory extends DataSource.Factory { private PostFilter postFilter; private String userWhere; private List<ReadPost> readPostList; - private List<SubredditFilter> subredditFilterList; private PostDataSource postDataSource; private MutableLiveData<PostDataSource> postDataSourceLiveData; @@ -52,7 +50,7 @@ class PostDataSourceFactory extends DataSource.Factory { PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditName, int postType, SortType sortType, PostFilter postFilter, - List<ReadPost> readPostList, List<SubredditFilter> subredditFilterList) { + List<ReadPost> readPostList) { this.retrofit = retrofit; this.accessToken = accessToken; this.accountName = accountName; @@ -64,7 +62,6 @@ class PostDataSourceFactory extends DataSource.Factory { this.sortType = sortType; this.postFilter = postFilter; this.readPostList = readPostList; - this.subredditFilterList = subredditFilterList; } PostDataSourceFactory(Retrofit retrofit, String accessToken, String accountName, @@ -118,7 +115,7 @@ class PostDataSourceFactory extends DataSource.Factory { Log.i("asdasfd", "s5 " + (postFilter == null)); postDataSource = new PostDataSource(retrofit, accessToken, accountName, sharedPreferences, postFeedScrolledPositionSharedPreferences, subredditName, postType, - sortType, postFilter, readPostList, subredditFilterList); + sortType, postFilter, readPostList); } else { postDataSource = new PostDataSource(retrofit, accessToken, accountName, sharedPreferences, postFeedScrolledPositionSharedPreferences, subredditName, postType, 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 29803616..2fc1ef33 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/PostViewModel.java @@ -19,7 +19,6 @@ import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.postfilter.PostFilter; import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.readpost.ReadPost; -import ml.docilealligator.infinityforreddit.subredditfilter.SubredditFilter; import retrofit2.Retrofit; public class PostViewModel extends ViewModel { @@ -68,10 +67,10 @@ public class PostViewModel extends ViewModel { public PostViewModel(Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences cache, String subredditName, int postType, SortType sortType, PostFilter postFilter, - List<ReadPost> readPostList, List<SubredditFilter> subredditFilterList) { + List<ReadPost> readPostList) { postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, accountName, sharedPreferences, cache, subredditName, postType, sortType, postFilter, - readPostList, subredditFilterList); + readPostList); initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(), PostDataSource::getInitialLoadStateLiveData); @@ -213,7 +212,6 @@ public class PostViewModel extends ViewModel { private PostFilter postFilter; private String userWhere; private List<ReadPost> readPostList; - private List<SubredditFilter> subredditFilterList; public Factory(Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, @@ -245,25 +243,6 @@ public class PostViewModel extends ViewModel { this.readPostList = readPostList; } - //With subreddit filter - public Factory(Retrofit retrofit, String accessToken, String accountName, - SharedPreferences sharedPreferences, - SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditName, - int postType, SortType sortType, PostFilter postFilter, - List<ReadPost> readPostList, List<SubredditFilter> subredditFilterList) { - this.retrofit = retrofit; - this.accessToken = accessToken; - this.accountName = accountName; - this.sharedPreferences = sharedPreferences; - this.postFeedScrolledPositionSharedPreferences = postFeedScrolledPositionSharedPreferences; - this.subredditName = subredditName; - this.postType = postType; - this.sortType = sortType; - this.postFilter = postFilter; - this.readPostList = readPostList; - this.subredditFilterList = subredditFilterList; - } - //User posts public Factory(Retrofit retrofit, String accessToken, String accountName, SharedPreferences sharedPreferences, SharedPreferences postFeedScrolledPositionSharedPreferences, String subredditName, @@ -310,7 +289,7 @@ public class PostViewModel extends ViewModel { } else if (postType == PostDataSource.TYPE_SUBREDDIT || postType == PostDataSource.TYPE_MULTI_REDDIT) { return (T) new PostViewModel(retrofit, accessToken, accountName, sharedPreferences, postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, - postFilter, readPostList, subredditFilterList); + postFilter, readPostList); } else { return (T) new PostViewModel(retrofit, accessToken, accountName, sharedPreferences, postFeedScrolledPositionSharedPreferences, subredditName, postType, sortType, diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java index 67ae558d..63e26ee3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java @@ -9,6 +9,7 @@ import androidx.room.Entity; import androidx.room.Ignore; import androidx.room.PrimaryKey; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -167,6 +168,60 @@ public class PostFilter implements Parcelable { return true; } + public static PostFilter mergePostFilter(List<PostFilter> postFilterList) { + PostFilter postFilter = new PostFilter(); + if (postFilterList.size() == 1) { + return postFilterList.get(0); + } + StringBuilder stringBuilder; + postFilter.name = "Merged"; + for (PostFilter p : postFilterList) { + postFilter.maxVote = Math.min(p.maxVote, postFilter.maxVote); + postFilter.minVote = Math.max(p.minVote, postFilter.minVote); + postFilter.maxComments = Math.min(p.maxComments, postFilter.maxComments); + postFilter.minComments = Math.max(p.minComments, postFilter.minComments); + postFilter.maxAwards = Math.min(p.maxAwards, postFilter.maxAwards); + postFilter.minAwards = Math.max(p.minAwards, postFilter.minAwards); + + postFilter.onlyNSFW = p.onlyNSFW ? p.onlyNSFW : postFilter.onlyNSFW; + postFilter.onlySpoiler = p.onlySpoiler ? p.onlySpoiler : postFilter.onlySpoiler; + + postFilter.postTitleExcludesRegex = p.postTitleExcludesRegex.equals("") ? postFilter.postTitleExcludesRegex : p.postTitleExcludesRegex; + stringBuilder = new StringBuilder(postFilter.postTitleExcludesStrings); + stringBuilder.append(",").append(p.postTitleExcludesStrings); + postFilter.postTitleExcludesStrings = stringBuilder.toString(); + + postFilter.excludeSubreddits = p.excludeSubreddits.equals("") ? postFilter.excludeSubreddits : p.postTitleExcludesRegex; + stringBuilder = new StringBuilder(postFilter.excludeSubreddits); + stringBuilder.append(",").append(p.excludeSubreddits); + postFilter.excludeSubreddits = stringBuilder.toString(); + + postFilter.excludeUsers = p.excludeUsers.equals("") ? postFilter.excludeUsers : p.postTitleExcludesRegex; + stringBuilder = new StringBuilder(postFilter.excludeUsers); + stringBuilder.append(",").append(p.excludeUsers); + postFilter.excludeUsers = stringBuilder.toString(); + + postFilter.containFlairs = p.containFlairs.equals("") ? postFilter.containFlairs : p.postTitleExcludesRegex; + stringBuilder = new StringBuilder(postFilter.containFlairs); + stringBuilder.append(",").append(p.containFlairs); + postFilter.containFlairs = stringBuilder.toString(); + + postFilter.excludeFlairs = p.excludeFlairs.equals("") ? postFilter.excludeFlairs : p.postTitleExcludesRegex; + stringBuilder = new StringBuilder(postFilter.excludeFlairs); + stringBuilder.append(",").append(p.excludeFlairs); + postFilter.excludeFlairs = stringBuilder.toString(); + + postFilter.containTextType = p.containTextType || postFilter.containTextType; + postFilter.containLinkType = p.containLinkType || postFilter.containLinkType; + postFilter.containImageType = p.containImageType || postFilter.containImageType; + postFilter.containGifType = p.containGifType || postFilter.containGifType; + postFilter.containVideoType = p.containVideoType || postFilter.containVideoType; + postFilter.containGalleryType = p.containGalleryType || postFilter.containGalleryType; + } + + return postFilter; + } + protected PostFilter(Parcel in) { name = in.readString(); maxVote = in.readInt(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterDao.java index 0e08e44a..6595db38 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterDao.java @@ -25,4 +25,7 @@ public interface PostFilterDao { @Query("SELECT * FROM post_filter") LiveData<List<PostFilter>> getAllPostFiltersLiveData(); + + @Query("SELECT * FROM post_filter WHERE post_filter.name IN (SELECT post_filter_usage.name FROM post_filter_usage WHERE usage = :usage AND name_of_usage = :nameOfUsage)") + List<PostFilter> getValidPostFilters(int usage, String nameOfUsage); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/readpost/FetchReadPosts.java b/app/src/main/java/ml/docilealligator/infinityforreddit/readpost/FetchReadPosts.java index 33ade1f3..e76fd959 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/readpost/FetchReadPosts.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/readpost/FetchReadPosts.java @@ -5,49 +5,42 @@ import android.os.AsyncTask; import java.util.ArrayList; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; -import ml.docilealligator.infinityforreddit.subredditfilter.SubredditFilter; public class FetchReadPosts { public interface FetchReadPostsListener { - void success(ArrayList<ReadPost> readPosts, ArrayList<SubredditFilter> subredditFilters); + void success(ArrayList<ReadPost> readPosts); } public static void fetchReadPosts(RedditDataRoomDatabase redditDataRoomDatabase, String username, - boolean fetchSubredditFilter, FetchReadPostsListener fetchReadPostsListener) { - new FetchAllReadPostsAsyncTask(redditDataRoomDatabase, username, fetchSubredditFilter, fetchReadPostsListener).execute(); + FetchReadPostsListener fetchReadPostsListener) { + new FetchAllReadPostsAsyncTask(redditDataRoomDatabase, username, fetchReadPostsListener).execute(); } private static class FetchAllReadPostsAsyncTask extends AsyncTask<Void, Void, Void> { private RedditDataRoomDatabase redditDataRoomDatabase; private String username; - private boolean fetchSubredditFilter; private FetchReadPostsListener fetchReadPostsListener; private ArrayList<ReadPost> readPosts; - private ArrayList<SubredditFilter> subredditFilters; private FetchAllReadPostsAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, String username, - boolean fetchSubredditFilter, FetchReadPostsListener fetchReadPostsListener) { + FetchReadPostsListener fetchReadPostsListener) { this.redditDataRoomDatabase = redditDataRoomDatabase; this.username = username; - this.fetchSubredditFilter = fetchSubredditFilter; this.fetchReadPostsListener = fetchReadPostsListener; } @Override protected Void doInBackground(Void... voids) { readPosts = (ArrayList<ReadPost>) redditDataRoomDatabase.readPostDao().getAllReadPosts(username); - if (fetchSubredditFilter) { - subredditFilters = (ArrayList<SubredditFilter>) redditDataRoomDatabase.subredditFilterDao().getAllSubredditFilters(); - } return null; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); - fetchReadPostsListener.success(readPosts, subredditFilters); + fetchReadPostsListener.success(readPosts); } } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/FilterPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/FilterPreferenceFragment.java deleted file mode 100644 index 91f40c98..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/FilterPreferenceFragment.java +++ /dev/null @@ -1,38 +0,0 @@ -package ml.docilealligator.infinityforreddit.settings; - -import android.app.Activity; -import android.content.Context; -import android.content.Intent; -import android.os.Bundle; - -import androidx.annotation.NonNull; -import androidx.preference.Preference; -import androidx.preference.PreferenceFragmentCompat; - -import ml.docilealligator.infinityforreddit.activities.SubredditFilterPopularAndAllActivity; -import ml.docilealligator.infinityforreddit.R; -import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; - -public class FilterPreferenceFragment extends PreferenceFragmentCompat { - - private Activity activity; - - @Override - public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { - setPreferencesFromResource(R.xml.filter_preferences, rootKey); - - Preference subredditFilterPopularAndAllPreference = findPreference(SharedPreferencesUtils.SUBREDDIT_FILTER_POPULAR_AND_ALL); - - subredditFilterPopularAndAllPreference.setOnPreferenceClickListener(preference -> { - Intent intent = new Intent(activity, SubredditFilterPopularAndAllActivity.class); - activity.startActivity(intent); - return true; - }); - } - - @Override - public void onAttach(@NonNull Context context) { - super.onAttach(context); - activity = (Activity) context; - } -}
\ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subredditfilter/DeleteSubredditFilter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subredditfilter/DeleteSubredditFilter.java deleted file mode 100644 index 051d247a..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subredditfilter/DeleteSubredditFilter.java +++ /dev/null @@ -1,41 +0,0 @@ -package ml.docilealligator.infinityforreddit.subredditfilter; - -import android.os.AsyncTask; - -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; - -public class DeleteSubredditFilter { - public interface DeleteSubredditFilterListener { - void success(); - } - - public static void deleteSubredditFilter(RedditDataRoomDatabase redditDataRoomDatabase, SubredditFilter subredditFilter, - DeleteSubredditFilterListener deleteSubredditFilterListener) { - new DeleteSubredditFilterAsyncTask(redditDataRoomDatabase, subredditFilter, deleteSubredditFilterListener).execute(); - } - - private static class DeleteSubredditFilterAsyncTask extends AsyncTask<Void, Void, Void> { - private RedditDataRoomDatabase redditDataRoomDatabase; - private SubredditFilter subredditFilter; - private DeleteSubredditFilterListener deleteSubredditFilterListener; - - DeleteSubredditFilterAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, SubredditFilter subredditFilter, - DeleteSubredditFilterListener deleteSubredditFilterListener) { - this.redditDataRoomDatabase = redditDataRoomDatabase; - this.subredditFilter = subredditFilter; - this.deleteSubredditFilterListener = deleteSubredditFilterListener; - } - - @Override - protected Void doInBackground(Void... voids) { - redditDataRoomDatabase.subredditFilterDao().deleteSubredditFilter(subredditFilter); - return null; - } - - @Override - protected void onPostExecute(Void aVoid) { - super.onPostExecute(aVoid); - deleteSubredditFilterListener.success(); - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subredditfilter/FetchSubredditFilters.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subredditfilter/FetchSubredditFilters.java deleted file mode 100644 index e9c6f406..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subredditfilter/FetchSubredditFilters.java +++ /dev/null @@ -1,42 +0,0 @@ -package ml.docilealligator.infinityforreddit.subredditfilter; - -import android.os.AsyncTask; - -import java.util.ArrayList; - -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; - -public class FetchSubredditFilters { - public interface FetchSubredditFiltersListener { - void success(ArrayList<SubredditFilter>subredditFilters); - } - - public static void fetchSubredditFilters(RedditDataRoomDatabase redditDataRoomDatabase, - FetchSubredditFiltersListener fetchSubredditFiltersListener) { - new FetchSubredditFiltersAsyncTask(redditDataRoomDatabase, fetchSubredditFiltersListener).execute(); - } - - private static class FetchSubredditFiltersAsyncTask extends AsyncTask<Void, Void, Void> { - private RedditDataRoomDatabase redditDataRoomDatabase; - private ArrayList<SubredditFilter> subredditFilters; - private FetchSubredditFiltersListener fetchSubredditFiltersListener; - - FetchSubredditFiltersAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, - FetchSubredditFiltersListener fetchSubredditFiltersListener) { - this.redditDataRoomDatabase = redditDataRoomDatabase; - this.fetchSubredditFiltersListener = fetchSubredditFiltersListener; - } - - @Override - protected Void doInBackground(Void... voids) { - subredditFilters = (ArrayList<SubredditFilter>) redditDataRoomDatabase.subredditFilterDao().getAllSubredditFilters(); - return null; - } - - @Override - protected void onPostExecute(Void aVoid) { - super.onPostExecute(aVoid); - fetchSubredditFiltersListener.success(subredditFilters); - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subredditfilter/InsertSubredditFilter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subredditfilter/InsertSubredditFilter.java deleted file mode 100644 index eb26a964..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subredditfilter/InsertSubredditFilter.java +++ /dev/null @@ -1,41 +0,0 @@ -package ml.docilealligator.infinityforreddit.subredditfilter; - -import android.os.AsyncTask; - -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; - -public class InsertSubredditFilter { - public interface InsertSubredditFilterListener { - void success(); - } - - public static void insertSubredditFilter(RedditDataRoomDatabase redditDataRoomDatabase, SubredditFilter subredditFilter, - InsertSubredditFilterListener insertSubredditFilterListener) { - new InsertSubredditFilterAsyncTask(redditDataRoomDatabase, subredditFilter, insertSubredditFilterListener).execute(); - } - - private static class InsertSubredditFilterAsyncTask extends AsyncTask<Void, Void, Void> { - private RedditDataRoomDatabase redditDataRoomDatabase; - private SubredditFilter subredditFilter; - private InsertSubredditFilterListener insertSubredditFilterListener; - - InsertSubredditFilterAsyncTask(RedditDataRoomDatabase redditDataRoomDatabase, SubredditFilter subredditFilter, - InsertSubredditFilterListener insertSubredditFilterListener) { - this.redditDataRoomDatabase = redditDataRoomDatabase; - this.subredditFilter = subredditFilter; - this.insertSubredditFilterListener = insertSubredditFilterListener; - } - - @Override - protected Void doInBackground(Void... voids) { - redditDataRoomDatabase.subredditFilterDao().insert(subredditFilter); - return null; - } - - @Override - protected void onPostExecute(Void aVoid) { - super.onPostExecute(aVoid); - insertSubredditFilterListener.success(); - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subredditfilter/SubredditFilter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subredditfilter/SubredditFilter.java deleted file mode 100644 index 337ebab4..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subredditfilter/SubredditFilter.java +++ /dev/null @@ -1,68 +0,0 @@ -package ml.docilealligator.infinityforreddit.subredditfilter; - -import android.os.Parcel; -import android.os.Parcelable; - -import androidx.annotation.NonNull; -import androidx.room.ColumnInfo; -import androidx.room.Entity; - -@Entity(tableName = "subreddit_filter", primaryKeys = {"subreddit_name", "type"}) -public class SubredditFilter implements Parcelable { - public static int TYPE_POPULAR_AND_ALL = 0; - @NonNull - @ColumnInfo(name = "subreddit_name") - private String subredditName; - @ColumnInfo(name = "type") - private int type; - - public SubredditFilter(@NonNull String subredditName, int type) { - this.subredditName = subredditName; - this.type = type; - } - - protected SubredditFilter(Parcel in) { - subredditName = in.readString(); - type = in.readInt(); - } - - public static final Creator<SubredditFilter> CREATOR = new Creator<SubredditFilter>() { - @Override - public SubredditFilter createFromParcel(Parcel in) { - return new SubredditFilter(in); - } - - @Override - public SubredditFilter[] newArray(int size) { - return new SubredditFilter[size]; - } - }; - - @NonNull - public String getSubredditName() { - return subredditName; - } - - public void setSubredditName(@NonNull String subredditName) { - this.subredditName = subredditName; - } - - public int getType() { - return type; - } - - public void setType(int type) { - this.type = type; - } - - @Override - public int describeContents() { - return 0; - } - - @Override - public void writeToParcel(Parcel parcel, int i) { - parcel.writeString(subredditName); - parcel.writeInt(type); - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subredditfilter/SubredditFilterDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subredditfilter/SubredditFilterDao.java deleted file mode 100644 index 1cdcd22b..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subredditfilter/SubredditFilterDao.java +++ /dev/null @@ -1,25 +0,0 @@ -package ml.docilealligator.infinityforreddit.subredditfilter; - -import androidx.lifecycle.LiveData; -import androidx.room.Dao; -import androidx.room.Delete; -import androidx.room.Insert; -import androidx.room.OnConflictStrategy; -import androidx.room.Query; - -import java.util.List; - -@Dao -public interface SubredditFilterDao { - @Insert(onConflict = OnConflictStrategy.REPLACE) - void insert(SubredditFilter subredditFilter); - - @Query("SELECT * FROM subreddit_filter") - LiveData<List<SubredditFilter>> getAllSubredditFiltersLiveData(); - - @Query("SELECT * FROM subreddit_filter") - List<SubredditFilter> getAllSubredditFilters(); - - @Delete - void deleteSubredditFilter(SubredditFilter subredditFilter); -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/subredditfilter/SubredditFilterViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/subredditfilter/SubredditFilterViewModel.java deleted file mode 100644 index 9f571f30..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/subredditfilter/SubredditFilterViewModel.java +++ /dev/null @@ -1,36 +0,0 @@ -package ml.docilealligator.infinityforreddit.subredditfilter; - -import androidx.annotation.NonNull; -import androidx.lifecycle.LiveData; -import androidx.lifecycle.ViewModel; -import androidx.lifecycle.ViewModelProvider; - -import java.util.List; - -import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; - -public class SubredditFilterViewModel extends ViewModel { - private LiveData<List<SubredditFilter>> subredditFilterLiveData; - - public SubredditFilterViewModel(RedditDataRoomDatabase redditDataRoomDatabase) { - subredditFilterLiveData = redditDataRoomDatabase.subredditFilterDao().getAllSubredditFiltersLiveData(); - } - - public LiveData<List<SubredditFilter>> getSubredditFilterLiveData() { - return subredditFilterLiveData; - } - - public static class Factory extends ViewModelProvider.NewInstanceFactory { - private RedditDataRoomDatabase redditDataRoomDatabase; - - public Factory(RedditDataRoomDatabase redditDataRoomDatabase) { - this.redditDataRoomDatabase = redditDataRoomDatabase; - } - - @NonNull - @Override - public <T extends ViewModel> T create(@NonNull Class<T> modelClass) { - return (T) new SubredditFilterViewModel(redditDataRoomDatabase); - } - } -} |