summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/danoeh/antennapod/dialog/ItemSortDialog.java
blob: 3090dd6a56f70a277e6116efc9fc3ec5a85427b3 (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
package de.danoeh.antennapod.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.material.bottomsheet.BottomSheetBehavior;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.bottomsheet.BottomSheetDialogFragment;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.databinding.SortDialogBinding;
import de.danoeh.antennapod.databinding.SortDialogItemActiveBinding;
import de.danoeh.antennapod.databinding.SortDialogItemBinding;
import de.danoeh.antennapod.model.feed.SortOrder;

public class ItemSortDialog extends BottomSheetDialogFragment {
    protected SortOrder sortOrder;
    protected SortDialogBinding viewBinding;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        viewBinding = SortDialogBinding.inflate(inflater);
        populateList();
        return viewBinding.getRoot();
    }

    private void populateList() {
        viewBinding.gridLayout.removeAllViews();
        onAddItem(R.string.episode_title, SortOrder.EPISODE_TITLE_A_Z, SortOrder.EPISODE_TITLE_Z_A, true);
        onAddItem(R.string.feed_title, SortOrder.FEED_TITLE_A_Z, SortOrder.FEED_TITLE_Z_A, true);
        onAddItem(R.string.duration, SortOrder.DURATION_SHORT_LONG, SortOrder.DURATION_LONG_SHORT, true);
        onAddItem(R.string.date, SortOrder.DATE_OLD_NEW, SortOrder.DATE_NEW_OLD, false);
        onAddItem(R.string.size, SortOrder.SIZE_SMALL_LARGE, SortOrder.SIZE_LARGE_SMALL, false);
        onAddItem(R.string.filename, SortOrder.EPISODE_FILENAME_A_Z, SortOrder.EPISODE_FILENAME_Z_A, true);
        onAddItem(R.string.random, SortOrder.RANDOM, SortOrder.RANDOM, true);
        onAddItem(R.string.smart_shuffle, SortOrder.SMART_SHUFFLE_OLD_NEW, SortOrder.SMART_SHUFFLE_NEW_OLD, false);
    }

    protected void onAddItem(int title, SortOrder ascending, SortOrder descending, boolean ascendingIsDefault) {
        if (sortOrder == ascending || sortOrder == descending) {
            SortDialogItemActiveBinding item = SortDialogItemActiveBinding.inflate(
                    getLayoutInflater(), viewBinding.gridLayout, false);
            SortOrder other;
            if (ascending == descending) {
                item.button.setText(title);
                other = ascending;
            } else if (sortOrder == ascending) {
                item.button.setText(getString(title) + "\u00A0▲");
                other = descending;
            } else {
                item.button.setText(getString(title) + "\u00A0▼");
                other = ascending;
            }
            item.button.setOnClickListener(v -> {
                sortOrder = other;
                populateList();
                onSelectionChanged();
            });
            viewBinding.gridLayout.addView(item.getRoot());
        } else {
            SortDialogItemBinding item = SortDialogItemBinding.inflate(
                    getLayoutInflater(), viewBinding.gridLayout, false);
            item.button.setText(title);
            item.button.setOnClickListener(v -> {
                sortOrder = ascendingIsDefault ? ascending : descending;
                populateList();
                onSelectionChanged();
            });
            viewBinding.gridLayout.addView(item.getRoot());
        }
    }

    protected void onSelectionChanged() {
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.setOnShowListener(dialogInterface -> {
            BottomSheetDialog bottomSheetDialog = (BottomSheetDialog) dialogInterface;
            setupFullHeight(bottomSheetDialog);
        });
        return dialog;
    }

    private void setupFullHeight(BottomSheetDialog bottomSheetDialog) {
        FrameLayout bottomSheet = bottomSheetDialog.findViewById(R.id.design_bottom_sheet);
        if (bottomSheet != null) {
            BottomSheetBehavior<FrameLayout> behavior = BottomSheetBehavior.from(bottomSheet);
            ViewGroup.LayoutParams layoutParams = bottomSheet.getLayoutParams();
            bottomSheet.setLayoutParams(layoutParams);
            behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
        }
    }
}