@page "/User"
@implements IDisposable
@if (_isInitialized) {
User (@WhoAmI.UserId#@WhoAmI.DeviceId[..5])
Manage devices
Raise alarm:
Fall
Toilet
Clear
Budget: @Math.Round(Budget.Amount, 2)
Spend
EUR at
for
Spend
Spend history is comming soon!
@* oops, forgot to use the DTO for getting own budget... *@
@* @foreach(var history in Budget.History) { *@
@* *@
@* @history.CreatedAt!.Value.ToLocalTime() - *@
@* @history.Amount EUR - *@
@* @history.Reason - *@
@* @history.Venue *@
@*
*@
@* } *@
}
@if (Alarm != null) {
Alarm ID: @Alarm.Id
Reason: @Alarm.Reason
Raised at: @Alarm.CreatedAt.ToLocalTime()
}
@code {
bool _isInitialized = false;
bool _running = true;
private WhoAmI WhoAmI { get; set; } = null!;
private AlarmDto? Alarm { get; set; } = null!;
private BudgetHistoryEntry ToSpend { get; set; } = new() {
Amount = 0,
Reason = string.Empty,
Venue = string.Empty
};
protected override async Task OnInitializedAsync() {
WhoAmI = await App.UserClient.WhoAmI();
_ = PollAlarm();
Budget = await App.UserClient.GetBudget();
NavigationManager.LocationChanged += (sender, args) => {
if (args.Location != NavigationManager.Uri) {
_running = false;
}
};
_isInitialized = true;
}
private async Task PollAlarm() {
while (_running) {
var newAlarm = await App.UserClient.GetAlarm();
if (Alarm?.Id != newAlarm?.Id) {
Alarm = newAlarm;
StateHasChanged();
}
var newBudget = await App.UserClient.GetBudget();
if (Math.Abs(Budget.Amount - newBudget.Amount) > 0.01) {
Budget = newBudget;
StateHasChanged();
}
await Task.Delay(1000);
}
}
public CurrentBalance Budget { get; set; }
private async Task RaiseAlarm(string? reason) {
if (string.IsNullOrWhiteSpace(reason))
await App.UserClient.DeleteAlarm();
else
await App.UserClient.SetAlarm(new() { Reason = reason });
}
private void ReleaseUnmanagedResources() {
_running = false;
}
public void Dispose() {
ReleaseUnmanagedResources();
GC.SuppressFinalize(this);
}
~User() => ReleaseUnmanagedResources();
private async Task SpendBudget(BudgetHistoryEntry toSpend) {
await App.UserClient.SpendBudget(toSpend);
}
}