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
+30 -52
View File
@@ -1,5 +1,4 @@
@using Services
@inject IToastService toastService
@inject IToastService toastService
@implements IDisposable
@@ -9,18 +8,18 @@
}
else
{
<div onclick="@Dismiss" class="toastContainer @FadeoutStyle @Toast.SeverityType.ToLower()">
<div onclick="@Dismiss" style="opacity: @Opacity()" class="toastContainer @Toast.SeverityType.ToLower()">
<div class="toastTitle">
@Toast.Title
</div>
<div>
@Toast.Message
</div>
</div>
<div>
@Toast.Message
</div>
</div>
}
<style>
.toastContainer {
.toastContainer {
border: 4px solid;
border-radius: 4px;
padding: 16px;
@@ -28,15 +27,9 @@ else
flex-direction: column;
justify-items: stretch;
width: 250px;
opacity: 1;
cursor: pointer;
}
.fadeout {
transition: opacity 3s ease-in;
opacity: 0;
}
.@SeverityType.Warning.ToLower() {
background-color: var(--severity-warning-color);
border-color: var(--severity-warning-border-color);
@@ -64,49 +57,28 @@ else
</style>
@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);
}
}
}
+20 -2
View File
@@ -5,8 +5,9 @@
@if (toastService.HasToasts())
{
<div class="toastsContainer">
@foreach( var toast in toastService.GetToasts())
@foreach(var toast in Toasts)
{
<ToastComponent Toast="toast"/>
}
</div>
@@ -19,21 +20,38 @@
right: 64px;
display: flex;
flex-direction: column;
gap: 8px;
gap: 5px;
}
</style>
@code {
private List<ToastModel> 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()
{
-5
View File
@@ -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");
+1
View File
@@ -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
+9 -1
View File
@@ -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;
}
+2
View File
@@ -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<ToastModel> GetToasts();
void AgeToasts();
void ClearAllToasts();
}
+10 -1
View File
@@ -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();