Skip to content

Commit

Permalink
New approach to dialog using change callback
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Jan 6, 2025
1 parent 5a01163 commit 2f6cfcb
Show file tree
Hide file tree
Showing 12 changed files with 253 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,13 @@ public boolean isValid() {

public static Link extract(final CharSequence text, final int pos) {
final int[] sel = TextViewUtils.getLineSelection(text, pos);
if (sel != null && sel[0] != -1 && sel[1] != -1) {
if (sel[0] != -1 && sel[1] != -1) {
final String line = text.subSequence(sel[0], sel[1]).toString();
final Matcher m = MarkdownSyntaxHighlighter.LINK.matcher(line);
final int po = pos - sel[0];

while (m.find()) {
final int start = m.start(), end = m.end();
if (start <= po && end >= po) {
final int start = m.start() + sel[0], end = m.end() + sel[0];
if (start <= pos && end >= pos) {
final boolean isImage = m.group(1) != null;
return new Link(m.group(2), m.group(3), isImage, start, end);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -1000,55 +1002,109 @@ public static void showInsertSnippetDialog(final Activity activity, final GsCall

public static void showFolderSortDialog(
final Activity activity,
final String sortType,
final boolean isReverse,
final boolean isFoldersFirst,
final boolean isDotFiles,
final boolean isSaved,
final GsCallback.a5<String, Boolean, Boolean, Boolean, Boolean> callback
final GsFileUtils.SortOrder currentOrder,
final GsFileUtils.SortOrder globalOrder,
final GsCallback.a1<GsFileUtils.SortOrder> callback
) {
final DialogOptions dopt = new DialogOptions();
baseConf(activity, dopt);

final List<String> data = new ArrayList<>();
data.add(activity.getString(R.string.save));
final List<Integer> icons = new ArrayList<>();
final List<Integer> layouts = new ArrayList<>();

data.add(activity.getString(R.string.folder_local));
icons.add(R.drawable.ic_save_black_24dp);
layouts.add(android.R.layout.simple_list_item_multiple_choice);

data.add(activity.getString(R.string.name));
icons.add(R.drawable.ic_sort_by_alpha_black_24dp);
layouts.add(android.R.layout.simple_list_item_single_choice);

data.add(activity.getString(R.string.date));
icons.add(R.drawable.ic_date_range_black_24dp);
layouts.add(android.R.layout.simple_list_item_single_choice);

data.add(activity.getString(R.string.size));
icons.add(R.drawable.ic_sd_card_black_24dp);
layouts.add(android.R.layout.simple_list_item_single_choice);

data.add(activity.getString(R.string.mime_type));
icons.add(R.drawable.ic_baseline_plagiarism_24);
layouts.add(android.R.layout.simple_list_item_single_choice);

data.add(activity.getString(R.string.folder_first));
icons.add(R.drawable.ic_baseline_rule_folder_24);
layouts.add(android.R.layout.simple_list_item_multiple_choice);

data.add(activity.getString(R.string.reverse_order));
icons.add(R.drawable.ic_baseline_arrow_upward_24);
layouts.add(android.R.layout.simple_list_item_multiple_choice);

data.add(activity.getString(R.string.dotfiles));
icons.add(R.drawable.ic_filter_center_focus_black_24dp);
layouts.add(android.R.layout.simple_list_item_multiple_choice);

dopt.data = data;
dopt.iconsForData = icons;
dopt.listItemLayouts = layouts;

dopt.preSelected = new HashSet<>();
if (isReverse) dopt.preSelected.add(6);
if (isFoldersFirst) dopt.preSelected.add(5);
if (isDotFiles) dopt.preSelected.add(7);
if (isSaved) dopt.preSelected.add(0);
if (GsFileUtils.SORT_BY_NAME.equals(sortType)) dopt.preSelected.add(1);
else if (GsFileUtils.SORT_BY_MTIME.equals(sortType)) dopt.preSelected.add(2);
else if (GsFileUtils.SORT_BY_FILESIZE.equals(sortType)) dopt.preSelected.add(3);
else if (GsFileUtils.SORT_BY_MIMETYPE.equals(sortType)) dopt.preSelected.add(4);

dopt.radioIndices = new HashSet<>(Arrays.asList(1, 2, 3, 4));
if (currentOrder.isFolderLocal) dopt.preSelected.add(0);
if (currentOrder.folderFirst) dopt.preSelected.add(5);
if (currentOrder.reverse) dopt.preSelected.add(6);
if (currentOrder.showDotFiles) dopt.preSelected.add(7);

final Map<String, Integer> typeToPos = new HashMap<>();
typeToPos.put(GsFileUtils.SORT_BY_NAME, 1);
typeToPos.put(GsFileUtils.SORT_BY_MTIME, 2);
typeToPos.put(GsFileUtils.SORT_BY_FILESIZE, 3);
typeToPos.put(GsFileUtils.SORT_BY_MIMETYPE, 4);
dopt.preSelected.add(GsCollectionUtils.getOrDefault(typeToPos, currentOrder.sortByType, 1));

dopt.isMultiSelectEnabled = true;
dopt.isSearchEnabled = false;
dopt.titleText = R.string.sort_by;
dopt.dialogWidthDp = WindowManager.LayoutParams.WRAP_CONTENT;
dopt.showCountInOkButton = false;
dopt.showSelectAllButton = false;

final Set<Integer> prevSelection = new HashSet<>(dopt.preSelected);
final boolean[] resetGlobal = {false};
final Set<Integer> radioSet = new HashSet<>(Arrays.asList(1, 2, 3, 4));
dopt.selectionChangedCallback = (selection) -> {
final Set<Integer> added = GsCollectionUtils.setDiff(selection, prevSelection);
final Set<Integer> removed = GsCollectionUtils.setDiff(prevSelection, selection);
if (globalOrder != null && currentOrder.isFolderLocal && removed.contains(0)) {
// Reset to global if folder local is unchecked
resetGlobal[0] = true;
selection.clear();
if (globalOrder.folderFirst) selection.add(5);
if (globalOrder.reverse) selection.add(6);
if (globalOrder.showDotFiles) selection.add(7);
selection.add(GsCollectionUtils.getOrDefault(typeToPos, globalOrder.sortByType, 1));
} else if (!Collections.disjoint(removed, radioSet)) {
// If a radio button is unchecked add it back
selection.addAll(removed);
} else if (!Collections.disjoint(added, radioSet)) {
// If a radio button is checked, remove all other radio buttons
selection.removeAll(GsCollectionUtils.setDiff(radioSet, added));
}
prevSelection.clear();
prevSelection.addAll(selection);
};

dopt.positionCallback = (selection) -> {
final boolean reverse = selection.contains(6);
final boolean foldersFirst = selection.contains(5);
final boolean dotFiles = selection.contains(7);
final String sortBy;
if (selection.contains(2)) sortBy = GsFileUtils.SORT_BY_MTIME;
else if (selection.contains(3)) sortBy = GsFileUtils.SORT_BY_FILESIZE;
else if (selection.contains(4)) sortBy = GsFileUtils.SORT_BY_MIMETYPE;
else sortBy = GsFileUtils.SORT_BY_NAME;
final boolean save = selection.contains(0);
callback.callback(sortBy, reverse, foldersFirst, dotFiles, save);
final GsFileUtils.SortOrder order = new GsFileUtils.SortOrder();
order.isFolderLocal = selection.contains(0);
order.folderFirst = selection.contains(5);
order.reverse = selection.contains(6);
order.showDotFiles = selection.contains(7);
if (selection.contains(2)) order.sortByType = GsFileUtils.SORT_BY_MTIME;
else if (selection.contains(3)) order.sortByType = GsFileUtils.SORT_BY_FILESIZE;
else if (selection.contains(4)) order.sortByType = GsFileUtils.SORT_BY_MIMETYPE;
else order.sortByType = GsFileUtils.SORT_BY_NAME;
callback.callback(order);
};

GsSearchOrCustomTextDialog.showMultiChoiceDialogWithSearchFilterUI(activity, dopt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,21 @@ public static GsFileBrowserOptions.Options prepareFsViewerOpts(
opts.folderImage = R.drawable.ic_folder_white_24dp;
opts.titleText = R.string.select;
opts.mountedStorageFolder = cu.getStorageAccessFolder(context);
opts.sortOrder = appSettings.getFolderSortOrder(null);

updateFsViewerOpts(opts, context, appSettings);

return opts;
}

// We update these because some of these settings can change
public static void updateFsViewerOpts(
final GsFileBrowserOptions.Options opts,
final Context context,
AppSettings appSettings
) {
appSettings = appSettings != null ? appSettings : ApplicationObject.settings();

opts.sortFolderFirst = appSettings.isFileBrowserSortFolderFirst();
opts.sortByType = appSettings.getFileBrowserSortByType();
opts.sortReverse = appSettings.isFileBrowserSortReverse();
opts.filterShowDotFiles = appSettings.isFileBrowserFilterShowDotFiles();
opts.favouriteFiles = appSettings.getFavouriteFiles();
opts.recentFiles = appSettings.getRecentFiles();
opts.popularFiles = appSettings.getPopularFiles();
Expand Down
Loading

0 comments on commit 2f6cfcb

Please sign in to comment.