-
Notifications
You must be signed in to change notification settings - Fork 350
/
SnackbarPage.xaml.cs
124 lines (105 loc) · 3.93 KB
/
SnackbarPage.xaml.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
using System.Reflection;
using CommunityToolkit.Maui.Alerts;
using CommunityToolkit.Maui.Core;
using CommunityToolkit.Maui.Extensions;
using CommunityToolkit.Maui.Markup;
using CommunityToolkit.Maui.Sample.Resources.Fonts;
using CommunityToolkit.Maui.Sample.ViewModels.Alerts;
using CommunityToolkit.Mvvm.Input;
using Font = Microsoft.Maui.Font;
namespace CommunityToolkit.Maui.Sample.Pages.Alerts;
public partial class SnackbarPage : BasePage<SnackbarViewModel>
{
const string displayCustomSnackbarText = "Display a Custom Snackbar, Anchored to this Button";
const string dismissCustomSnackbarText = "Dismiss Custom Snackbar";
readonly IReadOnlyList<Color> colors = typeof(Colors)
.GetFields(BindingFlags.Static | BindingFlags.Public)
.ToDictionary(c => c.Name, c => (Color)(c.GetValue(null) ?? throw new InvalidOperationException()))
.Values.ToList();
ISnackbar? customSnackbar;
public SnackbarPage(SnackbarViewModel snackbarViewModel) : base(snackbarViewModel)
{
InitializeComponent();
DisplayCustomSnackbarButton.Text = displayCustomSnackbarText;
Snackbar.Shown += Snackbar_Shown;
Snackbar.Dismissed += Snackbar_Dismissed;
}
async void DisplayDefaultSnackbarButtonClicked(object? sender, EventArgs args) =>
await this.DisplaySnackbar("This is a Snackbar.\nIt will disappear in 3 seconds.\nOr click OK to dismiss immediately");
async void DisplayCustomSnackbarButtonClicked(object? sender, EventArgs args)
{
if (DisplayCustomSnackbarButton.Text is displayCustomSnackbarText)
{
var options = new SnackbarOptions
{
BackgroundColor = Colors.Red,
TextColor = Colors.Green,
ActionButtonTextColor = Colors.Yellow,
CornerRadius = new CornerRadius(10),
Font = Font.SystemFontOfSize(14),
ActionButtonFont = Font.OfSize(FontFamilies.FontAwesomeBrands, 16, enableScaling: false),
};
customSnackbar = Snackbar.Make(
"This is a customized Snackbar",
async () =>
{
await DisplayCustomSnackbarButton.BackgroundColorTo(colors[Random.Shared.Next(colors.Count)], length: 500);
DisplayCustomSnackbarButton.Text = displayCustomSnackbarText;
},
FontAwesomeIcons.Microsoft,
TimeSpan.FromSeconds(30),
options,
DisplayCustomSnackbarButton);
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
await customSnackbar.Show(cts.Token);
DisplayCustomSnackbarButton.Text = dismissCustomSnackbarText;
}
else if (DisplayCustomSnackbarButton.Text is dismissCustomSnackbarText)
{
if (customSnackbar is not null)
{
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(5));
await customSnackbar.Dismiss(cts.Token);
customSnackbar.Dispose();
}
DisplayCustomSnackbarButton.Text = displayCustomSnackbarText;
}
else
{
throw new NotSupportedException($"{nameof(DisplayCustomSnackbarButton)}.{nameof(ITextButton.Text)} Not Recognized");
}
}
void Snackbar_Dismissed(object? sender, EventArgs e)
{
SnackbarShownStatus.Text = $"Snackbar dismissed. Snackbar.IsShown={Snackbar.IsShown}";
}
void Snackbar_Shown(object? sender, EventArgs e)
{
SnackbarShownStatus.Text = $"Snackbar shown. Snackbar.IsShown={Snackbar.IsShown}";
}
async void DisplaySnackbarInModalButtonClicked(object? sender, EventArgs e)
{
if (Application.Current?.MainPage is not null)
{
await Application.Current.MainPage.Navigation.PushModalAsync(new ContentPage
{
Content = new VerticalStackLayout
{
Spacing = 12,
Children =
{
new Button { Command = new AsyncRelayCommand(token => Snackbar.Make("Snackbar in a Modal Page").Show(token)) }
.Top().CenterHorizontal()
.Text("Display Snackbar"),
new Label()
.Center().TextCenter()
.Text("This is a Modal Page"),
new Button { Command = new AsyncRelayCommand(Application.Current.MainPage.Navigation.PopModalAsync) }
.Bottom().CenterHorizontal()
.Text("Back to Snackbar Page")
}
}.Center()
}.Padding(12));
}
}
}