-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPopupMauiService.cs
More file actions
85 lines (61 loc) · 3.76 KB
/
PopupMauiService.cs
File metadata and controls
85 lines (61 loc) · 3.76 KB
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
using CommunityToolkit.Maui;
using CommunityToolkit.Maui.Extensions;
using CommunityToolkit.Maui.Views;
namespace PopupV2NavigationWithParameters;
public interface IPopupMauiService
{
Task ShowPopupAsync<TView>(INavigation nav, bool closePreviousPopup = false)
where TView : Popup;
Task ShowPopupAsync<TView, TInit>(INavigation nav, TInit initData, bool closePreviousPopup = false)
where TView : Popup
where TInit : notnull;
Task<TReturn?> ShowPopupAsync<TView, TReturn>(INavigation nav, bool closePreviousPopup = false)
where TView : Popup<TReturn?>;
Task<TReturn?> ShowPopupAsync<TView, TInit, TReturn>(INavigation nav, TInit initData, bool closePreviousPopup = false)
where TView : Popup<TReturn?>
where TInit : notnull;
}
internal class PopupMauiService(IPopupService popupService) : IPopupMauiService
{
private readonly IPopupService _popupService = popupService;
public async Task ShowPopupAsync<TView>(INavigation nav, bool forceClosePreviousPopup = false)
where TView : Popup
{
nav ??= Application.Current?.Windows[0].Page?.Navigation ?? throw new NullReferenceException($"No Navigation available to show popup {typeof(TView)}");
if (forceClosePreviousPopup)
await _popupService.ClosePopupAsync(nav);
var laPopup = IPlatformApplication.Current?.Services.GetService<TView>() ?? throw new NullReferenceException($"Error while instantiating a popup of type {typeof(TView)}");
await nav.ShowPopupAsync<TView>(laPopup);
}
public async Task ShowPopupAsync<TView, TInit>(INavigation nav, TInit initData, bool forceClosePreviousPopup = false)
where TView : Popup
where TInit : notnull
{
nav ??= Application.Current?.Windows[0].Page?.Navigation ?? throw new NullReferenceException($"No Navigation available to show popup {typeof(TView)}");
if (forceClosePreviousPopup)
await _popupService.ClosePopupAsync(nav);
var laPopup = IPlatformApplication.Current?.Services?.GetRequiredService<Func<TInit, TView>>()(initData) ?? throw new NullReferenceException($"Error while instantiating a popup of type {typeof(TView)}");
await nav.ShowPopupAsync<TView>(laPopup);
}
public async Task<TReturn?> ShowPopupAsync<TView, TReturn>(INavigation nav, bool forceClosePreviousPopup = false)
where TView : Popup<TReturn?>
{
nav ??= Application.Current?.Windows[0].Page?.Navigation ?? throw new NullReferenceException($"No Navigation available to show popup {typeof(TView)}");
if (forceClosePreviousPopup)
await _popupService.ClosePopupAsync(nav);
var laPopup = IPlatformApplication.Current?.Services.GetService<TView>() ?? throw new NullReferenceException($"Error while instantiating a popup of type {typeof(TView)}");
var result = await nav.ShowPopupAsync<TReturn>(laPopup);
return result.Result;
}
public async Task<TReturn?> ShowPopupAsync<TView, TInit, TReturn>(INavigation nav, TInit initData, bool forceClosePreviousPopup = false)
where TView : Popup<TReturn?>
where TInit : notnull
{
nav ??= Application.Current?.Windows[0].Page?.Navigation ?? throw new NullReferenceException($"No Navigation available to show popup {typeof(TView)}");
if (forceClosePreviousPopup)
await _popupService.ClosePopupAsync(nav);
var laPopup = IPlatformApplication.Current?.Services?.GetRequiredService<Func<TInit, TView>>()(initData) ?? throw new NullReferenceException($"Error while instantiating a popup of type {typeof(TView)}");
var result = await nav.ShowPopupAsync<TReturn>(laPopup);
return result.Result;
}
}