/
PopupManager.cs
417 lines (360 loc) · 11 KB
/
PopupManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
using System;
using System.Collections.Generic;
using System.Linq;
using Android.App;
using Android.Content;
using Android.Text;
using Android.Views;
using Android.Widget;
using Xamarin.Forms.Internals;
#if __ANDROID_29__
using AppCompatAlertDialog = AndroidX.AppCompat.App.AlertDialog;
using AppCompatActivity = AndroidX.AppCompat.App.AppCompatActivity;
#else
using AppCompatAlertDialog = global::Android.Support.V7.App.AlertDialog;
using AppCompatActivity =global::Android.Support.V7.App.AppCompatActivity;
#endif
namespace Xamarin.Forms.Platform.Android
{
internal static class PopupManager
{
static readonly List<PopupRequestHelper> s_subscriptions = new List<PopupRequestHelper>();
internal static void Subscribe(Activity context)
{
if (s_subscriptions.Any(s => s.Activity == context))
{
return;
}
s_subscriptions.Add(new PopupRequestHelper(context));
}
internal static void Unsubscribe(Context context)
{
var toRemove = s_subscriptions.Where(s => s.Activity == context).ToList();
foreach (PopupRequestHelper popupRequestHelper in toRemove)
{
popupRequestHelper.Dispose();
s_subscriptions.Remove(popupRequestHelper);
}
}
internal static void ResetBusyCount(Activity context)
{
s_subscriptions.FirstOrDefault(s => s.Activity == context)?.ResetBusyCount();
}
internal sealed class PopupRequestHelper : IDisposable
{
int _busyCount;
bool? _supportsProgress;
internal PopupRequestHelper(Activity context)
{
Activity = context;
MessagingCenter.Subscribe<Page, bool>(Activity, Page.BusySetSignalName, OnPageBusy);
MessagingCenter.Subscribe<Page, AlertArguments>(Activity, Page.AlertSignalName, OnAlertRequested);
MessagingCenter.Subscribe<Page, PromptArguments>(Activity, Page.PromptSignalName, OnPromptRequested);
MessagingCenter.Subscribe<Page, ActionSheetArguments>(Activity, Page.ActionSheetSignalName, OnActionSheetRequested);
}
public Activity Activity { get; }
public void Dispose()
{
MessagingCenter.Unsubscribe<Page, bool>(Activity, Page.BusySetSignalName);
MessagingCenter.Unsubscribe<Page, AlertArguments>(Activity, Page.AlertSignalName);
MessagingCenter.Unsubscribe<Page, PromptArguments>(Activity, Page.PromptSignalName);
MessagingCenter.Unsubscribe<Page, ActionSheetArguments>(Activity, Page.ActionSheetSignalName);
}
public void ResetBusyCount()
{
_busyCount = 0;
}
void OnPageBusy(Page sender, bool enabled)
{
// Verify that the page making the request is part of this activity
if (!PageIsInThisContext(sender))
{
return;
}
_busyCount = Math.Max(0, enabled ? _busyCount + 1 : _busyCount - 1);
UpdateProgressBarVisibility(_busyCount > 0);
}
void OnActionSheetRequested(Page sender, ActionSheetArguments arguments)
{
// Verify that the page making the request is part of this activity
if (!PageIsInThisContext(sender))
{
return;
}
var builder = new DialogBuilder(Activity);
builder.SetTitle(arguments.Title);
string[] items = arguments.Buttons.ToArray();
builder.SetItems(items, (o, args) => arguments.Result.TrySetResult(items[args.Which]));
if (arguments.Cancel != null)
builder.SetPositiveButton(arguments.Cancel, (o, args) => arguments.Result.TrySetResult(arguments.Cancel));
if (arguments.Destruction != null)
builder.SetNegativeButton(arguments.Destruction, (o, args) => arguments.Result.TrySetResult(arguments.Destruction));
var dialog = builder.Create();
builder.Dispose();
//to match current functionality of renderer we set cancelable on outside
//and return null
dialog.SetCanceledOnTouchOutside(true);
dialog.SetCancelEvent((o, e) => arguments.SetResult(null));
dialog.Show();
}
void OnAlertRequested(Page sender, AlertArguments arguments)
{
// Verify that the page making the request is part of this activity
if (!PageIsInThisContext(sender))
{
return;
}
var alert = new DialogBuilder(Activity).Create();
alert.SetTitle(arguments.Title);
alert.SetMessage(arguments.Message);
if (arguments.Accept != null)
alert.SetButton((int)DialogButtonType.Positive, arguments.Accept, (o, args) => arguments.SetResult(true));
alert.SetButton((int)DialogButtonType.Negative, arguments.Cancel, (o, args) => arguments.SetResult(false));
alert.SetCancelEvent((o, args) => { arguments.SetResult(false); });
alert.Show();
}
void OnPromptRequested(Page sender, PromptArguments arguments)
{
// Verify that the page making the request is part of this activity
if (!PageIsInThisContext(sender))
{
return;
}
var alertDialog = new DialogBuilder(Activity).Create();
alertDialog.SetTitle(arguments.Title);
alertDialog.SetMessage(arguments.Message);
var frameLayout = new FrameLayout(Activity);
var editText = new EditText(Activity) { Hint = arguments.Placeholder, Text = arguments.InitialValue };
var layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent)
{
LeftMargin = (int)(22 * Activity.Resources.DisplayMetrics.Density),
RightMargin = (int)(22 * Activity.Resources.DisplayMetrics.Density)
};
editText.LayoutParameters = layoutParams;
editText.InputType = arguments.Keyboard.ToInputType();
if (arguments.Keyboard == Keyboard.Numeric)
editText.KeyListener = LocalizedDigitsKeyListener.Create(editText.InputType);
if (arguments.MaxLength > -1)
editText.SetFilters(new IInputFilter[]{ new InputFilterLengthFilter(arguments.MaxLength)});
frameLayout.AddView(editText);
alertDialog.SetView(frameLayout);
alertDialog.SetButton((int)DialogButtonType.Positive, arguments.Accept, (o, args) => arguments.SetResult(editText.Text));
alertDialog.SetButton((int)DialogButtonType.Negative, arguments.Cancel, (o, args) => arguments.SetResult(null));
alertDialog.SetCancelEvent((o, args) => { arguments.SetResult(null); });
alertDialog.Window.SetSoftInputMode(SoftInput.StateVisible);
alertDialog.Show();
editText.RequestFocus();
}
void UpdateProgressBarVisibility(bool isBusy)
{
if (!SupportsProgress)
return;
#pragma warning disable 612, 618
Activity.SetProgressBarIndeterminate(true);
Activity.SetProgressBarIndeterminateVisibility(isBusy);
#pragma warning restore 612, 618
}
internal bool SupportsProgress
{
get
{
if (_supportsProgress.HasValue)
{
return _supportsProgress.Value;
}
int progressCircularId = Activity.Resources.GetIdentifier("progress_circular", "id", "android");
if (progressCircularId > 0)
_supportsProgress = Activity.FindViewById(progressCircularId) != null;
else
_supportsProgress = true;
return _supportsProgress.Value;
}
}
bool PageIsInThisContext(Page page)
{
var renderer = Platform.GetRenderer(page);
if (renderer?.View?.Context == null)
{
return false;
}
return renderer.View.Context.Equals(Activity);
}
// This is a proxy dialog builder class to support both pre-appcompat and appcompat dialogs for Alert,
// ActionSheet, Prompt, etc.
internal sealed class DialogBuilder
{
AppCompatAlertDialog.Builder _appcompatBuilder;
AlertDialog.Builder _legacyBuilder;
bool _useAppCompat;
public DialogBuilder(Activity activity)
{
if (activity is AppCompatActivity)
{
_appcompatBuilder = new AppCompatAlertDialog.Builder(activity);
_useAppCompat = true;
}
else
{
_legacyBuilder = new AlertDialog.Builder(activity);
}
}
public void SetTitle(string title)
{
if (_useAppCompat)
{
_appcompatBuilder.SetTitle(title);
}
else
{
_legacyBuilder.SetTitle(title);
}
}
public void SetItems(string[] items, EventHandler<DialogClickEventArgs> handler)
{
if (_useAppCompat)
{
_appcompatBuilder.SetItems(items, handler);
}
else
{
_legacyBuilder.SetItems(items, handler);
}
}
public void SetPositiveButton(string text, EventHandler<DialogClickEventArgs> handler)
{
if (_useAppCompat)
{
_appcompatBuilder.SetPositiveButton(text, handler);
}
else
{
_legacyBuilder.SetPositiveButton(text, handler);
}
}
public void SetNegativeButton(string text, EventHandler<DialogClickEventArgs> handler)
{
if (_useAppCompat)
{
_appcompatBuilder.SetNegativeButton(text, handler);
}
else
{
_legacyBuilder.SetNegativeButton(text, handler);
}
}
public FlexibleAlertDialog Create()
{
if (_useAppCompat)
{
return new FlexibleAlertDialog(_appcompatBuilder.Create());
}
return new FlexibleAlertDialog(_legacyBuilder.Create());
}
public void Dispose()
{
if (_useAppCompat)
{
_appcompatBuilder.Dispose();
}
else
{
_legacyBuilder.Dispose();
}
}
}
internal sealed class FlexibleAlertDialog
{
readonly AppCompatAlertDialog _appcompatAlertDialog;
readonly AlertDialog _legacyAlertDialog;
bool _useAppCompat;
public FlexibleAlertDialog(AlertDialog alertDialog)
{
_legacyAlertDialog = alertDialog;
}
public FlexibleAlertDialog(AppCompatAlertDialog alertDialog)
{
_appcompatAlertDialog = alertDialog;
_useAppCompat = true;
}
public void SetTitle(string title)
{
if (_useAppCompat)
{
_appcompatAlertDialog.SetTitle(title);
}
else
{
_legacyAlertDialog.SetTitle(title);
}
}
public void SetMessage(string message)
{
if (_useAppCompat)
{
_appcompatAlertDialog.SetMessage(message);
}
else
{
_legacyAlertDialog.SetMessage(message);
}
}
public void SetButton(int whichButton, string text, EventHandler<DialogClickEventArgs> handler)
{
if (_useAppCompat)
{
_appcompatAlertDialog.SetButton(whichButton, text, handler);
}
else
{
_legacyAlertDialog.SetButton(whichButton, text, handler);
}
}
public void SetCancelEvent(EventHandler cancel)
{
if (_useAppCompat)
{
_appcompatAlertDialog.CancelEvent += cancel;
}
else
{
_legacyAlertDialog.CancelEvent += cancel;
}
}
public void SetCanceledOnTouchOutside(bool canceledOnTouchOutSide)
{
if (_useAppCompat)
{
_appcompatAlertDialog.SetCanceledOnTouchOutside(canceledOnTouchOutSide);
}
else
{
_legacyAlertDialog.SetCanceledOnTouchOutside(canceledOnTouchOutSide);
}
}
public void SetView(global::Android.Views.View view)
{
if (_useAppCompat)
{
_appcompatAlertDialog.SetView(view);
}
else
{
_legacyAlertDialog.SetView(view);
}
}
public Window Window => _useAppCompat ? _appcompatAlertDialog.Window : _legacyAlertDialog.Window;
public void Show()
{
if (_useAppCompat)
{
_appcompatAlertDialog.Show();
}
else
{
_legacyAlertDialog.Show();
}
}
}
}
}
}