2026-10-03 15:26:21 +02:00
|
|
|
@implements IAsyncDisposable
|
|
|
|
|
|
|
|
|
|
@* The app-wide state, cascaded to every page and component (LocalizableComponentBase.CascadingState): the saved
|
|
|
|
|
PublicCacheData with its theme settings, the signed-in user, and whether the app is online, polled every 10 seconds. *@
|
|
|
|
|
<CascadingValue IsFixed=false Value=this>
|
2022-02-14 01:51:52 +01:00
|
|
|
@ChildContent
|
|
|
|
|
</CascadingValue>
|
2022-02-14 01:51:37 +01:00
|
|
|
|
|
|
|
|
@code {
|
2022-02-14 01:51:52 +01:00
|
|
|
[Parameter] public RenderFragment ChildContent { get; set; }
|
|
|
|
|
[Inject] ILocalStorageService Storage { get; set; }
|
|
|
|
|
[Inject] IJSRuntime JS { get; set; }
|
|
|
|
|
[Inject] public AppStatusService Status { get; set; }
|
2026-10-03 15:26:21 +02:00
|
|
|
[Inject] ILoggingService Logger { get; set; }
|
|
|
|
|
[Inject] ToastService Toast { get; set; }
|
2022-02-14 01:51:52 +01:00
|
|
|
|
2026-10-03 15:26:21 +02:00
|
|
|
public bool IsOnline { get; private set; } = true;
|
|
|
|
|
public bool IsOffline => !IsOnline;
|
|
|
|
|
public PublicCacheData PublicCacheData { get; private set; } = new();
|
|
|
|
|
/// <summary>The signed-in user; Faker's until the login is wired to PrivaPub.</summary>
|
|
|
|
|
public User User { get; private set; } = Faker.CurrentUser;
|
2022-02-14 01:51:52 +01:00
|
|
|
|
|
|
|
|
DotNetObjectReference<CascadingState> cascadingStateReference;
|
2026-10-03 15:26:21 +02:00
|
|
|
PeriodicTimer onlineTimer;
|
|
|
|
|
readonly CancellationTokenSource disposeCancellation = new();
|
2022-02-14 01:51:52 +01:00
|
|
|
|
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
|
|
|
{
|
2026-10-03 15:26:21 +02:00
|
|
|
var cancellationToken = disposeCancellation.Token;
|
2022-02-14 01:51:52 +01:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
cascadingStateReference = DotNetObjectReference.Create(this);
|
2026-10-03 15:26:21 +02:00
|
|
|
await JS.InvokeVoidAsync("cascadingStateInstanceReference", cancellationToken, cascadingStateReference);
|
2022-02-14 01:51:52 +01:00
|
|
|
|
2026-10-03 15:26:21 +02:00
|
|
|
var publicCacheData = await Storage.GetItemAsync<PublicCacheData>(nameof(PublicCacheData), cancellationToken);
|
|
|
|
|
await UpdatePublicCache(publicCacheData ?? new(), cancellationToken);
|
|
|
|
|
|
|
|
|
|
IsOnline = await Status.IsOnline(cancellationToken);
|
|
|
|
|
onlineTimer = new(TimeSpan.FromSeconds(10));
|
|
|
|
|
_ = PollOnline(cancellationToken);
|
2022-02-14 01:51:52 +01:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-10-03 15:26:21 +02:00
|
|
|
await Logger.ProcessError(ex, $"{nameof(CascadingState)}.{nameof(OnInitializedAsync)}", cancellationToken);
|
2022-02-14 01:51:52 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-03 15:26:21 +02:00
|
|
|
/// <summary>Saves the public cache, applies its theme and re-renders the app.</summary>
|
|
|
|
|
public async ValueTask UpdatePublicCache(PublicCacheData publicCacheData, CancellationToken cancellationToken = default)
|
2022-02-14 01:51:52 +01:00
|
|
|
{
|
2026-10-03 15:26:21 +02:00
|
|
|
PublicCacheData = publicCacheData;
|
|
|
|
|
await Storage.SetItemAsync(nameof(PublicCacheData), PublicCacheData, cancellationToken);
|
|
|
|
|
Status.ApplyTheme(PublicCacheData.PageSettings);
|
|
|
|
|
StateHasChanged();
|
2022-02-14 01:51:52 +01:00
|
|
|
}
|
|
|
|
|
|
2026-10-03 12:43:07 +02:00
|
|
|
/// <summary>Changes the page settings, then saves and applies them like UpdatePublicCache.</summary>
|
2026-10-03 15:26:21 +02:00
|
|
|
public ValueTask UpdatePageSettings(Action<PageSettings> change, CancellationToken cancellationToken = default)
|
2026-10-03 12:43:07 +02:00
|
|
|
{
|
|
|
|
|
change(PublicCacheData.PageSettings);
|
2026-10-03 15:26:21 +02:00
|
|
|
return UpdatePublicCache(PublicCacheData, cancellationToken);
|
2026-10-03 12:43:07 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-14 01:51:52 +01:00
|
|
|
[JSInvokable]
|
2026-10-03 15:26:21 +02:00
|
|
|
public async Task LogFromJs(string message, string where) =>
|
|
|
|
|
await ProcessError(new(message), where);
|
2022-02-14 01:51:52 +01:00
|
|
|
|
2026-10-03 15:26:21 +02:00
|
|
|
async Task PollOnline(CancellationToken cancellationToken)
|
2022-02-14 01:51:52 +01:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-10-03 15:26:21 +02:00
|
|
|
while (await onlineTimer.WaitForNextTickAsync(cancellationToken))
|
2022-02-14 01:51:52 +01:00
|
|
|
{
|
2026-10-03 15:26:21 +02:00
|
|
|
var isOnline = await Status.IsOnline(cancellationToken);
|
|
|
|
|
if (isOnline == IsOnline)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
IsOnline = isOnline;
|
|
|
|
|
await InvokeAsync(StateHasChanged);
|
2022-02-14 01:51:52 +01:00
|
|
|
}
|
2026-10-03 15:26:21 +02:00
|
|
|
}
|
|
|
|
|
catch (OperationCanceledException)
|
|
|
|
|
{
|
2022-02-14 01:51:52 +01:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2026-10-03 15:26:21 +02:00
|
|
|
await Logger.ProcessError(ex, $"{nameof(CascadingState)}.{nameof(PollOnline)}", cancellationToken);
|
2022-02-14 01:51:52 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-03 15:26:21 +02:00
|
|
|
/// <summary>Logs an unexpected failure and tells the user.</summary>
|
|
|
|
|
public async ValueTask ProcessError(Exception exception, string where, CancellationToken cancellationToken = default)
|
2022-02-14 01:51:52 +01:00
|
|
|
{
|
2026-10-03 15:26:21 +02:00
|
|
|
await Logger.ProcessError(exception, where, cancellationToken);
|
|
|
|
|
Toast.AddException(exception);
|
2022-02-14 01:51:52 +01:00
|
|
|
}
|
|
|
|
|
|
2026-10-03 15:26:21 +02:00
|
|
|
public async ValueTask ProcessWarning(string message, string where, CancellationToken cancellationToken = default)
|
2022-02-14 01:51:52 +01:00
|
|
|
{
|
2026-10-03 15:26:21 +02:00
|
|
|
await Logger.ProcessWarning(message, where, cancellationToken);
|
|
|
|
|
Toast.AddWarning(message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async ValueTask DisposeAsync()
|
|
|
|
|
{
|
|
|
|
|
await disposeCancellation.CancelAsync();
|
|
|
|
|
disposeCancellation.Dispose();
|
|
|
|
|
onlineTimer?.Dispose();
|
|
|
|
|
cascadingStateReference?.Dispose();
|
2022-02-14 01:51:52 +01:00
|
|
|
}
|
2026-10-03 15:26:21 +02:00
|
|
|
}
|