Skip to content

Commit

Permalink
Switching to dialog based sort
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Jan 4, 2025
1 parent 6f79ce0 commit 20b7c5e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 155 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -165,22 +163,33 @@ 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(""));
_selectedItems = new HashSet<>(_dopt.preSelected != null ? _dopt.preSelected : Collections.emptyList());
_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) {
final int index = _filteredItems.get(pos);

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 {
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -290,8 +295,6 @@ private void updateMenuItems() {
_toolbar.setSubtitle("");
}
}

updateSortMenuStates();
}

@Override
Expand Down Expand Up @@ -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);
Expand All @@ -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();
Expand All @@ -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: {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
});
}
}
59 changes: 1 addition & 58 deletions app/src/main/res/menu/filesystem__menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,64 +85,7 @@
android:layout_width="match_parent"
android:icon="@drawable/ic_sort_black_24dp"
android:title="@string/sort_by"
app:showAsAction="always">

<menu>
<group
android:id="@+id/action_grp_sort1"
android:checkableBehavior="single">

<item
android:id="@+id/action_sort_by_name"
android:icon="@drawable/ic_sort_by_alpha_black_24dp"
android:title="@string/deleteme____1"
app:showAsAction="never" />
<item
android:id="@+id/action_sort_by_date"
android:icon="@drawable/ic_date_range_black_24dp"
android:title="@string/date"
app:showAsAction="never" />
<item
android:id="@+id/action_sort_by_filesize"
android:icon="@drawable/ic_sort_black_24dp"
android:title="@string/size"
app:showAsAction="never" />
<item
android:id="@+id/action_sort_by_mimetype"
android:icon="@drawable/ic_extension_black_24dp"
android:title="@string/mime_type"
app:showAsAction="never" />
</group>
<group
android:id="@+id/action_grp_sort2"
android:checkableBehavior="all">
<item
android:id="@+id/action_folder_first"
android:checkable="true"
android:icon="@drawable/ic_folder_white_24dp"
android:title="@string/folder_first"
app:showAsAction="never" />
<item
android:id="@+id/action_sort_reverse"
android:checkable="true"
android:icon="@drawable/ic_rotate_left_black_24dp"
android:title="@string/reverse_order"
app:showAsAction="never" />
<item
android:id="@+id/action_show_dotfiles"
android:checkable="true"
android:icon="@drawable/ic_visibility_black_24dp"
android:title="@string/dotfiles"
app:showAsAction="never" />
</group>
<item
android:id="@+id/action_save_sort_settings"
android:checkable="true"
android:icon="@drawable/ic_save_black_24dp"
android:title="@string/save"
app:showAsAction="never" />
</menu>
</item>
app:showAsAction="always" />

<item
android:id="@+id/action_search"
Expand Down

0 comments on commit 20b7c5e

Please sign in to comment.