-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
Copy pathBlazoredToast.razor.cs
94 lines (79 loc) · 2.65 KB
/
BlazoredToast.razor.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
using Blazored.Toast.Configuration;
using Blazored.Toast.Services;
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Web;
using System.Diagnostics;
namespace Blazored.Toast;
public partial class BlazoredToast : IDisposable
{
[CascadingParameter] private BlazoredToasts ToastsContainer { get; set; } = default!;
[Parameter, EditorRequired] public Guid ToastId { get; set; }
[Parameter, EditorRequired] public ToastSettings Settings { get; set; } = default!;
[Parameter] public ToastLevel? Level { get; set; }
[Parameter] public RenderFragment? Message { get; set; }
[Parameter] public RenderFragment? ChildContent { get; set; }
private RenderFragment? CloseButtonContent => ToastsContainer.CloseButtonContent;
private CountdownTimer? _countdownTimer;
private int _progress = 100;
protected override async Task OnInitializedAsync()
{
if (Settings.DisableTimeout ?? false)
{
return;
}
if (Settings.ShowProgressBar!.Value)
{
_countdownTimer = new CountdownTimer(Settings.Timeout, Settings.ExtendedTimeout!.Value)
.OnTick(CalculateProgressAsync)
.OnElapsed(Close);
}
else
{
_countdownTimer = new CountdownTimer(Settings.Timeout, Settings.ExtendedTimeout!.Value)
.OnElapsed(Close);
}
await _countdownTimer.StartAsync();
}
/// <summary>
/// Closes the toast
/// </summary>
public void Close()
=> ToastsContainer.RemoveToast(ToastId);
private void TryPauseCountdown()
{
if (Settings.PauseProgressOnHover!.Value)
{
Settings.ShowProgressBar= false;
_countdownTimer?.Pause();
}
}
private void TryResumeCountdown()
{
if (Settings.PauseProgressOnHover!.Value )
{
Settings.ShowProgressBar = true;
_countdownTimer?.UnPause();
}
}
private async Task CalculateProgressAsync(int percentComplete)
{
_progress = 100 - percentComplete;
await InvokeAsync(StateHasChanged);
}
private void ToastClick()
=> Settings.OnClick?.Invoke();
private bool ShowIconDiv()
=> Settings.IconType switch
{
IconType.None => false,
IconType.Blazored => true,
IconType.FontAwesome => !string.IsNullOrWhiteSpace(Settings.Icon),
IconType.Material => !string.IsNullOrWhiteSpace(Settings.Icon),
_ => false
};
public void Dispose()
{
_countdownTimer?.Dispose();
_countdownTimer = null;
}
}