diff options
author | Matej Drobnič <matej@matejdro.com> | 2024-02-04 19:54:46 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-04 19:54:46 +0100 |
commit | 0f5600932d5894d5653993d48d925df437fe6d00 (patch) | |
tree | ee34151ac28bbe55dcec9c45de1cb5dbe92ad299 /app/src/main/java/de/danoeh | |
parent | f0e96a269233e85a452eabcbfe09899157024ab3 (diff) | |
download | AntennaPod-0f5600932d5894d5653993d48d925df437fe6d00.zip |
Add next chapter button to notification (#6276)
Diffstat (limited to 'app/src/main/java/de/danoeh')
-rw-r--r-- | app/src/main/java/de/danoeh/antennapod/fragment/preferences/UserInterfacePreferencesFragment.java | 93 |
1 files changed, 66 insertions, 27 deletions
diff --git a/app/src/main/java/de/danoeh/antennapod/fragment/preferences/UserInterfacePreferencesFragment.java b/app/src/main/java/de/danoeh/antennapod/fragment/preferences/UserInterfacePreferencesFragment.java index c8521caab..0f3320e98 100644 --- a/app/src/main/java/de/danoeh/antennapod/fragment/preferences/UserInterfacePreferencesFragment.java +++ b/app/src/main/java/de/danoeh/antennapod/fragment/preferences/UserInterfacePreferencesFragment.java @@ -1,8 +1,10 @@ package de.danoeh.antennapod.fragment.preferences; import android.content.Context; +import android.content.DialogInterface; import android.os.Build; import android.os.Bundle; +import android.widget.Button; import android.widget.ListView; import androidx.appcompat.app.AlertDialog; import androidx.core.app.ActivityCompat; @@ -64,9 +66,9 @@ public class UserInterfacePreferencesFragment extends PreferenceFragmentCompat { return true; }); - findPreference(UserPreferences.PREF_COMPACT_NOTIFICATION_BUTTONS) + findPreference(UserPreferences.PREF_FULL_NOTIFICATION_BUTTONS) .setOnPreferenceClickListener(preference -> { - showNotificationButtonsDialog(); + showFullNotificationButtonsDialog(); return true; }); findPreference(UserPreferences.PREF_FILTER_FEED) @@ -91,48 +93,85 @@ public class UserInterfacePreferencesFragment extends PreferenceFragmentCompat { } } - private void showNotificationButtonsDialog() { + + private void showFullNotificationButtonsDialog() { final Context context = getActivity(); - final List<Integer> preferredButtons = UserPreferences.getCompactNotificationButtons(); + + final List<Integer> preferredButtons = UserPreferences.getFullNotificationButtons(); final String[] allButtonNames = context.getResources().getStringArray( - R.array.compact_notification_buttons_options); + R.array.full_notification_buttons_options); + final int[] buttonIDs = {2, 3, 4}; + final int exactItems = 2; + final DialogInterface.OnClickListener completeListener = (dialog, which) -> + UserPreferences.setFullNotificationButtons(preferredButtons); + final String title = context.getResources().getString( + R.string.pref_full_notification_buttons_title); + + showNotificationButtonsDialog(preferredButtons, allButtonNames, buttonIDs, title, + exactItems, completeListener + ); + } + + private void showNotificationButtonsDialog(List<Integer> preferredButtons, + String[] allButtonNames, int[] buttonIds, String title, + int exactItems, DialogInterface.OnClickListener completeListener) { boolean[] checked = new boolean[allButtonNames.length]; // booleans default to false in java + final Context context = getActivity(); + + // Clear buttons that are not part of the setting anymore + for (int i = preferredButtons.size() - 1; i >= 0; i--) { + boolean isValid = false; + for (int j = 0; j < checked.length; j++) { + if (buttonIds[j] == preferredButtons.get(i)) { + isValid = true; + } + } + + if (!isValid) { + preferredButtons.remove(i); + } + } + for(int i=0; i < checked.length; i++) { - if(preferredButtons.contains(i)) { + if (preferredButtons.contains(buttonIds[i])) { checked[i] = true; } } MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context); - builder.setTitle(String.format(context.getResources().getString( - R.string.pref_compact_notification_buttons_dialog_title), 2)); + builder.setTitle(title); builder.setMultiChoiceItems(allButtonNames, checked, (dialog, which, isChecked) -> { checked[which] = isChecked; if (isChecked) { - if (preferredButtons.size() < 2) { - preferredButtons.add(which); - } else { - // Only allow a maximum of two selections. This is because the notification - // on the lock screen can only display 3 buttons, and the play/pause button - // is always included. - checked[which] = false; - ListView selectionView = ((AlertDialog) dialog).getListView(); - selectionView.setItemChecked(which, false); - Snackbar.make( - selectionView, - String.format(context.getResources().getString( - R.string.pref_compact_notification_buttons_dialog_error), 2), - Snackbar.LENGTH_SHORT).show(); - } + preferredButtons.add(buttonIds[which]); } else { - preferredButtons.remove((Integer) which); + preferredButtons.remove((Integer) buttonIds[which]); } }); - builder.setPositiveButton(R.string.confirm_label, (dialog, which) -> - UserPreferences.setCompactNotificationButtons(preferredButtons)); + builder.setPositiveButton(R.string.confirm_label, null); builder.setNegativeButton(R.string.cancel_label, null); - builder.create().show(); + final AlertDialog dialog = builder.create(); + + dialog.show(); + + Button positiveButton = dialog.getButton(AlertDialog.BUTTON_POSITIVE); + + positiveButton.setOnClickListener(v -> { + if (preferredButtons.size() != exactItems) { + ListView selectionView = dialog.getListView(); + Snackbar.make( + selectionView, + String.format(context.getResources().getString( + R.string.pref_compact_notification_buttons_dialog_error_exact), exactItems), + Snackbar.LENGTH_SHORT).show(); + + } else { + completeListener.onClick(dialog, AlertDialog.BUTTON_POSITIVE); + dialog.cancel(); + } + } + ); } } |