aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java7
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizePostFilterActivity.java90
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterPreferenceActivity.java127
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/activities/SettingsActivity.java17
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/adapters/FilterFragmentPostFilterRecyclerViewAdapter.java85
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PostFilterOptionsBottomSheetFragment.java68
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilter.java2
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterDao.java8
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterViewModel.java36
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/SavePostFilter.java11
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/settings/MainPreferenceFragment.java11
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/utils/SharedPreferencesUtils.java1
12 files changed, 384 insertions, 79 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java
index 2f881530..d6c8ccf1 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java
@@ -6,22 +6,23 @@ import dagger.Component;
import ml.docilealligator.infinityforreddit.activities.AccountPostsActivity;
import ml.docilealligator.infinityforreddit.activities.AccountSavedThingActivity;
import ml.docilealligator.infinityforreddit.activities.CommentActivity;
-import ml.docilealligator.infinityforreddit.activities.CustomizePostFilterActivity;
-import ml.docilealligator.infinityforreddit.activities.FullMarkdownActivity;
import ml.docilealligator.infinityforreddit.activities.CreateMultiRedditActivity;
import ml.docilealligator.infinityforreddit.activities.CustomThemeListingActivity;
import ml.docilealligator.infinityforreddit.activities.CustomThemePreviewActivity;
+import ml.docilealligator.infinityforreddit.activities.CustomizePostFilterActivity;
import ml.docilealligator.infinityforreddit.activities.CustomizeThemeActivity;
import ml.docilealligator.infinityforreddit.activities.EditCommentActivity;
import ml.docilealligator.infinityforreddit.activities.EditMultiRedditActivity;
import ml.docilealligator.infinityforreddit.activities.EditPostActivity;
import ml.docilealligator.infinityforreddit.activities.FetchRandomSubredditOrPostActivity;
import ml.docilealligator.infinityforreddit.activities.FilteredPostsActivity;
+import ml.docilealligator.infinityforreddit.activities.FullMarkdownActivity;
import ml.docilealligator.infinityforreddit.activities.GiveAwardActivity;
import ml.docilealligator.infinityforreddit.activities.InboxActivity;
import ml.docilealligator.infinityforreddit.activities.LinkResolverActivity;
import ml.docilealligator.infinityforreddit.activities.LoginActivity;
import ml.docilealligator.infinityforreddit.activities.MainActivity;
+import ml.docilealligator.infinityforreddit.activities.PostFilterPreferenceActivity;
import ml.docilealligator.infinityforreddit.activities.PostImageActivity;
import ml.docilealligator.infinityforreddit.activities.PostLinkActivity;
import ml.docilealligator.infinityforreddit.activities.PostTextActivity;
@@ -233,4 +234,6 @@ public interface AppComponent {
void inject(CustomizePostFilterActivity customizePostFilterActivity);
void inject(PostHistoryFragment postHistoryFragment);
+
+ void inject(PostFilterPreferenceActivity postFilterPreferenceActivity);
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizePostFilterActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizePostFilterActivity.java
index 5a3d6185..3c2d675d 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizePostFilterActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizePostFilterActivity.java
@@ -1,7 +1,6 @@
package ml.docilealligator.infinityforreddit.activities;
import android.app.Activity;
-import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
@@ -10,9 +9,6 @@ import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
-import android.view.View;
-import android.view.inputmethod.InputMethodManager;
-import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
@@ -24,7 +20,6 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.appbar.CollapsingToolbarLayout;
import com.google.android.material.checkbox.MaterialCheckBox;
-import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.switchmaterial.SwitchMaterial;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
@@ -48,6 +43,7 @@ import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils;
public class CustomizePostFilterActivity extends BaseActivity {
public static final String EXTRA_POST_FILTER = "EPF";
+ public static final String EXTRA_FROM_SETTINGS = "EFS";
public static final String RETURN_EXTRA_POST_FILTER = "REPF";
private static final String POST_FILTER_STATE = "PFS";
@@ -59,6 +55,10 @@ public class CustomizePostFilterActivity extends BaseActivity {
CollapsingToolbarLayout collapsingToolbarLayout;
@BindView(R.id.toolbar_customize_post_filter_activity)
Toolbar toolbar;
+ @BindView(R.id.name_text_input_layout_customize_post_filter_activity)
+ TextInputLayout nameTextInputLayout;
+ @BindView(R.id.name_text_input_edit_text_customize_post_filter_activity)
+ TextInputEditText nameTextInputEditText;
@BindView(R.id.post_type_text_linear_layout_customize_post_filter_activity)
LinearLayout postTypeTextLinearLayout;
@BindView(R.id.post_type_text_text_view_customize_post_filter_activity)
@@ -165,6 +165,7 @@ public class CustomizePostFilterActivity extends BaseActivity {
@Inject
Executor mExecutor;
private PostFilter postFilter;
+ private boolean fromSettings;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -191,6 +192,8 @@ public class CustomizePostFilterActivity extends BaseActivity {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setToolbarGoToTop(toolbar);
+ fromSettings = getIntent().getBooleanExtra(EXTRA_FROM_SETTINGS, false);
+
postTypeTextLinearLayout.setOnClickListener(view -> {
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<RecyclerView.ViewHolder> {
+
+ private static final int VIEW_TYPE_HEADER = 1;
+ private static final int VIEW_TYPE_POST_FILTER = 2;
+
+ private final OnItemClickListener onItemClickListener;
+ private List<PostFilter> 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<PostFilter> 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<PostFilter> getAllPostFiltersLiveData();
+ LiveData<List<PostFilter>> 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<List<PostFilter>> mPostFilterListLiveData;
+
+ public PostFilterViewModel(RedditDataRoomDatabase redditDataRoomDatabase) {
+ mPostFilterListLiveData = redditDataRoomDatabase.postFilterDao().getAllPostFiltersLiveData();
+ }
+
+ public LiveData<List<PostFilter>> getPostFilterListLiveData() {
+ return mPostFilterListLiveData;
+ }
+
+ public static class Factory extends ViewModelProvider.NewInstanceFactory {
+
+ private final RedditDataRoomDatabase mRedditDataRoomDatabase;
+
+ public Factory(RedditDataRoomDatabase redditDataRoomDatabase) {
+ mRedditDataRoomDatabase = redditDataRoomDatabase;
+ }
+
+ @Override
+ public <T extends ViewModel> T create(Class<T> 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";