-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNotifications.razor
More file actions
117 lines (105 loc) · 4.16 KB
/
Notifications.razor
File metadata and controls
117 lines (105 loc) · 4.16 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
@using System.Text.Json
@using CodeCasa.AutoGenerated
@using CodeCasa.CustomEntities.Core.Notifications
@using CodeCasa.Dashboard.Resolvers
@using CodeCasa.Dashboard.ViewModels
@using global::NetDaemon.HassModel
@using global::NetDaemon.HassModel.Entities
@inject IHaContext HaContext
@inject InputSelectEntities InputSelectEntities
@if (_notifications.Any())
{
@for (var i = 0; i < _notifications.Length; i++)
{
var index = i;
var notificationVm = _notifications[index];
var notification = notificationVm.Notification;
var icon = notification.Icon == null ? null : MudIcons.TryResolveMudIcon(notification.Icon);
<MudButton FullWidth="true"
Variant="Variant.Filled"
Style="background-color: rgba(66, 66, 66, 0.3); border-radius: 12px;"
OnClick="() => NotificationClicked(index)">
<MudStack Row AlignItems="AlignItems.Center" Style="justify-content: flex-start; width: 100%;" Class="ma-2">
@if (icon != null)
{
<MudIcon Icon="@icon" Class="mr-2"/>
}
<MudStack Spacing="0" AlignItems="AlignItems.Start">
<MudText Typo="Typo.body1">@notification.Message</MudText>
@if (!string.IsNullOrEmpty(notification.SecondaryMessage))
{
<MudText Typo="Typo.body2">@notification.SecondaryMessage</MudText>
}
</MudStack>
<MudSpacer/>
@if (notificationVm.TimeAgoMessage != null)
{
<MudStack AlignItems="AlignItems.Start">
<MudStack Row AlignItems="AlignItems.Center">
<MudIcon Icon="@Icons.Material.Filled.AccessTime"
Style="font-size:18px;"
Class="mt-n1 mr-n2"/>
<MudText Typo="Typo.caption" style="white-space: nowrap;">@notificationVm.TimeAgoMessage</MudText>
</MudStack>
<MudSpacer/>
</MudStack>
}
</MudStack>
</MudButton>
}
}
@code {
private readonly System.Timers.Timer _uiTimer = new(1000);
private NotificationVm[] _notifications = [];
protected override void OnInitialized()
{
InputSelectEntities.LivingRoomPanelNotifications.StateAllChangesWithCurrent().Subscribe(stateChange =>
{
if (stateChange.New?.Attributes?.Options == null || string.IsNullOrEmpty(stateChange.New.Attributes.Options.FirstOrDefault()))
{
_notifications = [];
InvokeAsync(StateHasChanged);
return;
}
var jsonOptions = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true
};
_notifications = stateChange.New.Attributes.Options
.Select(o => JsonSerializer.Deserialize<LivingRoomPanelNotification>(o, jsonOptions))
.Where(n => n != null)
.Select(n => n!)
.Select(n => new NotificationVm(n)).ToArray();
InvokeAsync(StateHasChanged);
});
_uiTimer.Elapsed += (_, __) =>
{
if (!_notifications.Any())
{
return;
}
InvokeAsync(() =>
{
if (!_notifications.Any())
{
return;
}
var stateHasChanged = false;
foreach (var notification in _notifications)
{
stateHasChanged |= notification.Update();
}
if (!stateHasChanged)
{
return;
}
StateHasChanged();
});
};
_uiTimer.Start();
}
private void NotificationClicked(int index)
{
HaContext.SendEvent("notification_clicked", new { notificationEntity = InputSelectEntities.LivingRoomPanelNotifications.EntityId, notificationIndex = index });
}
}