Skip to content

Instantly share code, notes, and snippets.

@obfusk
Last active August 27, 2023 16:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save obfusk/8f39111cb655031f17d1ecd9cb23b9db to your computer and use it in GitHub Desktop.
Save obfusk/8f39111cb655031f17d1ecd9cb23b9db to your computer and use it in GitHub Desktop.
java stuff
public class DisplayOption {
String item;
boolean checked;
Consumer<Boolean> callback;
public DisplayOption(String item, boolean checked, Consumer<Boolean> callback) {
this.item = item;
this.checked = checked;
this.callback = callback;
}
}
public void showDisplayOptionsDialog() {
AlertDialog.Builder builder = new MaterialAlertDialogBuilder(mContext);
builder.setTitle(R.string.action_display_options);
ArrayList<DisplayOption> options = new ArrayList<>();
options.add(new DisplayOption(mContext.getString(R.string.show_name_below_image_thumbnail), showingNameBelowThumbnail(), this::showNameBelowThumbnail));
options.add(new DisplayOption(mContext.getString(R.string.show_note), showingNote(), this::showNote));
options.add(new DisplayOption(mContext.getString(R.string.show_balance), showingBalance(), this::showBalance));
options.add(new DisplayOption(mContext.getString(R.string.show_validity), showingValidity(), this::showValidity));
if (true) {
options.add(new DisplayOption(mContext.getString(R.string.show_archived_cards), showingArchivedCards(), this::showArchivedCards));
}
String[] items = options.stream().map(o -> o.item).toArray(String[]::new);
Boolean[] checkedItems = options.stream().map(o -> o.checked).toArray(Boolean[]::new);
boolean[] checkedItemsPrimitive = new boolean[checkedItems.length];
for (int i = 0; i < checkedItems.length; i++) checkedItemsPrimitive[i] = checkedItems[i];
builder.setMultiChoiceItems(
items,
checkedItemsPrimitive,
(dialogInterface, i, b) -> options.get(i).callback.accept(b)
);
builder.setPositiveButton(R.string.ok, (dialog, i) -> dialog.dismiss());
AlertDialog dialog = builder.create();
dialog.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment