From b0032bd8659fd0186e292b5f4205cc5ec457c2e0 Mon Sep 17 00:00:00 2001 From: Jonathan McCaffrey Date: Tue, 12 Apr 2022 03:03:20 -0400 Subject: [PATCH] fix(Toast) Fixing fading. --- Components/Feedback/ToastComponent.razor | 82 +++++++++--------------- IGP/Portals/ToastPortal.razor | 22 ++++++- IGP/Program.cs | 5 -- IGP/_Imports.razor | 1 + Model/Feedback/ToastModel.cs | 10 ++- Services/IServices.cs | 2 + Services/Website/ToastService.cs | 11 +++- 7 files changed, 72 insertions(+), 61 deletions(-) diff --git a/Components/Feedback/ToastComponent.razor b/Components/Feedback/ToastComponent.razor index dbeb029..869b62c 100644 --- a/Components/Feedback/ToastComponent.razor +++ b/Components/Feedback/ToastComponent.razor @@ -1,5 +1,4 @@ -@using Services -@inject IToastService toastService +@inject IToastService toastService @implements IDisposable @@ -9,18 +8,18 @@ } else { -
+
@Toast.Title
-
- @Toast.Message -
-
+
+ @Toast.Message +
+
} @code { - + [Parameter] public ToastModel? Toast { get; set; } = default!; - private bool isFadingOut = false; + private float removalTime = 1300; + private float fadeoutTime = 1200; - private string FadeoutStyle => isFadingOut ? "fadeout" : ""; + private float Opacity() + { + if (Toast!.Age < fadeoutTime) + { + return 1; + } - private int removalTime = 150000; - private int fadeoutTime = 1000; + return 1.0f - (Toast.Age - fadeoutTime) / (removalTime - fadeoutTime); + } - private Timer removalTimer = null!; - private Timer fadeoutTimer = null!; + int elapsed = 0; protected override void OnInitialized() { -#if DEBUG - removalTime = 8000; -#endif - - removalTimer = new Timer(removalTime); - removalTimer.Elapsed += OnRemoval!; - removalTimer.Enabled = true; - - fadeoutTimer = new Timer(removalTime - fadeoutTime); - fadeoutTimer.Elapsed += OnFadeout!; - fadeoutTimer.Enabled = true; - - toastService.Subscribe(StateHasChanged); - } - - void OnFadeout(object source, ElapsedEventArgs eventArgs) - { - // isFadingOut = true; - - StateHasChanged(); - } - - void OnRemoval(object source, ElapsedEventArgs eventArgs) - { - //toastService.RemoveToast(Toast!); - - StateHasChanged(); + toastService.Subscribe(OnUpdate); } void Dismiss() @@ -116,9 +88,15 @@ else public void Dispose() { - removalTimer.Elapsed -= OnRemoval!; - fadeoutTimer.Elapsed -= OnFadeout!; - - toastService.Unsubscribe(StateHasChanged); + toastService.Unsubscribe(OnUpdate); } + + void OnUpdate() + { + if (Toast!.Age > removalTime) + { + toastService.RemoveToast(Toast); + } + } + } \ No newline at end of file diff --git a/IGP/Portals/ToastPortal.razor b/IGP/Portals/ToastPortal.razor index aa43dc3..22a579b 100644 --- a/IGP/Portals/ToastPortal.razor +++ b/IGP/Portals/ToastPortal.razor @@ -5,8 +5,9 @@ @if (toastService.HasToasts()) {
- @foreach( var toast in toastService.GetToasts()) + @foreach(var toast in Toasts) { + }
@@ -19,21 +20,38 @@ right: 64px; display: flex; flex-direction: column; - gap: 8px; + gap: 5px; } @code { + private List Toasts => toastService.GetToasts(); + + private Timer ageTimer = null!; + protected override void OnInitialized() { toastService.Subscribe(OnUpdate); + + ageTimer = new Timer(10); + ageTimer.Elapsed += OnAge!; + ageTimer.Enabled = true; + } public void Dispose() { toastService.Unsubscribe(OnUpdate); } + + + void OnAge(object? sender, ElapsedEventArgs elapsedEventArgs) + { + toastService.AgeToasts(); + ageTimer.Enabled = true; + } + void OnUpdate() { diff --git a/IGP/Program.cs b/IGP/Program.cs index 3af9c8f..c29c579 100644 --- a/IGP/Program.cs +++ b/IGP/Program.cs @@ -6,11 +6,6 @@ using Services.Development; using Services.Immortal; using Services.Website; -#if NO_SQL -#else -using Contexts; -using Microsoft.EntityFrameworkCore; -#endif CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US"); CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US"); diff --git a/IGP/_Imports.razor b/IGP/_Imports.razor index 50122f8..d31c9e3 100644 --- a/IGP/_Imports.razor +++ b/IGP/_Imports.razor @@ -26,6 +26,7 @@ @using IGP.Pages.RoadMap.Parts @using IGP.Pages.Notes @using IGP.Pages.Notes.Parts +@using System.Timers @using IGP.Portals @using Markdig @using Model.Feedback diff --git a/Model/Feedback/ToastModel.cs b/Model/Feedback/ToastModel.cs index 04bd9f5..a0e3f07 100644 --- a/Model/Feedback/ToastModel.cs +++ b/Model/Feedback/ToastModel.cs @@ -1,8 +1,16 @@ -namespace Model.Feedback; + +using System.Timers; + +namespace Model.Feedback; public class ToastModel { public string Title { get; set; } = "addTitle"; public string Message { get; set; } = "addMessage"; public string SeverityType { get; set; } = "addType"; + + public float Age { get; set; } = 0; + + + } \ No newline at end of file diff --git a/Services/IServices.cs b/Services/IServices.cs index 3d68215..7d19d21 100644 --- a/Services/IServices.cs +++ b/Services/IServices.cs @@ -21,6 +21,7 @@ using Services.Immortal; namespace Services; + public interface IToastService { public void Subscribe(Action action); @@ -29,6 +30,7 @@ public interface IToastService void RemoveToast(ToastModel toast); bool HasToasts(); List GetToasts(); + void AgeToasts(); void ClearAllToasts(); } diff --git a/Services/Website/ToastService.cs b/Services/Website/ToastService.cs index f6c1f8f..15ea94c 100644 --- a/Services/Website/ToastService.cs +++ b/Services/Website/ToastService.cs @@ -41,7 +41,6 @@ public class ToastService : IToastService public void RemoveToast(ToastModel toast) { toasts.Remove(toast); - NotifyDataChanged(); } public bool HasToasts() @@ -54,6 +53,16 @@ public class ToastService : IToastService return toasts; } + public void AgeToasts() + { + foreach (var toast in toasts) + { + toast.Age++; + } + + NotifyDataChanged(); + } + public void ClearAllToasts() { toasts.Clear();