Skip to content

Commit

Permalink
Move GetEffectiveTheme to new themeProvider.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Aug 4, 2023
1 parent 8df614f commit a3b0fa9
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 28 deletions.
5 changes: 4 additions & 1 deletion Server/Components/Devices/DeviceCard.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!;

Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Server/Components/LoadingSignal.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@inject IClientAppState AppState
@inject IThemeProvider ThemeProvider

<div class="signal-background">
<div class="signal-wrapper text-center">
Expand All @@ -20,7 +20,7 @@

protected override async Task OnInitializedAsync()
{
_theme = await AppState.GetEffectiveTheme();
_theme = await ThemeProvider.GetEffectiveTheme();
await base.OnInitializedAsync();
}

Expand Down
1 change: 1 addition & 0 deletions Server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@
services.AddSingleton<IOtpProvider, OtpProvider>();
services.AddSingleton<IEmbeddedServerDataSearcher, EmbeddedServerDataSearcher>();
services.AddSingleton<ILogsManager, LogsManager>();
services.AddSingleton<IThemeProvider, ThemeProvider>();
services.AddSingleton(WeakReferenceMessenger.Default);

services.AddRemoteControlServer(config =>
Expand Down
25 changes: 0 additions & 25 deletions Server/Services/ClientAppState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,14 @@ public interface IClientAppState : INotifyPropertyChanged, IInvokePropertyChange

void AddTerminalLine(string content, string className = "", string title = "");

Task<Theme> GetEffectiveTheme();
string GetTerminalHistory(bool forward);
}

public class ClientAppState : ViewModelBase, IClientAppState
{
private readonly IApplicationConfig _appConfig;
private readonly IAuthService _authService;
private readonly ConcurrentQueue<string> _terminalHistory = new();
private int _terminalHistoryIndex = 0;

public ClientAppState(
IAuthService authService,
IApplicationConfig appConfig)
{
_authService = authService;
_appConfig = appConfig;
}

public ConcurrentList<ChatSession> DevicesFrameChatSessions { get; } = new();

public DeviceCardState DevicesFrameFocusedCardState
Expand Down Expand Up @@ -84,20 +73,6 @@ public void AddTerminalLine(string content, string className = "", string title
});
}

public async Task<Theme> 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())
Expand Down
36 changes: 36 additions & 0 deletions Server/Services/ThemeProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using Microsoft.Identity.Client;
using Remotely.Shared.Enums;
using System.Threading.Tasks;

namespace Remotely.Server.Services;

public interface IThemeProvider
{
Task<Theme> 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<Theme> GetEffectiveTheme()
{
if (await _authService.IsAuthenticated())
{
var userResult = await _authService.GetUser();
if (userResult.IsSuccess)
{
return userResult.Value.UserOptions?.Theme ?? _appConfig.Theme;
}
}

return _appConfig.Theme;
}
}

0 comments on commit a3b0fa9

Please sign in to comment.