diff options
Diffstat (limited to 'app')
4 files changed, 178 insertions, 22 deletions
diff --git a/app/src/main/java/de/danoeh/antennapod/dialog/FilterDialog.java b/app/src/main/java/de/danoeh/antennapod/dialog/FilterDialog.java index d2912f90f..6e50be917 100644 --- a/app/src/main/java/de/danoeh/antennapod/dialog/FilterDialog.java +++ b/app/src/main/java/de/danoeh/antennapod/dialog/FilterDialog.java @@ -1,8 +1,12 @@ package de.danoeh.antennapod.dialog; import android.content.Context; -import androidx.appcompat.app.AlertDialog; import android.text.TextUtils; +import android.view.LayoutInflater; +import android.widget.LinearLayout; +import android.widget.RadioButton; + +import androidx.appcompat.app.AlertDialog; import java.util.Arrays; import java.util.HashSet; @@ -10,6 +14,8 @@ import java.util.Set; import de.danoeh.antennapod.R; import de.danoeh.antennapod.core.feed.FeedItemFilter; +import de.danoeh.antennapod.core.feed.FeedItemFilterGroup; +import de.danoeh.antennapod.view.RecursiveRadioGroup; public abstract class FilterDialog { @@ -22,38 +28,49 @@ public abstract class FilterDialog { } public void openDialog() { - final String[] items = context.getResources().getStringArray(R.array.episode_filter_options); - final String[] values = context.getResources().getStringArray(R.array.episode_filter_values); - final boolean[] checkedItems = new boolean[items.length]; final Set<String> filterValues = new HashSet<>(Arrays.asList(filter.getValues())); + AlertDialog.Builder builder = new AlertDialog.Builder(context); + builder.setTitle(R.string.filter); - // make sure we have no empty strings in the filter list - for (String filterValue : filterValues) { - if (TextUtils.isEmpty(filterValue)) { - filterValues.remove(filterValue); - } + LayoutInflater inflater = LayoutInflater.from(this.context); + LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.filter_dialog, null, false); + builder.setView(layout); + + for (FeedItemFilterGroup item : FeedItemFilterGroup.values()) { + RecursiveRadioGroup row = (RecursiveRadioGroup) inflater.inflate(R.layout.filter_dialog_row, null); + RadioButton filter1 = row.findViewById(R.id.filter_dialog_radioButton1); + RadioButton filter2 = row.findViewById(R.id.filter_dialog_radioButton2); + filter1.setText(item.values[0].displayName); + filter1.setTag(item.values[0].filterId); + filter2.setText(item.values[1].displayName); + filter2.setTag(item.values[1].filterId); + layout.addView(row); } - for (int i = 0; i < values.length; i++) { - String value = values[i]; - if (filterValues.contains(value)) { - checkedItems[i] = true; + for (String filterId : filterValues) { + if (!TextUtils.isEmpty(filterId)) { + ((RadioButton) layout.findViewWithTag(filterId)).setChecked(true); } } - AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setTitle(R.string.filter); - builder.setMultiChoiceItems(items, checkedItems, (dialog, which, isChecked) -> { - if (isChecked) { - filterValues.add(values[which]); - } else { - filterValues.remove(values[which]); - } - }); builder.setPositiveButton(R.string.confirm_label, (dialog, which) -> { + filterValues.clear(); + for (int i = 0; i < layout.getChildCount(); i++) { + if (!(layout.getChildAt(i) instanceof RecursiveRadioGroup)) { + continue; + } + RecursiveRadioGroup group = (RecursiveRadioGroup) layout.getChildAt(i); + if (group.getCheckedButton() != null) { + String tag = (String) group.getCheckedButton().getTag(); + if (tag != null) { // Clear buttons use no tag + filterValues.add((String) group.getCheckedButton().getTag()); + } + } + } updateFilter(filterValues); }); + builder.setNegativeButton(R.string.cancel_label, null); builder.create().show(); } diff --git a/app/src/main/java/de/danoeh/antennapod/view/RecursiveRadioGroup.java b/app/src/main/java/de/danoeh/antennapod/view/RecursiveRadioGroup.java new file mode 100644 index 000000000..ee5e7c51d --- /dev/null +++ b/app/src/main/java/de/danoeh/antennapod/view/RecursiveRadioGroup.java @@ -0,0 +1,67 @@ +package de.danoeh.antennapod.view; + +import android.content.Context; +import android.util.AttributeSet; +import android.view.View; +import android.view.ViewGroup; +import android.widget.LinearLayout; +import android.widget.RadioButton; +import java.util.ArrayList; + +/** + * An alternative to {@link android.widget.RadioGroup} that allows to nest children. + * Basend on https://stackoverflow.com/a/14309274. + */ +public class RecursiveRadioGroup extends LinearLayout { + private final ArrayList<RadioButton> radioButtons = new ArrayList<>(); + private RadioButton checkedButton = null; + + public RecursiveRadioGroup(Context context) { + super(context); + } + + public RecursiveRadioGroup(Context context, AttributeSet attrs) { + this(context, attrs, 0); + } + + public RecursiveRadioGroup(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + @Override + public void addView(View child, int index, ViewGroup.LayoutParams params) { + super.addView(child, index, params); + parseChild(child); + } + + public void parseChild(final View child) { + if (child instanceof RadioButton) { + RadioButton button = (RadioButton) child; + radioButtons.add(button); + button.setOnCheckedChangeListener((buttonView, isChecked) -> { + if (!isChecked) { + return; + } + checkedButton = (RadioButton) buttonView; + + for (RadioButton view : radioButtons) { + if (view != buttonView) { + view.setChecked(false); + } + } + }); + } else if (child instanceof ViewGroup) { + parseChildren((ViewGroup) child); + } + } + + public void parseChildren(final ViewGroup child) { + for (int i = 0; i < child.getChildCount(); i++) { + parseChild(child.getChildAt(i)); + } + } + + public RadioButton getCheckedButton() { + return checkedButton; + } +} diff --git a/app/src/main/res/layout/filter_dialog.xml b/app/src/main/res/layout/filter_dialog.xml new file mode 100644 index 000000000..39e9258d9 --- /dev/null +++ b/app/src/main/res/layout/filter_dialog.xml @@ -0,0 +1,11 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="vertical" + android:paddingLeft="24dp" + android:paddingTop="24dp" + android:paddingRight="24dp" + android:paddingBottom="8dp"> + +</LinearLayout>
\ No newline at end of file diff --git a/app/src/main/res/layout/filter_dialog_row.xml b/app/src/main/res/layout/filter_dialog_row.xml new file mode 100644 index 000000000..0863997b3 --- /dev/null +++ b/app/src/main/res/layout/filter_dialog_row.xml @@ -0,0 +1,61 @@ +<?xml version="1.0" encoding="utf-8"?> +<de.danoeh.antennapod.view.RecursiveRadioGroup + xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + android:layout_width="match_parent" + android:layout_height="match_parent" + android:paddingBottom="8dp" + android:orientation="horizontal"> + + <androidx.cardview.widget.CardView + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_weight="1" + android:clipChildren="true" + app:cardCornerRadius="32dp" + app:cardElevation="0dp"> + + <LinearLayout + android:layout_width="match_parent" + android:layout_height="match_parent" + android:orientation="horizontal"> + + <RadioButton + android:id="@+id/filter_dialog_radioButton1" + android:layout_width="0dp" + android:layout_height="match_parent" + android:layout_marginEnd="2dp" + android:layout_marginRight="2dp" + android:layout_weight="1" + android:background="?attr/filter_dialog_button_background" + android:button="@android:color/transparent" + android:foreground="?android:attr/selectableItemBackground" + android:checked="false" + android:gravity="center" + android:textColor="@color/filter_dialog_button_text" /> + + <RadioButton + android:id="@+id/filter_dialog_radioButton2" + android:layout_width="0dp" + android:layout_height="match_parent" + android:layout_weight="1" + android:background="?attr/filter_dialog_button_background" + android:button="@android:color/transparent" + android:foreground="?android:attr/selectableItemBackground" + android:checked="false" + android:gravity="center" + android:textColor="@color/filter_dialog_button_text" /> + </LinearLayout> + </androidx.cardview.widget.CardView> + + <RadioButton + android:id="@+id/filter_dialog_clear" + android:layout_width="48dp" + android:layout_height="48dp" + android:background="@drawable/ic_filter_close" + android:button="@android:color/transparent" + android:foreground="?android:attr/selectableItemBackground" + android:layout_gravity="center_vertical" + android:checked="true" /> + +</de.danoeh.antennapod.view.RecursiveRadioGroup> |