Skip to content

Commit

Permalink
Cleanups to dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Jan 4, 2025
1 parent 20b7c5e commit 5a01163
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1031,11 +1031,12 @@ public static void showFolderSortDialog(
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.radioSet = new HashSet<>(Arrays.asList(1, 2, 3, 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@

import androidx.annotation.ColorInt;
import androidx.annotation.DrawableRes;
import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
Expand Down Expand Up @@ -84,11 +83,15 @@ public static class DialogOptions {
@Nullable
public GsCallback.a1<List<Integer>> positionCallback = null;

// For multi select
public boolean isMultiSelectEnabled = false;
public Collection<Integer> preSelected = null; // Indices of pre-selected items
public Collection<Integer> radioIndices = null; // Only one item can be selected among these
public boolean showCountInOkButton = true;

public List<? extends CharSequence> data = null;
public List<? extends CharSequence> highlightData = null;
public List<Integer> iconsForData;
public Collection<Integer> radioSet = null;
public CharSequence messageText = "";
public String defaultText = "";
public boolean isDarkDialog = false;
Expand All @@ -100,7 +103,6 @@ public static class DialogOptions {
public int dialogHeightDp = WindowManager.LayoutParams.WRAP_CONTENT;
public int searchInputType = InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS;
public String extraFilter = null;
public Collection<Integer> preSelected = null;
public GsCallback.a1<Spannable> highlighter = null;
public GsCallback.a1<AlertDialog> neutralButtonCallback = null;
public GsCallback.a1<DialogInterface> dismissCallback = null;
Expand Down Expand Up @@ -172,7 +174,7 @@ private Adapter(final Context context, final DialogOptions dopt) {

private int chooseLayout(final int pos) {
if (_dopt.isMultiSelectEnabled) {
if (_dopt.radioSet != null && _dopt.radioSet.contains(pos)) {
if (_dopt.radioIndices != null && _dopt.radioIndices.contains(pos)) {
return android.R.layout.simple_list_item_single_choice;
} else {
return android.R.layout.simple_list_item_multiple_choice;
Expand Down Expand Up @@ -289,7 +291,6 @@ public static void showMultiChoiceDialogWithSearchFilterUI(final Activity activi
// SearchView is currently constructed even if it isn't needed
final View searchView = makeSearchView(activity, dopt);
final EditText searchEditText = searchView.findViewWithTag("EDIT");
searchEditText.addTextChangedListener(GsTextWatcherAdapter.after(listAdapter::filter));

if (dopt.isSearchEnabled) {
mainLayout.addView(searchView);
Expand Down Expand Up @@ -335,6 +336,18 @@ public static void showMultiChoiceDialogWithSearchFilterUI(final Activity activi

// =========================================================================================

final GsCallback.a0 setSelectAllButtonState = () -> {
if (selectAll != null) {
final boolean allVisibleSelected = listAdapter._selectedItems.containsAll(listAdapter._filteredItems);
((Checkable) selectAll).setChecked(allVisibleSelected);
}
};

searchEditText.addTextChangedListener(GsTextWatcherAdapter.after((constraint) -> {
listAdapter.filter(constraint);
setSelectAllButtonState.callback();
}));

// Ok button only present under these circumstances
final boolean isSearchOk = dopt.callback != null && dopt.isSearchEnabled;
final boolean isMultiSelOk = dopt.positionCallback != null && dopt.isMultiSelectEnabled;
Expand Down Expand Up @@ -418,7 +431,7 @@ public static void showMultiChoiceDialogWithSearchFilterUI(final Activity activi
final Button okButton = dialog.getButton(Dialog.BUTTON_POSITIVE);
final GsCallback.a0 setOkButtonState = () -> {
if (okButton != null) {
if (dopt.isMultiSelectEnabled) {
if (dopt.isMultiSelectEnabled && dopt.showCountInOkButton) {
okButton.setText(okString + String.format(" (%d)", listAdapter._selectedItems.size()));
} else {
okButton.setText(okString);
Expand All @@ -428,21 +441,17 @@ public static void showMultiChoiceDialogWithSearchFilterUI(final Activity activi

// Set ok button text initially
setOkButtonState.callback();
final GsCallback.a0 setSelectAllButtonState = () -> {
if (selectAll != null) {
((Checkable) selectAll).setChecked(listAdapter._selectedItems.size() >= dopt.data.size());
}
};

// Set select all button state initially
setSelectAllButtonState.callback();

if (selectAll != null && dopt.isMultiSelectEnabled) {
selectAll.setOnClickListener((v) -> {
if (listAdapter._selectedItems.size() < dopt.data.size()) {
listAdapter._selectedItems.addAll(GsCollectionUtils.range(dopt.data.size()));
final boolean allVisibleSelected = listAdapter._selectedItems.containsAll(listAdapter._filteredItems);
if (!allVisibleSelected) {
listAdapter._selectedItems.addAll(listAdapter._filteredItems);
} else {
listAdapter._selectedItems.clear();
listAdapter._selectedItems.removeAll(listAdapter._filteredItems);
}
listAdapter.notifyDataSetChanged();
setOkButtonState.callback();
Expand All @@ -460,8 +469,8 @@ public static void showMultiChoiceDialogWithSearchFilterUI(final Activity activi
listAdapter._selectedItems.remove(index);
} else {
listAdapter._selectedItems.add(index);
if (dopt.radioSet != null && dopt.radioSet.contains(index)) {
for (int i : dopt.radioSet) {
if (dopt.radioIndices != null && dopt.radioIndices.contains(index)) {
for (int i : dopt.radioIndices) {
if (i != index) {
listAdapter._selectedItems.remove(i);
}
Expand Down Expand Up @@ -527,7 +536,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.radioIndices == 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

0 comments on commit 5a01163

Please sign in to comment.