Migrate subscriptions filter dialog to DialogFragment (#6846)

Co-authored-by: ByteHamster <info@bytehamster.com>
This commit is contained in:
ueen 2024-01-02 19:56:57 +01:00 committed by GitHub
parent b2ea588b54
commit f1e91f9d8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 87 additions and 45 deletions

View File

@ -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<String> 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<String> 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<String> getFilterValues() {
Set<String> 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<FrameLayout> behavior = BottomSheetBehavior.from(bottomSheet);
ViewGroup.LayoutParams layoutParams = bottomSheet.getLayoutParams();
bottomSheet.setLayoutParams(layoutParams);
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
private static void updateFilter(Set<String> filterValues) {

View File

@ -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");
}
}

View File

@ -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());

View File

@ -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;
}));