Using state lists for colors

This commit is contained in:
ByteHamster 2020-06-21 12:49:02 +02:00
parent f243bcd4cc
commit 7de9e0d4c1
12 changed files with 198 additions and 285 deletions

View File

@ -1,22 +1,18 @@
package de.danoeh.antennapod.dialog;
import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import androidx.appcompat.app.AlertDialog;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
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;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public abstract class FilterDialog {
@ -35,91 +31,35 @@ public abstract class FilterDialog {
builder.setTitle(R.string.filter);
LayoutInflater inflater = LayoutInflater.from(this.context);
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.filter_dialog_layout, null, false);
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.filter_dialog, null, false);
builder.setView(layout);
for (FeedItemFilterGroup.FeedItemEnum item : FeedItemFilterGroup.FeedItemEnum.values()) {
RelativeLayout row = (RelativeLayout) inflater.inflate(R.layout.filter_dialog_relative_cardview, null);
RadioButton radioButton1 = row.findViewById(R.id.filter_dialog_radioButton1);
RadioButton radioButton2 = row.findViewById(R.id.filter_dialog_radioButton2);
RadioButton radioButton3 = row.findViewById(R.id.filter_dialog_radioButton3);
radioButton1.setText(item.values[1].displayName);
radioButton1.setTextColor(Color.BLACK);
radioButton2.setText(item.values[0].displayName);
radioButton2.setTextColor(Color.BLACK);
Iterator<String> filterIterator = filterValues.iterator();
while (filterIterator.hasNext()) {
String nextItem = filterIterator.next();
if (item.values[1].filterId.equals(nextItem)) {
item.values[1].setSelected(true);
item.values[0].setSelected(false);
radioButton1.setBackgroundResource(R.color.accent_light);
radioButton2.setBackgroundResource(R.color.master_switch_background_light);
radioButton3.setBackgroundResource(R.drawable.filter_dialog_x_on);
radioButton1.setSelected(true);
radioButton2.setSelected(false);
radioButton1.setTextColor(Color.WHITE);
radioButton2.setTextColor(Color.BLACK);
}
if (item.values[0].filterId.equals(nextItem)) {
item.values[0].setSelected(true);
item.values[1].setSelected(false);
radioButton2.setBackgroundResource(R.color.accent_light);
radioButton1.setBackgroundResource(R.color.master_switch_background_light);
radioButton3.setBackgroundResource(R.drawable.filter_dialog_x_on);
radioButton2.setSelected(true);
radioButton1.setSelected(false);
radioButton2.setTextColor(Color.WHITE);
radioButton1.setTextColor(Color.BLACK);
}
}
radioButton1.setOnClickListener(arg0 -> {
item.values[1].setSelected(true);
item.values[0].setSelected(false);
radioButton1.setBackgroundResource(R.color.accent_light);
radioButton2.setBackgroundResource(R.color.master_switch_background_light);
radioButton3.setBackgroundResource(R.drawable.filter_dialog_x_on);
radioButton2.setSelected(false);
radioButton2.setTextColor(Color.BLACK);
radioButton1.setSelected(true);
radioButton1.setTextColor(Color.WHITE);
});
radioButton2.setOnClickListener(arg0 -> {
item.values[0].setSelected(true);
item.values[1].setSelected(false);
radioButton2.setBackgroundResource(R.color.accent_light);
radioButton1.setBackgroundResource(R.color.master_switch_background_light);
radioButton3.setBackgroundResource(R.drawable.filter_dialog_x_on);
radioButton1.setSelected(false);
radioButton1.setTextColor(Color.BLACK);
radioButton2.setSelected(true);
radioButton2.setTextColor(Color.WHITE);
});
radioButton3.setOnClickListener(arg0 -> {
item.values[0].setSelected(false);
item.values[1].setSelected(false);
radioButton1.setBackgroundResource(R.color.master_switch_background_light);
radioButton2.setBackgroundResource(R.color.master_switch_background_light);
radioButton3.setBackgroundResource(R.drawable.filter_dialog_x_off);
radioButton2.setTextColor(Color.BLACK);
radioButton2.setSelected(false);
radioButton1.setTextColor(Color.BLACK);
radioButton1.setSelected(false);
});
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 (String filterId : filterValues) {
if (!TextUtils.isEmpty(filterId)) {
((RadioButton) layout.findViewWithTag(filterId)).setChecked(true);
}
}
builder.setPositiveButton(R.string.confirm_label, (dialog, which) -> {
filterValues.clear();
for (FeedItemFilterGroup.FeedItemEnum item : FeedItemFilterGroup.FeedItemEnum.values()) {
for (int i = 0; i < item.values.length; i++) {
if (item.values[i].getSelected()) {
filterValues.add(item.values[i].filterId);
}
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) {
filterValues.add((String) group.getCheckedButton().getTag());
}
}
updateFilter(filterValues);

View File

@ -0,0 +1,68 @@
package de.danoeh.antennapod.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.ActionMode;
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;
}
}

View File

@ -1,55 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="260dp"
android:layout_height="48dp"
android:layout_marginBottom="10dp"
android:clipChildren="true"
app:cardCornerRadius="24dp"
app:cardElevation="0dp">
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<RadioButton
android:id="@+id/filter_dialog_radioButton1"
android:layout_width="130dp"
android:layout_height="48dp"
android:layout_weight="1"
android:background="@color/master_switch_background_light"
android:button="@android:color/transparent"
android:checked="false"
android:gravity="center" />
<RadioButton
android:id="@+id/filter_dialog_radioButton2"
android:layout_width="130dp"
android:layout_height="48dp"
android:layout_weight="1"
android:background="@color/master_switch_background_light"
android:button="@android:color/transparent"
android:checked="false"
android:gravity="center" />
</RadioGroup>
</androidx.cardview.widget.CardView>
<RadioButton
android:id="@+id/filter_dialog_radioButton3"
android:layout_width="50dp"
android:layout_height="48dp"
android:layout_marginStart="270dp"
android:layout_marginLeft="270dp"
android:layout_marginEnd="0dp"
android:layout_marginRight="0dp"
android:background="@drawable/filter_dialog_x_off"
android:button="@android:color/transparent"
android:checked="false" />
</RelativeLayout>

View File

@ -0,0 +1,63 @@
<?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:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:clipChildren="true"
app:cardCornerRadius="24dp"
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_weight="1"
android:background="@color/filter_dialog_button_background_light"
android:textColor="@color/filter_dialog_button_text_light"
android:button="@android:color/transparent"
android:layout_marginRight="2dp"
android:layout_marginEnd="2dp"
android:checked="false"
android:foreground="?attr/selectableItemBackground"
android:tag=""
android:gravity="center" />
<RadioButton
android:id="@+id/filter_dialog_radioButton2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/filter_dialog_button_background_light"
android:textColor="@color/filter_dialog_button_text_light"
android:button="@android:color/transparent"
android:checked="false"
android:foreground="?attr/selectableItemBackground"
android:tag=""
android:gravity="center" />
</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_light"
android:button="@android:color/transparent"
android:foreground="?attr/selectableItemBackground"
android:tag=""
android:checked="true" />
</de.danoeh.antennapod.view.RecursiveRadioGroup>

View File

@ -2,83 +2,35 @@ package de.danoeh.antennapod.core.feed;
import de.danoeh.antennapod.core.R;
public class FeedItemFilterGroup {
public enum FeedItemFilterGroup {
PLAYED(new ItemProperties(R.string.hide_played_episodes_label, "played"),
new ItemProperties(R.string.not_played, "unplayed")),
PAUSED(new ItemProperties(R.string.hide_paused_episodes_label, "paused"),
new ItemProperties(R.string.not_paused, "")),
FAVORITE(new ItemProperties(R.string.hide_is_favorite_label, "is_favorite"),
new ItemProperties(R.string.not_favorite, "")),
MEDIA(new ItemProperties(R.string.has_media, ""),
new ItemProperties(R.string.no_media, "no_media")),
QUEUED(new ItemProperties(R.string.queued_label, "queued"),
new ItemProperties(R.string.not_queued_label, "not_queued")),
DOWNLOADED(new ItemProperties(R.string.hide_downloaded_episodes_label, "downloaded"),
new ItemProperties(R.string.hide_not_downloaded_episodes_label, "not_downloaded"));
private static boolean DEFAULT_SELECTED_STATE = false;
public final ItemProperties[] values;
private static final String NO_FILTERID = "";
private static final int HASMEDIA_LABEL = R.string.has_media;
private static final int NOTPAUSED_LABEL = R.string.not_paused;
private static final int NOTFAVORITE_LABEL = R.string.not_favorite;
FeedItemFilterGroup(ItemProperties... values) {
this.values = values;
}
private static final int UNPLAYED_LABEL = R.string.not_played;
private static final String UNPLAYED_FILTERID = "unplayed";
public static class ItemProperties {
private static final int PLAYED_LABEL = R.string.hide_played_episodes_label;
private static final String PLAYED_FILTERID = "played";
public final int displayName;
public final String filterId;
private static final int PAUSED_LABEL = R.string.hide_paused_episodes_label;
private static final String PAUSED_FILTERID = "paused";
private static final int ISFAVORITE_LABEL = R.string.hide_is_favorite_label;
private static final String ISFAVORITE_FILTERID = "is_favorite";
private static final int NOMEDIA_LABEL = R.string.no_media;
private static final String NOMEDIA_FILTERID = "no_media";
private static final int QUEUED_LABEL = R.string.queue_label;
private static final String QUEUED_FILTERID = "queued";
private static final int NOTQUEUED_LABEL = R.string.not_queued_label;
private static final String NOTQUEUED_FILTERID = "not_queued";
private static final int NOTDOWNLOADED_LABEL = R.string.hide_downloaded_episodes_label;
private static final String NOTDOWNLOADED_FILTERID = "not_downloaded";
private static final int DOWNLOADED_LABEL = R.string.hide_downloaded_episodes_label;
private static final String DOWNLOADED_FILTERID = "downloaded";
public enum FeedItemEnum {
PLAYED(new ItemProperties(DEFAULT_SELECTED_STATE, UNPLAYED_LABEL, UNPLAYED_FILTERID),
new ItemProperties(DEFAULT_SELECTED_STATE, PLAYED_LABEL, PLAYED_FILTERID)),
PAUSED(new ItemProperties(DEFAULT_SELECTED_STATE, NOTPAUSED_LABEL, NO_FILTERID),
new ItemProperties(DEFAULT_SELECTED_STATE, PAUSED_LABEL, PAUSED_FILTERID)),
FAVORITE(new ItemProperties(DEFAULT_SELECTED_STATE, NOTFAVORITE_LABEL, NO_FILTERID),
new ItemProperties(DEFAULT_SELECTED_STATE, ISFAVORITE_LABEL, ISFAVORITE_FILTERID)),
MEDIA(new ItemProperties(DEFAULT_SELECTED_STATE, NOMEDIA_LABEL, NOMEDIA_FILTERID),
new ItemProperties(DEFAULT_SELECTED_STATE, HASMEDIA_LABEL, NO_FILTERID)),
QUEUED(new ItemProperties(DEFAULT_SELECTED_STATE, NOTQUEUED_LABEL, NOTQUEUED_FILTERID),
new ItemProperties(DEFAULT_SELECTED_STATE, QUEUED_LABEL, QUEUED_FILTERID)),
DOWNLOADED(new ItemProperties(DEFAULT_SELECTED_STATE, NOTDOWNLOADED_LABEL, NOTDOWNLOADED_FILTERID),
new ItemProperties(DEFAULT_SELECTED_STATE, DOWNLOADED_LABEL, DOWNLOADED_FILTERID));
public final ItemProperties[] values;
FeedItemEnum(ItemProperties... values) {
this.values = values;
public ItemProperties(int displayName, String filterId) {
this.displayName = displayName;
this.filterId = filterId;
}
public static class ItemProperties {
public final int displayName;
public boolean selected;
public final String filterId;
public void setSelected(boolean value) {
this.selected = value;
}
public boolean getSelected() {
return this.selected;
}
public ItemProperties(boolean selected, int displayName, String filterId) {
this.selected = selected;
this.displayName = displayName;
this.filterId = filterId;
}
}
}
}

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/accent_light" />
<item android:color="@color/master_switch_background_light" />
</selector>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/grey" />
<item android:color="@color/master_switch_background_light" />
</selector>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/white" />
<item android:color="@color/black" />
</selector>

View File

@ -1,56 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:bottom="5dp"
android:left="5dp"
android:right="5dp"
android:top="5dp">
<shape android:shape="oval">
<stroke
android:width="4dp"
android:color="@color/master_switch_background_light" />
</shape>
</item>
<!-- x -->
<item
android:bottom="12dp"
android:left="12dp"
android:right="12dp"
android:top="12dp">
<rotate
android:fromDegrees="135"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="135">
<shape android:shape="line">
<stroke
android:width="4dp"
android:color="@color/master_switch_background_light" />
</shape>
</rotate>
</item>
<item
android:bottom="12dp"
android:left="12dp"
android:right="12dp"
android:top="12dp">
<rotate
android:fromDegrees="45"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="45">
<shape android:shape="line">
<stroke
android:width="4dp"
android:color="@color/master_switch_background_light" />
</shape>
</rotate>
</item>
</layer-list>

View File

@ -10,9 +10,8 @@
<shape android:shape="oval">
<stroke
android:width="4dp"
android:color="@color/grey" />
android:color="@color/filter_dialog_button_clear" />
</shape>
</item>
<!-- x -->
@ -29,7 +28,7 @@
<shape android:shape="line">
<stroke
android:width="4dp"
android:color="@color/grey" />
android:color="@color/filter_dialog_button_clear" />
</shape>
</rotate>
</item>
@ -47,9 +46,8 @@
<shape android:shape="line">
<stroke
android:width="4dp"
android:color="@color/grey" />
android:color="@color/filter_dialog_button_clear" />
</shape>
</rotate>
</item>

View File

@ -245,18 +245,6 @@
<item>exoplayer</item>
</string-array>
<string-array name="episode_filter_options">
<item>@string/hide_unplayed_episodes_label</item>
<item>@string/hide_paused_episodes_label</item>
<item>@string/hide_played_episodes_label</item>
<item>@string/hide_queued_episodes_label</item>
<item>@string/hide_not_queued_episodes_label</item>
<item>@string/hide_downloaded_episodes_label</item>
<item>@string/hide_not_downloaded_episodes_label</item>
<item>@string/hide_has_media_label</item>
<item>@string/hide_is_favorite_label</item>
</string-array>
<!-- sort for podcast screen, not for queue -->
<string-array name="feed_episodes_sort_options">
<item>@string/sort_date_new_old</item>