From f1e91f9d8b152fb022d227d866e8666d73171a9f Mon Sep 17 00:00:00 2001 From: ueen Date: Tue, 2 Jan 2024 19:56:57 +0100 Subject: [PATCH] Migrate subscriptions filter dialog to DialogFragment (#6846) Co-authored-by: ByteHamster --- .../dialog/SubscriptionsFilterDialog.java | 123 ++++++++++++------ .../fragment/NavDrawerFragment.java | 2 +- .../fragment/SubscriptionFragment.java | 5 +- .../UserInterfacePreferencesFragment.java | 2 +- 4 files changed, 87 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/de/danoeh/antennapod/dialog/SubscriptionsFilterDialog.java b/app/src/main/java/de/danoeh/antennapod/dialog/SubscriptionsFilterDialog.java index cf77c2838..929e6b1ad 100644 --- a/app/src/main/java/de/danoeh/antennapod/dialog/SubscriptionsFilterDialog.java +++ b/app/src/main/java/de/danoeh/antennapod/dialog/SubscriptionsFilterDialog.java @@ -1,42 +1,49 @@ package de.danoeh.antennapod.dialog; -import android.content.Context; +import android.app.Dialog; +import android.os.Bundle; import android.text.TextUtils; import android.view.LayoutInflater; import android.view.View; +import android.view.ViewGroup; import android.widget.Button; +import android.widget.FrameLayout; import android.widget.LinearLayout; - +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import com.google.android.material.bottomsheet.BottomSheetBehavior; +import com.google.android.material.bottomsheet.BottomSheetDialog; +import com.google.android.material.bottomsheet.BottomSheetDialogFragment; import com.google.android.material.button.MaterialButtonToggleGroup; -import com.google.android.material.dialog.MaterialAlertDialogBuilder; - -import org.greenrobot.eventbus.EventBus; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; - import de.danoeh.antennapod.R; import de.danoeh.antennapod.core.feed.SubscriptionsFilterGroup; +import de.danoeh.antennapod.databinding.FilterDialogBinding; import de.danoeh.antennapod.databinding.FilterDialogRowBinding; import de.danoeh.antennapod.event.UnreadItemsUpdateEvent; import de.danoeh.antennapod.model.feed.SubscriptionsFilter; import de.danoeh.antennapod.storage.preferences.UserPreferences; +import org.greenrobot.eventbus.EventBus; -public class SubscriptionsFilterDialog { - public static void showDialog(Context context) { +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; + +public class SubscriptionsFilterDialog extends BottomSheetDialogFragment { + private LinearLayout rows; + + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, + @Nullable Bundle savedInstanceState) { SubscriptionsFilter subscriptionsFilter = UserPreferences.getSubscriptionsFilter(); - final Set filterValues = new HashSet<>(Arrays.asList(subscriptionsFilter.getValues())); - MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context); - builder.setTitle(context.getString(R.string.pref_filter_feed_title)); - - LayoutInflater inflater = LayoutInflater.from(context); - View layout = inflater.inflate(R.layout.filter_dialog, null, false); - LinearLayout rows = layout.findViewById(R.id.filter_rows); - builder.setView(layout); + FilterDialogBinding dialogBinding = FilterDialogBinding.inflate(inflater); + rows = dialogBinding.filterRows; for (SubscriptionsFilterGroup item : SubscriptionsFilterGroup.values()) { FilterDialogRowBinding binding = FilterDialogRowBinding.inflate(inflater); + binding.getRoot().addOnButtonCheckedListener( + (group, checkedId, isChecked) -> updateFilter(getFilterValues())); binding.buttonGroup.setWeightSum(item.values.length); binding.filterButton1.setText(item.values[0].displayName); binding.filterButton1.setTag(item.values[0].filterId); @@ -50,38 +57,72 @@ public class SubscriptionsFilterDialog { binding.filterButton1.setSingleLine(false); binding.filterButton2.setMaxLines(3); binding.filterButton2.setSingleLine(false); - rows.addView(binding.getRoot()); + rows.addView(binding.getRoot(), rows.getChildCount() - 1); } + final Set filterValues = new HashSet<>(Arrays.asList(subscriptionsFilter.getValues())); for (String filterId : filterValues) { if (!TextUtils.isEmpty(filterId)) { - Button button = layout.findViewWithTag(filterId); + Button button = dialogBinding.getRoot().findViewWithTag(filterId); if (button != null) { ((MaterialButtonToggleGroup) button.getParent()).check(button.getId()); } } } - builder.setPositiveButton(R.string.confirm_label, (dialog, which) -> { - filterValues.clear(); - for (int i = 0; i < rows.getChildCount(); i++) { - if (!(rows.getChildAt(i) instanceof MaterialButtonToggleGroup)) { - continue; - } - MaterialButtonToggleGroup group = (MaterialButtonToggleGroup) rows.getChildAt(i); - if (group.getCheckedButtonId() == View.NO_ID) { - continue; - } - String tag = (String) group.findViewById(group.getCheckedButtonId()).getTag(); - if (tag == null) { // Clear buttons use no tag - continue; - } - filterValues.add(tag); - } - updateFilter(filterValues); + dialogBinding.confirmFiltermenu.setOnClickListener(view -> { + updateFilter(getFilterValues()); + dismiss(); }); - builder.setNegativeButton(R.string.cancel_label, null); - builder.show(); + dialogBinding.resetFiltermenu.setOnClickListener(view -> { + updateFilter(Collections.emptySet()); + for (int i = 0; i < rows.getChildCount(); i++) { + if (rows.getChildAt(i) instanceof MaterialButtonToggleGroup) { + ((MaterialButtonToggleGroup) rows.getChildAt(i)).clearChecked(); + } + } + }); + return dialogBinding.getRoot(); + } + + private Set getFilterValues() { + Set filterValues = new HashSet<>(); + for (int i = 0; i < rows.getChildCount(); i++) { + if (!(rows.getChildAt(i) instanceof MaterialButtonToggleGroup)) { + continue; + } + MaterialButtonToggleGroup group = (MaterialButtonToggleGroup) rows.getChildAt(i); + if (group.getCheckedButtonId() == View.NO_ID) { + continue; + } + String tag = (String) group.findViewById(group.getCheckedButtonId()).getTag(); + if (tag == null) { // Clear buttons use no tag + continue; + } + filterValues.add(tag); + } + return filterValues; + } + + @NonNull + @Override + public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { + Dialog dialog = super.onCreateDialog(savedInstanceState); + dialog.setOnShowListener(dialogInterface -> { + BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) dialogInterface; + setupFullHeight(bottomSheetDialog); + }); + return dialog; + } + + private void setupFullHeight(BottomSheetDialog bottomSheetDialog) { + FrameLayout bottomSheet = bottomSheetDialog.findViewById(R.id.design_bottom_sheet); + if (bottomSheet != null) { + BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet); + ViewGroup.LayoutParams layoutParams = bottomSheet.getLayoutParams(); + bottomSheet.setLayoutParams(layoutParams); + behavior.setState(BottomSheetBehavior.STATE_EXPANDED); + } } private static void updateFilter(Set filterValues) { diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/NavDrawerFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/NavDrawerFragment.java index eeca181cf..636c0245b 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/NavDrawerFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/NavDrawerFragment.java @@ -382,7 +382,7 @@ public class NavDrawerFragment extends Fragment implements SharedPreferences.OnS } } else if (UserPreferences.getSubscriptionsFilter().isEnabled() && navAdapter.showSubscriptionList) { - SubscriptionsFilterDialog.showDialog(requireContext()); + new SubscriptionsFilterDialog().show(getChildFragmentManager(), "filter"); } } diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/SubscriptionFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/SubscriptionFragment.java index 93ed4c2c9..7006edca9 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/SubscriptionFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/SubscriptionFragment.java @@ -166,7 +166,8 @@ public class SubscriptionFragment extends Fragment }); feedsFilteredMsg = root.findViewById(R.id.feeds_filtered_message); - feedsFilteredMsg.setOnClickListener((l) -> SubscriptionsFilterDialog.showDialog(requireContext())); + feedsFilteredMsg.setOnClickListener((l) -> + new SubscriptionsFilterDialog().show(getChildFragmentManager(), "filter")); SwipeRefreshLayout swipeRefreshLayout = root.findViewById(R.id.swipeRefresh); swipeRefreshLayout.setDistanceToTriggerSync(getResources().getInteger(R.integer.swipe_refresh_distance)); @@ -221,7 +222,7 @@ public class SubscriptionFragment extends Fragment FeedUpdateManager.runOnceOrAsk(requireContext()); return true; } else if (itemId == R.id.subscriptions_filter) { - SubscriptionsFilterDialog.showDialog(requireContext()); + new SubscriptionsFilterDialog().show(getChildFragmentManager(), "filter"); return true; } else if (itemId == R.id.subscriptions_sort) { FeedSortDialog.showDialog(requireContext()); diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/preferences/UserInterfacePreferencesFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/preferences/UserInterfacePreferencesFragment.java index 66f592af2..c8521caab 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/preferences/UserInterfacePreferencesFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/preferences/UserInterfacePreferencesFragment.java @@ -71,7 +71,7 @@ public class UserInterfacePreferencesFragment extends PreferenceFragmentCompat { }); findPreference(UserPreferences.PREF_FILTER_FEED) .setOnPreferenceClickListener((preference -> { - SubscriptionsFilterDialog.showDialog(requireContext()); + new SubscriptionsFilterDialog().show(getChildFragmentManager(), "filter"); return true; }));