@if (PublicCacheData != null) { if (PublicCacheData.ThemeIsDarkMode) { } else { } } @ChildContent @code { [Parameter] public RenderFragment ChildContent { get; set; } [Inject] public IStringLocalizer Localizer { get; set; } [Inject] IStorage DbStorage { get; set; } [Inject] ILocalStorageService Storage { get; set; } [Inject] IJSRuntime JS { get; set; } // [Inject] DataService DataService { get; set; } [Inject] public AppStatusService Status { get; set; } [Inject] ILogger Logger { get; set; } public bool IsOnline { get; set; } = true; Timer IsOnlineTimer { get; set; } public PublicCacheData PublicCacheData { get; set; } public User User { get; set; } DotNetObjectReference cascadingStateReference; protected override async Task OnInitializedAsync() { try { IsOnlineTimer = new Timer(async _ => await UpdateIsOnline(), new AutoResetEvent(false), 0, 10000); cascadingStateReference = DotNetObjectReference.Create(this); await JS.InvokeVoidAsync("cascadingStateInstanceReference", cascadingStateReference); PublicCacheData = await Storage.GetItemAsync(nameof(PublicCacheData)); if (PublicCacheData == null) { PublicCacheData = new(); await UpdatePublicCache(PublicCacheData); } User = new(); } catch (Exception ex) { Logger.LogError(ex, $"{nameof(CascadingState)}.{nameof(OnInitializedAsync)}"); } } public async ValueTask UpdatePublicCache(PublicCacheData publicCacheData) { try { PublicCacheData = publicCacheData; await Storage.SetItemAsync(nameof(PublicCacheData), PublicCacheData); StateHasChanged(); } catch (Exception ex) { Console.WriteLine($"{nameof(CascadingState)}.{nameof(UpdatePublicCache)}"); Console.WriteLine(ex.ToString()); } } [JSInvokable] public async Task LogFromJs(string message, string where) { try { await ProcessError(new(message), where); } catch (Exception ex) { Logger.LogError(ex, $"{nameof(CascadingState)}.{nameof(LogFromJs)}"); } } async Task UpdateIsOnline() { try { var latestOnlineState = await Status.IsOnline(); //var latestOnlineState = Random.Shared.Next() % 2 == 0; if (latestOnlineState != IsOnline) { IsOnline = latestOnlineState; StateHasChanged(); } // else // { // var pingIsOnline = await DataService.Ping(); // if (pingIsOnline != IsOnline) // { // IsOnline = pingIsOnline; // StateHasChanged(); // } // } } catch (Exception ex) { Logger.LogError(ex, $"{nameof(CascadingState)}.{nameof(UpdateIsOnline)}"); } } public async ValueTask ProcessError(Exception ex, string where) { try { await DbStorage.AddLog(ex, where); Logger.LogError(ex, where); await Task.Run(() => { }); } catch (Exception exception) { Logger.LogError(exception, $"{nameof(CascadingState)}.{nameof(ProcessError)}"); } } public async ValueTask ProcessWarning(string message, string where) { try { await DbStorage.AddLog(message, where); Logger.LogWarning("{where} - {message}", where, message); await Task.Run(() => { }); } catch (Exception exception) { Logger.LogError(exception, $"{nameof(CascadingState)}.{nameof(ProcessWarning)}"); } } }