summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/danoeh/antennapod/dialog/SwipeActionsDialog.java
blob: 2cd03e21d9420b068713df5961330f9ce5daf212 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package de.danoeh.antennapod.dialog;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;

import androidx.appcompat.app.AlertDialog;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import androidx.appcompat.content.res.AppCompatResources;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.gridlayout.widget.GridLayout;

import java.util.ArrayList;
import java.util.List;

import de.danoeh.antennapod.R;
import de.danoeh.antennapod.databinding.FeeditemlistItemBinding;
import de.danoeh.antennapod.databinding.SwipeactionsDialogBinding;
import de.danoeh.antennapod.databinding.SwipeactionsPickerBinding;
import de.danoeh.antennapod.databinding.SwipeactionsPickerItemBinding;
import de.danoeh.antennapod.databinding.SwipeactionsRowBinding;
import de.danoeh.antennapod.fragment.AllEpisodesFragment;
import de.danoeh.antennapod.fragment.CompletedDownloadsFragment;
import de.danoeh.antennapod.fragment.FeedItemlistFragment;
import de.danoeh.antennapod.fragment.InboxFragment;
import de.danoeh.antennapod.fragment.PlaybackHistoryFragment;
import de.danoeh.antennapod.fragment.QueueFragment;
import de.danoeh.antennapod.fragment.swipeactions.AddToQueueSwipeAction;
import de.danoeh.antennapod.fragment.swipeactions.DeleteSwipeAction;
import de.danoeh.antennapod.fragment.swipeactions.MarkFavoriteSwipeAction;
import de.danoeh.antennapod.fragment.swipeactions.RemoveFromHistorySwipeAction;
import de.danoeh.antennapod.fragment.swipeactions.RemoveFromInboxSwipeAction;
import de.danoeh.antennapod.fragment.swipeactions.RemoveFromQueueSwipeAction;
import de.danoeh.antennapod.fragment.swipeactions.StartDownloadSwipeAction;
import de.danoeh.antennapod.fragment.swipeactions.SwipeAction;
import de.danoeh.antennapod.fragment.swipeactions.SwipeActions;
import de.danoeh.antennapod.fragment.swipeactions.TogglePlaybackStateSwipeAction;
import de.danoeh.antennapod.ui.common.ThemeUtils;

public class SwipeActionsDialog {
    private static final int LEFT = 1;
    private static final int RIGHT = 0;

    private final Context context;
    private final String tag;

    private SwipeAction rightAction;
    private SwipeAction leftAction;
    private List<SwipeAction> keys;

    public SwipeActionsDialog(Context context, String tag) {
        this.context = context;
        this.tag = tag;
    }

    public void show(Callback prefsChanged) {
        SwipeActions.Actions actions = SwipeActions.getPrefsWithDefaults(context, tag);
        leftAction = actions.left;
        rightAction = actions.right;

        final MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context);

        keys = new ArrayList<>();
        if (tag.equals(QueueFragment.TAG)) {
            keys.add(new RemoveFromQueueSwipeAction());
        } else {
            keys.add(new AddToQueueSwipeAction());
        }
        if (!tag.equals(CompletedDownloadsFragment.TAG)) {
            keys.add(new StartDownloadSwipeAction());
        }
        if (!tag.equals(CompletedDownloadsFragment.TAG)
                && ! tag.equals(QueueFragment.TAG)
                && !tag.equals(PlaybackHistoryFragment.TAG)) {
            keys.add(new RemoveFromInboxSwipeAction());
        }
        if (!tag.equals(InboxFragment.TAG)) {
            keys.add(new DeleteSwipeAction());
        }
        keys.add(new MarkFavoriteSwipeAction());
        if (tag.equals(PlaybackHistoryFragment.TAG)) {
            keys.add(new RemoveFromHistorySwipeAction());
        }
        if (!tag.equals(InboxFragment.TAG)) {
            keys.add(new TogglePlaybackStateSwipeAction());
        }

        String forFragment = "";
        switch (tag) {
            case InboxFragment.TAG:
                forFragment = context.getString(R.string.inbox_label);
                break;
            case AllEpisodesFragment.TAG:
                forFragment = context.getString(R.string.episodes_label);
                break;
            case CompletedDownloadsFragment.TAG:
                forFragment = context.getString(R.string.downloads_label);
                break;
            case FeedItemlistFragment.TAG:
                forFragment = context.getString(R.string.individual_subscription);
                break;
            case QueueFragment.TAG:
                forFragment = context.getString(R.string.queue_label);
                break;
            case PlaybackHistoryFragment.TAG:
                forFragment = context.getString(R.string.playback_history_label);
                break;
            default: break;
        }

        builder.setTitle(context.getString(R.string.swipeactions_label) + " - " + forFragment);
        SwipeactionsDialogBinding viewBinding = SwipeactionsDialogBinding.inflate(LayoutInflater.from(context));
        builder.setView(viewBinding.getRoot());

        viewBinding.enableSwitch.setOnCheckedChangeListener((compoundButton, b) -> {
            viewBinding.actionLeftContainer.getRoot().setAlpha(b ? 1.0f : 0.4f);
            viewBinding.actionRightContainer.getRoot().setAlpha(b ? 1.0f : 0.4f);
        });

        viewBinding.enableSwitch.setChecked(SwipeActions.isSwipeActionEnabled(context, tag));

        setupSwipeDirectionView(viewBinding.actionLeftContainer, LEFT);
        setupSwipeDirectionView(viewBinding.actionRightContainer, RIGHT);

        builder.setPositiveButton(R.string.confirm_label, (dialog, which) -> {
            savePrefs(tag, rightAction.getId(), leftAction.getId());
            saveActionsEnabledPrefs(viewBinding.enableSwitch.isChecked());
            prefsChanged.onCall();
        });

        builder.setNegativeButton(R.string.cancel_label, null);
        builder.create().show();
    }

    private void setupSwipeDirectionView(SwipeactionsRowBinding view, int direction) {
        SwipeAction action = direction == LEFT ? leftAction : rightAction;

        view.swipeDirectionLabel.setText(direction == LEFT ? R.string.swipe_left : R.string.swipe_right);
        view.swipeActionLabel.setText(action.getTitle(context));
        populateMockEpisode(view.mockEpisode);
        if (direction == RIGHT && view.previewContainer.getChildAt(0) != view.swipeIcon) {
            view.previewContainer.removeView(view.swipeIcon);
            view.previewContainer.addView(view.swipeIcon, 0);
        }

        view.swipeIcon.setImageResource(action.getActionIcon());
        view.swipeIcon.setColorFilter(ThemeUtils.getColorFromAttr(context, action.getActionColor()));

        view.changeButton.setOnClickListener(v -> showPicker(view, direction));
        view.previewContainer.setOnClickListener(v -> showPicker(view, direction));
    }

    private void showPicker(SwipeactionsRowBinding view, int direction) {
        MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context);
        builder.setTitle(direction == LEFT ? R.string.swipe_left : R.string.swipe_right);

        SwipeactionsPickerBinding picker = SwipeactionsPickerBinding.inflate(LayoutInflater.from(context));
        builder.setView(picker.getRoot());
        builder.setNegativeButton(R.string.cancel_label, null);
        AlertDialog dialog = builder.show();

        for (int i = 0; i < keys.size(); i++) {
            final int actionIndex = i;
            SwipeAction action = keys.get(actionIndex);
            SwipeactionsPickerItemBinding item = SwipeactionsPickerItemBinding.inflate(LayoutInflater.from(context));
            item.swipeActionLabel.setText(action.getTitle(context));

            Drawable icon = DrawableCompat.wrap(AppCompatResources.getDrawable(context, action.getActionIcon()));
            icon.mutate();
            icon.setTintMode(PorterDuff.Mode.SRC_ATOP);
            if ((direction == LEFT && leftAction == action) || (direction == RIGHT && rightAction == action)) {
                icon.setTint(ThemeUtils.getColorFromAttr(context, action.getActionColor()));
                item.swipeActionLabel.setTextColor(ThemeUtils.getColorFromAttr(context, action.getActionColor()));
            } else {
                icon.setTint(ThemeUtils.getColorFromAttr(context, R.attr.action_icon_color));
            }
            item.swipeIcon.setImageDrawable(icon);

            item.getRoot().setOnClickListener(v -> {
                if (direction == LEFT) {
                    leftAction = keys.get(actionIndex);
                } else {
                    rightAction = keys.get(actionIndex);
                }
                setupSwipeDirectionView(view, direction);
                dialog.dismiss();
            });
            GridLayout.LayoutParams param = new GridLayout.LayoutParams(
                    GridLayout.spec(GridLayout.UNDEFINED, GridLayout.BASELINE),
                    GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL, 1f));
            param.width  = 0;
            picker.pickerGridLayout.addView(item.getRoot(), param);
        }
        picker.pickerGridLayout.setColumnCount(2);
        picker.pickerGridLayout.setRowCount((keys.size() + 1) / 2);
    }

    private void populateMockEpisode(FeeditemlistItemBinding view) {
        view.container.setAlpha(0.3f);
        view.secondaryActionButton.secondaryActionButton.setVisibility(View.GONE);
        view.dragHandle.setVisibility(View.GONE);
        view.statusInbox.setVisibility(View.GONE);
        view.txtvTitle.setText("███████");
        view.txtvPosition.setText("█████");
    }

    private void savePrefs(String tag, String right, String left) {
        SharedPreferences prefs = context.getSharedPreferences(SwipeActions.PREF_NAME, Context.MODE_PRIVATE);
        prefs.edit().putString(SwipeActions.KEY_PREFIX_SWIPEACTIONS + tag, right + "," + left).apply();
    }

    private void saveActionsEnabledPrefs(Boolean enabled) {
        SharedPreferences prefs = context.getSharedPreferences(SwipeActions.PREF_NAME, Context.MODE_PRIVATE);
        prefs.edit().putBoolean(SwipeActions.KEY_PREFIX_NO_ACTION + tag, enabled).apply();
    }

    public interface Callback {
        void onCall();
    }
}