From a3b0fa9e33c58fb2fc3caf630afa52e0829f8da8 Mon Sep 17 00:00:00 2001 From: Jared Goodwin Date: Fri, 4 Aug 2023 12:58:16 -0700 Subject: [PATCH] Move GetEffectiveTheme to new themeProvider. --- Server/Components/Devices/DeviceCard.razor.cs | 5 ++- Server/Components/LoadingSignal.razor | 4 +-- Server/Program.cs | 1 + Server/Services/ClientAppState.cs | 25 ------------- Server/Services/ThemeProvider.cs | 36 +++++++++++++++++++ 5 files changed, 43 insertions(+), 28 deletions(-) create mode 100644 Server/Services/ThemeProvider.cs diff --git a/Server/Components/Devices/DeviceCard.razor.cs b/Server/Components/Devices/DeviceCard.razor.cs index e11b2eb24..b5851e2b0 100644 --- a/Server/Components/Devices/DeviceCard.razor.cs +++ b/Server/Components/Devices/DeviceCard.razor.cs @@ -36,6 +36,9 @@ public partial class DeviceCard : AuthComponentBase, IDisposable [Inject] private IClientAppState AppState { get; init; } = null!; + [Inject] + private IThemeProvider ThemeProvider { get; init; } = null!; + [Inject] private ICircuitConnection CircuitConnection { get; init; } = null!; @@ -79,7 +82,7 @@ protected override async Task OnInitializedAsync() { await base.OnInitializedAsync(); EnsureUserSet(); - _theme = await AppState.GetEffectiveTheme(); + _theme = await ThemeProvider.GetEffectiveTheme(); _currentVersion = UpgradeService.GetCurrentVersion(); _deviceGroups = DataService.GetDeviceGroups(UserName); AppState.PropertyChanged += AppState_PropertyChanged; diff --git a/Server/Components/LoadingSignal.razor b/Server/Components/LoadingSignal.razor index a05168c18..0466046bf 100644 --- a/Server/Components/LoadingSignal.razor +++ b/Server/Components/LoadingSignal.razor @@ -1,4 +1,4 @@ -@inject IClientAppState AppState +@inject IThemeProvider ThemeProvider
@@ -20,7 +20,7 @@ protected override async Task OnInitializedAsync() { - _theme = await AppState.GetEffectiveTheme(); + _theme = await ThemeProvider.GetEffectiveTheme(); await base.OnInitializedAsync(); } diff --git a/Server/Program.cs b/Server/Program.cs index 706771988..a6a5d245f 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -210,6 +210,7 @@ services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); +services.AddSingleton(); services.AddSingleton(WeakReferenceMessenger.Default); services.AddRemoteControlServer(config => diff --git a/Server/Services/ClientAppState.cs b/Server/Services/ClientAppState.cs index 3f6272f48..069fc7372 100644 --- a/Server/Services/ClientAppState.cs +++ b/Server/Services/ClientAppState.cs @@ -21,25 +21,14 @@ public interface IClientAppState : INotifyPropertyChanged, IInvokePropertyChange void AddTerminalLine(string content, string className = "", string title = ""); - Task GetEffectiveTheme(); string GetTerminalHistory(bool forward); } public class ClientAppState : ViewModelBase, IClientAppState { - private readonly IApplicationConfig _appConfig; - private readonly IAuthService _authService; private readonly ConcurrentQueue _terminalHistory = new(); private int _terminalHistoryIndex = 0; - public ClientAppState( - IAuthService authService, - IApplicationConfig appConfig) - { - _authService = authService; - _appConfig = appConfig; - } - public ConcurrentList DevicesFrameChatSessions { get; } = new(); public DeviceCardState DevicesFrameFocusedCardState @@ -84,20 +73,6 @@ public void AddTerminalLine(string content, string className = "", string title }); } - public async Task GetEffectiveTheme() - { - if (await _authService.IsAuthenticated()) - { - var userResult = await _authService.GetUser(); - if (userResult.IsSuccess) - { - return userResult.Value.UserOptions?.Theme ?? _appConfig.Theme; - } - } - - return _appConfig.Theme; - } - public string GetTerminalHistory(bool forward) { if (!_terminalHistory.Any()) diff --git a/Server/Services/ThemeProvider.cs b/Server/Services/ThemeProvider.cs new file mode 100644 index 000000000..3e9106197 --- /dev/null +++ b/Server/Services/ThemeProvider.cs @@ -0,0 +1,36 @@ +using Microsoft.Identity.Client; +using Remotely.Shared.Enums; +using System.Threading.Tasks; + +namespace Remotely.Server.Services; + +public interface IThemeProvider +{ + Task GetEffectiveTheme(); +} + +public class ThemeProvider : IThemeProvider +{ + private readonly IAuthService _authService; + private readonly IApplicationConfig _appConfig; + + public ThemeProvider(IAuthService authService, IApplicationConfig appConfig) + { + _authService = authService; + _appConfig = appConfig; + } + + public async Task GetEffectiveTheme() + { + if (await _authService.IsAuthenticated()) + { + var userResult = await _authService.GetUser(); + if (userResult.IsSuccess) + { + return userResult.Value.UserOptions?.Theme ?? _appConfig.Theme; + } + } + + return _appConfig.Theme; + } +}