From 6c61fbe1a46030fc874767a4d9041e0de9dddb06 Mon Sep 17 00:00:00 2001 From: Leo TUAILLON Date: Mon, 15 May 2023 11:46:29 +0200 Subject: [PATCH] user-notifier --- .editorconfig | 4 ++ ShoopNCook.sln | 7 +++- UserNotifier.cs | 62 ++++++++++++++++------------ Views/Components/NoticePopup.xaml | 3 +- Views/Components/NoticePopup.xaml.cs | 40 +++++++++++++++--- 5 files changed, 81 insertions(+), 35 deletions(-) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..a0c05f8 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,4 @@ +[*.cs] + +# CS0618: Le type ou le membre est obsolète +dotnet_diagnostic.CS0618.severity = silent diff --git a/ShoopNCook.sln b/ShoopNCook.sln index 3b6c720..17d11be 100644 --- a/ShoopNCook.sln +++ b/ShoopNCook.sln @@ -10,7 +10,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Models", "Models\Models.csp EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LocalServices", "LocalServices\LocalServices.csproj", "{57732316-93B9-4DA0-A212-F8892D3D968B}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Services", "Services\Services.csproj", "{C976BDD8-710D-4162-8A42-973B634491F9}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Services", "Services\Services.csproj", "{C976BDD8-710D-4162-8A42-973B634491F9}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{6DEA92EF-71CD-4A21-9CC0-67F228E1155D}" + ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig + EndProjectSection EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/UserNotifier.cs b/UserNotifier.cs index aa97bf2..6846a0d 100644 --- a/UserNotifier.cs +++ b/UserNotifier.cs @@ -1,4 +1,6 @@ -using CommunityToolkit.Maui.Views; +using CommunityToolkit.Maui.Alerts; +using CommunityToolkit.Maui.Core; +using CommunityToolkit.Maui.Views; using ShoopNCook.Views.Components; using System; using System.Collections.Generic; @@ -8,33 +10,39 @@ using System.Threading.Tasks; namespace ShoopNCook { - internal class UserNotifier - { - - private static void Show(NoticePopup popup) - { - var page = Shell.Current.CurrentPage; - page.ShowPopup(new Popup - { - Content = popup - }); + internal class UserNotifier + { + private static async Task Show(string message, string messageType) + { + CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(); + + // Vous pouvez configurer la durée et la taille de police ici. + ToastDuration duration = ToastDuration.Short; + double fontSize = 14; + + var toast = Toast.Make(message, duration, fontSize); + + await toast.Show(cancellationTokenSource.Token); } - public static void Error(string message) - { - Show(new NoticePopup()); - } - public static void Warn(string message) - { - Show(new NoticePopup()); - } - public static void Notice(string message) - { - Show(new NoticePopup()); - } - public static void Success(string message) - { - Show(new NoticePopup()); - } + + + public static void Error(string message) + { + Show(message, "Error"); + } + public static void Warn(string message) + { + Show(message, "Warning"); + } + public static void Notice(string message) + { + Show(message, "Notice"); + } + public static void Success(string message) + { + Show(message, "Success"); + } } + } diff --git a/Views/Components/NoticePopup.xaml b/Views/Components/NoticePopup.xaml index ab69bae..d4ac661 100644 --- a/Views/Components/NoticePopup.xaml +++ b/Views/Components/NoticePopup.xaml @@ -1,10 +1,9 @@ - diff --git a/Views/Components/NoticePopup.xaml.cs b/Views/Components/NoticePopup.xaml.cs index 1957bc7..700634a 100644 --- a/Views/Components/NoticePopup.xaml.cs +++ b/Views/Components/NoticePopup.xaml.cs @@ -1,9 +1,39 @@ namespace ShoopNCook.Views.Components; +using Microsoft.Maui.Graphics; public partial class NoticePopup : ContentView { - public NoticePopup() - { - InitializeComponent(); - } -} \ No newline at end of file + public NoticePopup(string message, string messageType) + { + + InitializeComponent(); + MessageLabel.Text = message; + + switch (messageType) + { + case "Error": + this.BackgroundColor = Microsoft.Maui.Graphics.Colors.Red; + break; + case "Warning": + this.BackgroundColor = Microsoft.Maui.Graphics.Colors.Yellow; + break; + case "Notice": + this.BackgroundColor = Microsoft.Maui.Graphics.Colors.Blue; + break; + case "Success": + this.BackgroundColor = Microsoft.Maui.Graphics.Colors.Green; + break; + } + + // Display the toast for 3 seconds + + Device.StartTimer(TimeSpan.FromSeconds(3), () => + { + // Close the toast + // You need to replace this with your actual code to close the toast + this.IsVisible = false; + return false; + }); + + } +}