using Remotely.Server.Enums; using Remotely.Shared.Enums; using Remotely.Shared.Models; using Remotely.Shared.ViewModels; using System.Collections.Concurrent; using System.ComponentModel; using System.Linq; namespace Remotely.Server.Services { public interface IClientAppState : INotifyPropertyChanged, IInvokePropertyChanged { ConcurrentList DevicesFrameChatSessions { get; } DeviceCardState DevicesFrameFocusedCardState { get; set; } string DevicesFrameFocusedDevice { get; set; } ConcurrentList DevicesFrameSelectedDevices { get; } Theme EffectiveTheme { get; } ConcurrentQueue TerminalLines { get; } void AddTerminalHistory(string content); void AddTerminalLine(string content, string className = "", string title = ""); 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 { get => Get(); set => Set(value); } public string DevicesFrameFocusedDevice { get => Get(); set => Set(value); } public ConcurrentList DevicesFrameSelectedDevices { get; } = new(); public Theme EffectiveTheme { get { if (_authService.IsAuthenticated) { return _authService.User.UserOptions.Theme; } return _appConfig.Theme; } } public ConcurrentQueue TerminalLines { get; } = new(); public void AddTerminalHistory(string content) { while (_terminalHistory.Count > 500) { _terminalHistory.TryDequeue(out _); } _terminalHistory.Enqueue(content); _terminalHistoryIndex = _terminalHistory.Count; } public void AddTerminalLine(string content, string className = "", string title = "") { while (TerminalLines.Count > 500) { TerminalLines.TryDequeue(out _); } TerminalLines.Enqueue(new TerminalLineItem() { Text = content, ClassName = className, Title = title }); } public string GetTerminalHistory(bool forward) { if (!_terminalHistory.Any()) { return ""; } if (forward && _terminalHistoryIndex < _terminalHistory.Count) { _terminalHistoryIndex++; } else if (!forward && _terminalHistoryIndex > 0) { _terminalHistoryIndex--; } if (_terminalHistoryIndex < 0 || _terminalHistoryIndex >= _terminalHistory.Count) { return ""; } return _terminalHistory.ElementAt(_terminalHistoryIndex); } } }