Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Per folder sort order #2499

Merged
merged 14 commits into from
Feb 5, 2025
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ protected void onCreate(final Bundle savedInstanceState) {
} catch (Exception ignored) {
}


_cu = new MarkorContextUtils(this);
setContentView(R.layout.main__activity);
_bottomNav = findViewById(R.id.bottom_navigation_bar);
Expand Down Expand Up @@ -427,7 +426,6 @@ public void onFsViewerDoUiUpdate(final GsFileBrowserListAdapter adapter) {
if (getCurrentPos() == tabIdToPos(R.id.nav_notebook)) {
setTitle(getFileBrowserTitle());
}
invalidateOptionsMenu();
gsantner marked this conversation as resolved.
Show resolved Hide resolved
}

if (toShow != null && adapter != null) {
Expand Down
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) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated bug parsing links

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 @@ -998,6 +1000,116 @@ public static void showInsertSnippetDialog(final Activity activity, final GsCall
GsSearchOrCustomTextDialog.showMultiChoiceDialogWithSearchFilterUI(activity, dopt);
}

public static void showFolderSortDialog(
final Activity activity,
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<>();
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 (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 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);
}

public static void baseConf(Activity activity, DialogOptions dopt) {
dopt.isDarkDialog = GsContextUtils.instance.isDarkModeEnabled(activity);
dopt.clearInputIcon = R.drawable.ic_baseline_clear_24;
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);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Null = global


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
Loading