fix(Toast) Fixing fading.

This commit is contained in:
2022-04-12 03:03:20 -04:00
parent 1939bbe70a
commit b0032bd865
7 changed files with 72 additions and 61 deletions
+23 -45
View File
@@ -1,5 +1,4 @@
@using Services @inject IToastService toastService
@inject IToastService toastService
@implements IDisposable @implements IDisposable
@@ -9,7 +8,7 @@
} }
else else
{ {
<div onclick="@Dismiss" class="toastContainer @FadeoutStyle @Toast.SeverityType.ToLower()"> <div onclick="@Dismiss" style="opacity: @Opacity()" class="toastContainer @Toast.SeverityType.ToLower()">
<div class="toastTitle"> <div class="toastTitle">
@Toast.Title @Toast.Title
</div> </div>
@@ -28,15 +27,9 @@ else
flex-direction: column; flex-direction: column;
justify-items: stretch; justify-items: stretch;
width: 250px; width: 250px;
opacity: 1;
cursor: pointer; cursor: pointer;
} }
.fadeout {
transition: opacity 3s ease-in;
opacity: 0;
}
.@SeverityType.Warning.ToLower() { .@SeverityType.Warning.ToLower() {
background-color: var(--severity-warning-color); background-color: var(--severity-warning-color);
border-color: var(--severity-warning-border-color); border-color: var(--severity-warning-border-color);
@@ -68,45 +61,24 @@ else
[Parameter] [Parameter]
public ToastModel? Toast { get; set; } = default!; 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; return 1.0f - (Toast.Age - fadeoutTime) / (removalTime - fadeoutTime);
private int fadeoutTime = 1000; }
private Timer removalTimer = null!; int elapsed = 0;
private Timer fadeoutTimer = null!;
protected override void OnInitialized() protected override void OnInitialized()
{ {
#if DEBUG toastService.Subscribe(OnUpdate);
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();
} }
void Dismiss() void Dismiss()
@@ -116,9 +88,15 @@ else
public void Dispose() public void Dispose()
{ {
removalTimer.Elapsed -= OnRemoval!; toastService.Unsubscribe(OnUpdate);
fadeoutTimer.Elapsed -= OnFadeout!; }
toastService.Unsubscribe(StateHasChanged); void OnUpdate()
{
if (Toast!.Age > removalTime)
{
toastService.RemoveToast(Toast);
} }
} }
}
+20 -2
View File
@@ -5,8 +5,9 @@
@if (toastService.HasToasts()) @if (toastService.HasToasts())
{ {
<div class="toastsContainer"> <div class="toastsContainer">
@foreach( var toast in toastService.GetToasts()) @foreach(var toast in Toasts)
{ {
<ToastComponent Toast="toast"/> <ToastComponent Toast="toast"/>
} }
</div> </div>
@@ -19,15 +20,24 @@
right: 64px; right: 64px;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
gap: 8px; gap: 5px;
} }
</style> </style>
@code { @code {
private List<ToastModel> Toasts => toastService.GetToasts();
private Timer ageTimer = null!;
protected override void OnInitialized() protected override void OnInitialized()
{ {
toastService.Subscribe(OnUpdate); toastService.Subscribe(OnUpdate);
ageTimer = new Timer(10);
ageTimer.Elapsed += OnAge!;
ageTimer.Enabled = true;
} }
public void Dispose() public void Dispose()
@@ -35,6 +45,14 @@
toastService.Unsubscribe(OnUpdate); toastService.Unsubscribe(OnUpdate);
} }
void OnAge(object? sender, ElapsedEventArgs elapsedEventArgs)
{
toastService.AgeToasts();
ageTimer.Enabled = true;
}
void OnUpdate() void OnUpdate()
{ {
StateHasChanged(); StateHasChanged();
-5
View File
@@ -6,11 +6,6 @@ using Services.Development;
using Services.Immortal; using Services.Immortal;
using Services.Website; using Services.Website;
#if NO_SQL
#else
using Contexts;
using Microsoft.EntityFrameworkCore;
#endif
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US"); CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US"); CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US");
+1
View File
@@ -26,6 +26,7 @@
@using IGP.Pages.RoadMap.Parts @using IGP.Pages.RoadMap.Parts
@using IGP.Pages.Notes @using IGP.Pages.Notes
@using IGP.Pages.Notes.Parts @using IGP.Pages.Notes.Parts
@using System.Timers
@using IGP.Portals @using IGP.Portals
@using Markdig @using Markdig
@using Model.Feedback @using Model.Feedback
+9 -1
View File
@@ -1,8 +1,16 @@
namespace Model.Feedback;
using System.Timers;
namespace Model.Feedback;
public class ToastModel public class ToastModel
{ {
public string Title { get; set; } = "addTitle"; public string Title { get; set; } = "addTitle";
public string Message { get; set; } = "addMessage"; public string Message { get; set; } = "addMessage";
public string SeverityType { get; set; } = "addType"; public string SeverityType { get; set; } = "addType";
public float Age { get; set; } = 0;
} }
+2
View File
@@ -21,6 +21,7 @@ using Services.Immortal;
namespace Services; namespace Services;
public interface IToastService public interface IToastService
{ {
public void Subscribe(Action action); public void Subscribe(Action action);
@@ -29,6 +30,7 @@ public interface IToastService
void RemoveToast(ToastModel toast); void RemoveToast(ToastModel toast);
bool HasToasts(); bool HasToasts();
List<ToastModel> GetToasts(); List<ToastModel> GetToasts();
void AgeToasts();
void ClearAllToasts(); void ClearAllToasts();
} }
+10 -1
View File
@@ -41,7 +41,6 @@ public class ToastService : IToastService
public void RemoveToast(ToastModel toast) public void RemoveToast(ToastModel toast)
{ {
toasts.Remove(toast); toasts.Remove(toast);
NotifyDataChanged();
} }
public bool HasToasts() public bool HasToasts()
@@ -54,6 +53,16 @@ public class ToastService : IToastService
return toasts; return toasts;
} }
public void AgeToasts()
{
foreach (var toast in toasts)
{
toast.Age++;
}
NotifyDataChanged();
}
public void ClearAllToasts() public void ClearAllToasts()
{ {
toasts.Clear(); toasts.Clear();