|
60 | 60 | import java.util.Arrays;
|
61 | 61 | import java.util.Collection;
|
62 | 62 | import java.util.Collections;
|
| 63 | +import java.util.HashMap; |
63 | 64 | import java.util.HashSet;
|
64 | 65 | import java.util.List;
|
| 66 | +import java.util.Map; |
65 | 67 | import java.util.Set;
|
66 | 68 | import java.util.TreeSet;
|
67 | 69 | import java.util.regex.Matcher;
|
@@ -998,6 +1000,116 @@ public static void showInsertSnippetDialog(final Activity activity, final GsCall
|
998 | 1000 | GsSearchOrCustomTextDialog.showMultiChoiceDialogWithSearchFilterUI(activity, dopt);
|
999 | 1001 | }
|
1000 | 1002 |
|
| 1003 | + public static void showFolderSortDialog( |
| 1004 | + final Activity activity, |
| 1005 | + final GsFileUtils.SortOrder currentOrder, |
| 1006 | + final GsFileUtils.SortOrder globalOrder, |
| 1007 | + final GsCallback.a1<GsFileUtils.SortOrder> callback |
| 1008 | + ) { |
| 1009 | + final DialogOptions dopt = new DialogOptions(); |
| 1010 | + baseConf(activity, dopt); |
| 1011 | + |
| 1012 | + final List<String> data = new ArrayList<>(); |
| 1013 | + final List<Integer> icons = new ArrayList<>(); |
| 1014 | + final List<Integer> layouts = new ArrayList<>(); |
| 1015 | + |
| 1016 | + data.add(activity.getString(R.string.folder_local)); |
| 1017 | + icons.add(R.drawable.ic_save_black_24dp); |
| 1018 | + layouts.add(android.R.layout.simple_list_item_multiple_choice); |
| 1019 | + |
| 1020 | + data.add(activity.getString(R.string.name)); |
| 1021 | + icons.add(R.drawable.ic_sort_by_alpha_black_24dp); |
| 1022 | + layouts.add(android.R.layout.simple_list_item_single_choice); |
| 1023 | + |
| 1024 | + data.add(activity.getString(R.string.date)); |
| 1025 | + icons.add(R.drawable.ic_date_range_black_24dp); |
| 1026 | + layouts.add(android.R.layout.simple_list_item_single_choice); |
| 1027 | + |
| 1028 | + data.add(activity.getString(R.string.size)); |
| 1029 | + icons.add(R.drawable.ic_sd_card_black_24dp); |
| 1030 | + layouts.add(android.R.layout.simple_list_item_single_choice); |
| 1031 | + |
| 1032 | + data.add(activity.getString(R.string.mime_type)); |
| 1033 | + icons.add(R.drawable.ic_baseline_plagiarism_24); |
| 1034 | + layouts.add(android.R.layout.simple_list_item_single_choice); |
| 1035 | + |
| 1036 | + data.add(activity.getString(R.string.folder_first)); |
| 1037 | + icons.add(R.drawable.ic_baseline_rule_folder_24); |
| 1038 | + layouts.add(android.R.layout.simple_list_item_multiple_choice); |
| 1039 | + |
| 1040 | + data.add(activity.getString(R.string.reverse_order)); |
| 1041 | + icons.add(R.drawable.ic_baseline_arrow_upward_24); |
| 1042 | + layouts.add(android.R.layout.simple_list_item_multiple_choice); |
| 1043 | + |
| 1044 | + data.add(activity.getString(R.string.dotfiles)); |
| 1045 | + icons.add(R.drawable.ic_regex_black_24dp); |
| 1046 | + layouts.add(android.R.layout.simple_list_item_multiple_choice); |
| 1047 | + |
| 1048 | + dopt.data = data; |
| 1049 | + dopt.iconsForData = icons; |
| 1050 | + dopt.listItemLayouts = layouts; |
| 1051 | + |
| 1052 | + dopt.preSelected = new HashSet<>(); |
| 1053 | + if (currentOrder.isFolderLocal) dopt.preSelected.add(0); |
| 1054 | + if (currentOrder.folderFirst) dopt.preSelected.add(5); |
| 1055 | + if (currentOrder.reverse) dopt.preSelected.add(6); |
| 1056 | + if (currentOrder.showDotFiles) dopt.preSelected.add(7); |
| 1057 | + |
| 1058 | + final Map<String, Integer> typeToPos = new HashMap<>(); |
| 1059 | + typeToPos.put(GsFileUtils.SORT_BY_NAME, 1); |
| 1060 | + typeToPos.put(GsFileUtils.SORT_BY_MTIME, 2); |
| 1061 | + typeToPos.put(GsFileUtils.SORT_BY_FILESIZE, 3); |
| 1062 | + typeToPos.put(GsFileUtils.SORT_BY_MIMETYPE, 4); |
| 1063 | + dopt.preSelected.add(GsCollectionUtils.getOrDefault(typeToPos, currentOrder.sortByType, 1)); |
| 1064 | + |
| 1065 | + dopt.isMultiSelectEnabled = true; |
| 1066 | + dopt.isSearchEnabled = false; |
| 1067 | + dopt.titleText = R.string.sort_by; |
| 1068 | + dopt.dialogWidthDp = WindowManager.LayoutParams.WRAP_CONTENT; |
| 1069 | + dopt.showCountInOkButton = false; |
| 1070 | + dopt.showSelectAllButton = false; |
| 1071 | + |
| 1072 | + final Set<Integer> prevSelection = new HashSet<>(dopt.preSelected); |
| 1073 | + final boolean[] resetGlobal = {false}; |
| 1074 | + final Set<Integer> radioSet = new HashSet<>(Arrays.asList(1, 2, 3, 4)); |
| 1075 | + dopt.selectionChangedCallback = (selection) -> { |
| 1076 | + final Set<Integer> added = GsCollectionUtils.setDiff(selection, prevSelection); |
| 1077 | + final Set<Integer> removed = GsCollectionUtils.setDiff(prevSelection, selection); |
| 1078 | + if (globalOrder != null && currentOrder.isFolderLocal && removed.contains(0)) { |
| 1079 | + // Reset to global if folder local is unchecked |
| 1080 | + resetGlobal[0] = true; |
| 1081 | + selection.clear(); |
| 1082 | + if (globalOrder.folderFirst) selection.add(5); |
| 1083 | + if (globalOrder.reverse) selection.add(6); |
| 1084 | + if (globalOrder.showDotFiles) selection.add(7); |
| 1085 | + selection.add(GsCollectionUtils.getOrDefault(typeToPos, globalOrder.sortByType, 1)); |
| 1086 | + } else if (!Collections.disjoint(removed, radioSet)) { |
| 1087 | + // If a radio button is unchecked add it back |
| 1088 | + selection.addAll(removed); |
| 1089 | + } else if (!Collections.disjoint(added, radioSet)) { |
| 1090 | + // If a radio button is checked, remove all other radio buttons |
| 1091 | + selection.removeAll(GsCollectionUtils.setDiff(radioSet, added)); |
| 1092 | + } |
| 1093 | + prevSelection.clear(); |
| 1094 | + prevSelection.addAll(selection); |
| 1095 | + }; |
| 1096 | + |
| 1097 | + dopt.positionCallback = (selection) -> { |
| 1098 | + final GsFileUtils.SortOrder order = new GsFileUtils.SortOrder(); |
| 1099 | + order.isFolderLocal = selection.contains(0); |
| 1100 | + order.folderFirst = selection.contains(5); |
| 1101 | + order.reverse = selection.contains(6); |
| 1102 | + order.showDotFiles = selection.contains(7); |
| 1103 | + if (selection.contains(2)) order.sortByType = GsFileUtils.SORT_BY_MTIME; |
| 1104 | + else if (selection.contains(3)) order.sortByType = GsFileUtils.SORT_BY_FILESIZE; |
| 1105 | + else if (selection.contains(4)) order.sortByType = GsFileUtils.SORT_BY_MIMETYPE; |
| 1106 | + else order.sortByType = GsFileUtils.SORT_BY_NAME; |
| 1107 | + callback.callback(order); |
| 1108 | + }; |
| 1109 | + |
| 1110 | + GsSearchOrCustomTextDialog.showMultiChoiceDialogWithSearchFilterUI(activity, dopt); |
| 1111 | + } |
| 1112 | + |
1001 | 1113 | public static void baseConf(Activity activity, DialogOptions dopt) {
|
1002 | 1114 | dopt.isDarkDialog = GsContextUtils.instance.isDarkModeEnabled(activity);
|
1003 | 1115 | dopt.clearInputIcon = R.drawable.ic_baseline_clear_24;
|
|
0 commit comments