diff --git a/BTCPayServer/Controllers/ServerController.cs b/BTCPayServer/Controllers/ServerController.cs index b13839ab7..08c41e878 100644 --- a/BTCPayServer/Controllers/ServerController.cs +++ b/BTCPayServer/Controllers/ServerController.cs @@ -25,17 +25,14 @@ namespace BTCPayServer.Controllers private UserManager _UserManager; SettingsRepository _SettingsRepository; private IRateProviderFactory _RateProviderFactory; - private CssThemeManager _CssThemeManager; public ServerController(UserManager userManager, IRateProviderFactory rateProviderFactory, - SettingsRepository settingsRepository, - CssThemeManager cssThemeManager) + SettingsRepository settingsRepository) { _UserManager = userManager; _SettingsRepository = settingsRepository; _RateProviderFactory = rateProviderFactory; - _CssThemeManager = cssThemeManager; } [Route("server/rates")] @@ -234,9 +231,6 @@ namespace BTCPayServer.Controllers public async Task Theme(ThemeSettings settings) { await _SettingsRepository.UpdateSetting(settings); - // TODO: remove controller/class-level property and have only reference to - // CssThemeManager here in this method - _CssThemeManager.Update(settings); TempData["StatusMessage"] = "Theme settings updated successfully"; return View(settings); } diff --git a/BTCPayServer/HostedServices/CssThemeManager.cs b/BTCPayServer/HostedServices/CssThemeManager.cs index 09f733d44..ca845d723 100644 --- a/BTCPayServer/HostedServices/CssThemeManager.cs +++ b/BTCPayServer/HostedServices/CssThemeManager.cs @@ -16,47 +16,59 @@ namespace BTCPayServer.HostedServices { public class CssThemeManager { - public CssThemeManager(SettingsRepository settingsRepository) - { - Update(settingsRepository); - } - - private async void Update(SettingsRepository settingsRepository) - { - var data = (await settingsRepository.GetSettingAsync()) ?? new ThemeSettings(); - Update(data); - } - public void Update(ThemeSettings data) { - UpdateBootstrap(data.BootstrapCssUri); - UpdateCreativeStart(data.CreativeStartCssUri); - } + if (String.IsNullOrWhiteSpace(data.BootstrapCssUri)) + _bootstrapUri = "/vendor/bootstrap4/css/bootstrap.css?v=" + DateTime.Now.Ticks; + else + _bootstrapUri = data.BootstrapCssUri; - private string _bootstrapUri = "/vendor/bootstrap4/css/bootstrap.css?v=" + DateTime.Now.Ticks; + + if (String.IsNullOrWhiteSpace(data.CreativeStartCssUri)) + _creativeStartUri = "/vendor/bootstrap4-creativestart/creative.css?v=" + DateTime.Now.Ticks; + else + _creativeStartUri = data.CreativeStartCssUri; + } + + private string _bootstrapUri; public string BootstrapUri { get { return _bootstrapUri; } } - public void UpdateBootstrap(string newUri) - { - if (String.IsNullOrWhiteSpace(newUri)) - _bootstrapUri = "/vendor/bootstrap4/css/bootstrap.css?v="+ DateTime.Now.Ticks; - else - _bootstrapUri = newUri; - } - private string _creativeStartUri = "/vendor/bootstrap4-creativestart/creative.css?v=" + DateTime.Now.Ticks; + private string _creativeStartUri; public string CreativeStartUri { get { return _creativeStartUri; } } - public void UpdateCreativeStart(string newUri) + } + + public class CssThemeManagerHostedService : BaseAsyncService + { + private SettingsRepository _SettingsRepository; + private CssThemeManager _CssThemeManager; + + public CssThemeManagerHostedService(SettingsRepository settingsRepository, CssThemeManager cssThemeManager) { - if (String.IsNullOrWhiteSpace(newUri)) - _creativeStartUri = "/vendor/bootstrap4-creativestart/creative.css?v=" + DateTime.Now.Ticks; - else - _creativeStartUri = newUri; + _SettingsRepository = settingsRepository; + _CssThemeManager = cssThemeManager; + } + + internal override Task[] initializeTasks() + { + return new[] + { + createLoopTask(ListenForThemeChanges) + }; + } + + async Task ListenForThemeChanges() + { + await new SynchronizationContextRemover(); + var data = (await _SettingsRepository.GetSettingAsync()) ?? new ThemeSettings(); + _CssThemeManager.Update(data); + + await _SettingsRepository.WaitSettingsChanged(_Cts.Token); } } } diff --git a/BTCPayServer/Hosting/BTCPayServerServices.cs b/BTCPayServer/Hosting/BTCPayServerServices.cs index b9c4b36b1..f2ce5b78a 100644 --- a/BTCPayServer/Hosting/BTCPayServerServices.cs +++ b/BTCPayServer/Hosting/BTCPayServerServices.cs @@ -138,7 +138,6 @@ namespace BTCPayServer.Hosting services.TryAddSingleton(); services.TryAddSingleton(); - services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); services.TryAddSingleton(); @@ -148,6 +147,9 @@ namespace BTCPayServer.Hosting BlockTarget = 20 }); + services.AddSingleton(); + services.AddSingleton(); + services.AddSingleton, Payments.Bitcoin.BitcoinLikePaymentHandler>(); services.AddSingleton();