-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
DialogService.cs
344 lines (293 loc) · 13 KB
/
DialogService.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
using Prism.AppModel;
using Prism.Common;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Services.Dialogs.Xaml;
using System;
using System.Linq;
using Xamarin.Forms;
namespace Prism.Services.Dialogs
{
public sealed class DialogService : IDialogService
{
public const string PopupOverlayStyle = "PrismDialogMaskStyle";
private IContainerProvider _containerExtension { get; }
private IApplicationProvider _applicationProvider { get; }
public DialogService(IApplicationProvider applicationProvider, IContainerExtension containerExtension)
{
_applicationProvider = applicationProvider;
_containerExtension = containerExtension;
}
public void ShowDialog(string name, IDialogParameters parameters, Action<IDialogResult> callback)
{
try
{
parameters = UriParsingHelper.GetSegmentParameters(name, parameters);
var view = CreateViewFor(UriParsingHelper.GetSegmentName(name));
var dialogAware = InitializeDialog(view, parameters);
var currentPage = GetCurrentPage();
dialogAware.RequestClose += DialogAware_RequestClose;
void DialogAware_RequestClose(IDialogParameters outParameters)
{
try
{
var result = CloseDialog(outParameters ?? new DialogParameters(), currentPage);
if(result.Exception is DialogException de && de.Message == DialogException.CanCloseIsFalse)
{
return;
}
dialogAware.RequestClose -= DialogAware_RequestClose;
callback(result);
GC.Collect();
}
catch(DialogException dex)
{
if(dex.Message != DialogException.CanCloseIsFalse)
{
callback(new DialogResult
{
Exception = dex,
Parameters = parameters
});
}
}
catch (Exception ex)
{
callback(new DialogResult
{
Exception = ex,
Parameters = parameters
});
}
}
if(!parameters.TryGetValue<bool>(KnownDialogParameters.CloseOnBackgroundTapped, out var closeOnBackgroundTapped))
{
var dialogLayoutCloseOnBackgroundTapped = DialogLayout.GetCloseOnBackgroundTapped(view);
if(dialogLayoutCloseOnBackgroundTapped.HasValue)
{
closeOnBackgroundTapped = dialogLayoutCloseOnBackgroundTapped.Value;
}
}
InsertPopupViewInCurrentPage(currentPage as ContentPage, view, closeOnBackgroundTapped, DialogAware_RequestClose);
PageUtilities.InvokeViewAndViewModelAction<IActiveAware>(currentPage, aa => aa.IsActive = false);
PageUtilities.InvokeViewAndViewModelAction<IActiveAware>(view, aa => aa.IsActive = true);
}
catch (Exception ex)
{
var error = ex.ToString();
callback(new DialogResult { Exception = ex });
}
}
private IDialogResult CloseDialog(IDialogParameters parameters, ContentPage currentPage)
{
try
{
if(parameters is null)
{
parameters = new DialogParameters();
}
if (!(currentPage is ContentPage contentPage))
{
throw new DialogException(DialogException.RequiresContentPage);
}
if (!(bool)contentPage.Content.GetValue(IsPopupHostProperty))
{
throw new DialogException(DialogException.HostPageIsNotDialogHost);
}
var hostView = (AbsoluteLayout)contentPage.Content;
var popupContainer = (DialogContainer)hostView.Children.First(x => x is DialogContainer dc && dc.IsPopupContent);
var view = popupContainer.Content;
var dialogAware = GetDialogController(view);
if(!dialogAware.CanCloseDialog())
{
throw new DialogException(DialogException.CanCloseIsFalse);
}
var pageContainer = (DialogContainer)hostView.Children.First(x => x is DialogContainer dc && dc.IsPageContent);
contentPage.Content = pageContainer.Content;
contentPage.Padding = pageContainer.Padding;
contentPage.SetValue(IsPopupHostProperty, false);
PageUtilities.InvokeViewAndViewModelAction<IActiveAware>(view, aa => aa.IsActive = false);
PageUtilities.InvokeViewAndViewModelAction<IActiveAware>(currentPage, aa => aa.IsActive = true);
dialogAware.OnDialogClosed();
return new DialogResult
{
Parameters = parameters
};
}
catch(DialogException)
{
throw;
}
catch (Exception ex)
{
return new DialogResult
{
Exception = ex,
Parameters = parameters
};
}
}
private View CreateViewFor(string name)
{
var view = (View)_containerExtension.Resolve<object>(name);
if (ViewModelLocator.GetAutowireViewModel(view) is null)
{
ViewModelLocator.SetAutowireViewModel(view, true);
}
return view;
}
private IDialogAware GetDialogController(View view)
{
if(view is IDialogAware viewAsDialogAware)
{
return viewAsDialogAware;
}
else if(view.BindingContext is null)
{
throw new DialogException(DialogException.NoViewModel);
}
else if(view.BindingContext is IDialogAware dialogAware)
{
return dialogAware;
}
throw new DialogException(DialogException.ImplementIDialogAware);
}
private IDialogAware InitializeDialog(View view, IDialogParameters parameters)
{
var dialog = GetDialogController(view);
dialog.OnDialogOpened(parameters);
if(dialog is IAbracadabra)
{
PageUtilities.Abracadabra(dialog, parameters);
}
return dialog;
}
private ContentPage GetCurrentPage(Page page = null)
{
switch(page)
{
case ContentPage cp:
return cp;
case TabbedPage tp:
return GetCurrentPage(tp.CurrentPage);
case NavigationPage np:
return GetCurrentPage(np.CurrentPage);
case CarouselPage carouselPage:
return GetCurrentPage(carouselPage.CurrentPage);
case MasterDetailPage mdp:
mdp.IsPresented = false;
return GetCurrentPage(mdp.Detail);
case Shell shell:
return GetCurrentPage((shell.CurrentItem.CurrentItem as IShellSectionController).PresentedPage);
default:
// If we get some random Page Type
if(page != null)
{
Xamarin.Forms.Internals.Log.Warning("Warning", $"An Unknown Page type {page.GetType()} was found walk walking the Navigation Stack. This is not supported by the DialogService");
return null;
}
var mainPage = _applicationProvider.MainPage;
if (mainPage is null)
{
return null;
}
return GetCurrentPage(mainPage);
}
}
private void InsertPopupViewInCurrentPage(ContentPage currentPage, View popupView, bool hideOnBackgroundTapped, Action<IDialogParameters> callback)
{
View mask = DialogLayout.GetMask(popupView);
if(mask is null)
{
Style overlayStyle = GetOverlayStyle(popupView);
mask = new BoxView
{
Style = overlayStyle
};
}
mask.SetBinding(VisualElement.WidthRequestProperty, new Binding { Path = "Width", Source = currentPage });
mask.SetBinding(VisualElement.HeightRequestProperty, new Binding { Path = "Height", Source = currentPage });
if (hideOnBackgroundTapped)
{
var dismissCommand = new Command(() => callback(new DialogParameters()));
mask.GestureRecognizers.Add(new TapGestureRecognizer
{
Command = dismissCommand
});
}
var overlay = new AbsoluteLayout();
overlay.SetValue(IsPopupHostProperty, true);
var existingContent = currentPage.Content;
var content = new DialogContainer
{
Padding = currentPage.Padding,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
IsPageContent = true
};
currentPage.Padding = new Thickness(0);
var popupContainer = new DialogContainer
{
IsPopupContent = true,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center,
Content = popupView,
};
var relativeWidth = DialogLayout.GetRelativeWidthRequest(popupView);
if (relativeWidth != null)
{
popupContainer.SetBinding(DialogContainer.WidthRequestProperty,
new Binding("Width",
BindingMode.OneWay,
new RelativeContentSizeConverter { RelativeSize = relativeWidth.Value },
source: currentPage));
}
var relativeHeight = DialogLayout.GetRelativeHeightRequest(popupView);
if (relativeHeight != null)
{
popupContainer.SetBinding(DialogContainer.WidthRequestProperty,
new Binding("Height",
BindingMode.OneWay,
new RelativeContentSizeConverter { RelativeSize = relativeWidth.Value },
source: currentPage));
}
AbsoluteLayout.SetLayoutFlags(content, AbsoluteLayoutFlags.PositionProportional);
AbsoluteLayout.SetLayoutBounds(content, new Rectangle(0f, 0f, currentPage.Width, currentPage.Height));
AbsoluteLayout.SetLayoutFlags(popupContainer, AbsoluteLayoutFlags.PositionProportional);
var popupBounds = DialogLayout.GetLayoutBounds(popupView);
AbsoluteLayout.SetLayoutBounds(popupContainer, popupBounds);
overlay.Children.Add(content);
if(DialogLayout.GetUseMask(popupContainer) ?? true)
{
overlay.Children.Add(mask);
}
overlay.Children.Add(popupContainer);
currentPage.Content = overlay;
// The original content needs to be reparented after the Page Content has been reset.
content.Content = existingContent;
}
private static Style GetOverlayStyle(View popupView)
{
var style = DialogLayout.GetMaskStyle(popupView);
if(style != null)
{
return style;
}
if (Application.Current.Resources.ContainsKey(PopupOverlayStyle))
{
style = (Style)Application.Current.Resources[PopupOverlayStyle];
if (style.TargetType == typeof(BoxView))
{
return style;
}
}
var overlayStyle = new Style(typeof(BoxView));
overlayStyle.Setters.Add(new Setter { Property = VisualElement.OpacityProperty, Value = 0.75 });
overlayStyle.Setters.Add(new Setter { Property = VisualElement.BackgroundColorProperty, Value = Color.Black });
Application.Current.Resources[PopupOverlayStyle] = overlayStyle;
return overlayStyle;
}
private static readonly BindableProperty IsPopupHostProperty =
BindableProperty.CreateAttached("IsPopupHost", typeof(bool), typeof(DialogService), false);
}
}