Skip to content

Commit

Permalink
Sample uses injected IUserDialogs
Browse files Browse the repository at this point in the history
Replaced static instance access with injected interface, much cleaner!
  • Loading branch information
Axemasta committed Sep 4, 2023
1 parent d65c9e5 commit b766290
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
40 changes: 22 additions & 18 deletions Sample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,36 @@ namespace Sample
{
public partial class MainPage : ContentPage
{
public MainPage()
private readonly IUserDialogs _userDialogs;

public MainPage(IUserDialogs userDialogs)
{
_userDialogs = userDialogs;

InitializeComponent();
}

private void Button_Clicked(object sender, EventArgs e)
{
UserDialogs.Instance.Alert("This is Alert dialog", "Alert dialog", "Understand", "dotnet_bot.png");
_userDialogs.Alert("This is Alert dialog", "Alert dialog", "Understand", "dotnet_bot.png");
}

private async void Button_Clicked_1(object sender, EventArgs e)
{
await UserDialogs.Instance.AlertAsync("This is asyn Alert dialog", "Async Alert dialog", "Understand", "dotnet_bot.png");
await _userDialogs.AlertAsync("This is asyn Alert dialog", "Async Alert dialog", "Understand", "dotnet_bot.png");
}

private void Button_Clicked_2(object sender, EventArgs e)
{
UserDialogs.Instance.Confirm("This is Confirm dialog", "Confirm dialog", "Understand", "Nope", "dotnet_bot.png", res =>
_userDialogs.Confirm("This is Confirm dialog", "Confirm dialog", "Understand", "Nope", "dotnet_bot.png", res =>
{
});
}

private async void Button_Clicked_3(object sender, EventArgs e)
{
var res = await UserDialogs.Instance.ConfirmAsync("This is Async Confirm dialog", "Async Confirm dialog", "Understand", "Cancel", "dotnet_bot.png");
var res = await _userDialogs.ConfirmAsync("This is Async Confirm dialog", "Async Confirm dialog", "Understand", "Cancel", "dotnet_bot.png");
}

private void Button_Clicked_4(object sender, EventArgs e)
Expand All @@ -52,7 +56,7 @@ private void Button_Clicked_4(object sender, EventArgs e)
}
};

UserDialogs.Instance.ActionSheet(config);
_userDialogs.ActionSheet(config);
#endif
}

Expand All @@ -61,7 +65,7 @@ private async void Button_Clicked_5(object sender, EventArgs e)
#if IOS
UserDialogs.Instance.Alert("Async Action sheet is not supported on ios", "Warning", "Understand", "dotnet_bot.png");
#else
var res = await UserDialogs.Instance.ActionSheetAsync(
var res = await _userDialogs.ActionSheetAsync(
"not supported",
"Async Action sheet",
"Cancel",
Expand Down Expand Up @@ -97,7 +101,7 @@ private void Button_Clicked_6(object sender, EventArgs e)
}
};

UserDialogs.Instance.ActionSheet(config);
_userDialogs.ActionSheet(config);
#endif
}

Expand All @@ -106,7 +110,7 @@ private async void Button_Clicked_7(object sender, EventArgs e)
#if MACCATALYST
UserDialogs.Instance.Alert("Async Bottom Action sheet is not supported on mac catalyst", "Warning", "Understand", "dotnet_bot.png");
#else
var res = await UserDialogs.Instance.ActionSheetAsync(
var res = await _userDialogs.ActionSheetAsync(
"This is Async Bottom Action sheet",
"Async Bottom Action sheet",
"Cancel",
Expand All @@ -123,11 +127,11 @@ private async void Button_Clicked_7(object sender, EventArgs e)

private async void Button_Clicked_8(object sender, EventArgs e)
{
UserDialogs.Instance.ShowLoading("Loading HUD");
_userDialogs.ShowLoading("Loading HUD");
await Task.Delay(3000);
UserDialogs.Instance.HideHud();
_userDialogs.HideHud();

var hudDialog = UserDialogs.Instance.Loading("Another loading HUD");
var hudDialog = _userDialogs.Loading("Another loading HUD");
await Task.Delay(3000);
hudDialog.Update("Previous loading but with action", -1, null, "Cancel", cancel: () =>
{
Expand All @@ -141,7 +145,7 @@ private async void Button_Clicked_8(object sender, EventArgs e)

private async void Button_Clicked_9(object sender, EventArgs e)
{
var hudDialog = UserDialogs.Instance.Progress("Progress HUD");
var hudDialog = _userDialogs.Progress("Progress HUD");
await Task.Delay(3000);

for (int i = 0; i < 100; i++)
Expand All @@ -159,11 +163,11 @@ private async void Button_Clicked_9(object sender, EventArgs e)

private async void Button_Clicked_10(object sender, EventArgs e)
{
var hudDialog = UserDialogs.Instance.ShowHudImage("dotnet_bot.png", "Image HUD");
var hudDialog = _userDialogs.ShowHudImage("dotnet_bot.png", "Image HUD");
await Task.Delay(3000);
hudDialog.Dispose();

hudDialog = UserDialogs.Instance.ShowHudImage("dotnet_bot.png", "Another Image HUD");
hudDialog = _userDialogs.ShowHudImage("dotnet_bot.png", "Another Image HUD");
#if ANDROID
await Task.Delay(3000);
hudDialog.Update("Previous Image but with action", -1, "dotnet_bot.png", "Cancel", cancel: () =>
Expand All @@ -179,7 +183,7 @@ private async void Button_Clicked_10(object sender, EventArgs e)

private void Button_Clicked_11(object sender, EventArgs e)
{
UserDialogs.Instance.ShowToast(new ToastConfig()
_userDialogs.ShowToast(new ToastConfig()
{
Icon = "dotnet_bot.png",
Message = "This is toast notification"
Expand All @@ -188,7 +192,7 @@ private void Button_Clicked_11(object sender, EventArgs e)

private void Button_Clicked_12(object sender, EventArgs e)
{
UserDialogs.Instance.ShowSnackbar(new SnackbarConfig()
_userDialogs.ShowSnackbar(new SnackbarConfig()
{
Icon = "dotnet_bot.png",
Message = "This is snackbar",
Expand All @@ -204,7 +208,7 @@ private void Button_Clicked_12(object sender, EventArgs e)

private async void Button_Clicked_13(object sender, EventArgs e)
{
var res = await UserDialogs.Instance.ShowSnackbarAsync(new SnackbarConfig()
var res = await _userDialogs.ShowSnackbarAsync(new SnackbarConfig()
{
Icon = "dotnet_bot.png",
Message = "This is a Snackbar",
Expand Down
2 changes: 2 additions & 0 deletions Sample/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public static MauiApp CreateMauiApp()
builder.Logging.AddDebug();
#endif

builder.Services.AddTransient<MainPage>();

return builder.Build();
}
}
Expand Down

0 comments on commit b766290

Please sign in to comment.