using FluentTerminal.Models; using FluentTerminal.Models.Enums; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; namespace FluentTerminal.App.Services.Implementation { public class SettingsService : ISettingsService { public const string CurrentThemeKey = "CurrentTheme"; public const string DefaultShellProfileKey = "DefaultShellProfile"; private readonly IDefaultValueProvider _defaultValueProvider; private readonly IApplicationDataContainer _localSettings; private readonly IApplicationDataContainer _themes; private readonly IApplicationDataContainer _keyBindings; private readonly IApplicationDataContainer _shellProfiles; private readonly IApplicationDataContainer _roamingSettings; public event EventHandler CurrentThemeChanged; public event EventHandler TerminalOptionsChanged; public event EventHandler ApplicationSettingsChanged; public event EventHandler KeyBindingsChanged; public SettingsService(IDefaultValueProvider defaultValueProvider, ApplicationDataContainers containers) { _defaultValueProvider = defaultValueProvider; _localSettings = containers.LocalSettings; _roamingSettings = containers.RoamingSettings; _themes = containers.Themes; _keyBindings = containers.KeyBindings; _shellProfiles = containers.ShellProfiles; foreach (var theme in _defaultValueProvider.GetPreInstalledThemes()) { _themes.WriteValueAsJson(theme.Id.ToString(), theme); } foreach (var shellProfile in _defaultValueProvider.GetPreinstalledShellProfiles()) { _shellProfiles.WriteValueAsJson(shellProfile.Id.ToString(), shellProfile); } } public TerminalTheme GetCurrentTheme() { var id = GetCurrentThemeId(); var theme = GetTheme(id); if (theme == null) { id = _defaultValueProvider.GetDefaultThemeId(); SaveCurrentThemeId(id); theme = GetTheme(id); } return theme; } public Guid GetCurrentThemeId() { if (_roamingSettings.TryGetValue(CurrentThemeKey, out object value)) { return (Guid)value; } return _defaultValueProvider.GetDefaultThemeId(); } public void SaveCurrentThemeId(Guid id) { _roamingSettings.SetValue(CurrentThemeKey, id); CurrentThemeChanged?.Invoke(this, id); } public void SaveTheme(TerminalTheme theme) { _themes.WriteValueAsJson(theme.Id.ToString(), theme); if (theme.Id == GetCurrentThemeId()) { CurrentThemeChanged?.Invoke(this, theme.Id); } } public void DeleteTheme(Guid id) { _themes.Delete(id.ToString()); } public IEnumerable GetThemes() { return _themes.GetAll().Select(x => JsonConvert.DeserializeObject((string)x)).ToList(); } public TerminalTheme GetTheme(Guid id) { return _themes.ReadValueFromJson(id.ToString(), default(TerminalTheme)); } public TerminalOptions GetTerminalOptions() { return _roamingSettings.ReadValueFromJson(nameof(TerminalOptions), _defaultValueProvider.GetDefaultTerminalOptions()); } public void SaveTerminalOptions(TerminalOptions terminalOptions) { _roamingSettings.WriteValueAsJson(nameof(TerminalOptions), terminalOptions); TerminalOptionsChanged?.Invoke(this, terminalOptions); } public ApplicationSettings GetApplicationSettings() { return _roamingSettings.ReadValueFromJson(nameof(ApplicationSettings), _defaultValueProvider.GetDefaultApplicationSettings()); } public void SaveApplicationSettings(ApplicationSettings applicationSettings) { _roamingSettings.WriteValueAsJson(nameof(ApplicationSettings), applicationSettings); ApplicationSettingsChanged?.Invoke(this, applicationSettings); } public IDictionary> GetKeyBindings() { var keyBindings = new Dictionary>(); foreach (Command command in Enum.GetValues(typeof(Command))) { keyBindings.Add(command, _keyBindings.ReadValueFromJson>(command.ToString(), null) ?? _defaultValueProvider.GetDefaultKeyBindings(command)); } return keyBindings; } public void SaveKeyBindings(Command command, ICollection keyBindings) { _keyBindings.WriteValueAsJson(command.ToString(), keyBindings); KeyBindingsChanged?.Invoke(this, System.EventArgs.Empty); } public void ResetKeyBindings() { foreach (Command command in Enum.GetValues(typeof(Command))) { _keyBindings.WriteValueAsJson(command.ToString(), _defaultValueProvider.GetDefaultKeyBindings(command)); } KeyBindingsChanged?.Invoke(this, System.EventArgs.Empty); } public Guid GetDefaultShellProfileId() { if (_localSettings.TryGetValue(DefaultShellProfileKey, out object value)) { return (Guid)value; } return _defaultValueProvider.GetDefaultShellProfileId(); } public ShellProfile GetDefaultShellProfile() { var id = GetDefaultShellProfileId(); var profile = _shellProfiles.ReadValueFromJson(id.ToString(), default(ShellProfile)); if (profile == null) { id = _defaultValueProvider.GetDefaultShellProfileId(); SaveDefaultShellProfileId(id); profile = _shellProfiles.ReadValueFromJson(id.ToString(), default(ShellProfile)); } return profile; } public void SaveDefaultShellProfileId(Guid id) { _localSettings.SetValue(DefaultShellProfileKey, id); } public IEnumerable GetShellProfiles() { return _shellProfiles.GetAll().Select(x => JsonConvert.DeserializeObject((string)x)).ToList(); } public void SaveShellProfile(ShellProfile shellProfile) { _shellProfiles.WriteValueAsJson(shellProfile.Id.ToString(), shellProfile); } public void DeleteShellProfile(Guid id) { _shellProfiles.Delete(id.ToString()); } } }