From 20b7c5e1d58e457472a398db4db04d50036bb166 Mon Sep 17 00:00:00 2001 From: Harshad Vedartham Date: Sat, 4 Jan 2025 09:08:31 -0800 Subject: [PATCH] Switching to dialog based sort --- .../markor/frontend/MarkorDialogFactory.java | 1 + .../frontend/GsSearchOrCustomTextDialog.java | 19 ++- .../filebrowser/GsFileBrowserFragment.java | 114 ++++-------------- app/src/main/res/menu/filesystem__menu.xml | 59 +-------- 4 files changed, 38 insertions(+), 155 deletions(-) diff --git a/app/src/main/java/net/gsantner/markor/frontend/MarkorDialogFactory.java b/app/src/main/java/net/gsantner/markor/frontend/MarkorDialogFactory.java index 60b27620b..0babd42fb 100644 --- a/app/src/main/java/net/gsantner/markor/frontend/MarkorDialogFactory.java +++ b/app/src/main/java/net/gsantner/markor/frontend/MarkorDialogFactory.java @@ -1035,6 +1035,7 @@ public static void showFolderSortDialog( dopt.isMultiSelectEnabled = true; dopt.isSearchEnabled = false; dopt.titleText = R.string.sort_by; + dopt.dialogWidthDp = WindowManager.LayoutParams.WRAP_CONTENT; dopt.positionCallback = (selection) -> { final boolean reverse = selection.contains(6); diff --git a/app/src/main/java/net/gsantner/opoc/frontend/GsSearchOrCustomTextDialog.java b/app/src/main/java/net/gsantner/opoc/frontend/GsSearchOrCustomTextDialog.java index d5cbf3b1c..6bb0573cd 100644 --- a/app/src/main/java/net/gsantner/opoc/frontend/GsSearchOrCustomTextDialog.java +++ b/app/src/main/java/net/gsantner/opoc/frontend/GsSearchOrCustomTextDialog.java @@ -136,8 +136,6 @@ public static class DialogState { } public static class Adapter extends BaseAdapter { - @LayoutRes - private final int _layout; private final int _layoutHeight; private final LayoutInflater _inflater; private final DialogOptions _dopt; @@ -165,7 +163,6 @@ public long getItemId(int position) { private Adapter(final Context context, final DialogOptions dopt) { super(); _filteredItems = new ArrayList<>(); - _layout = dopt.isMultiSelectEnabled ? android.R.layout.simple_list_item_multiple_choice : android.R.layout.simple_list_item_1; _inflater = LayoutInflater.from(context); _dopt = dopt; _extraPattern = (_dopt.extraFilter == null ? null : Pattern.compile(_dopt.extraFilter).matcher("")); @@ -173,6 +170,18 @@ private Adapter(final Context context, final DialogOptions dopt) { _layoutHeight = GsContextUtils.instance.convertDpToPx(context, 36); } + private int chooseLayout(final int pos) { + if (_dopt.isMultiSelectEnabled) { + if (_dopt.radioSet != null && _dopt.radioSet.contains(pos)) { + return android.R.layout.simple_list_item_single_choice; + } else { + return android.R.layout.simple_list_item_multiple_choice; + } + } else { + return android.R.layout.simple_list_item_1; + } + } + @NonNull @Override public View getView(int pos, @Nullable View convertView, @NonNull ViewGroup parent) { @@ -180,7 +189,7 @@ public View getView(int pos, @Nullable View convertView, @NonNull ViewGroup pare final TextView textView; if (convertView == null) { - textView = (TextView) _inflater.inflate(_layout, parent, false); + textView = (TextView) _inflater.inflate(chooseLayout(pos), parent, false); textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); textView.setMinHeight(_layoutHeight); } else { @@ -518,7 +527,7 @@ private static View makeTitleView(final Context context, final DialogOptions dop LinearLayout.LayoutParams.WRAP_CONTENT)); } - if (dopt.isMultiSelectEnabled && dopt.radioSet != null) { + if (dopt.isMultiSelectEnabled && dopt.radioSet == null) { // Using a multiple choice text view as a selectable checkbox button // Requires no styling to match the existing check boxes final LayoutInflater inflater = LayoutInflater.from(context); diff --git a/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserFragment.java b/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserFragment.java index aba04150d..5e980084c 100644 --- a/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserFragment.java +++ b/app/src/main/java/net/gsantner/opoc/frontend/filebrowser/GsFileBrowserFragment.java @@ -280,6 +280,11 @@ private void updateMenuItems() { _fragmentMenu.findItem(R.id.action_create_shortcut).setVisible(selMulti1 && (selFilesOnly || selDirectoriesOnly)); _fragmentMenu.findItem(R.id.action_check_all).setVisible(_filesystemViewerAdapter.areItemsSelected() && selCount < totalCount); _fragmentMenu.findItem(R.id.action_clear_selection).setVisible(_filesystemViewerAdapter.areItemsSelected()); + + final MenuItem sortItem = _fragmentMenu.findItem(R.id.action_sort); + if (sortItem != null) { + _cu.tintDrawable(sortItem.getIcon(), _isSortOrderSaved ? GsFileBrowserListAdapter.FAVOURITE_COLOR : Color.WHITE); + } } // Update subtitle with count @@ -290,8 +295,6 @@ private void updateMenuItems() { _toolbar.setSubtitle(""); } } - - updateSortMenuStates(); } @Override @@ -345,39 +348,6 @@ public void onResume() { _reloadRequiredOnResume = true; } - private void updateSortMenuStates() { - if (_fragmentMenu == null) { - return; - } - - MenuItem item; - if ((item = _fragmentMenu.findItem(R.id.action_folder_first)) != null) { - item.setChecked(_dopt.sortFolderFirst); - } - if ((item = _fragmentMenu.findItem(R.id.action_sort_reverse)) != null) { - item.setChecked(_dopt.sortReverse); - } - if ((item = _fragmentMenu.findItem(R.id.action_show_dotfiles)) != null) { - item.setChecked(_dopt.filterShowDotFiles); - } - if ((item = _fragmentMenu.findItem(R.id.action_save_sort_settings)) != null) { - item.setChecked(_isSortOrderSaved); - } - if ((item = _fragmentMenu.findItem(R.id.action_sort)) != null) { - _cu.tintDrawable(item.getIcon(), _isSortOrderSaved ? GsFileBrowserListAdapter.FAVOURITE_COLOR : Color.WHITE); - } - - if ((item = _fragmentMenu.findItem(R.id.action_sort_by_name)) != null && GsFileUtils.SORT_BY_NAME.equals(_dopt.sortByType)) { - item.setChecked(true); - } else if ((item = _fragmentMenu.findItem(R.id.action_sort_by_date)) != null && GsFileUtils.SORT_BY_MTIME.equals(_dopt.sortByType)) { - item.setChecked(true); - } else if ((item = _fragmentMenu.findItem(R.id.action_sort_by_filesize)) != null && GsFileUtils.SORT_BY_FILESIZE.equals(_dopt.sortByType)) { - item.setChecked(true); - } else if ((item = _fragmentMenu.findItem(R.id.action_sort_by_mimetype)) != null && GsFileUtils.SORT_BY_MIMETYPE.equals(_dopt.sortByType)) { - item.setChecked(true); - } - } - @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); @@ -401,24 +371,6 @@ public GsFileBrowserListAdapter getAdapter() { return _filesystemViewerAdapter; } - private void updateSortSettingsAndReload() { - if (_isSortOrderSaved) { - final AppSettings.FolderSortOrder order = new AppSettings.FolderSortOrder(); - order.sortByType = _dopt.sortByType; - order.reverse = _dopt.sortReverse; - order.folderFirst = _dopt.sortFolderFirst; - order.showDotFiles = _dopt.filterShowDotFiles; - _appSettings.setFolderSortOrder(getCurrentFolder(), order); - } else { - _appSettings.setFolderSortOrder(getCurrentFolder(), null); - _appSettings.setFileBrowserSortByType(_dopt.sortByType); - _appSettings.setFileBrowserSortReverse(_dopt.sortReverse); - _appSettings.setFileBrowserSortFolderFirst(_dopt.sortFolderFirst); - _appSettings.setFileBrowserFilterShowDotFiles(_dopt.filterShowDotFiles); - } - reloadCurrentFolder(); // onFsViewerFolderChange will update the UI - } - @Override public boolean onOptionsItemSelected(final MenuItem item) { final int _id = item.getItemId(); @@ -430,39 +382,8 @@ public boolean onOptionsItemSelected(final MenuItem item) { _cu.createLauncherDesktopShortcut(getContext(), file); return true; } - case R.id.action_sort_by_name: { - _dopt.sortByType = GsFileUtils.SORT_BY_NAME; - updateSortSettingsAndReload(); - return true; - } - case R.id.action_sort_by_date: { - _dopt.sortByType = GsFileUtils.SORT_BY_MTIME; - updateSortSettingsAndReload(); - return true; - } - case R.id.action_sort_by_filesize: { - _dopt.sortByType = GsFileUtils.SORT_BY_FILESIZE; - updateSortSettingsAndReload(); - return true; - } - case R.id.action_sort_by_mimetype: { - _dopt.sortByType = GsFileUtils.SORT_BY_MIMETYPE; - updateSortSettingsAndReload(); - return true; - } - case R.id.action_sort_reverse: { - _dopt.sortReverse = !_dopt.sortReverse; - updateSortSettingsAndReload(); - return true; - } - case R.id.action_show_dotfiles: { - _dopt.filterShowDotFiles = !_dopt.filterShowDotFiles; - updateSortSettingsAndReload(); - return true; - } - case R.id.action_folder_first: { - _dopt.sortFolderFirst = !_dopt.sortFolderFirst; - updateSortSettingsAndReload(); + case R.id.action_sort: { + updateSortSettings(); return true; } case R.id.action_import: { @@ -550,11 +471,6 @@ public boolean onOptionsItemSelected(final MenuItem item) { } return true; } - case R.id.action_save_sort_settings: { - _isSortOrderSaved = !_isSortOrderSaved; - updateSortSettingsAndReload(); - return true; - } } return false; @@ -695,7 +611,21 @@ private void updateSortSettings() { _dopt.sortFolderFirst = sortFolderFirst; _dopt.filterShowDotFiles = filterShowDotFiles; _isSortOrderSaved = saved; - updateSortSettingsAndReload(); + if (_isSortOrderSaved) { + final AppSettings.FolderSortOrder order = new AppSettings.FolderSortOrder(); + order.sortByType = _dopt.sortByType; + order.reverse = _dopt.sortReverse; + order.folderFirst = _dopt.sortFolderFirst; + order.showDotFiles = _dopt.filterShowDotFiles; + _appSettings.setFolderSortOrder(getCurrentFolder(), order); + } else { + _appSettings.setFolderSortOrder(getCurrentFolder(), null); + _appSettings.setFileBrowserSortByType(_dopt.sortByType); + _appSettings.setFileBrowserSortReverse(_dopt.sortReverse); + _appSettings.setFileBrowserSortFolderFirst(_dopt.sortFolderFirst); + _appSettings.setFileBrowserFilterShowDotFiles(_dopt.filterShowDotFiles); + } + reloadCurrentFolder(); // Ui will be updated by onFsViewerDoUiUpdate after the load }); } } diff --git a/app/src/main/res/menu/filesystem__menu.xml b/app/src/main/res/menu/filesystem__menu.xml index 833243fb1..b9639cdde 100644 --- a/app/src/main/res/menu/filesystem__menu.xml +++ b/app/src/main/res/menu/filesystem__menu.xml @@ -85,64 +85,7 @@ android:layout_width="match_parent" android:icon="@drawable/ic_sort_black_24dp" android:title="@string/sort_by" - app:showAsAction="always"> - - - - - - - - - - - - - - - - - + app:showAsAction="always" />