Skip to content

Commit

Permalink
Migrate subscriptions filter dialog to DialogFragment (AntennaPod#6846)
Browse files Browse the repository at this point in the history
Co-authored-by: ByteHamster <info@bytehamster.com>
  • Loading branch information
2 people authored and quails4Eva committed Jan 20, 2024
1 parent 95cc7e3 commit 2adc04c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -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) {
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));
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

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);
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();
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);
Expand All @@ -50,38 +57,72 @@ public static void showDialog(Context context) {
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();
dialogBinding.confirmFiltermenu.setOnClickListener(view -> {
updateFilter(getFilterValues());
dismiss();
});
dialogBinding.resetFiltermenu.setOnClickListener(view -> {
updateFilter(Collections.emptySet());
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;
if (rows.getChildAt(i) instanceof MaterialButtonToggleGroup) {
((MaterialButtonToggleGroup) rows.getChildAt(i)).clearChecked();
}
filterValues.add(tag);
}
updateFilter(filterValues);
});
builder.setNegativeButton(R.string.cancel_label, null);
builder.show();
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ public void onItemClick(int position) {
}
} else if (UserPreferences.getSubscriptionsFilter().isEnabled()
&& navAdapter.showSubscriptionList) {
SubscriptionsFilterDialog.showDialog(requireContext());
new SubscriptionsFilterDialog().show(getChildFragmentManager(), "filter");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMen
});

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));
Expand Down Expand Up @@ -221,7 +222,7 @@ public boolean onMenuItemClick(MenuItem item) {
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ private void setupInterfaceScreen() {
});
findPreference(UserPreferences.PREF_FILTER_FEED)
.setOnPreferenceClickListener((preference -> {
SubscriptionsFilterDialog.showDialog(requireContext());
new SubscriptionsFilterDialog().show(getChildFragmentManager(), "filter");
return true;
}));

Expand Down

0 comments on commit 2adc04c

Please sign in to comment.