Skip to content

Commit 28e2efe

Browse files
authored
Merge pull request #67 from egvijayanand/working
PopupDialogs updated as MVVM sample
2 parents 7e571cf + c001437 commit 28e2efe

File tree

7 files changed

+165
-152
lines changed

7 files changed

+165
-152
lines changed

src/PopupDialogs/Imports.cs

+9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
global using CommunityToolkit.Mvvm.Input;
2+
13
global using VijayAnand.MauiToolkit;
4+
global using VijayAnand.MauiToolkit.Core;
25
global using VijayAnand.MauiToolkit.Core.Services;
6+
global using VijayAnand.MauiToolkit.Core.ViewModels;
37
global using VijayAnand.MauiToolkit.Pro;
48
global using VijayAnand.MauiToolkit.Pro.Services;
9+
global using VijayAnand.MauiToolkit.Views;
10+
11+
// Local usings
12+
global using PopupDialogs.ViewModels;
13+
global using PopupDialogs.Views;
514

615
// Static
716
global using static Microsoft.Maui.Graphics.Colors;

src/PopupDialogs/MainPage.xaml.cs

-128
This file was deleted.

src/PopupDialogs/MauiProgram.cs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public static MauiApp CreateMauiApp()
1919
})
2020
.ConfigureServices(services =>
2121
{
22+
services.AddSingleton<MainViewModel>();
2223
services.AddSingleton<MainPage>();
2324
});
2425
#if DEBUG

src/PopupDialogs/PopupDialogs.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
<ItemGroup>
6060
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
6161
<PackageReference Include="CommunityToolkit.Maui" Version="3.1.0" />
62+
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.0.0" />
6263
<PackageReference Include="VijayAnand.MauiToolkit.Pro" Version="2.1.0-preview.4" />
6364
</ItemGroup>
6465

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
using VijayAnand.MauiToolkit.Services;
2+
3+
namespace PopupDialogs.ViewModels
4+
{
5+
public partial class MainViewModel : BaseViewModel
6+
{
7+
private readonly IDialogService _genericDialog;
8+
private readonly IMauiDialogService _mauiDialog;
9+
10+
public MainViewModel(IDialogService genericDialog, IMauiDialogService mauiDialog)
11+
=> (_genericDialog, _mauiDialog) = (genericDialog, mauiDialog);
12+
13+
[RelayCommand]
14+
private async Task GenericDialog(string arg)
15+
{
16+
// DI approach
17+
// Switch betwen native/custom implementations in the MauiProgram.cs
18+
// The order in which dependencies are registered determines the instance injected
19+
// The latest one wins
20+
21+
switch (arg)
22+
{
23+
case "Alert1":
24+
await _genericDialog.DisplayAlertAsync("Greeting", "Hello from .NET MAUI!!!", "OK");
25+
break;
26+
case "Alert2":
27+
var response = await _genericDialog.DisplayAlertAsync("Confirm", "Would you like to proceed?", "Yes", "No");
28+
var message = response is true ? "Glad that you opted in." : "Sorry to miss you, hope to see you soon.";
29+
await _genericDialog.DisplayAlertAsync("Response", message, "OK");
30+
break;
31+
case "Prompt":
32+
var result = await _genericDialog.DisplayPromptAsync("Subscribe", "Enter your email to send notifications.", maxLength: 100, inputType: InputType.Email);
33+
34+
if (!string.IsNullOrWhiteSpace(result))
35+
{
36+
await _genericDialog.DisplayAlertAsync("Response", result, "OK");
37+
}
38+
break;
39+
case "ActionSheet":
40+
var action = await _genericDialog.DisplayActionSheetAsync("Unsaved Changes ...", "What would you like to do?", "Cancel", "Discard", "Save", "Save", "Upload", "Share");
41+
await _genericDialog.DisplayAlertAsync("Response", action, "OK");
42+
break;
43+
default:
44+
// Static approach for custom implementation
45+
// Static approach for native dialogs soon in an upcoming preview
46+
await PopupDialog.Instance.DisplayAlertAsync("Static Invocation", "Welcome to .NET MAUI!!!", "OK");
47+
break;
48+
}
49+
}
50+
51+
[RelayCommand]
52+
private async Task MauiDialog(string arg)
53+
{
54+
// DI approach
55+
// Switch betwen native/custom implementations in the MauiProgram.cs
56+
// The order in which dependencies are registered determines the instance injected
57+
// The latest one wins
58+
59+
var rightToLeft = FlowDirection.RightToLeft;
60+
61+
switch (arg)
62+
{
63+
case "Alert1":
64+
await _mauiDialog.DisplayAlertAsync("Greeting", "Hello from .NET MAUI!!!", "OK", rightToLeft);
65+
break;
66+
case "Alert2":
67+
var response = await _mauiDialog.DisplayAlertAsync("Confirm", "Would you like to proceed?", "Yes", "No", rightToLeft);
68+
var message = response is true ? "Glad that you opted in." : "Sorry to miss you, hope to see you soon.";
69+
await _mauiDialog.DisplayAlertAsync("Response", message, "OK", rightToLeft);
70+
break;
71+
case "Prompt":
72+
var result = await _mauiDialog.DisplayPromptAsync("Lucky Draw", "Enter your age to participate.", maxLength: 3, inputType: InputType.Numeric, predicate: ValidateUserEntry);
73+
74+
if (!string.IsNullOrWhiteSpace(result))
75+
{
76+
await _mauiDialog.DisplayAlertAsync("Response", result, "OK");
77+
}
78+
break;
79+
case "ActionSheet":
80+
var action = await _mauiDialog.DisplayActionSheetAsync("Unsaved Changes ..."/*, "What would you like to do?"*/, "Cancel", "Discard", rightToLeft, "Save", "Upload", "Share");
81+
await _mauiDialog.DisplayAlertAsync("Response", action, "OK", rightToLeft);
82+
break;
83+
default:
84+
// Static approach for custom implementation
85+
// Static approach for native dialogs soon in an upcoming preview
86+
await MauiPopupDialog.Instance.DisplayAlertAsync("Static Invocation", "Welcome to .NET MAUI!!!", "OK", rightToLeft);
87+
break;
88+
}
89+
}
90+
91+
private (bool, string) ValidateUserEntry(string value)
92+
{
93+
if (string.IsNullOrWhiteSpace(value))
94+
{
95+
return (false, "Age is required.");
96+
}
97+
98+
if (int.TryParse(value, out int age))
99+
{
100+
if (age < 0 || age > 120)
101+
{
102+
return (false, "Enter a valid age.");
103+
}
104+
else if (age >= 18)
105+
{
106+
return (true, string.Empty);
107+
}
108+
else
109+
{
110+
return (false, "You should be atleast 18 years old to participate.");
111+
}
112+
}
113+
else
114+
{
115+
return (false, "Enter a valid age.");
116+
}
117+
}
118+
}
119+
}

src/PopupDialogs/MainPage.xaml renamed to src/PopupDialogs/Views/MainPage.xaml

+29-24
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
2-
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3-
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4-
x:Class="PopupDialogs.MainPage">
2+
<pages:MauiPage
3+
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
5+
xmlns:pages="clr-namespace:VijayAnand.MauiToolkit.Views;assembly=VijayAnand.MauiToolkit"
6+
xmlns:vm="clr-namespace:PopupDialogs.ViewModels"
7+
x:Class="PopupDialogs.Views.MainPage"
8+
x:DataType="vm:MainViewModel"
9+
x:TypeArguments="vm:MainViewModel">
510

611
<ScrollView>
712
<VerticalStackLayout Padding="30" Spacing="15">
@@ -37,32 +42,32 @@
3742
HorizontalOptions="Center"
3843
Spacing="15">
3944
<Button
40-
AutomationId="Alert1"
41-
Clicked="OnBtnClicked"
45+
Command="{Binding GenericDialogCommand}"
46+
CommandParameter="Alert1"
4247
SemanticProperties.Hint="Shows an alert dialog"
4348
Style="{StaticResource PrimaryAction}"
4449
Text="Show Alert" />
4550
<Button
46-
AutomationId="Alert2"
47-
Clicked="OnBtnClicked"
51+
Command="{Binding GenericDialogCommand}"
52+
CommandParameter="Alert2"
4853
SemanticProperties.Hint="Shows an alert dialog with accept and cancel actions"
4954
Style="{StaticResource PrimaryAction}"
5055
Text="Show Alert 2" />
5156
<Button
52-
AutomationId="Prompt"
53-
Clicked="OnBtnClicked"
57+
Command="{Binding GenericDialogCommand}"
58+
CommandParameter="Prompt"
5459
SemanticProperties.Hint="Shows a prompt dialog"
5560
Style="{StaticResource PrimaryAction}"
5661
Text="Show Prompt" />
5762
<Button
58-
AutomationId="ActionSheet"
59-
Clicked="OnBtnClicked"
63+
Command="{Binding GenericDialogCommand}"
64+
CommandParameter="ActionSheet"
6065
SemanticProperties.Hint="Shows an action sheet"
6166
Style="{StaticResource PrimaryAction}"
6267
Text="Show ActionSheet" />
6368
<Button
64-
AutomationId="Static"
65-
Clicked="OnBtnClicked"
69+
Command="{Binding GenericDialogCommand}"
70+
CommandParameter="Static"
6671
SemanticProperties.Hint="Shows an action sheet"
6772
Style="{StaticResource PrimaryAction}"
6873
Text="Static Approach" />
@@ -76,32 +81,32 @@
7681
HorizontalOptions="Center"
7782
Spacing="15">
7883
<Button
79-
AutomationId="Alert1"
80-
Clicked="OnMauiBtnClicked"
84+
Command="{Binding MauiDialogCommand}"
85+
CommandParameter="Alert1"
8186
SemanticProperties.Hint="Shows an alert dialog"
8287
Style="{StaticResource PrimaryAction}"
8388
Text="Show Alert" />
8489
<Button
85-
AutomationId="Alert2"
86-
Clicked="OnMauiBtnClicked"
90+
Command="{Binding MauiDialogCommand}"
91+
CommandParameter="Alert2"
8792
SemanticProperties.Hint="Shows an alert dialog with accept and cancel actions"
8893
Style="{StaticResource PrimaryAction}"
8994
Text="Show Alert 2" />
9095
<Button
91-
AutomationId="Prompt"
92-
Clicked="OnMauiBtnClicked"
96+
Command="{Binding MauiDialogCommand}"
97+
CommandParameter="Prompt"
9398
SemanticProperties.Hint="Shows a prompt dialog"
9499
Style="{StaticResource PrimaryAction}"
95100
Text="Show Prompt" />
96101
<Button
97-
AutomationId="ActionSheet"
98-
Clicked="OnMauiBtnClicked"
102+
Command="{Binding MauiDialogCommand}"
103+
CommandParameter="ActionSheet"
99104
SemanticProperties.Hint="Shows an action sheet"
100105
Style="{StaticResource PrimaryAction}"
101106
Text="Show ActionSheet" />
102107
<Button
103-
AutomationId="Static"
104-
Clicked="OnMauiBtnClicked"
108+
Command="{Binding MauiDialogCommand}"
109+
CommandParameter="Static"
105110
SemanticProperties.Hint="Shows an action sheet"
106111
Style="{StaticResource PrimaryAction}"
107112
Text="Static Approach" />
@@ -116,4 +121,4 @@
116121

117122
</VerticalStackLayout>
118123
</ScrollView>
119-
</ContentPage>
124+
</pages:MauiPage>
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace PopupDialogs.Views;
2+
3+
public partial class MainPage : MauiPage<MainViewModel>
4+
{
5+
public MainPage() => InitializeComponent();
6+
}

0 commit comments

Comments
 (0)