From 398d53c162f32acb38feee588d2342d0876772ad Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 1 Apr 2021 08:33:17 +0800 Subject: New option in ViewPostDetailFragment: Add to Post Filter. Fix a bug when saving a Post Filter. --- .../activities/CustomizePostFilterActivity.java | 46 ++++++++++++++- .../activities/PostFilterPreferenceActivity.java | 68 +++++++++++++++++++--- .../adapters/PostFilterRecyclerViewAdapter.java | 2 +- .../fragments/ViewPostDetailFragment.java | 6 ++ 4 files changed, 112 insertions(+), 10 deletions(-) (limited to 'app/src/main/java') 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 b517226e..c02d0687 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizePostFilterActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/CustomizePostFilterActivity.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.ColorStateList; +import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.Handler; @@ -49,6 +50,11 @@ 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 EXTRA_EXCLUDE_SUBREDDIT = "EES"; + public static final String EXTRA_EXCLUDE_USER = "EEU"; + public static final String EXTRA_EXCLUDE_FLAIR = "EEF"; + public static final String EXTRA_CONTAIN_FLAIR = "ECF"; + public static final String EXTRA_EXCLUDE_DOMAIN = "EED"; public static final String RETURN_EXTRA_POST_FILTER = "REPF"; private static final String POST_FILTER_STATE = "PFS"; private static final String ORIGINAL_NAME_STATE = "ONS"; @@ -309,6 +315,44 @@ public class CustomizePostFilterActivity extends BaseActivity { maxCommentsTextInputEditText.setText(Integer.toString(postFilter.maxComments)); minAwardsTextInputEditText.setText(Integer.toString(postFilter.minAwards)); maxAwardsTextInputEditText.setText(Integer.toString(postFilter.maxAwards)); + + Intent intent = getIntent(); + String excludeSubreddit = intent.getStringExtra(EXTRA_EXCLUDE_SUBREDDIT); + String excludeUser = intent.getStringExtra(EXTRA_EXCLUDE_USER); + String excludeFlair = intent.getStringExtra(EXTRA_EXCLUDE_FLAIR); + String containFlair = intent.getStringExtra(EXTRA_CONTAIN_FLAIR); + String excludeDomain = intent.getStringExtra(EXTRA_EXCLUDE_DOMAIN); + + if (excludeSubreddit != null && !excludeSubreddit.equals("")) { + if (!excludesSubredditsTextInputEditText.getText().toString().equals("")) { + excludesSubredditsTextInputEditText.append(","); + } + excludesSubredditsTextInputEditText.append(excludeSubreddit); + } + if (excludeUser != null && !excludeUser.equals("")) { + if (!excludesUsersTextInputEditText.getText().toString().equals("")) { + excludesUsersTextInputEditText.append(","); + } + excludesUsersTextInputEditText.append(excludeUser); + } + if (excludeFlair != null && !excludeFlair.equals("")) { + if (!excludesFlairsTextInputEditText.getText().toString().equals("")) { + excludesFlairsTextInputEditText.append(","); + } + excludesFlairsTextInputEditText.append(excludeFlair); + } + if (containFlair != null && !containFlair.equals("")) { + if (!containsFlairsTextInputEditText.getText().toString().equals("")) { + containsFlairsTextInputEditText.append(","); + } + containsFlairsTextInputEditText.append(containFlair); + } + if (excludeDomain != null && !excludeDomain.equals("")) { + if (!excludesDomainsTextInputEditText.getText().toString().equals("")) { + excludesDomainsTextInputEditText.append(","); + } + excludesDomainsTextInputEditText.append(Uri.parse(excludeDomain).getHost()); + } } @Override @@ -496,7 +540,7 @@ public class CustomizePostFilterActivity extends BaseActivity { postFilter.postTitleExcludesStrings = titleExcludesStringsTextInputEditText.getText().toString(); postFilter.excludeSubreddits = excludesSubredditsTextInputEditText.getText().toString(); postFilter.excludeUsers = excludesUsersTextInputEditText.getText().toString(); - postFilter.excludeFlairs = excludesUsersTextInputEditText.getText().toString(); + postFilter.excludeFlairs = excludesFlairsTextInputEditText.getText().toString(); postFilter.containFlairs = containsFlairsTextInputEditText.getText().toString(); postFilter.excludeDomains = excludesDomainsTextInputEditText.getText().toString(); postFilter.containTextType = postTypeTextCheckBox.isChecked(); 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 f75f08f7..e64bf21e 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterPreferenceActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostFilterPreferenceActivity.java @@ -12,6 +12,7 @@ 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 java.util.concurrent.Executor; @@ -27,12 +28,15 @@ import ml.docilealligator.infinityforreddit.RedditDataRoomDatabase; import ml.docilealligator.infinityforreddit.adapters.PostFilterRecyclerViewAdapter; import ml.docilealligator.infinityforreddit.bottomsheetfragments.PostFilterOptionsBottomSheetFragment; import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.post.Post; import ml.docilealligator.infinityforreddit.postfilter.DeletePostFilter; import ml.docilealligator.infinityforreddit.postfilter.PostFilter; import ml.docilealligator.infinityforreddit.postfilter.PostFilterViewModel; public class PostFilterPreferenceActivity extends BaseActivity { + public static final String EXTRA_POST = "EP"; + @BindView(R.id.coordinator_layout_post_filter_preference_activity) CoordinatorLayout coordinatorLayout; @BindView(R.id.appbar_layout_post_filter_preference_activity) @@ -71,18 +75,28 @@ public class PostFilterPreferenceActivity extends BaseActivity { setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); + Post post = getIntent().getParcelableExtra(EXTRA_POST); + fab.setOnClickListener(view -> { - Intent intent = new Intent(PostFilterPreferenceActivity.this, CustomizePostFilterActivity.class); - intent.putExtra(CustomizePostFilterActivity.EXTRA_FROM_SETTINGS, true); - startActivity(intent); + if (post == null) { + Intent intent = new Intent(PostFilterPreferenceActivity.this, CustomizePostFilterActivity.class); + intent.putExtra(CustomizePostFilterActivity.EXTRA_FROM_SETTINGS, true); + startActivity(intent); + } else { + showPostFilterOptions(post, null); + } }); adapter = new PostFilterRecyclerViewAdapter(postFilter -> { - PostFilterOptionsBottomSheetFragment postFilterOptionsBottomSheetFragment = new PostFilterOptionsBottomSheetFragment(); - Bundle bundle = new Bundle(); - bundle.putParcelable(PostFilterOptionsBottomSheetFragment.EXTRA_POST_FILTER, postFilter); - postFilterOptionsBottomSheetFragment.setArguments(bundle); - postFilterOptionsBottomSheetFragment.show(getSupportFragmentManager(), postFilterOptionsBottomSheetFragment.getTag()); + if (post == null) { + PostFilterOptionsBottomSheetFragment postFilterOptionsBottomSheetFragment = new PostFilterOptionsBottomSheetFragment(); + Bundle bundle = new Bundle(); + bundle.putParcelable(PostFilterOptionsBottomSheetFragment.EXTRA_POST_FILTER, postFilter); + postFilterOptionsBottomSheetFragment.setArguments(bundle); + postFilterOptionsBottomSheetFragment.show(getSupportFragmentManager(), postFilterOptionsBottomSheetFragment.getTag()); + } else { + showPostFilterOptions(post, postFilter); + } }); recyclerView.setAdapter(adapter); @@ -93,6 +107,44 @@ public class PostFilterPreferenceActivity extends BaseActivity { postFilterViewModel.getPostFilterListLiveData().observe(this, postFilters -> adapter.setPostFilterList(postFilters)); } + public void showPostFilterOptions(Post post, PostFilter postFilter) { + String[] options = getResources().getStringArray(R.array.add_to_post_filter_options); + boolean[] selectedOptions = new boolean[]{false, false, false, false, false}; + new MaterialAlertDialogBuilder(this, R.style.MaterialAlertDialogTheme) + .setTitle(R.string.select) + .setMultiChoiceItems(options, selectedOptions, (dialogInterface, i, b) -> selectedOptions[i] = b) + .setPositiveButton(R.string.ok, (dialogInterface, i) -> { + Intent intent = new Intent(PostFilterPreferenceActivity.this, CustomizePostFilterActivity.class); + if (postFilter != null) { + intent.putExtra(CustomizePostFilterActivity.EXTRA_POST_FILTER, postFilter); + } + intent.putExtra(CustomizePostFilterActivity.EXTRA_FROM_SETTINGS, true); + for (int j = 0; j < selectedOptions.length; j++) { + if (selectedOptions[j]) { + switch (j) { + case 0: + intent.putExtra(CustomizePostFilterActivity.EXTRA_EXCLUDE_SUBREDDIT, post.getSubredditName()); + break; + case 1: + intent.putExtra(CustomizePostFilterActivity.EXTRA_EXCLUDE_USER, post.getAuthor()); + break; + case 2: + intent.putExtra(CustomizePostFilterActivity.EXTRA_EXCLUDE_FLAIR, post.getFlair()); + break; + case 3: + intent.putExtra(CustomizePostFilterActivity.EXTRA_CONTAIN_FLAIR, post.getFlair()); + break; + case 4: + intent.putExtra(CustomizePostFilterActivity.EXTRA_EXCLUDE_DOMAIN, post.getUrl()); + break; + } + } + } + startActivity(intent); + }) + .show(); + } + public void editPostFilter(PostFilter postFilter) { Intent intent = new Intent(PostFilterPreferenceActivity.this, CustomizePostFilterActivity.class); intent.putExtra(CustomizePostFilterActivity.EXTRA_POST_FILTER, postFilter); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostFilterRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostFilterRecyclerViewAdapter.java index 344e968c..4faab908 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostFilterRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/PostFilterRecyclerViewAdapter.java @@ -71,7 +71,7 @@ public class PostFilterRecyclerViewAdapter extends RecyclerView.Adapter { - onItemClickListener.onItemClick(postFilterList.get(getAdapterPosition() - 1)); + onItemClickListener.onItemClick(postFilterList.get(getBindingAdapterPosition() - 1)); }); } } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java index e9fb018c..91b82fdf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java @@ -72,6 +72,7 @@ import ml.docilealligator.infinityforreddit.SortType; import ml.docilealligator.infinityforreddit.activities.CommentActivity; import ml.docilealligator.infinityforreddit.activities.EditPostActivity; import ml.docilealligator.infinityforreddit.activities.GiveAwardActivity; +import ml.docilealligator.infinityforreddit.activities.PostFilterPreferenceActivity; import ml.docilealligator.infinityforreddit.activities.ReportActivity; import ml.docilealligator.infinityforreddit.activities.SubmitCrosspostActivity; import ml.docilealligator.infinityforreddit.activities.ViewPostDetailActivity; @@ -953,6 +954,11 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic submitCrosspostIntent.putExtra(SubmitCrosspostActivity.EXTRA_POST, mPost); startActivity(submitCrosspostIntent); return true; + } else if (itemId == R.id.action_add_to_post_filter_view_post_detail_fragment) { + Intent intent = new Intent(activity, PostFilterPreferenceActivity.class); + intent.putExtra(PostFilterPreferenceActivity.EXTRA_POST, mPost); + startActivity(intent); + return true; } return false; } -- cgit v1.2.3