Skip to content

Commit

Permalink
Merge pull request #67 from egvijayanand/working
Browse files Browse the repository at this point in the history
PopupDialogs updated as MVVM sample
  • Loading branch information
egvijayanand authored Jan 3, 2023
2 parents 7e571cf + c001437 commit 28e2efe
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 152 deletions.
9 changes: 9 additions & 0 deletions src/PopupDialogs/Imports.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
global using CommunityToolkit.Mvvm.Input;

global using VijayAnand.MauiToolkit;
global using VijayAnand.MauiToolkit.Core;
global using VijayAnand.MauiToolkit.Core.Services;
global using VijayAnand.MauiToolkit.Core.ViewModels;
global using VijayAnand.MauiToolkit.Pro;
global using VijayAnand.MauiToolkit.Pro.Services;
global using VijayAnand.MauiToolkit.Views;

// Local usings
global using PopupDialogs.ViewModels;
global using PopupDialogs.Views;

// Static
global using static Microsoft.Maui.Graphics.Colors;
128 changes: 0 additions & 128 deletions src/PopupDialogs/MainPage.xaml.cs

This file was deleted.

1 change: 1 addition & 0 deletions src/PopupDialogs/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static MauiApp CreateMauiApp()
})
.ConfigureServices(services =>
{
services.AddSingleton<MainViewModel>();
services.AddSingleton<MainPage>();
});
#if DEBUG
Expand Down
1 change: 1 addition & 0 deletions src/PopupDialogs/PopupDialogs.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
<PackageReference Include="CommunityToolkit.Maui" Version="3.1.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.0.0" />
<PackageReference Include="VijayAnand.MauiToolkit.Pro" Version="2.1.0-preview.4" />
</ItemGroup>

Expand Down
119 changes: 119 additions & 0 deletions src/PopupDialogs/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
using VijayAnand.MauiToolkit.Services;

namespace PopupDialogs.ViewModels
{
public partial class MainViewModel : BaseViewModel
{
private readonly IDialogService _genericDialog;
private readonly IMauiDialogService _mauiDialog;

public MainViewModel(IDialogService genericDialog, IMauiDialogService mauiDialog)
=> (_genericDialog, _mauiDialog) = (genericDialog, mauiDialog);

[RelayCommand]
private async Task GenericDialog(string arg)
{
// DI approach
// Switch betwen native/custom implementations in the MauiProgram.cs
// The order in which dependencies are registered determines the instance injected
// The latest one wins

switch (arg)
{
case "Alert1":
await _genericDialog.DisplayAlertAsync("Greeting", "Hello from .NET MAUI!!!", "OK");
break;
case "Alert2":
var response = await _genericDialog.DisplayAlertAsync("Confirm", "Would you like to proceed?", "Yes", "No");
var message = response is true ? "Glad that you opted in." : "Sorry to miss you, hope to see you soon.";
await _genericDialog.DisplayAlertAsync("Response", message, "OK");
break;
case "Prompt":
var result = await _genericDialog.DisplayPromptAsync("Subscribe", "Enter your email to send notifications.", maxLength: 100, inputType: InputType.Email);

if (!string.IsNullOrWhiteSpace(result))
{
await _genericDialog.DisplayAlertAsync("Response", result, "OK");
}
break;
case "ActionSheet":
var action = await _genericDialog.DisplayActionSheetAsync("Unsaved Changes ...", "What would you like to do?", "Cancel", "Discard", "Save", "Save", "Upload", "Share");
await _genericDialog.DisplayAlertAsync("Response", action, "OK");
break;
default:
// Static approach for custom implementation
// Static approach for native dialogs soon in an upcoming preview
await PopupDialog.Instance.DisplayAlertAsync("Static Invocation", "Welcome to .NET MAUI!!!", "OK");
break;
}
}

[RelayCommand]
private async Task MauiDialog(string arg)
{
// DI approach
// Switch betwen native/custom implementations in the MauiProgram.cs
// The order in which dependencies are registered determines the instance injected
// The latest one wins

var rightToLeft = FlowDirection.RightToLeft;

switch (arg)
{
case "Alert1":
await _mauiDialog.DisplayAlertAsync("Greeting", "Hello from .NET MAUI!!!", "OK", rightToLeft);
break;
case "Alert2":
var response = await _mauiDialog.DisplayAlertAsync("Confirm", "Would you like to proceed?", "Yes", "No", rightToLeft);
var message = response is true ? "Glad that you opted in." : "Sorry to miss you, hope to see you soon.";
await _mauiDialog.DisplayAlertAsync("Response", message, "OK", rightToLeft);
break;
case "Prompt":
var result = await _mauiDialog.DisplayPromptAsync("Lucky Draw", "Enter your age to participate.", maxLength: 3, inputType: InputType.Numeric, predicate: ValidateUserEntry);

if (!string.IsNullOrWhiteSpace(result))
{
await _mauiDialog.DisplayAlertAsync("Response", result, "OK");
}
break;
case "ActionSheet":
var action = await _mauiDialog.DisplayActionSheetAsync("Unsaved Changes ..."/*, "What would you like to do?"*/, "Cancel", "Discard", rightToLeft, "Save", "Upload", "Share");
await _mauiDialog.DisplayAlertAsync("Response", action, "OK", rightToLeft);
break;
default:
// Static approach for custom implementation
// Static approach for native dialogs soon in an upcoming preview
await MauiPopupDialog.Instance.DisplayAlertAsync("Static Invocation", "Welcome to .NET MAUI!!!", "OK", rightToLeft);
break;
}
}

private (bool, string) ValidateUserEntry(string value)
{
if (string.IsNullOrWhiteSpace(value))
{
return (false, "Age is required.");
}

if (int.TryParse(value, out int age))
{
if (age < 0 || age > 120)
{
return (false, "Enter a valid age.");
}
else if (age >= 18)
{
return (true, string.Empty);
}
else
{
return (false, "You should be atleast 18 years old to participate.");
}
}
else
{
return (false, "Enter a valid age.");
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="PopupDialogs.MainPage">
<pages:MauiPage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:pages="clr-namespace:VijayAnand.MauiToolkit.Views;assembly=VijayAnand.MauiToolkit"
xmlns:vm="clr-namespace:PopupDialogs.ViewModels"
x:Class="PopupDialogs.Views.MainPage"
x:DataType="vm:MainViewModel"
x:TypeArguments="vm:MainViewModel">

<ScrollView>
<VerticalStackLayout Padding="30" Spacing="15">
Expand Down Expand Up @@ -37,32 +42,32 @@
HorizontalOptions="Center"
Spacing="15">
<Button
AutomationId="Alert1"
Clicked="OnBtnClicked"
Command="{Binding GenericDialogCommand}"
CommandParameter="Alert1"
SemanticProperties.Hint="Shows an alert dialog"
Style="{StaticResource PrimaryAction}"
Text="Show Alert" />
<Button
AutomationId="Alert2"
Clicked="OnBtnClicked"
Command="{Binding GenericDialogCommand}"
CommandParameter="Alert2"
SemanticProperties.Hint="Shows an alert dialog with accept and cancel actions"
Style="{StaticResource PrimaryAction}"
Text="Show Alert 2" />
<Button
AutomationId="Prompt"
Clicked="OnBtnClicked"
Command="{Binding GenericDialogCommand}"
CommandParameter="Prompt"
SemanticProperties.Hint="Shows a prompt dialog"
Style="{StaticResource PrimaryAction}"
Text="Show Prompt" />
<Button
AutomationId="ActionSheet"
Clicked="OnBtnClicked"
Command="{Binding GenericDialogCommand}"
CommandParameter="ActionSheet"
SemanticProperties.Hint="Shows an action sheet"
Style="{StaticResource PrimaryAction}"
Text="Show ActionSheet" />
<Button
AutomationId="Static"
Clicked="OnBtnClicked"
Command="{Binding GenericDialogCommand}"
CommandParameter="Static"
SemanticProperties.Hint="Shows an action sheet"
Style="{StaticResource PrimaryAction}"
Text="Static Approach" />
Expand All @@ -76,32 +81,32 @@
HorizontalOptions="Center"
Spacing="15">
<Button
AutomationId="Alert1"
Clicked="OnMauiBtnClicked"
Command="{Binding MauiDialogCommand}"
CommandParameter="Alert1"
SemanticProperties.Hint="Shows an alert dialog"
Style="{StaticResource PrimaryAction}"
Text="Show Alert" />
<Button
AutomationId="Alert2"
Clicked="OnMauiBtnClicked"
Command="{Binding MauiDialogCommand}"
CommandParameter="Alert2"
SemanticProperties.Hint="Shows an alert dialog with accept and cancel actions"
Style="{StaticResource PrimaryAction}"
Text="Show Alert 2" />
<Button
AutomationId="Prompt"
Clicked="OnMauiBtnClicked"
Command="{Binding MauiDialogCommand}"
CommandParameter="Prompt"
SemanticProperties.Hint="Shows a prompt dialog"
Style="{StaticResource PrimaryAction}"
Text="Show Prompt" />
<Button
AutomationId="ActionSheet"
Clicked="OnMauiBtnClicked"
Command="{Binding MauiDialogCommand}"
CommandParameter="ActionSheet"
SemanticProperties.Hint="Shows an action sheet"
Style="{StaticResource PrimaryAction}"
Text="Show ActionSheet" />
<Button
AutomationId="Static"
Clicked="OnMauiBtnClicked"
Command="{Binding MauiDialogCommand}"
CommandParameter="Static"
SemanticProperties.Hint="Shows an action sheet"
Style="{StaticResource PrimaryAction}"
Text="Static Approach" />
Expand All @@ -116,4 +121,4 @@

</VerticalStackLayout>
</ScrollView>
</ContentPage>
</pages:MauiPage>
6 changes: 6 additions & 0 deletions src/PopupDialogs/Views/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace PopupDialogs.Views;

public partial class MainPage : MauiPage<MainViewModel>
{
public MainPage() => InitializeComponent();
}

0 comments on commit 28e2efe

Please sign in to comment.