From 97b5c5d5c6e4f0327aaa0e864ae1ce36b6e26a15 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Sat, 26 Dec 2020 15:25:40 +0800 Subject: Continue adding post filter. --- .../infinityforreddit/AppComponent.java | 3 + .../activities/CustomThemeListingActivity.java | 4 +- .../activities/PostFilterPreferenceActivity.java | 11 +- .../activities/PostFilterUsageListingActivity.java | 197 +++++++++++++++++++++ .../CustomizeThemeRecyclerViewAdapter.java | 2 +- .../PostFilterUsageRecyclerViewAdapter.java | 93 ++++++++++ .../NewPostFilterUsageBottomSheetFragment.java | 79 +++++++++ .../PostFilterUsageOptionsBottomSheetFragment.java | 66 +++++++ .../postfilter/DeletePostFilter.java | 16 ++ .../postfilter/DeletePostFilterUsage.java | 12 ++ .../postfilter/PostFilterUsage.java | 48 ++++- .../postfilter/PostFilterUsageDao.java | 15 ++ .../postfilter/PostFilterUsageViewModel.java | 38 ++++ .../postfilter/SavePostFilterUsage.java | 12 ++ 14 files changed, 589 insertions(+), 7 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterUsageListingActivity.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostFilterUsageRecyclerViewAdapter.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/NewPostFilterUsageBottomSheetFragment.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PostFilterUsageOptionsBottomSheetFragment.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/DeletePostFilter.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/DeletePostFilterUsage.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterUsageViewModel.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/SavePostFilterUsage.java (limited to 'app/src/main/java') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index d6c8ccf1..38b477e8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -22,6 +22,7 @@ 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.PostFilterUsageListingActivity; import ml.docilealligator.infinityforreddit.activities.PostFilterPreferenceActivity; import ml.docilealligator.infinityforreddit.activities.PostImageActivity; import ml.docilealligator.infinityforreddit.activities.PostLinkActivity; @@ -236,4 +237,6 @@ public interface AppComponent { void inject(PostHistoryFragment postHistoryFragment); void inject(PostFilterPreferenceActivity postFilterPreferenceActivity); + + void inject(PostFilterUsageListingActivity postFilterUsageListingActivity); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomThemeListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomThemeListingActivity.java index edc3a2ac..a42c3fa4 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomThemeListingActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomThemeListingActivity.java @@ -155,7 +155,7 @@ public class CustomThemeListingActivity extends BaseActivity implements @Override public void changeName(String oldThemeName) { View dialogView = getLayoutInflater().inflate(R.layout.dialog_edit_name, null); - EditText themeNameEditText = dialogView.findViewById(R.id.theme_name_edit_text_edit_name_dialog); + EditText themeNameEditText = dialogView.findViewById(R.id.name_edit_text_edit_name_dialog); themeNameEditText.setText(oldThemeName); themeNameEditText.requestFocus(); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); @@ -294,7 +294,7 @@ public class CustomThemeListingActivity extends BaseActivity implements .setPositiveButton(R.string.rename, (dialogInterface, i) -> { View dialogView = getLayoutInflater().inflate(R.layout.dialog_edit_name, null); - EditText themeNameEditText = dialogView.findViewById(R.id.theme_name_edit_text_edit_name_dialog); + EditText themeNameEditText = dialogView.findViewById(R.id.name_edit_text_edit_name_dialog); themeNameEditText.setText(customTheme.name); themeNameEditText.requestFocus(); InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterPreferenceActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterPreferenceActivity.java index b6ff5e9d..94597df6 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterPreferenceActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterPreferenceActivity.java @@ -14,6 +14,8 @@ import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.appbar.AppBarLayout; import com.google.android.material.floatingactionbutton.FloatingActionButton; +import java.util.concurrent.Executor; + import javax.inject.Inject; import javax.inject.Named; @@ -25,6 +27,7 @@ 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.DeletePostFilter; import ml.docilealligator.infinityforreddit.postfilter.PostFilter; import ml.docilealligator.infinityforreddit.postfilter.PostFilterViewModel; @@ -47,6 +50,8 @@ public class PostFilterPreferenceActivity extends BaseActivity { RedditDataRoomDatabase redditDataRoomDatabase; @Inject CustomThemeWrapper customThemeWrapper; + @Inject + Executor executor; public PostFilterViewModel postFilterViewModel; private FilterFragmentPostFilterRecyclerViewAdapter adapter; @@ -94,11 +99,13 @@ public class PostFilterPreferenceActivity extends BaseActivity { } public void applyPostFilterTo(PostFilter postFilter) { - + Intent intent = new Intent(this, PostFilterUsageListingActivity.class); + intent.putExtra(PostFilterUsageListingActivity.EXTRA_POST_FILTER, postFilter); + startActivity(intent); } public void deletePostFilter(PostFilter postFilter) { - + DeletePostFilter.deletePostFilter(redditDataRoomDatabase, executor, postFilter); } @Override diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterUsageListingActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterUsageListingActivity.java new file mode 100644 index 00000000..08ee8829 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterUsageListingActivity.java @@ -0,0 +1,197 @@ +package ml.docilealligator.infinityforreddit.activities; + +import android.content.Context; +import android.content.SharedPreferences; +import android.content.res.ColorStateList; +import android.os.Bundle; +import android.view.View; +import android.view.inputmethod.InputMethodManager; + +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.dialog.MaterialAlertDialogBuilder; +import com.google.android.material.floatingactionbutton.FloatingActionButton; +import com.google.android.material.textfield.TextInputEditText; +import com.google.android.material.textfield.TextInputLayout; + +import java.util.concurrent.Executor; + +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.PostFilterUsageRecyclerViewAdapter; +import ml.docilealligator.infinityforreddit.bottomsheetfragments.NewPostFilterUsageBottomSheetFragment; +import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostFilterUsageOptionsBottomSheetFragment; +import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.postfilter.DeletePostFilterUsage; +import ml.docilealligator.infinityforreddit.postfilter.PostFilter; +import ml.docilealligator.infinityforreddit.postfilter.PostFilterUsage; +import ml.docilealligator.infinityforreddit.postfilter.PostFilterUsageViewModel; +import ml.docilealligator.infinityforreddit.postfilter.SavePostFilterUsage; + +public class PostFilterUsageListingActivity extends BaseActivity { + + public static final String EXTRA_POST_FILTER = "EPF"; + @BindView(R.id.coordinator_layout_post_filter_application_activity) + CoordinatorLayout coordinatorLayout; + @BindView(R.id.appbar_layout_post_filter_application_activity) + AppBarLayout appBarLayout; + @BindView(R.id.toolbar_post_filter_application_activity) + Toolbar toolbar; + @BindView(R.id.recycler_view_post_filter_application_activity) + RecyclerView recyclerView; + @BindView(R.id.fab_post_filter_application_activity) + FloatingActionButton fab; + @Inject + @Named("default") + SharedPreferences sharedPreferences; + @Inject + RedditDataRoomDatabase redditDataRoomDatabase; + @Inject + CustomThemeWrapper customThemeWrapper; + @Inject + Executor executor; + public PostFilterUsageViewModel postFilterUsageViewModel; + private PostFilterUsageRecyclerViewAdapter adapter; + private PostFilter postFilter; + + @Override + protected void onCreate(Bundle savedInstanceState) { + ((Infinity) getApplication()).getAppComponent().inject(this); + + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_post_filter_application); + + ButterKnife.bind(this); + + applyCustomTheme(); + + setSupportActionBar(toolbar); + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + + postFilter = getIntent().getParcelableExtra(EXTRA_POST_FILTER); + + setTitle(postFilter.name); + + fab.setOnClickListener(view -> { + NewPostFilterUsageBottomSheetFragment newPostFilterUsageBottomSheetFragment = new NewPostFilterUsageBottomSheetFragment(); + newPostFilterUsageBottomSheetFragment.show(getSupportFragmentManager(), newPostFilterUsageBottomSheetFragment.getTag()); + }); + + adapter = new PostFilterUsageRecyclerViewAdapter(this, postFilterUsage -> { + PostFilterUsageOptionsBottomSheetFragment postFilterUsageOptionsBottomSheetFragment = new PostFilterUsageOptionsBottomSheetFragment(); + Bundle bundle = new Bundle(); + bundle.putParcelable(PostFilterUsageOptionsBottomSheetFragment.EXTRA_POST_FILTER_USAGE, postFilterUsage); + postFilterUsageOptionsBottomSheetFragment.setArguments(bundle); + postFilterUsageOptionsBottomSheetFragment.show(getSupportFragmentManager(), postFilterUsageOptionsBottomSheetFragment.getTag()); + }); + recyclerView.setAdapter(adapter); + + postFilterUsageViewModel = new ViewModelProvider(this, + new PostFilterUsageViewModel.Factory(redditDataRoomDatabase, postFilter.name)).get(PostFilterUsageViewModel.class); + + postFilterUsageViewModel.getPostFilterUsageListLiveData().observe(this, postFilterUsages -> adapter.setPostFilterUsages(postFilterUsages)); + } + + public void newPostFilterUsage(int type) { + switch (type) { + case PostFilterUsage.HOME_TYPE: + case PostFilterUsage.SEARCH_TYPE: + PostFilterUsage postFilterUsage = new PostFilterUsage(postFilter.name, type, PostFilterUsage.NO_USAGE); + SavePostFilterUsage.savePostFilterUsage(redditDataRoomDatabase, executor, postFilterUsage); + break; + case PostFilterUsage.SUBREDDIT_TYPE: + case PostFilterUsage.USER_TYPE: + case PostFilterUsage.MULTIREDDIT_TYPE: + editAndPostFilterUsageNameOfUsage(type, null); + break; + } + } + + private void editAndPostFilterUsageNameOfUsage(int type, String nameOfUsage) { + View dialogView = getLayoutInflater().inflate(R.layout.dialog_edit_post_filter_name_of_usage, null); + TextInputLayout textInputLayout = dialogView.findViewById(R.id.text_input_layout_edit_post_filter_name_of_usage_dialog); + TextInputEditText textInputEditText = dialogView.findViewById(R.id.text_input_edit_text_edit_post_filter_name_of_usage_dialog); + int primaryTextColor = customThemeWrapper.getPrimaryTextColor(); + textInputLayout.setBoxStrokeColor(primaryTextColor); + textInputLayout.setDefaultHintTextColor(ColorStateList.valueOf(primaryTextColor)); + textInputEditText.setTextColor(primaryTextColor); + if (nameOfUsage != null && !nameOfUsage.equals(PostFilterUsage.NO_USAGE)) { + textInputEditText.setText(nameOfUsage); + } + textInputEditText.requestFocus(); + switch (type) { + case PostFilterUsage.SUBREDDIT_TYPE: + textInputEditText.setHint(R.string.settings_tab_subreddit_name); + break; + case PostFilterUsage.USER_TYPE: + textInputEditText.setHint(R.string.settings_tab_username); + break; + case PostFilterUsage.MULTIREDDIT_TYPE: + textInputEditText.setHint(R.string.settings_tab_multi_reddit_name); + break; + } + + 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.edit_post_filter_name_of_usage_info) + .setView(dialogView) + .setPositiveButton(R.string.ok, (editTextDialogInterface, i1) + -> { + if (imm != null) { + imm.hideSoftInputFromWindow(textInputEditText.getWindowToken(), 0); + } + + PostFilterUsage postFilterUsage; + if (textInputEditText.getText().toString().equals("")) { + postFilterUsage = new PostFilterUsage(postFilter.name, type, PostFilterUsage.NO_USAGE); + } else { + postFilterUsage = new PostFilterUsage(postFilter.name, type, textInputEditText.getText().toString()); + } + + SavePostFilterUsage.savePostFilterUsage(redditDataRoomDatabase, executor, postFilterUsage); + }) + .setNegativeButton(R.string.cancel, null) + .setOnDismissListener(editTextDialogInterface -> { + if (imm != null) { + imm.hideSoftInputFromWindow(textInputEditText.getWindowToken(), 0); + } + }) + .show(); + } + + public void editPostFilterUsage(PostFilterUsage postFilterUsage) { + editAndPostFilterUsageNameOfUsage(postFilterUsage.usage, postFilterUsage.nameOfUsage); + } + + public void deletePostFilterUsage(PostFilterUsage postFilterUsage) { + DeletePostFilterUsage.deletePostFilterUsage(redditDataRoomDatabase, executor, postFilterUsage); + } + + @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/CustomizeThemeRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CustomizeThemeRecyclerViewAdapter.java index d41d318b..37af8c46 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CustomizeThemeRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/CustomizeThemeRecyclerViewAdapter.java @@ -88,7 +88,7 @@ public class CustomizeThemeRecyclerViewAdapter extends RecyclerView.Adapter { View dialogView = activity.getLayoutInflater().inflate(R.layout.dialog_edit_name, null); - EditText themeNameEditText = dialogView.findViewById(R.id.theme_name_edit_text_edit_name_dialog); + EditText themeNameEditText = dialogView.findViewById(R.id.name_edit_text_edit_name_dialog); themeNameEditText.setText(themeName); themeNameEditText.requestFocus(); InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostFilterUsageRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostFilterUsageRecyclerViewAdapter.java new file mode 100644 index 00000000..d6d4c938 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostFilterUsageRecyclerViewAdapter.java @@ -0,0 +1,93 @@ +package ml.docilealligator.infinityforreddit.adapters; + +import android.content.Context; +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.PostFilterUsage; + +public class PostFilterUsageRecyclerViewAdapter extends RecyclerView.Adapter { + private List postFilterUsages; + private OnItemClickListener onItemClickListener; + private Context context; + + public interface OnItemClickListener { + void onClick(PostFilterUsage postFilterUsage); + } + + public PostFilterUsageRecyclerViewAdapter(Context context, + OnItemClickListener onItemClickListener) { + this.context = context; + this.onItemClickListener = onItemClickListener; + } + + @NonNull + @Override + public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + return new PostFilterUsageViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_post_filter_usage, parent, false)); + } + + @Override + public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { + PostFilterUsage postFilterUsage = postFilterUsages.get(position); + switch (postFilterUsage.usage) { + case PostFilterUsage.HOME_TYPE: + ((PostFilterUsageViewHolder) holder).usageTextView.setText(R.string.post_filter_usage_home); + break; + case PostFilterUsage.SUBREDDIT_TYPE: + if (postFilterUsage.nameOfUsage.equals(PostFilterUsage.NO_USAGE)) { + ((PostFilterUsageViewHolder) holder).usageTextView.setText(R.string.post_filter_usage_subreddit_all); + } else { + ((PostFilterUsageViewHolder) holder).usageTextView.setText(context.getString(R.string.post_filter_usage_subreddit, postFilterUsage.nameOfUsage)); + } + break; + case PostFilterUsage.USER_TYPE: + if (postFilterUsage.nameOfUsage.equals(PostFilterUsage.NO_USAGE)) { + ((PostFilterUsageViewHolder) holder).usageTextView.setText(R.string.post_filter_usage_user_all); + } else { + ((PostFilterUsageViewHolder) holder).usageTextView.setText(context.getString(R.string.post_filter_usage_user, postFilterUsage.nameOfUsage)); + } + break; + case PostFilterUsage.MULTIREDDIT_TYPE: + if (postFilterUsage.nameOfUsage.equals(PostFilterUsage.NO_USAGE)) { + ((PostFilterUsageViewHolder) holder).usageTextView.setText(R.string.post_filter_usage_multireddit_all); + } else { + ((PostFilterUsageViewHolder) holder).usageTextView.setText(context.getString(R.string.post_filter_usage_multireddit, postFilterUsage.nameOfUsage)); + } + break; + case PostFilterUsage.SEARCH_TYPE: + ((PostFilterUsageViewHolder) holder).usageTextView.setText(R.string.post_filter_usage_search); + } + } + + @Override + public int getItemCount() { + return postFilterUsages == null ? 0 : postFilterUsages.size(); + } + + public void setPostFilterUsages(List postFilterUsages) { + this.postFilterUsages = postFilterUsages; + notifyDataSetChanged(); + } + + private class PostFilterUsageViewHolder extends RecyclerView.ViewHolder { + TextView usageTextView; + + public PostFilterUsageViewHolder(@NonNull View itemView) { + super(itemView); + usageTextView = (TextView) itemView; + + usageTextView.setOnClickListener(view -> { + onItemClickListener.onClick(postFilterUsages.get(getAdapterPosition())); + }); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/NewPostFilterUsageBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/NewPostFilterUsageBottomSheetFragment.java new file mode 100644 index 00000000..fed822c4 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/NewPostFilterUsageBottomSheetFragment.java @@ -0,0 +1,79 @@ +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.PostFilterUsageListingActivity; +import ml.docilealligator.infinityforreddit.postfilter.PostFilterUsage; + +public class NewPostFilterUsageBottomSheetFragment extends RoundedBottomSheetDialogFragment { + + @BindView(R.id.home_text_view_new_post_filter_usage_bottom_sheet_fragment) + TextView homeTextView; + @BindView(R.id.subreddit_text_view_new_post_filter_usage_bottom_sheet_fragment) + TextView subredditTextView; + @BindView(R.id.user_text_view_new_post_filter_usage_bottom_sheet_fragment) + TextView userTextView; + @BindView(R.id.multireddit_text_view_new_post_filter_usage_bottom_sheet_fragment) + TextView multiRedditTextView; + @BindView(R.id.search_text_view_new_post_filter_usage_bottom_sheet_fragment) + TextView searchTextView; + private PostFilterUsageListingActivity activity; + + public NewPostFilterUsageBottomSheetFragment() { + // 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_new_post_filter_usage_bottom_sheet, container, false); + + ButterKnife.bind(this, rootView); + + homeTextView.setOnClickListener(view -> { + activity.newPostFilterUsage(PostFilterUsage.HOME_TYPE); + dismiss(); + }); + + subredditTextView.setOnClickListener(view -> { + activity.newPostFilterUsage(PostFilterUsage.SUBREDDIT_TYPE); + dismiss(); + }); + + userTextView.setOnClickListener(view -> { + activity.newPostFilterUsage(PostFilterUsage.USER_TYPE); + dismiss(); + }); + + multiRedditTextView.setOnClickListener(view -> { + activity.newPostFilterUsage(PostFilterUsage.MULTIREDDIT_TYPE); + dismiss(); + }); + + searchTextView.setOnClickListener(view -> { + activity.newPostFilterUsage(PostFilterUsage.SEARCH_TYPE); + dismiss(); + }); + + return rootView; + } + + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + activity = (PostFilterUsageListingActivity) context; + } +} \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PostFilterUsageOptionsBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PostFilterUsageOptionsBottomSheetFragment.java new file mode 100644 index 00000000..6e9ccd15 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/bottomsheetfragments/PostFilterUsageOptionsBottomSheetFragment.java @@ -0,0 +1,66 @@ +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.PostFilterUsageListingActivity; +import ml.docilealligator.infinityforreddit.postfilter.PostFilterUsage; + +public class PostFilterUsageOptionsBottomSheetFragment extends RoundedBottomSheetDialogFragment { + + public static final String EXTRA_POST_FILTER_USAGE = "EPFU"; + + @BindView(R.id.edit_text_view_post_filter_usage_options_bottom_sheet_fragment) + TextView editTextView; + @BindView(R.id.delete_text_view_post_filter_usage_options_bottom_sheet_fragment) + TextView deleteTextView; + private PostFilterUsageListingActivity activity; + + public PostFilterUsageOptionsBottomSheetFragment() { + // 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_usage_options_bottom_sheet, container, false); + + ButterKnife.bind(this, rootView); + + PostFilterUsage postFilterUsage = getArguments().getParcelable(EXTRA_POST_FILTER_USAGE); + + if (postFilterUsage.usage == PostFilterUsage.HOME_TYPE || postFilterUsage.usage == PostFilterUsage.SEARCH_TYPE) { + editTextView.setVisibility(View.GONE); + } else { + editTextView.setOnClickListener(view -> { + activity.editPostFilterUsage(postFilterUsage); + dismiss(); + }); + } + + deleteTextView.setOnClickListener(view -> { + activity.deletePostFilterUsage(postFilterUsage); + dismiss(); + }); + + return rootView; + } + + @Override + public void onAttach(@NonNull Context context) { + super.onAttach(context); + activity = (PostFilterUsageListingActivity) context; + } +} \ No newline at end of file diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/DeletePostFilter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/DeletePostFilter.java new file mode 100644 index 00000000..468c0498 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/DeletePostFilter.java @@ -0,0 +1,16 @@ +package ml.docilealligator.infinityforreddit.postfilter; + +import java.util.concurrent.Executor; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class DeletePostFilter { + public static void deletePostFilter(RedditDataRoomDatabase redditDataRoomDatabase, Executor executor, PostFilter postFilter) { + executor.execute(new Runnable() { + @Override + public void run() { + redditDataRoomDatabase.postFilterDao().deletePostFilter(postFilter); + } + }); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/DeletePostFilterUsage.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/DeletePostFilterUsage.java new file mode 100644 index 00000000..5e465afc --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/DeletePostFilterUsage.java @@ -0,0 +1,12 @@ +package ml.docilealligator.infinityforreddit.postfilter; + +import java.util.concurrent.Executor; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class DeletePostFilterUsage { + public static void deletePostFilterUsage(RedditDataRoomDatabase redditDataRoomDatabase, Executor executor, + PostFilterUsage postFilterUsage) { + executor.execute(() -> redditDataRoomDatabase.postFilterUsageDao().deletePostFilterUsage(postFilterUsage)); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterUsage.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterUsage.java index 05cf05cc..558abeeb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterUsage.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterUsage.java @@ -1,5 +1,8 @@ package ml.docilealligator.infinityforreddit.postfilter; +import android.os.Parcel; +import android.os.Parcelable; + import androidx.annotation.NonNull; import androidx.room.ColumnInfo; import androidx.room.Entity; @@ -8,7 +11,14 @@ import androidx.room.ForeignKey; @Entity(tableName = "post_filter_usage", primaryKeys = {"name", "usage", "name_of_usage"}, foreignKeys = @ForeignKey(entity = PostFilter.class, parentColumns = "name", childColumns = "name", onDelete = ForeignKey.CASCADE)) -public class PostFilterUsage { +public class PostFilterUsage implements Parcelable { + public static final int HOME_TYPE = 1; + public static final int SUBREDDIT_TYPE = 2; + public static final int USER_TYPE = 3; + public static final int MULTIREDDIT_TYPE = 4; + public static final int SEARCH_TYPE = 5; + public static final String NO_USAGE = "--"; + @NonNull @ColumnInfo(name = "name") public String name; @@ -21,6 +31,40 @@ public class PostFilterUsage { public PostFilterUsage(@NonNull String name, int usage, String nameOfUsage) { this.name = name; this.usage = usage; - this.nameOfUsage = nameOfUsage; + if (nameOfUsage == null || nameOfUsage.equals("")) { + this.nameOfUsage = NO_USAGE; + } else { + this.nameOfUsage = nameOfUsage; + } + } + + protected PostFilterUsage(Parcel in) { + name = in.readString(); + usage = in.readInt(); + nameOfUsage = in.readString(); + } + + public static final Creator CREATOR = new Creator() { + @Override + public PostFilterUsage createFromParcel(Parcel in) { + return new PostFilterUsage(in); + } + + @Override + public PostFilterUsage[] newArray(int size) { + return new PostFilterUsage[size]; + } + }; + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel parcel, int i) { + parcel.writeString(name); + parcel.writeInt(usage); + parcel.writeString(nameOfUsage); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterUsageDao.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterUsageDao.java index 165df55a..a0ba8843 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterUsageDao.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterUsageDao.java @@ -1,7 +1,22 @@ 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 PostFilterUsageDao { + @Query("SELECT * FROM post_filter_usage WHERE name = :name") + LiveData> getAllPostFilterUsageLiveData(String name); + + @Insert(onConflict = OnConflictStrategy.REPLACE) + void insertPostFilterUsage(PostFilterUsage postFilterUsage); + + @Delete + void deletePostFilterUsage(PostFilterUsage postFilterUsage); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterUsageViewModel.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterUsageViewModel.java new file mode 100644 index 00000000..f63ed46a --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/PostFilterUsageViewModel.java @@ -0,0 +1,38 @@ +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 PostFilterUsageViewModel extends ViewModel { + private LiveData> mPostFilterUsageListLiveData; + + public PostFilterUsageViewModel(RedditDataRoomDatabase redditDataRoomDatabase, String name) { + mPostFilterUsageListLiveData = redditDataRoomDatabase.postFilterUsageDao().getAllPostFilterUsageLiveData(name); + } + + public LiveData> getPostFilterUsageListLiveData() { + return mPostFilterUsageListLiveData; + } + + public static class Factory extends ViewModelProvider.NewInstanceFactory { + + private final RedditDataRoomDatabase mRedditDataRoomDatabase; + private final String mName; + + public Factory(RedditDataRoomDatabase redditDataRoomDatabase, String name) { + mRedditDataRoomDatabase = redditDataRoomDatabase; + mName = name; + } + + @Override + public T create(Class modelClass) { + //noinspection unchecked + return (T) new PostFilterUsageViewModel(mRedditDataRoomDatabase, mName); + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/SavePostFilterUsage.java b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/SavePostFilterUsage.java new file mode 100644 index 00000000..567da3b1 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/postfilter/SavePostFilterUsage.java @@ -0,0 +1,12 @@ +package ml.docilealligator.infinityforreddit.postfilter; + +import java.util.concurrent.Executor; + +import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; + +public class SavePostFilterUsage { + public static void savePostFilterUsage(RedditDataRoomDatabase redditDataRoomDatabase, Executor executor, + PostFilterUsage postFilterUsage) { + executor.execute(() -> redditDataRoomDatabase.postFilterUsageDao().insertPostFilterUsage(postFilterUsage)); + } +} -- cgit v1.2.3