Migrate ListPreference to Material Design 3

This commit is contained in:
ByteHamster 2022-12-05 22:36:35 +01:00
parent 4513711981
commit ef97411fbb
7 changed files with 59 additions and 19 deletions

View File

@ -409,7 +409,6 @@ public class PreferencesTest {
clickPreference(R.string.network_pref);
onView(withText(R.string.pref_automatic_download_title)).perform(click());
onView(withText(R.string.pref_episode_cleanup_title)).perform(click());
onView(withId(R.id.select_dialog_listview)).perform(swipeUp());
onView(withText(R.string.episode_cleanup_after_listening)).perform(click());
Awaitility.await().atMost(1000, MILLISECONDS)
.until(() -> {
@ -455,7 +454,6 @@ public class PreferencesTest {
// Find next value (wrapping around to next)
int newIndex = (currentIndex + 1) % deltas.length;
onView(withText(deltas[newIndex] + " seconds")).perform(click());
onView(withText("Confirm")).perform(click());
Awaitility.await().atMost(1000, MILLISECONDS)
.until(() -> UserPreferences.getRewindSecs() == deltas[newIndex]);
@ -477,7 +475,6 @@ public class PreferencesTest {
int newIndex = (currentIndex + 1) % deltas.length;
onView(withText(deltas[newIndex] + " seconds")).perform(click());
onView(withText("Confirm")).perform(click());
Awaitility.await().atMost(1000, MILLISECONDS)
.until(() -> UserPreferences.getFastForwardSecs() == deltas[newIndex]);

View File

@ -37,9 +37,7 @@ public class SkipPreferenceDialog {
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context);
builder.setTitle(direction == SkipDirection.SKIP_FORWARD ? R.string.pref_fast_forward : R.string.pref_rewind);
builder.setSingleChoiceItems(choices, checked, null);
builder.setNegativeButton(R.string.cancel_label, null);
builder.setPositiveButton(R.string.confirm_label, (dialog, which) -> {
builder.setSingleChoiceItems(choices, checked, (dialog, which) -> {
int choice = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
if (choice < 0 || choice >= values.length) {
System.err.printf("Choice in showSkipPreference is out of bounds %d", choice);
@ -53,9 +51,11 @@ public class SkipPreferenceDialog {
if (textView != null) {
textView.setText(NumberFormat.getInstance().format(seconds));
}
dialog.dismiss();
}
});
builder.create().show();
builder.setNegativeButton(R.string.cancel_label, null);
builder.show();
}
public enum SkipDirection {

View File

@ -0,0 +1,43 @@
package de.danoeh.antennapod.preferences;
import android.content.Context;
import android.util.AttributeSet;
import androidx.preference.ListPreference;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
public class MaterialListPreference extends ListPreference {
public MaterialListPreference(Context context) {
super(context);
}
public MaterialListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onClick() {
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(getContext());
builder.setTitle(getTitle());
builder.setIcon(getDialogIcon());
builder.setNegativeButton(getNegativeButtonText(), null);
CharSequence[] values = getEntryValues();
int selected = -1;
for (int i = 0; i < values.length; i++) {
if (values[i].toString().equals(getValue())) {
selected = i;
}
}
builder.setSingleChoiceItems(getEntries(), selected, (dialog, which) -> {
dialog.dismiss();
if (which >= 0 && getEntryValues() != null) {
String value = getEntryValues()[which].toString();
if (callChangeListener(value)) {
setValue(value);
}
}
});
builder.show();
}
}

View File

@ -40,7 +40,7 @@
android:summary="@string/pref_feed_skip_sum"
android:title="@string/pref_feed_skip" />
<ListPreference
<de.danoeh.antennapod.preferences.MaterialListPreference
android:entries="@array/spnAutoDeleteItems"
android:entryValues="@array/spnAutoDeleteValues"
android:icon="@drawable/ic_delete"
@ -48,7 +48,7 @@
android:summary="@string/feed_auto_download_global"
android:title="@string/auto_delete_label" />
<ListPreference
<de.danoeh.antennapod.preferences.MaterialListPreference
android:defaultValue="off"
android:entries="@array/spnVolumeReductionItems"
android:entryValues="@array/spnVolumeReductionValues"

View File

@ -8,14 +8,14 @@
android:title="@string/pref_automatic_download_title"
search:summary="@string/pref_automatic_download_sum"
android:defaultValue="false"/>
<ListPreference
<de.danoeh.antennapod.preferences.MaterialListPreference
android:defaultValue="25"
android:entries="@array/episode_cache_size_entries"
android:key="prefEpisodeCacheSize"
android:title="@string/pref_episode_cache_title"
android:summary="@string/pref_episode_cache_summary"
android:entryValues="@array/episode_cache_size_values"/>
<ListPreference
<de.danoeh.antennapod.preferences.MaterialListPreference
android:defaultValue="-1"
android:entries="@array/episode_cleanup_entries"
android:key="prefEpisodeCleanup"

View File

@ -62,14 +62,14 @@
</PreferenceCategory>
<PreferenceCategory android:title="@string/reassign_hardware_buttons">
<ListPreference
<de.danoeh.antennapod.preferences.MaterialListPreference
android:defaultValue="@string/keycode_media_fast_forward"
android:entries="@array/button_action_options"
android:entryValues="@array/button_action_values"
android:key="prefHardwareForwardButton"
android:title="@string/pref_hardware_forward_button_title"
android:summary="@string/pref_hardware_forward_button_summary"/>
<ListPreference
<de.danoeh.antennapod.preferences.MaterialListPreference
android:defaultValue="@string/keycode_media_rewind"
android:entries="@array/button_action_options"
android:entryValues="@array/button_action_values"
@ -85,7 +85,7 @@
android:key="prefEnqueueDownloaded"
android:summary="@string/pref_enqueue_downloaded_summary"
android:title="@string/pref_enqueue_downloaded_title" />
<ListPreference
<de.danoeh.antennapod.preferences.MaterialListPreference
android:defaultValue="BACK"
android:entries="@array/enqueue_location_options"
android:entryValues="@array/enqueue_location_values"
@ -97,7 +97,7 @@
android:key="prefFollowQueue"
android:summary="@string/pref_followQueue_sum"
android:title="@string/pref_followQueue_title"/>
<ListPreference
<de.danoeh.antennapod.preferences.MaterialListPreference
android:defaultValue="30"
android:entries="@array/smart_mark_as_played_values"
android:entryValues="@array/smart_mark_as_played_values"
@ -113,7 +113,7 @@
</PreferenceCategory>
<PreferenceCategory android:title="@string/experimental_pref">
<ListPreference
<de.danoeh.antennapod.preferences.MaterialListPreference
android:defaultValue="exoplayer"
android:entries="@array/media_player_options"
android:key="prefMediaPlayer"

View File

@ -4,7 +4,7 @@
xmlns:search="http://schemas.android.com/apk/com.bytehamster.lib.preferencesearch">
<PreferenceCategory android:title="@string/appearance">
<ListPreference
<de.danoeh.antennapod.preferences.MaterialListPreference
android:entryValues="@array/theme_values"
android:entries="@array/theme_options"
android:title="@string/pref_set_theme_title"
@ -33,7 +33,7 @@
android:title="@string/pref_nav_drawer_feed_order_title"
android:key="prefDrawerFeedOrder"
android:summary="@string/pref_nav_drawer_feed_order_sum"/>
<ListPreference
<de.danoeh.antennapod.preferences.MaterialListPreference
android:entryValues="@array/nav_drawer_feed_counter_values"
android:entries="@array/nav_drawer_feed_counter_options"
android:title="@string/pref_nav_drawer_feed_counter_title"
@ -76,7 +76,7 @@
android:title="@string/pref_lockscreen_background_title"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/behavior">
<ListPreference
<de.danoeh.antennapod.preferences.MaterialListPreference
android:entryValues="@array/default_page_values"
android:entries="@array/default_page_titles"
android:key="prefDefaultPage"