summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/danoeh/antennapod/dialog/SkipPreferenceDialog.java
blob: 7bb8f5ad66b4087419a9d97598606666037ce4b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package de.danoeh.antennapod.dialog;

import android.content.Context;
import android.widget.TextView;
import androidx.appcompat.app.AlertDialog;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.core.preferences.UserPreferences;

/**
 * Shows the dialog that allows setting the skip time.
 */
public class SkipPreferenceDialog {
    public static void showSkipPreference(Context context, SkipDirection direction, TextView textView) {
        int checked = 0;

        int skipSecs;
        if (direction == SkipDirection.SKIP_FORWARD) {
            skipSecs = UserPreferences.getFastForwardSecs();
        } else {
            skipSecs = UserPreferences.getRewindSecs();
        }

        final int[] values = context.getResources().getIntArray(R.array.seek_delta_values);
        final String[] choices = new String[values.length];
        for (int i = 0; i < values.length; i++) {
            if (skipSecs == values[i]) {
                checked = i;
            }
            choices[i] = values[i] + " " + context.getString(R.string.time_seconds);
        }

        AlertDialog.Builder builder = new AlertDialog.Builder(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) -> {
            int choice = ((AlertDialog) dialog).getListView().getCheckedItemPosition();
            if (choice < 0 || choice >= values.length) {
                System.err.printf("Choice in showSkipPreference is out of bounds %d", choice);
            } else {
                int seconds = values[choice];
                if (direction == SkipDirection.SKIP_FORWARD) {
                    UserPreferences.setFastForwardSecs(seconds);
                } else {
                    UserPreferences.setRewindSecs(seconds);
                }
                if (textView != null) {
                    textView.setText(String.valueOf(seconds));
                }
            }
        });
        builder.create().show();
    }

    public enum SkipDirection {
        SKIP_FORWARD, SKIP_REWIND
    }
}