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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
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 @@ -998,6 +998,62 @@ public static void showInsertSnippetDialog(final Activity activity, final GsCall
GsSearchOrCustomTextDialog.showMultiChoiceDialogWithSearchFilterUI(activity, dopt);
}

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 DialogOptions dopt = new DialogOptions();
baseConf(activity, dopt);

final List<String> data = new ArrayList<>();
data.add(activity.getString(R.string.save));
data.add(activity.getString(R.string.name));
data.add(activity.getString(R.string.date));
data.add(activity.getString(R.string.size));
data.add(activity.getString(R.string.mime_type));
data.add(activity.getString(R.string.folder_first));
data.add(activity.getString(R.string.reverse_order));
data.add(activity.getString(R.string.dotfiles));
dopt.data = data;

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));
dopt.isMultiSelectEnabled = true;
dopt.isSearchEnabled = false;
dopt.titleText = R.string.sort_by;
dopt.dialogWidthDp = WindowManager.LayoutParams.WRAP_CONTENT;
dopt.showCountInOkButton = false;

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);
};

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
102 changes: 83 additions & 19 deletions app/src/main/java/net/gsantner/markor/model/AppSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import android.graphics.Color;
import android.os.Build;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Pair;

import androidx.annotation.ColorRes;
Expand Down Expand Up @@ -44,6 +45,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
Expand Down Expand Up @@ -85,7 +87,7 @@ public boolean isPreferViewMode() {
}

public void setNotebookDirectory(final File file) {
setString(R.string.pref_key__notebook_directory, Document.getPath(file));
setString(R.string.pref_key__notebook_directory, GsFileUtils.getPath(file));
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Moved this routine to GsFileUtils

}

public File getNotebookDirectory() {
Expand All @@ -104,7 +106,7 @@ public File getQuickNoteFile() {
}

public void setQuickNoteFile(final File file) {
setString(R.string.pref_key__quicknote_filepath, Document.getPath(file));
setString(R.string.pref_key__quicknote_filepath, GsFileUtils.getPath(file));
}

public File getDefaultQuickNoteFile() {
Expand All @@ -116,7 +118,7 @@ public File getTodoFile() {
}

public void setTodoFile(final File file) {
setString(R.string.pref_key__todo_filepath, Document.getPath(file));
setString(R.string.pref_key__todo_filepath, GsFileUtils.getPath(file));
}

public File getDefaultTodoFile() {
Expand All @@ -130,7 +132,7 @@ public File getSnippetsDirectory() {
}

public void setSnippetDirectory(final File folder) {
setString(R.string.pref_key__snippet_directory_path, Document.getPath(folder));
setString(R.string.pref_key__snippet_directory_path, GsFileUtils.getPath(folder));
}

public String getFontFamily() {
Expand Down Expand Up @@ -259,6 +261,68 @@ public boolean isRecreateMainRequired() {
return ret;
}

private final String PREF_PREFIX_FOLDER_SORT_ORDER = "PREF_PREFIX_FOLDER_SORT_ORDER";

public static class FolderSortOrder {
private final static String SORT_BY_KEY = "SORT_BY";
private final static String REVERSE_KEY = "REVERSE";
private final static String SHOW_DOT_FILES_KEY = "SHOW_DOT_FILES";
private final static String FOLDER_FIRST_KEY = "FOLDER_FIRST";

public String sortByType = GsFileUtils.SORT_BY_NAME;
public boolean reverse = false;
public boolean showDotFiles = false;
public boolean folderFirst = true;
public boolean hasCustomOrder = false;
}

public void setFolderSortOrder(final File folder, final @Nullable FolderSortOrder sortOrder) {
final String canonicalPath = GsFileUtils.getPath(folder);

if (TextUtils.isEmpty(canonicalPath)) {
return;
}

final String key = PREF_PREFIX_FOLDER_SORT_ORDER + canonicalPath;

if (sortOrder == null) {
remove(key);
return;
}

final Map<String, String> order = new HashMap<>();
order.put(FolderSortOrder.SORT_BY_KEY, sortOrder.sortByType != null ? sortOrder.sortByType : GsFileUtils.SORT_BY_NAME);
order.put(FolderSortOrder.REVERSE_KEY, String.valueOf(sortOrder.reverse));
order.put(FolderSortOrder.SHOW_DOT_FILES_KEY, String.valueOf(sortOrder.showDotFiles));
order.put(FolderSortOrder.FOLDER_FIRST_KEY, String.valueOf(sortOrder.folderFirst));

setString(key, mapToJsonString(order));
}

public FolderSortOrder getFolderSortOrder(final File folder) {
final String path = GsFileUtils.getPath(folder);
final String key = PREF_PREFIX_FOLDER_SORT_ORDER + path;
final String json = getString(key, null);

final FolderSortOrder sortOrder = new FolderSortOrder();
if (GsTextUtils.isNullOrEmpty(path) || json == null) {
sortOrder.sortByType = getFileBrowserSortByType();
sortOrder.reverse = isFileBrowserSortReverse();
sortOrder.folderFirst = isFileBrowserSortFolderFirst();
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Fall back to global if this folder does not have a custom setting

sortOrder.showDotFiles = isFileBrowserFilterShowDotFiles();
sortOrder.hasCustomOrder = false;
} else {
final Map<String, String> order = jsonStringToMap(getString(key, "{}"));
sortOrder.sortByType = order.get(FolderSortOrder.SORT_BY_KEY);
sortOrder.reverse = Boolean.parseBoolean(order.get(FolderSortOrder.REVERSE_KEY));
sortOrder.showDotFiles = Boolean.parseBoolean(order.get(FolderSortOrder.SHOW_DOT_FILES_KEY));
sortOrder.folderFirst = Boolean.parseBoolean(order.get(FolderSortOrder.FOLDER_FIRST_KEY));
sortOrder.hasCustomOrder = true;
}

return sortOrder;
}

public String setFileBrowserSortByType(String v) {
setString(R.string.pref_key__file_browser__sort_by_type, v);
return v;
Expand Down Expand Up @@ -338,12 +402,12 @@ public void addRecentFile(final File file) {
if (!listFileInRecents(file)) {
return;
}
final String path = Document.getPath(file);
final String path = GsFileUtils.getPath(file);
if (!file.equals(getTodoFile()) && !file.equals(getQuickNoteFile())) {
ArrayList<String> recent = getRecentDocuments();
recent.add(0, path);
recent.remove(Document.getPath(getTodoFile()));
recent.remove(Document.getPath(getQuickNoteFile()));
recent.remove(GsFileUtils.getPath(getTodoFile()));
recent.remove(GsFileUtils.getPath(getQuickNoteFile()));
recent.remove("");
recent.remove(null);

Expand All @@ -357,7 +421,7 @@ public void setFavouriteFiles(final Collection<File> files) {
final Set<String> set = new LinkedHashSet<>();
for (final File f : files) {
if (f != null && (f.exists() || GsFileBrowserListAdapter.isVirtualFolder(f))) {
set.add(Document.getPath(f));
set.add(GsFileUtils.getPath(f));
}
}
setStringList(R.string.pref_key__favourite_files, GsCollectionUtils.map(set, p -> p));
Expand Down Expand Up @@ -421,8 +485,8 @@ public void setLastViewPosition(File file, int scrollX, int scrollY) {
return;
}
if (!file.equals(getTodoFile()) && !file.equals(getQuickNoteFile())) {
setInt(PREF_PREFIX_VIEW_SCROLL_X + Document.getPath(file), scrollX, _prefCache);
setInt(PREF_PREFIX_VIEW_SCROLL_Y + Document.getPath(file), scrollY, _prefCache);
setInt(PREF_PREFIX_VIEW_SCROLL_X + GsFileUtils.getPath(file), scrollX, _prefCache);
setInt(PREF_PREFIX_VIEW_SCROLL_Y + GsFileUtils.getPath(file), scrollY, _prefCache);
}
}

Expand Down Expand Up @@ -549,14 +613,14 @@ public int getLastViewPositionX(File file) {
if (file == null || !file.exists()) {
return -1;
}
return getInt(PREF_PREFIX_VIEW_SCROLL_X + Document.getPath(file), -3, _prefCache);
return getInt(PREF_PREFIX_VIEW_SCROLL_X + GsFileUtils.getPath(file), -3, _prefCache);
}

public int getLastViewPositionY(File file) {
if (file == null || !file.exists()) {
return -1;
}
return getInt(PREF_PREFIX_VIEW_SCROLL_Y + Document.getPath(file), -3, _prefCache);
return getInt(PREF_PREFIX_VIEW_SCROLL_Y + GsFileUtils.getPath(file), -3, _prefCache);
}

private List<String> getPopularDocumentsSorted() {
Expand Down Expand Up @@ -800,16 +864,16 @@ public int getTabWidth() {
}

public boolean listFileInRecents(File file) {
return getBool(Document.getPath(file) + "_list_in_recents", true);
return getBool(GsFileUtils.getPath(file) + "_list_in_recents", true);
}

public void setListFileInRecents(File file, boolean value) {
setBool(Document.getPath(file) + "_list_in_recents", value);
setBool(GsFileUtils.getPath(file) + "_list_in_recents", value);

if (!value) {
ArrayList<String> recent = getRecentDocuments();
if (recent.contains(Document.getPath(file))) {
recent.remove(Document.getPath(file));
if (recent.contains(GsFileUtils.getPath(file))) {
recent.remove(GsFileUtils.getPath(file));
setRecentDocuments(recent);
}
}
Expand All @@ -824,11 +888,11 @@ public ArrayList<String> getFilesTaggedWith(String tag) {
}*/

public int getRating(File file) {
return getInt(Document.getPath(file) + "_rating", 0);
return getInt(GsFileUtils.getPath(file) + "_rating", 0);
}

public void setRating(File file, int value) {
setInt(Document.getPath(file) + "_rating", value);
setInt(GsFileUtils.getPath(file) + "_rating", value);
}

public boolean isEditorLineBreakingEnabled() {
Expand Down Expand Up @@ -926,7 +990,7 @@ public void setNewFileDialogLastUsedType(final int format) {
}

public void setFileBrowserLastBrowsedFolder(File f) {
setString(R.string.pref_key__file_browser_last_browsed_folder, Document.getPath(f));
setString(R.string.pref_key__file_browser_last_browsed_folder, GsFileUtils.getPath(f));
}

public File getFileBrowserLastBrowsedFolder() {
Expand Down
12 changes: 1 addition & 11 deletions app/src/main/java/net/gsantner/markor/model/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class Document implements Serializable {
private int _lastLength = -1;

public Document(@NonNull final File f) {
path = getPath(f);
path = GsFileUtils.getPath(f);
file = new File(path);
title = GsFileUtils.getFilenameWithoutExtension(file);
extension = GsFileUtils.getFilenameExtension(file);
Expand All @@ -86,16 +86,6 @@ public Document(@NonNull final File f) {
}
}

public static String getPath(final File file) {
try {
return file.getCanonicalPath();
} catch (IOException e) {
return file.getAbsolutePath();
} catch (NullPointerException e) {
return "";
}
}

// Get a default file
public static Document getDefault(final Context context) {
final File notebook = ApplicationObject.settings().getNotebookDirectory();
Expand Down
Loading
Loading