private void AddChoiceItems(int type, ModifiableChoiceArrayAdapter <ModifiableChoice> list, GeckoSession.romptDelegateClassChoice[] items, string indent) { if (type == GeckoSession.romptDelegateClassChoice.ChoiceTypeMenu) { foreach (GeckoSession.romptDelegateClassChoice item in items) { list.Add(new ModifiableChoice(item)); } return; } foreach (GeckoSession.romptDelegateClassChoice item in items) { ModifiableChoice modItem = new ModifiableChoice(item); GeckoSession.romptDelegateClassChoice[] children = item.Items?.ToArray(); if (indent != null && children == null) { modItem.ModifiableLabel = indent + modItem.ModifiableLabel; } list.Add(modItem); if (children != null) { string newIndent; if (type == GeckoSession.romptDelegateClassChoice.ChoiceTypeSingle || type == GeckoSession.romptDelegateClassChoice.ChoiceTypeMultiple) { newIndent = (indent != null) ? indent + '\t' : "\t"; } else { newIndent = null; } AddChoiceItems(type, list, children, newIndent); } } }
public virtual void OnChoicePrompt(GeckoSession session, string title, string msg, int type, GeckoSession.romptDelegateClassChoice[] choices, GeckoSession.PromptDelegateClassChoiceCallback prompt) { //This code is highly inspired of https://github.com/mozilla-mobile/focus-android/blob/f5b22ff78fca22765b8b873b4f70693943ae559a/app/src/main/java/org/mozilla/focus/gecko/GeckoViewPrompt.java#L265 var currentActivity = BlazorWebViewService.GetCurrentActivity(); if (currentActivity == null) { prompt.Dismiss(); return; } var builder = new Android.App.AlertDialog.Builder(currentActivity); AddStandardLayout(builder, title, msg); Android.Widget.ListView list = new Android.Widget.ListView(builder.Context); if (type == GeckoSession.romptDelegateClassChoice.ChoiceTypeMultiple) { list.ChoiceMode = ChoiceMode.Multiple; } ModifiableChoiceArrayAdapter <ModifiableChoice> adapter = new ModifiableChoiceArrayAdapter <ModifiableChoice>(builder.Context, Android.Resource.Layout.SimpleListItem1, type, builder, list); AddChoiceItems(type, adapter, choices, /* indent */ null); list.SetAdapter(adapter); builder.SetView(list); AlertDialog dialog; if (type == GeckoSession.romptDelegateClassChoice.ChoiceTypeSingle || type == GeckoSession.romptDelegateClassChoice.ChoiceTypeMenu) { dialog = CreateStandardDialog(builder, prompt); list.ItemClick += (sender, e) => { ModifiableChoice item = adapter.GetItem(e.Position); if (type == GeckoSession.romptDelegateClassChoice.ChoiceTypeMenu) { GeckoSession.romptDelegateClassChoice[] children = item.Choice.Items?.ToArray(); if (children != null) { // Show sub-menu. dialog.SetOnDismissListener(null); dialog.Dismiss(); OnChoicePrompt(session, item.ModifiableLabel, /* msg */ null, type, children, prompt); return; } } prompt.Confirm(item.Choice); dialog.Dismiss(); }; } else if (type == GeckoSession.romptDelegateClassChoice.ChoiceTypeMultiple) { list.ItemClick += (sender, e) => { ModifiableChoice item = adapter.GetItem(e.Position); item.ModifiableSelected = ((CheckedTextView)e.View).Checked; }; builder .SetNegativeButton(Android.Resource.String.Cancel, /* listener */ (IDialogInterfaceOnClickListener)null) .SetPositiveButton(Android.Resource.String.Ok, (sender, e) => { int len = adapter.Count; List <string> items = new List <string>(len); for (int i = 0; i < len; i++) { ModifiableChoice item = adapter.GetItem(i); if (item.ModifiableSelected) { items.Add(item.Choice.Id); } } prompt.Confirm(items.ToArray()); }); dialog = CreateStandardDialog(builder, prompt); } else { throw new UnsupportedOperationException(); } dialog.Show(); //TODO: Don't forget to Dispose dialogbuilder }