From fa55fb369dd4c893f300e75bfadb705a9e82233f Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sat, 26 Dec 2020 00:26:30 +0800 Subject: Replace Filter settings with Post Filter. --- app/src/main/AndroidManifest.xml | 7 +- .../infinityforreddit/AppComponent.java | 7 +- .../activities/CustomizePostFilterActivity.java | 90 +++++---------- .../activities/PostFilterPreferenceActivity.java | 127 +++++++++++++++++++++ .../activities/SettingsActivity.java | 17 +-- ...ilterFragmentPostFilterRecyclerViewAdapter.java | 85 ++++++++++++++ .../PostFilterOptionsBottomSheetFragment.java | 68 +++++++++++ .../infinityforreddit/postfilter/PostFilter.java | 2 + .../postfilter/PostFilterDao.java | 8 +- .../postfilter/PostFilterViewModel.java | 36 ++++++ .../postfilter/SavePostFilter.java | 11 +- .../settings/MainPreferenceFragment.java | 11 ++ .../utils/SharedPreferencesUtils.java | 1 + .../main/res/drawable-night/ic_apply_to_24dp.xml | 9 ++ app/src/main/res/drawable/ic_apply_to_24dp.xml | 9 ++ .../res/layout/activity_customize_post_filter.xml | 28 ++++- .../res/layout/activity_post_filter_preference.xml | 53 +++++++++ .../fragment_post_filter_options_bottom_sheet.xml | 70 ++++++++++++ app/src/main/res/layout/fragment_post_history.xml | 3 +- .../res/layout/item_filter_fragment_header.xml | 25 ++++ app/src/main/res/layout/item_post_filter.xml | 14 +++ app/src/main/res/values-de/strings.xml | 2 +- app/src/main/res/values-es/strings.xml | 2 +- app/src/main/res/values-fr/strings.xml | 2 +- app/src/main/res/values-hi/strings.xml | 2 +- app/src/main/res/values-hr/strings.xml | 2 +- app/src/main/res/values-it/strings.xml | 2 +- app/src/main/res/values-ja/strings.xml | 2 +- app/src/main/res/values-pt-rBR/strings.xml | 2 +- app/src/main/res/values-tr-rTR/strings.xml | 2 +- app/src/main/res/values-zh-rCN/strings.xml | 2 +- app/src/main/res/values/strings.xml | 29 ++--- app/src/main/res/xml/main_preferences.xml | 4 +- 33 files changed, 622 insertions(+), 112 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterPreferenceActivity.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/adapters/FilterFragmentPostFilterRecyclerViewAdapter.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PostFilterOptionsBottomSheetFragment.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterViewModel.java create mode 100644 app/src/main/res/drawable-night/ic_apply_to_24dp.xml create mode 100644 app/src/main/res/drawable/ic_apply_to_24dp.xml create mode 100644 app/src/main/res/layout/activity_post_filter_preference.xml create mode 100644 app/src/main/res/layout/fragment_post_filter_options_bottom_sheet.xml create mode 100644 app/src/main/res/layout/item_filter_fragment_header.xml create mode 100644 app/src/main/res/layout/item_post_filter.xml (limited to 'app/src/main') diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4374f488..d6f42976 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -32,9 +32,14 @@ android:theme="@style/AppTheme" android:usesCleartextTraffic="true" tools:replace="android:label"> - + { postTypeTextCheckBox.performClick(); }); @@ -235,6 +238,7 @@ public class CustomizePostFilterActivity extends BaseActivity { } private void bindView() { + nameTextInputEditText.setText(postFilter.name); postTypeTextCheckBox.setChecked(postFilter.containTextType); postTypeLinkCheckBox.setChecked(postFilter.containLinkType); postTypeImageCheckBox.setChecked(postFilter.containImageType); @@ -270,6 +274,9 @@ public class CustomizePostFilterActivity extends BaseActivity { coordinatorLayout.setBackgroundColor(mCustomThemeWrapper.getBackgroundColor()); applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar); int primaryTextColor = mCustomThemeWrapper.getPrimaryTextColor(); + nameTextInputLayout.setBoxStrokeColor(primaryTextColor); + nameTextInputLayout.setDefaultHintTextColor(ColorStateList.valueOf(primaryTextColor)); + nameTextInputEditText.setTextColor(primaryTextColor); postTypeTextTextView.setTextColor(primaryTextColor); postTypeLinkTextView.setTextColor(primaryTextColor); postTypeImageTextView.setTextColor(primaryTextColor); @@ -319,6 +326,9 @@ public class CustomizePostFilterActivity extends BaseActivity { @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.customize_post_filter_activity, menu); + if (fromSettings) { + menu.findItem(R.id.action_save_customize_post_filter_activity).setVisible(false); + } applyMenuItemTheme(menu); return true; } @@ -329,7 +339,7 @@ public class CustomizePostFilterActivity extends BaseActivity { finish(); return true; } else if (item.getItemId() == R.id.action_save_customize_post_filter_activity) { - PostFilter postFilter = constructPostFilter(); + constructPostFilter(); Intent returnIntent = new Intent(); returnIntent.putExtra(RETURN_EXTRA_POST_FILTER, postFilter); setResult(Activity.RESULT_OK, returnIntent); @@ -337,63 +347,25 @@ public class CustomizePostFilterActivity extends BaseActivity { return true; } else if (item.getItemId() == R.id.action_save_to_database_customize_post_filter_activity) { - PostFilter postFilter = constructPostFilter(); - - View dialogView = getLayoutInflater().inflate(R.layout.dialog_edit_name, null); - EditText nameEditText = dialogView.findViewById(R.id.theme_name_edit_text_edit_name_dialog); - nameEditText.setHint(R.string.post_filter_name_hint); - nameEditText.setText(postFilter.name); - nameEditText.requestFocus(); - InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); - if (imm != null) { - imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); + constructPostFilter(); + + if (!postFilter.name.equals("")) { + Handler handler = new Handler(); + SavePostFilter.savePostFilter(mRedditDataRoomDatabase, mExecutor, postFilter, () -> handler.post(() -> { + Intent returnIntent = new Intent(); + returnIntent.putExtra(RETURN_EXTRA_POST_FILTER, postFilter); + setResult(Activity.RESULT_OK, returnIntent); + finish(); + })); + } else { + Toast.makeText(CustomizePostFilterActivity.this, R.string.post_filter_requires_a_name, Toast.LENGTH_LONG).show(); } - new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) - .setTitle(R.string.edit_theme_name) - .setView(dialogView) - .setPositiveButton(R.string.ok, (dialogInterface, i) - -> { - if (imm != null) { - imm.hideSoftInputFromWindow(nameEditText.getWindowToken(), 0); - } - if (!nameEditText.getText().toString().equals("")) { - postFilter.name = nameEditText.getText().toString(); - Handler handler = new Handler(); - SavePostFilter.savePostFilter(mRedditDataRoomDatabase, mExecutor, postFilter, new SavePostFilter.SavePostFilterListener() { - @Override - public void success() { - handler.post(() -> { - Intent returnIntent = new Intent(); - returnIntent.putExtra(RETURN_EXTRA_POST_FILTER, postFilter); - setResult(Activity.RESULT_OK, returnIntent); - finish(); - }); - } - - @Override - public void failed(int errorCode) { - handler.post(() -> Toast.makeText(CustomizePostFilterActivity.this, R.string.duplicate_post_filter, Toast.LENGTH_LONG).show()); - } - }); - } - }) - .setNegativeButton(R.string.cancel, (dialogInterface, i) -> { - if (imm != null) { - imm.hideSoftInputFromWindow(nameEditText.getWindowToken(), 0); - } - }) - .setOnDismissListener(dialogInterface -> { - if (imm != null) { - imm.hideSoftInputFromWindow(nameEditText.getWindowToken(), 0); - } - }) - .show(); } return false; } - private PostFilter constructPostFilter() { - PostFilter postFilter = new PostFilter(); + private void constructPostFilter() { + postFilter.name = nameTextInputEditText.getText().toString(); postFilter.maxVote = maxVoteTextInputEditText.getText() == null || maxVoteTextInputEditText.getText().toString().equals("") ? -1 : Integer.parseInt(maxVoteTextInputEditText.getText().toString()); postFilter.minVote = minVoteTextInputEditText.getText() == null || minVoteTextInputEditText.getText().toString().equals("") ? -1 : Integer.parseInt(minVoteTextInputEditText.getText().toString()); postFilter.maxComments = maxCommentsTextInputEditText.getText() == null || maxCommentsTextInputEditText.getText().toString().equals("") ? -1 : Integer.parseInt(maxCommentsTextInputEditText.getText().toString()); @@ -414,8 +386,6 @@ public class CustomizePostFilterActivity extends BaseActivity { postFilter.containGalleryType = postTypeGalleryCheckBox.isChecked(); postFilter.onlyNSFW = onlyNSFWSwitch.isChecked(); postFilter.onlySpoiler = onlySpoilerSwitch.isChecked(); - - return postFilter; } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterPreferenceActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterPreferenceActivity.java new file mode 100644 index 00000000..b6ff5e9d --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterPreferenceActivity.java @@ -0,0 +1,127 @@ +package ml.docilealligator.infinityforreddit.activities; + +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.view.MenuItem; + +import androidx.annotation.NonNull; +import androidx.appcompat.widget.Toolbar; +import androidx.coordinatorlayout.widget.CoordinatorLayout; +import androidx.lifecycle.ViewModelProvider; +import androidx.recyclerview.widget.RecyclerView; + +import com.google.android.material.appbar.AppBarLayout; +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.Infinity; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.adapters.FilterFragmentPostFilterRecyclerViewAdapter; +import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostFilterOptionsBottomSheetFragment; +import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.postfilter.PostFilter; +import ml.docilealligator.infinityforreddit.postfilter.PostFilterViewModel; + +public class PostFilterPreferenceActivity extends BaseActivity { + + @BindView(R.id.coordinator_layout_post_filter_preference_activity) + CoordinatorLayout coordinatorLayout; + @BindView(R.id.appbar_layout_post_filter_preference_activity) + AppBarLayout appBarLayout; + @BindView(R.id.toolbar_post_filter_preference_activity) + Toolbar toolbar; + @BindView(R.id.recycler_view_post_filter_preference_activity) + RecyclerView recyclerView; + @BindView(R.id.fab_post_filter_preference_activity) + FloatingActionButton fab; + @Inject + @Named("default") + SharedPreferences sharedPreferences; + @Inject + RedditDataRoomDatabase redditDataRoomDatabase; + @Inject + CustomThemeWrapper customThemeWrapper; + public PostFilterViewModel postFilterViewModel; + private FilterFragmentPostFilterRecyclerViewAdapter adapter; + + @Override + protected void onCreate(Bundle savedInstanceState) { + ((Infinity) getApplication()).getAppComponent().inject(this); + + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_post_filter_preference); + + ButterKnife.bind(this); + + applyCustomTheme(); + + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + fab.setOnClickListener(view -> { + Intent intent = new Intent(PostFilterPreferenceActivity.this, CustomizePostFilterActivity.class); + intent.putExtra(CustomizePostFilterActivity.EXTRA_FROM_SETTINGS, true); + startActivity(intent); + }); + + adapter = new FilterFragmentPostFilterRecyclerViewAdapter(postFilter -> { + PostFilterOptionsBottomSheetFragment postFilterOptionsBottomSheetFragment = new PostFilterOptionsBottomSheetFragment(); + Bundle bundle = new Bundle(); + bundle.putParcelable(PostFilterOptionsBottomSheetFragment.EXTRA_POST_FILTER, postFilter); + postFilterOptionsBottomSheetFragment.setArguments(bundle); + postFilterOptionsBottomSheetFragment.show(getSupportFragmentManager(), postFilterOptionsBottomSheetFragment.getTag()); + }); + + recyclerView.setAdapter(adapter); + + postFilterViewModel = new ViewModelProvider(this, + new PostFilterViewModel.Factory(redditDataRoomDatabase)).get(PostFilterViewModel.class); + + postFilterViewModel.getPostFilterListLiveData().observe(this, postFilters -> adapter.setPostFilterList(postFilters)); + } + + public void editPostFilter(PostFilter postFilter) { + Intent intent = new Intent(PostFilterPreferenceActivity.this, CustomizePostFilterActivity.class); + intent.putExtra(CustomizePostFilterActivity.EXTRA_POST_FILTER, postFilter); + intent.putExtra(CustomizePostFilterActivity.EXTRA_FROM_SETTINGS, true); + startActivity(intent); + } + + public void applyPostFilterTo(PostFilter postFilter) { + + } + + public void deletePostFilter(PostFilter postFilter) { + + } + + @Override + protected SharedPreferences getDefaultSharedPreferences() { + return sharedPreferences; + } + + @Override + protected CustomThemeWrapper getCustomThemeWrapper() { + return customThemeWrapper; + } + + @Override + protected void applyCustomTheme() { + applyAppBarLayoutAndToolbarTheme(appBarLayout, toolbar); + } + + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + if (item.getItemId() == android.R.id.home) { + finish(); + return true; + } + return false; + } +} \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SettingsActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SettingsActivity.java index df889ed7..c8e19cb3 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SettingsActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/SettingsActivity.java @@ -22,12 +22,12 @@ import javax.inject.Named; import butterknife.BindView; import butterknife.ButterKnife; -import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccountAsyncTask; -import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; -import ml.docilealligator.infinityforreddit.events.RecreateActivityEvent; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; +import ml.docilealligator.infinityforreddit.asynctasks.GetCurrentAccountAsyncTask; +import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.events.RecreateActivityEvent; import ml.docilealligator.infinityforreddit.settings.AboutPreferenceFragment; import ml.docilealligator.infinityforreddit.settings.CustomizeBottomAppBarFragment; import ml.docilealligator.infinityforreddit.settings.CustomizeMainPageTabsFragment; @@ -85,13 +85,16 @@ public class SettingsActivity extends BaseActivity implements getSupportFragmentManager().addOnBackStackChangedListener(() -> { if (getSupportFragmentManager().getBackStackEntryCount() == 0) { setTitle(R.string.settings_activity_label); - } else if (getSupportFragmentManager().findFragmentById(R.id.frame_layout_settings_activity) instanceof AboutPreferenceFragment) { + return; + } + Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frame_layout_settings_activity); + if (fragment instanceof AboutPreferenceFragment) { setTitle(R.string.settings_about_master_title); - } else if (getSupportFragmentManager().findFragmentById(R.id.frame_layout_settings_activity) instanceof InterfacePreferenceFragment) { + } else if (fragment instanceof InterfacePreferenceFragment) { setTitle(R.string.settings_interface_title); - } else if (getSupportFragmentManager().findFragmentById(R.id.frame_layout_settings_activity) instanceof FontPreferenceFragment) { + } else if (fragment instanceof FontPreferenceFragment) { setTitle(R.string.settings_font_title); - } else if (getSupportFragmentManager().findFragmentById(R.id.frame_layout_settings_activity) instanceof GesturesAndButtonsPreferenceFragment) { + } else if (fragment instanceof GesturesAndButtonsPreferenceFragment) { setTitle(R.string.settings_gestures_and_buttons_title); } }); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/FilterFragmentPostFilterRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/FilterFragmentPostFilterRecyclerViewAdapter.java new file mode 100644 index 00000000..a7cc4981 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/FilterFragmentPostFilterRecyclerViewAdapter.java @@ -0,0 +1,85 @@ +package ml.docilealligator.infinityforreddit.adapters; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.List; + +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.postfilter.PostFilter; + +public class FilterFragmentPostFilterRecyclerViewAdapter extends RecyclerView.Adapter { + + private static final int VIEW_TYPE_HEADER = 1; + private static final int VIEW_TYPE_POST_FILTER = 2; + + private final OnItemClickListener onItemClickListener; + private List postFilterList; + + public interface OnItemClickListener { + void onItemClick(PostFilter postFilter); + } + + public FilterFragmentPostFilterRecyclerViewAdapter(OnItemClickListener onItemClickListener) { + this.onItemClickListener = onItemClickListener; + } + + @Override + public int getItemViewType(int position) { + if (position == 0) { + return VIEW_TYPE_HEADER; + } + return VIEW_TYPE_POST_FILTER; + } + + @NonNull + @Override + public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + if (viewType == VIEW_TYPE_HEADER) { + return new HeaderViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_filter_fragment_header, parent, false)); + } else { + return new PostFilterViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_post_filter, parent, false)); + } + } + + @Override + public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { + if (holder instanceof PostFilterViewHolder) { + ((PostFilterViewHolder) holder).textView.setText(postFilterList.get(position - 1).name); + } + } + + @Override + public int getItemCount() { + return postFilterList == null ? 1 : 1 + postFilterList.size(); + } + + public void setPostFilterList(List postFilterList) { + this.postFilterList = postFilterList; + notifyDataSetChanged(); + } + + private class PostFilterViewHolder extends RecyclerView.ViewHolder { + TextView textView; + + public PostFilterViewHolder(@NonNull View itemView) { + super(itemView); + textView = (TextView) itemView; + itemView.setOnClickListener(view -> { + onItemClickListener.onItemClick(postFilterList.get(getAdapterPosition() - 1)); + }); + } + } + + private static class HeaderViewHolder extends RecyclerView.ViewHolder { + + public HeaderViewHolder(@NonNull View itemView) { + super(itemView); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PostFilterOptionsBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PostFilterOptionsBottomSheetFragment.java new file mode 100644 index 00000000..bf1aff0b --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PostFilterOptionsBottomSheetFragment.java @@ -0,0 +1,68 @@ +package ml.docilealligator.infinityforreddit.bottomsheetfragments; + +import android.content.Context; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.TextView; + +import androidx.annotation.NonNull; + +import com.deishelon.roundedbottomsheet.RoundedBottomSheetDialogFragment; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.activities.PostFilterPreferenceActivity; +import ml.docilealligator.infinityforreddit.postfilter.PostFilter; + +public class PostFilterOptionsBottomSheetFragment extends RoundedBottomSheetDialogFragment { + + @BindView(R.id.edit_text_view_post_filter_options_bottom_sheet_fragment) + TextView editTextView; + @BindView(R.id.apply_to_text_view_post_filter_options_bottom_sheet_fragment) + TextView applyToTextView; + @BindView(R.id.delete_text_view_post_filter_options_bottom_sheet_fragment) + TextView deleteTextView; + public static final String EXTRA_POST_FILTER = "EPF"; + private PostFilterPreferenceActivity activity; + + public PostFilterOptionsBottomSheetFragment() { + // Required empty public constructor + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + // Inflate the layout for this fragment + View rootView = inflater.inflate(R.layout.fragment_post_filter_options_bottom_sheet, container, false); + + ButterKnife.bind(this, rootView); + + PostFilter postFilter = getArguments().getParcelable(EXTRA_POST_FILTER); + + editTextView.setOnClickListener(view -> { + activity.editPostFilter(postFilter); + dismiss(); + }); + + applyToTextView.setOnClickListener(view -> { + activity.applyPostFilterTo(postFilter); + dismiss(); + }); + + deleteTextView.setOnClickListener(view -> { + activity.deletePostFilter(postFilter); + dismiss(); + }); + + return rootView; + } + + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + activity = (PostFilterPreferenceActivity) context; + } +} \ No newline at end of file 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 b23db7a1..67ae558d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java @@ -168,6 +168,7 @@ public class PostFilter implements Parcelable { } protected PostFilter(Parcel in) { + name = in.readString(); maxVote = in.readInt(); minVote = in.readInt(); maxComments = in.readInt(); @@ -209,6 +210,7 @@ public class PostFilter implements Parcelable { @Override public void writeToParcel(Parcel parcel, int i) { + parcel.writeString(name); parcel.writeInt(maxVote); parcel.writeInt(minVote); parcel.writeInt(maxComments); 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 baddaa8f..0e08e44a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterDao.java @@ -2,10 +2,13 @@ package ml.docilealligator.infinityforreddit.postfilter; 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 PostFilterDao { @Insert(onConflict = OnConflictStrategy.REPLACE) @@ -14,9 +17,12 @@ public interface PostFilterDao { @Query("DELETE FROM post_filter") void deleteAllPostFilters(); + @Delete + void deletePostFilter(PostFilter postFilter); + @Query("SELECT * FROM post_filter WHERE name = :name LIMIT 1") PostFilter getPostFilter(String name); @Query("SELECT * FROM post_filter") - LiveData getAllPostFiltersLiveData(); + LiveData> getAllPostFiltersLiveData(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterViewModel.java new file mode 100644 index 00000000..48acd7f7 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterViewModel.java @@ -0,0 +1,36 @@ +package ml.docilealligator.infinityforreddit.postfilter; + +import androidx.lifecycle.LiveData; +import androidx.lifecycle.ViewModel; +import androidx.lifecycle.ViewModelProvider; + +import java.util.List; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class PostFilterViewModel extends ViewModel { + private LiveData> mPostFilterListLiveData; + + public PostFilterViewModel(RedditDataRoomDatabase redditDataRoomDatabase) { + mPostFilterListLiveData = redditDataRoomDatabase.postFilterDao().getAllPostFiltersLiveData(); + } + + public LiveData> getPostFilterListLiveData() { + return mPostFilterListLiveData; + } + + public static class Factory extends ViewModelProvider.NewInstanceFactory { + + private final RedditDataRoomDatabase mRedditDataRoomDatabase; + + public Factory(RedditDataRoomDatabase redditDataRoomDatabase) { + mRedditDataRoomDatabase = redditDataRoomDatabase; + } + + @Override + public T create(Class modelClass) { + //noinspection unchecked + return (T) new PostFilterViewModel(mRedditDataRoomDatabase); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/SavePostFilter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/SavePostFilter.java index f129a4dd..f7362925 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/SavePostFilter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/SavePostFilter.java @@ -5,23 +5,16 @@ import java.util.concurrent.Executor; import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; public class SavePostFilter { - public static final int ERROR_DUPLICATE_NAME = 1; - public interface SavePostFilterListener { //Need to make sure it is running in the UI thread. void success(); - void failed(int errorCode); } public static void savePostFilter(RedditDataRoomDatabase redditDataRoomDatabase, Executor executor, PostFilter postFilter, SavePostFilterListener savePostFilterListener) { executor.execute(() -> { - if (redditDataRoomDatabase.postFilterDao().getPostFilter(postFilter.name) == null) { - redditDataRoomDatabase.postFilterDao().insert(postFilter); - savePostFilterListener.success(); - } else { - savePostFilterListener.failed(ERROR_DUPLICATE_NAME); - } + redditDataRoomDatabase.postFilterDao().insert(postFilter); + savePostFilterListener.success(); }); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/MainPreferenceFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/MainPreferenceFragment.java index e9adbe8b..92a53a90 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/MainPreferenceFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/MainPreferenceFragment.java @@ -2,6 +2,7 @@ package ml.docilealligator.infinityforreddit.settings; import android.content.Context; +import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; @@ -16,6 +17,7 @@ import javax.inject.Named; import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.activities.PostFilterPreferenceActivity; import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; import static androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_STRONG; @@ -34,6 +36,7 @@ public class MainPreferenceFragment extends PreferenceFragmentCompat { ((Infinity) activity.getApplication()).getAppComponent().inject(this); Preference securityPreference = findPreference(SharedPreferencesUtils.SECURITY); + Preference postFilterPreference = findPreference(SharedPreferencesUtils.POST_FILTER); BiometricManager biometricManager = BiometricManager.from(activity); if (biometricManager.canAuthenticate(BIOMETRIC_STRONG | DEVICE_CREDENTIAL) != BiometricManager.BIOMETRIC_SUCCESS) { @@ -41,6 +44,14 @@ public class MainPreferenceFragment extends PreferenceFragmentCompat { securityPreference.setVisible(false); } } + + if (postFilterPreference != null) { + postFilterPreference.setOnPreferenceClickListener(preference -> { + Intent intent = new Intent(activity, PostFilterPreferenceActivity.class); + activity.startActivity(intent); + return true; + }); + } } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java index 1f3fab50..a8f0aa34 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java @@ -148,6 +148,7 @@ public class SharedPreferencesUtils { public static final String LANGUAGE = "language"; public static final String LANGUAGE_DEFAULT_VALUE = "auto"; public static final String ENABLE_SEARCH_HISTORY = "enable_search_history"; + public static final String POST_FILTER = "post_filter"; public static final String MAIN_PAGE_TABS_SHARED_PREFERENCES_FILE = "ml.docilealligator.infinityforreddit.main_page_tabs"; public static final String MAIN_PAGE_TAB_COUNT = "_main_page_tab_count"; diff --git a/app/src/main/res/drawable-night/ic_apply_to_24dp.xml b/app/src/main/res/drawable-night/ic_apply_to_24dp.xml new file mode 100644 index 00000000..ee73a4df --- /dev/null +++ b/app/src/main/res/drawable-night/ic_apply_to_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_apply_to_24dp.xml b/app/src/main/res/drawable/ic_apply_to_24dp.xml new file mode 100644 index 00000000..23e47b34 --- /dev/null +++ b/app/src/main/res/drawable/ic_apply_to_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/activity_customize_post_filter.xml b/app/src/main/res/layout/activity_customize_post_filter.xml index 72aeb6bc..b7940c7a 100644 --- a/app/src/main/res/layout/activity_customize_post_filter.xml +++ b/app/src/main/res/layout/activity_customize_post_filter.xml @@ -43,6 +43,26 @@ android:layout_height="wrap_content" android:orientation="vertical"> + + + + + + + android:hint="@string/exclude_subreddits_hint" /> @@ -359,7 +379,7 @@ android:layout_height="wrap_content" android:fontFamily="?attr/font_family" android:textSize="?attr/font_default" - android:hint="@string/excludes_users_hint" /> + android:hint="@string/exclude_users_hint" /> @@ -379,7 +399,7 @@ android:layout_height="wrap_content" android:fontFamily="?attr/font_family" android:textSize="?attr/font_default" - android:hint="@string/excludes_flairs_hint" /> + android:hint="@string/exclude_flairs_hint" /> @@ -399,7 +419,7 @@ android:layout_height="wrap_content" android:fontFamily="?attr/font_family" android:textSize="?attr/font_default" - android:hint="@string/contains_flairs_hint" /> + android:hint="@string/contain_flairs_hint" /> diff --git a/app/src/main/res/layout/activity_post_filter_preference.xml b/app/src/main/res/layout/activity_post_filter_preference.xml new file mode 100644 index 00000000..7db6ad95 --- /dev/null +++ b/app/src/main/res/layout/activity_post_filter_preference.xml @@ -0,0 +1,53 @@ + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_post_filter_options_bottom_sheet.xml b/app/src/main/res/layout/fragment_post_filter_options_bottom_sheet.xml new file mode 100644 index 00000000..a9c9310e --- /dev/null +++ b/app/src/main/res/layout/fragment_post_filter_options_bottom_sheet.xml @@ -0,0 +1,70 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_post_history.xml b/app/src/main/res/layout/fragment_post_history.xml index 9ed65c7f..325d74e1 100644 --- a/app/src/main/res/layout/fragment_post_history.xml +++ b/app/src/main/res/layout/fragment_post_history.xml @@ -4,8 +4,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" - android:orientation="vertical" - tools:context=".settings.NsfwAndBlurringFragment"> + tools:context=".settings.PostHistoryFragment"> + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_post_filter.xml b/app/src/main/res/layout/item_post_filter.xml new file mode 100644 index 00000000..62a5e886 --- /dev/null +++ b/app/src/main/res/layout/item_post_filter.xml @@ -0,0 +1,14 @@ + + \ No newline at end of file diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index e6e64517..d4068354 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -892,7 +892,7 @@ Reiter könnten den Inhalt verlieren, wenn zu anderen gewechselt wird. Selbes Ve "Sonstiges" "Vom Subreddit vorgeschlagenen Kommentar Sortier-Typ respektieren" "Kommentar Sortier-Typ wird nicht gespeichert" - "Filter" + "Filter" "Verstecke Subreddits" "In r/popular und r/all" "UFO-Aufnahmeanimation" diff --git a/app/src/main/res/values-es/strings.xml b/app/src/main/res/values-es/strings.xml index fba9fb2b..6419c67e 100644 --- a/app/src/main/res/values-es/strings.xml +++ b/app/src/main/res/values-es/strings.xml @@ -872,6 +872,6 @@ Videos de Reddit están en menor resolución" "Descargando" "r/all y r/popular" "Misceláneo" - "Filtrar" + "Filtrar" "Seleccione calidad del video" \ No newline at end of file diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 2a25371e..24a5fbcf 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -930,7 +930,7 @@ Les onglets pourraient perdre tout leur contenu quand vous passez d'un onglet à "Divers" "Respecter l'ordre de tri recommandé par le subreddit pour les commentaires " "L'ordre de tri des commentaires ne sera pas sauvegardé " - "Filtre" + "Filtre" "Cacher des Subreddits" "Dans r/popular et r/all" "Animation de capture de l'OVNI" diff --git a/app/src/main/res/values-hi/strings.xml b/app/src/main/res/values-hi/strings.xml index 61990aa4..158888e1 100644 --- a/app/src/main/res/values-hi/strings.xml +++ b/app/src/main/res/values-hi/strings.xml @@ -918,7 +918,7 @@ https://play.google.com/store/apps/details?id=ml.docilealligator.infinityforredd "विविध " "सबरैडिट द्वारा संस्तुतित टिप्पणी क्रम प्रकार बनाए रखें " "टिप्पणी क्रम प्रकार सरंक्षित नहीं किया जाएगा " - "छंटनी " + "छंटनी " "सबरैडिट छुपाएं " "r/popular एवं r/all में " "UFO बंदी बनाते हुए एनिमेशन " diff --git a/app/src/main/res/values-hr/strings.xml b/app/src/main/res/values-hr/strings.xml index e92edeab..5ee49cff 100644 --- a/app/src/main/res/values-hr/strings.xml +++ b/app/src/main/res/values-hr/strings.xml @@ -880,7 +880,7 @@ Poruka: %2$s" "Razno" "Poštuj tip sortiranja komentara preporučen na podredditu" "Tip sortiranja komentara neće biti spremljen" - "Filter" + "Filter" "Sakrij podreddite" "U r/popular i r/all" "UFO Capturing Animation" diff --git a/app/src/main/res/values-it/strings.xml b/app/src/main/res/values-it/strings.xml index d9af97a8..d404728c 100644 --- a/app/src/main/res/values-it/strings.xml +++ b/app/src/main/res/values-it/strings.xml @@ -935,7 +935,7 @@ I video di Reddit sono in minore risoluzione." "Il tipo di ordinamento dei commenti non verrà salvato" - "Filtro" + "Filtro" "Nascondi i Subreddit" diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index a0b55ad4..01c478b6 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -926,7 +926,7 @@ https://play.google.com/store/apps/details?id=ml.docilealligator.infinityforredd "その他の設定" "Subredditで推奨されているコメントの並び順を使用" "コメントの並び順は保存されません" - "フィルター" + "フィルター" "Subredditを非表示" "r/all と r/popular 内" "UFO捕獲アニメーション" diff --git a/app/src/main/res/values-pt-rBR/strings.xml b/app/src/main/res/values-pt-rBR/strings.xml index 73021efd..81f4493a 100644 --- a/app/src/main/res/values-pt-rBR/strings.xml +++ b/app/src/main/res/values-pt-rBR/strings.xml @@ -849,7 +849,7 @@ Os vídeos do Reddit ficam em menor resolução." "Miscelãnea" "Respeitar modo de classificação de comentário recomendado pelo subreddit" "Modo de classificação de comentário não será salvo" - "Filtrar" + "Filtrar" "Ocultar subreddits" "Em r/popular e r/all" "Animação de captura de OVNIs" diff --git a/app/src/main/res/values-tr-rTR/strings.xml b/app/src/main/res/values-tr-rTR/strings.xml index b845444a..37f7aff0 100644 --- a/app/src/main/res/values-tr-rTR/strings.xml +++ b/app/src/main/res/values-tr-rTR/strings.xml @@ -863,7 +863,7 @@ Sekmeler, başkalarına geçtikten sonra tüm içeriği kaybedebilir. Bu, sayfay "Diğerleri" "İlgili Subreddit Önerilen Yorum Sıralaması Türü" "Yorum sıralaması türü kaydedilemedi" - "Filtrele" + "Filtrele" "Subredditleri Gizle" "r/popular ve r/all" "UFO Yakalama Animasyonu" diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 2f9c2a2e..e27facdb 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -913,7 +913,7 @@ https://play.google.com/store/apps/details?id=ml.docilealligator.infinityforredd "其他" "关于板块推荐评论排序类型" "评论排序类型将不会保存" - "过滤器" + "过滤器" "隐藏版块" "在“r/all”和“r/popular”板块中" "开屏UFO动画" diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index afeeb6f4..4d50431b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -34,8 +34,9 @@ Crosspost Give Award r/all and r/popular - Post Filter + Customize Post Filter Filtered Posts + Post Filter Open navigation drawer Close navigation drawer @@ -504,7 +505,7 @@ Miscellaneous Respect Subreddit Recommended Comment Sort Type Comment sort type will not be saved - Filter + Post Filter Hide Subreddits In r/popular and r/all UFO Capturing Animation @@ -965,18 +966,18 @@ Only Spoiler Title: excludes keywords Title: excludes regex - Excludes subreddits - Excludes users - Exclude flairs - Contains flairs - Min vote - Max vote - Min comments - Max comments - Min awards - Max awards + Exclude subreddits + Exclude users + Exclude flairs + Contain flairs + Min vote (-1: no restriction) + Max vote (-1: no restriction) + Min comments (-1: no restriction) + Max comments (-1: no restriction) + Min awards (-1: no restriction) + Max awards (-1: no restriction) Post Filter Name - Duplicate post filter found. Please use another name. - + What is the name of this post filter? + Apply to diff --git a/app/src/main/res/xml/main_preferences.xml b/app/src/main/res/xml/main_preferences.xml index 334ebd1a..e2dac88d 100644 --- a/app/src/main/res/xml/main_preferences.xml +++ b/app/src/main/res/xml/main_preferences.xml @@ -63,9 +63,9 @@ app:fragment="ml.docilealligator.infinityforreddit.settings.PostHistoryFragment" /> + app:title="@string/settings_post_filter_title" />