diff --git a/Moonlight/App/Services/Interop/AlertService.cs b/Moonlight/App/Services/Interop/AlertService.cs index 24a15fa4..a5cad8f1 100644 --- a/Moonlight/App/Services/Interop/AlertService.cs +++ b/Moonlight/App/Services/Interop/AlertService.cs @@ -1,21 +1,40 @@ using CurrieTechnologies.Razor.SweetAlert2; +using Microsoft.JSInterop; namespace Moonlight.App.Services.Interop; public class AlertService { - private readonly SweetAlertService SweetAlertService; private readonly SmartTranslateService SmartTranslateService; + private readonly IJSRuntime JsRuntime; + private SweetAlertService? SweetAlertService; - public AlertService(SweetAlertService service, SmartTranslateService smartTranslateService) + public AlertService(SmartTranslateService smartTranslateService, IJSRuntime jsRuntime) { - SweetAlertService = service; SmartTranslateService = smartTranslateService; + JsRuntime = jsRuntime; + } + + // We create the swal service here and not using the dependency injection + // because it initializes when instantiated which leads to js invoke errors + private Task EnsureService() + { + if (SweetAlertService == null) + { + SweetAlertService = new(JsRuntime, new() + { + Theme = SweetAlertTheme.Dark + }); + } + + return Task.CompletedTask; } public async Task Info(string title, string desciption) { - await SweetAlertService.FireAsync(new SweetAlertOptions() + await EnsureService(); + + await SweetAlertService!.FireAsync(new SweetAlertOptions() { Title = title, Text = desciption, @@ -30,7 +49,9 @@ public class AlertService public async Task Success(string title, string desciption) { - await SweetAlertService.FireAsync(new SweetAlertOptions() + await EnsureService(); + + await SweetAlertService!.FireAsync(new SweetAlertOptions() { Title = title, Text = desciption, @@ -45,7 +66,9 @@ public class AlertService public async Task Warning(string title, string desciption) { - await SweetAlertService.FireAsync(new SweetAlertOptions() + await EnsureService(); + + await SweetAlertService!.FireAsync(new SweetAlertOptions() { Title = title, Text = desciption, @@ -60,7 +83,9 @@ public class AlertService public async Task Error(string title, string desciption) { - await SweetAlertService.FireAsync(new SweetAlertOptions() + await EnsureService(); + + await SweetAlertService!.FireAsync(new SweetAlertOptions() { Title = title, Text = desciption, @@ -75,7 +100,9 @@ public class AlertService public async Task YesNo(string title, string desciption, string yesText, string noText) { - var result = await SweetAlertService.FireAsync(new SweetAlertOptions() + await EnsureService(); + + var result = await SweetAlertService!.FireAsync(new SweetAlertOptions() { Title = title, Text = desciption, @@ -91,7 +118,9 @@ public class AlertService public async Task Text(string title, string desciption, string setValue) { - var result = await SweetAlertService.FireAsync(new SweetAlertOptions() + await EnsureService(); + + var result = await SweetAlertService!.FireAsync(new SweetAlertOptions() { Title = title, Text = desciption, diff --git a/Moonlight/Program.cs b/Moonlight/Program.cs index cc18c698..d78d7967 100644 --- a/Moonlight/Program.cs +++ b/Moonlight/Program.cs @@ -248,7 +248,6 @@ namespace Moonlight // Third party services builder.Services.AddBlazorTable(); - builder.Services.AddSweetAlert2(options => { options.Theme = SweetAlertTheme.Dark; }); builder.Services.AddBlazorContextMenu(); builder.Services.AddBlazorDownloadFile(); diff --git a/Moonlight/Shared/Layouts/MainLayout.razor b/Moonlight/Shared/Layouts/MainLayout.razor index 040186ad..8eac6b55 100644 --- a/Moonlight/Shared/Layouts/MainLayout.razor +++ b/Moonlight/Shared/Layouts/MainLayout.razor @@ -163,13 +163,11 @@ private bool IsIpBanned = false; - protected override void OnInitialized() - { - AddBodyAttribute("data-kt-app-page-loading", "on"); - } - protected override void OnAfterRender(bool firstRender) { + if(firstRender) + AddBodyAttribute("data-kt-app-page-loading", "on"); + //Initialize classes and attributes for layout with dark sidebar AddBodyAttribute("data-kt-app-reset-transition", "true");