Skip to content
This repository was archived by the owner on Feb 16, 2021. It is now read-only.

Commit 267c5e0

Browse files
committed
Miscellaneous
1 parent 7af099c commit 267c5e0

File tree

2 files changed

+203
-38
lines changed

2 files changed

+203
-38
lines changed

MaterialDialogs.Core/MaterialDialog.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,59 @@
33
using Android.Views;
44
using Java.Lang;
55
using System.Linq;
6+
using Android.Content;
67

78
namespace AFollestad.MaterialDialogs
89
{
910
public partial class MaterialDialog
1011
{
1112
public partial class Builder
1213
{
14+
class DialogInterfaceOnCancelListenerActionWrapper : Java.Lang.Object, IDialogInterfaceOnCancelListener
15+
{
16+
Action<IDialogInterface> _action;
17+
18+
public DialogInterfaceOnCancelListenerActionWrapper(Action<IDialogInterface> action)
19+
{
20+
_action = action;
21+
}
22+
23+
public void OnCancel(IDialogInterface dialog)
24+
{
25+
_action?.Invoke(dialog);
26+
}
27+
}
28+
29+
class DialogInterfaceOnDismissListenerActionWrapper : Java.Lang.Object, IDialogInterfaceOnDismissListener
30+
{
31+
Action<IDialogInterface> _action;
32+
33+
public DialogInterfaceOnDismissListenerActionWrapper(Action<IDialogInterface> action)
34+
{
35+
_action = action;
36+
}
37+
38+
public void OnDismiss(IDialogInterface dialog)
39+
{
40+
_action?.Invoke(dialog);
41+
}
42+
}
43+
44+
class DialogInterfaceOnShowListenerActionWrapper : Java.Lang.Object, IDialogInterfaceOnShowListener
45+
{
46+
Action<IDialogInterface> _action;
47+
48+
public DialogInterfaceOnShowListenerActionWrapper(Action<IDialogInterface> action)
49+
{
50+
_action = action;
51+
}
52+
53+
public void OnShow(IDialogInterface dialog)
54+
{
55+
_action?.Invoke(dialog);
56+
}
57+
}
58+
1359
class ListCallbackActionWrapper : Java.Lang.Object, IListCallback
1460
{
1561
Action<MaterialDialog, View, int, string> _action;
@@ -127,6 +173,21 @@ public Builder ItemsDisabledIndices(params int[] indices)
127173
{
128174
return ItemsDisabledIndices(indices.Select(i => new Integer(i)).ToArray());
129175
}
176+
177+
public Builder ShowListener(Action<IDialogInterface> action)
178+
{
179+
return ShowListener(new DialogInterfaceOnShowListenerActionWrapper(action));
180+
}
181+
182+
public Builder CancelListener(Action<IDialogInterface> action)
183+
{
184+
return CancelListener(new DialogInterfaceOnCancelListenerActionWrapper(action));
185+
}
186+
187+
public Builder DismissListener(Action<IDialogInterface> action)
188+
{
189+
return DismissListener(new DialogInterfaceOnDismissListenerActionWrapper(action));
190+
}
130191
}
131192

132193
static IntPtr id_items_arrayLjava_lang_CharSequence_;

MaterialDialogs.Sample/MainActivity.cs

Lines changed: 142 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
using AFollestad.MaterialDialogs.Internal;
55
using AFollestad.MaterialDialogs.SimpleList;
66
using AFollestad.MaterialDialogs.Util;
7+
using Android;
8+
using Android.Annotation;
79
using Android.App;
810
using Android.Graphics;
911
using Android.Graphics.Drawables;
1012
using Android.OS;
13+
using Android.Support.V4.App;
1114
using Android.Support.V4.Content;
1215
using Android.Support.V7.App;
1316
using Android.Text;
@@ -19,40 +22,41 @@
1922
using System.Linq;
2023
using System.Text;
2124
using System.Threading;
25+
using Java.IO;
2226

2327
namespace MaterialDialogs.Sample
2428
{
2529
[Activity(Label = "@string/app_name", MainLauncher = true)]
2630
public class MainActivity : AppCompatActivity,
27-
//FolderChooserDialog.IFolderCallback,
28-
//FileChooserDialog.IFileCallback,
31+
FolderChooserDialog.IFolderCallback,
32+
FileChooserDialog.IFileCallback,
2933
ColorChooserDialog.IColorCallback
3034
{
3135
const int StoragePermissionRC = 69;
3236

33-
EditText passwordInput;
34-
View positiveAction;
35-
int primaryPreselect;
37+
EditText _passwordInput;
38+
View _positiveAction;
39+
int _primaryPreselect;
3640

37-
int accentPreselect;
38-
Toast toast;
39-
Thread thread;
40-
Handler handler;
41+
int _accentPreselect;
42+
Toast _toast;
43+
Thread _thread;
44+
Handler _handler;
4145

42-
int chooserDialog;
46+
int _chooserDialog;
4347

4448
void ShowToast(string message)
4549
{
46-
toast?.Cancel();
47-
toast = Toast.MakeText(this, message, ToastLength.Short);
48-
toast.Show();
50+
_toast?.Cancel();
51+
_toast = Toast.MakeText(this, message, ToastLength.Short);
52+
_toast.Show();
4953
}
5054

5155
void StartThread(Action action)
5256
{
53-
thread?.Interrupt();
54-
thread = new Thread(new ThreadStart(action));
55-
thread.Start();
57+
_thread?.Interrupt();
58+
_thread = new Thread(new ThreadStart(action));
59+
_thread.Start();
5660
}
5761

5862
void ShowToast(int message)
@@ -66,23 +70,23 @@ protected override void OnCreate(Bundle bundle)
6670
SetContentView (Resource.Layout.activity_main);
6771
Cheeseknife.Bind(this);
6872

69-
handler = new Handler();
70-
primaryPreselect = DialogUtils.ResolveColor(this, Resource.Attribute.colorPrimary);
71-
accentPreselect = DialogUtils.ResolveColor(this, Resource.Attribute.colorAccent);
73+
_handler = new Handler();
74+
_primaryPreselect = DialogUtils.ResolveColor(this, Resource.Attribute.colorPrimary);
75+
_accentPreselect = DialogUtils.ResolveColor(this, Resource.Attribute.colorAccent);
7276
}
7377

7478
protected override void OnDestroy()
7579
{
7680
base.OnDestroy();
77-
handler = null;
81+
_handler = null;
7882
}
7983

8084
protected override void OnPause()
8185
{
8286
base.OnPause();
83-
if (thread != null && thread.IsAlive)
87+
if (_thread != null && _thread.IsAlive)
8488
{
85-
thread.Interrupt();
89+
_thread.Interrupt();
8690
}
8791
}
8892

@@ -480,33 +484,33 @@ public void ShowCustomView(object sender, EventArgs e)
480484
.CustomView(Resource.Layout.dialog_customview, true)
481485
.PositiveText(Resource.String.connect)
482486
.NegativeText(Android.Resource.String.Cancel)
483-
.OnPositive((dialog1, which) => ShowToast("Password: " + passwordInput.Text)).Build();
487+
.OnPositive((dialog1, which) => ShowToast("Password: " + _passwordInput.Text)).Build();
484488

485-
positiveAction = dialog.GetActionButton(DialogAction.Positive);
489+
_positiveAction = dialog.GetActionButton(DialogAction.Positive);
486490

487-
passwordInput = dialog.CustomView.FindViewById<EditText>(Resource.Id.password);
488-
passwordInput.TextChanged += (s, ev) =>
491+
_passwordInput = dialog.CustomView.FindViewById<EditText>(Resource.Id.password);
492+
_passwordInput.TextChanged += (s, ev) =>
489493
{
490-
positiveAction.Enabled = String.Concat(ev.Text.Select(c => c.ToString())).Trim().Length > 0;
494+
_positiveAction.Enabled = String.Concat(ev.Text.Select(c => c.ToString())).Trim().Length > 0;
491495
};
492496

493497

494498
CheckBox checkbox = dialog.CustomView.FindViewById<CheckBox>(Resource.Id.showPassword);
495499
checkbox.CheckedChange += (s, ev) =>
496500
{
497-
passwordInput.InputType = (!ev.IsChecked) ? InputTypes.TextVariationPassword : InputTypes.ClassText;
498-
passwordInput.TransformationMethod = (!ev.IsChecked) ? PasswordTransformationMethod.Instance : null;
501+
_passwordInput.InputType = (!ev.IsChecked) ? InputTypes.TextVariationPassword : InputTypes.ClassText;
502+
_passwordInput.TransformationMethod = (!ev.IsChecked) ? PasswordTransformationMethod.Instance : null;
499503
};
500504

501505
int widgetColor = ThemeSingleton.Get().WidgetColor;
502506
MDTintHelper.SetTint(checkbox,
503507
widgetColor == 0 ? ContextCompat.GetColor(this, Resource.Color.accent) : widgetColor);
504508

505-
MDTintHelper.SetTint(passwordInput,
509+
MDTintHelper.SetTint(_passwordInput,
506510
widgetColor == 0 ? ContextCompat.GetColor(this, Resource.Color.accent) : widgetColor);
507511

508512
dialog.Show();
509-
positiveAction.Enabled = false;
513+
_positiveAction.Enabled = false;
510514
}
511515

512516
[OnClick(Resource.Id.customView_webView)]
@@ -539,7 +543,7 @@ public void ShowColorChooserPrimary(object sender, EventArgs e)
539543
{
540544
new ColorChooserDialog.Builder(this, Resource.String.color_palette)
541545
.TitleSub(Resource.String.colors)
542-
.Preselect(primaryPreselect)
546+
.Preselect(_primaryPreselect)
543547
.Show();
544548
}
545549

@@ -549,7 +553,7 @@ public void ShowColorChooserAccent(object sender, EventArgs e)
549553
new ColorChooserDialog.Builder(this, Resource.String.color_palette)
550554
.TitleSub(Resource.String.colors)
551555
.AccentMode(true)
552-
.Preselect(accentPreselect)
556+
.Preselect(_accentPreselect)
553557
.Show();
554558
}
555559

@@ -598,7 +602,7 @@ public void ShowColorChooserCustomColors(object sender, EventArgs e)
598602

599603
new ColorChooserDialog.Builder(this, Resource.String.color_palette)
600604
.TitleSub(Resource.String.colors)
601-
.Preselect(primaryPreselect)
605+
.Preselect(_primaryPreselect)
602606
.CustomColors(Resource.Array.custom_colors, subColors)
603607
.Show();
604608
}
@@ -608,7 +612,7 @@ public void ShowColorChooserCustomColorsNoSub(object sender, EventArgs e)
608612
{
609613
new ColorChooserDialog.Builder(this, Resource.String.color_palette)
610614
.TitleSub(Resource.String.colors)
611-
.Preselect(primaryPreselect)
615+
.Preselect(_primaryPreselect)
612616
.CustomColors(Resource.Array.custom_colors, null)
613617
.Show();
614618
}
@@ -619,15 +623,15 @@ public void OnColorSelection(ColorChooserDialog dialog, int color)
619623
{
620624
if (dialog.IsAccentMode)
621625
{
622-
accentPreselect = color;
626+
_accentPreselect = color;
623627
ThemeSingleton.Get().PositiveColor = DialogUtils.GetActionTextStateList(this, color);
624628
ThemeSingleton.Get().NeutralColor = DialogUtils.GetActionTextStateList(this, color);
625629
ThemeSingleton.Get().NegativeColor = DialogUtils.GetActionTextStateList(this, color);
626630
ThemeSingleton.Get().WidgetColor = color;
627631
}
628632
else
629633
{
630-
primaryPreselect = color;
634+
_primaryPreselect = color;
631635
SupportActionBar?.SetBackgroundDrawable(new ColorDrawable(new Color(color)));
632636
if (Build.VERSION.SdkInt >= BuildVersionCodes.Lollipop)
633637
{
@@ -645,6 +649,106 @@ public void OnColorChooserDismissed(ColorChooserDialog dialog)
645649
#endregion
646650

647651
#endregion
652+
653+
#region Miscellaneous
654+
655+
[OnClick(Resource.Id.themed)]
656+
public void ShowThemed(object sender, EventArgs e)
657+
{
658+
new MaterialDialog.Builder(this)
659+
.Title(Resource.String.useGoogleLocationServices)
660+
.Content(Resource.String.useGoogleLocationServicesPrompt)
661+
.PositiveText(Resource.String.agree)
662+
.NegativeText(Resource.String.disagree)
663+
.PositiveColorRes(Resource.Color.material_red_400)
664+
.NegativeColorRes(Resource.Color.material_red_400)
665+
.TitleGravity(GravityEnum.Center)
666+
.TitleColorRes(Resource.Color.material_red_400)
667+
.ContentColorRes(Android.Resource.Color.White)
668+
.BackgroundColorRes(Resource.Color.material_blue_grey_800)
669+
.DividerColorRes(Resource.Color.accent)
670+
.BtnSelector(Resource.Drawable.md_btn_selector_custom, DialogAction.Positive)
671+
.PositiveColor(Color.White)
672+
.NegativeColorAttr(Android.Resource.Attribute.TextColorSecondaryInverse)
673+
.Theme(AFollestad.MaterialDialogs.Theme.Dark)
674+
.Show();
675+
}
676+
677+
[OnClick(Resource.Id.showCancelDismiss)]
678+
public void ShowShowCancelDismissCallbacks(object sender, EventArgs e)
679+
{
680+
new MaterialDialog.Builder(this)
681+
.Title(Resource.String.useGoogleLocationServices)
682+
.Content(Resource.String.useGoogleLocationServicesPrompt)
683+
.PositiveText(Resource.String.agree)
684+
.NegativeText(Resource.String.disagree)
685+
.NeutralText(Resource.String.more_info)
686+
.ShowListener(dialog => ShowToast("onShow"))
687+
.CancelListener(dialog => ShowToast("onCancel"))
688+
.DismissListener(dialog => ShowToast("onDismiss"))
689+
.Show();
690+
}
691+
692+
[TargetApi(Value=(int)BuildVersionCodes.JellyBean)]
693+
[OnClick(Resource.Id.file_chooser)]
694+
public void ShowFileChooser(object sender, EventArgs e)
695+
{
696+
_chooserDialog = Resource.Id.file_chooser;
697+
if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.ReadExternalStorage)
698+
!= Android.Content.PM.Permission.Granted)
699+
{
700+
ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.ReadExternalStorage }, StoragePermissionRC);
701+
return;
702+
}
703+
new FileChooserDialog.Builder(this)
704+
.Show();
705+
}
706+
707+
#region FileChooserDialog.IFileCallback implementation
708+
709+
public void OnFileSelection(FileChooserDialog dialog, File file)
710+
{
711+
ShowToast(file.AbsolutePath);
712+
}
713+
714+
public void OnFileChooserDismissed(FileChooserDialog dialog)
715+
{
716+
ShowToast("File chooser dismissed!");
717+
}
718+
719+
#endregion
720+
721+
[TargetApi(Value = (int)BuildVersionCodes.JellyBean)]
722+
[OnClick(Resource.Id.folder_chooser)]
723+
public void ShowFolderChooser(object sender, EventArgs e)
724+
{
725+
_chooserDialog = Resource.Id.folder_chooser;
726+
if (ActivityCompat.CheckSelfPermission(this, Manifest.Permission.WriteExternalStorage)
727+
!= Android.Content.PM.Permission.Granted)
728+
{
729+
ActivityCompat.RequestPermissions(this, new string[] { Manifest.Permission.WriteExternalStorage }, StoragePermissionRC);
730+
return;
731+
}
732+
new FolderChooserDialog.Builder(this)
733+
.ChooseButton(Resource.String.md_choose_label)
734+
.AllowNewFolder(true, 0)
735+
.Show();
736+
}
737+
738+
#region FolderChooserDialog.IFolderCallback implementation
739+
740+
public void OnFolderSelection(FolderChooserDialog dialog, File folder)
741+
{
742+
ShowToast(folder.AbsolutePath);
743+
}
744+
745+
public void OnFolderChooserDismissed(FolderChooserDialog dialog)
746+
{
747+
ShowToast("Folder chooser dismissed!");
748+
}
749+
750+
#endregion
751+
752+
#endregion
648753
}
649754
}
650-

0 commit comments

Comments
 (0)