From 0e924884380197b14b75625fb905659e0966caa0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB=20=D0=9C=D0=B8=D1=85?= =?UTF-8?q?=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2?= Date: Tue, 21 Apr 2020 18:25:22 +0300 Subject: [PATCH] Save settings to roaming --- .../MonikDesktop/Common/Config/AppConfig.cs | 10 +++ .../Common/Config/AppConfigStorage.cs | 37 ++++++++++ .../Properties/Settings.Designer.cs | 74 ------------------- .../MonikDesktop/Properties/Settings.settings | 18 ----- .../ViewModels/StartupViewModel.cs | 62 +++++++++------- 5 files changed, 84 insertions(+), 117 deletions(-) create mode 100644 MonikDesktopSolution/MonikDesktop/Common/Config/AppConfig.cs create mode 100644 MonikDesktopSolution/MonikDesktop/Common/Config/AppConfigStorage.cs delete mode 100644 MonikDesktopSolution/MonikDesktop/Properties/Settings.Designer.cs delete mode 100644 MonikDesktopSolution/MonikDesktop/Properties/Settings.settings diff --git a/MonikDesktopSolution/MonikDesktop/Common/Config/AppConfig.cs b/MonikDesktopSolution/MonikDesktop/Common/Config/AppConfig.cs new file mode 100644 index 0000000..ea47a17 --- /dev/null +++ b/MonikDesktopSolution/MonikDesktop/Common/Config/AppConfig.cs @@ -0,0 +1,10 @@ +namespace MonikDesktop.Common.Config +{ + public class AppConfig + { + public string Accent { get; set; } + public bool IsDark { get; set; } + public string ServerUrl { get; set; } + public string AuthToken { get; set; } + } +} diff --git a/MonikDesktopSolution/MonikDesktop/Common/Config/AppConfigStorage.cs b/MonikDesktopSolution/MonikDesktop/Common/Config/AppConfigStorage.cs new file mode 100644 index 0000000..5db527f --- /dev/null +++ b/MonikDesktopSolution/MonikDesktop/Common/Config/AppConfigStorage.cs @@ -0,0 +1,37 @@ +using System; +using System.IO; +using System.Text; +using Newtonsoft.Json; + +namespace MonikDesktop.Common.Config +{ + public class AppConfigStorage + { + private readonly FileInfo _fileInfo; + + public AppConfigStorage() + { + _fileInfo = new FileInfo(Path.Combine( + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), + @"MonikDesktop\app.config")); + _fileInfo.Directory?.Create(); + } + + public void Save(AppConfig value) + { + var data = JsonConvert.SerializeObject(value); + File.WriteAllText(_fileInfo.ToString(), data, Encoding.UTF8); + } + + public AppConfig Load() + { + if (!_fileInfo.Exists) + { + return new AppConfig(); + } + + var data = File.ReadAllText(_fileInfo.ToString(), Encoding.UTF8); + return JsonConvert.DeserializeObject(data); + } + } +} diff --git a/MonikDesktopSolution/MonikDesktop/Properties/Settings.Designer.cs b/MonikDesktopSolution/MonikDesktop/Properties/Settings.Designer.cs deleted file mode 100644 index d6d1a5f..0000000 --- a/MonikDesktopSolution/MonikDesktop/Properties/Settings.Designer.cs +++ /dev/null @@ -1,74 +0,0 @@ -//------------------------------------------------------------------------------ -// -// This code was generated by a tool. -// Runtime Version:4.0.30319.42000 -// -// Changes to this file may cause incorrect behavior and will be lost if -// the code is regenerated. -// -//------------------------------------------------------------------------------ - -namespace MonikDesktop.Properties { - - - [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.7.0.0")] - internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { - - private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); - - public static Settings Default { - get { - return defaultInstance; - } - } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("http://localhost:2222/")] - public string ServerUrl { - get { - return ((string)(this["ServerUrl"])); - } - set { - this["ServerUrl"] = value; - } - } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("")] - public string AuthToken { - get { - return ((string)(this["AuthToken"])); - } - set { - this["AuthToken"] = value; - } - } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("Blue")] - public string Accent { - get { - return ((string)(this["Accent"])); - } - set { - this["Accent"] = value; - } - } - - [global::System.Configuration.UserScopedSettingAttribute()] - [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] - [global::System.Configuration.DefaultSettingValueAttribute("False")] - public bool IsDark { - get { - return ((bool)(this["IsDark"])); - } - set { - this["IsDark"] = value; - } - } - } -} diff --git a/MonikDesktopSolution/MonikDesktop/Properties/Settings.settings b/MonikDesktopSolution/MonikDesktop/Properties/Settings.settings deleted file mode 100644 index 89f491f..0000000 --- a/MonikDesktopSolution/MonikDesktop/Properties/Settings.settings +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - http://localhost:2222/ - - - - - - Blue - - - False - - - \ No newline at end of file diff --git a/MonikDesktopSolution/MonikDesktop/ViewModels/StartupViewModel.cs b/MonikDesktopSolution/MonikDesktop/ViewModels/StartupViewModel.cs index 3ec8861..c57dc29 100644 --- a/MonikDesktopSolution/MonikDesktop/ViewModels/StartupViewModel.cs +++ b/MonikDesktopSolution/MonikDesktop/ViewModels/StartupViewModel.cs @@ -1,7 +1,6 @@ using DynamicData; using MahApps.Metro; using MonikDesktop.Common.Interfaces; -using MonikDesktop.Properties; using MonikDesktop.Views; using ReactiveUI; using ReactiveUI.Fody.Helpers; @@ -15,6 +14,7 @@ using System.Reactive.Linq; using System.Threading.Tasks; using System.Windows; +using MonikDesktop.Common.Config; using Ui.Wpf.Common; using Ui.Wpf.Common.ViewModels; @@ -47,6 +47,9 @@ public StartupViewModel(IShell shell, ISourcesCacheProvider cacheProvider, IDock _shell = shell; _cacheProvider = cacheProvider; _window = window; + + var appConfigStorage = new AppConfigStorage(); + var config = appConfigStorage.Load(); Title = "App settings"; @@ -58,26 +61,39 @@ public StartupViewModel(IShell shell, ISourcesCacheProvider cacheProvider, IDock // Theme - Accent = Settings.Default.Accent; - IsDark = Settings.Default.IsDark; - UpdateTheme(false); + Accent = config.Accent ?? "Blue"; + IsDark = config.IsDark; + UpdateTheme(); this.ObservableForProperty(x => x.Accent) - .Subscribe(_ => UpdateTheme()); + .Subscribe(x => + { + config.Accent = x.Value; + appConfigStorage.Save(config); + UpdateTheme(); + }); this.ObservableForProperty(x => x.IsDark) - .Subscribe(_ => UpdateTheme()); + .Subscribe(x => + { + config.IsDark = x.Value; + appConfigStorage.Save(config); + UpdateTheme(); + }); // Server Urls - var urls = Settings.Default.ServerUrl + var urls = config.ServerUrl? .Split(';') .Select(x => Uri.TryCreate(x, UriKind.Absolute, out var result) ? result : null as Uri) .Where(x => x != null) .ToArray(); ServerUrlsSource = new SourceList(); - ServerUrlsSource.AddRange(urls); - ServerUrl = urls.FirstOrDefault(); + if (urls != null) + { + ServerUrlsSource.AddRange(urls); + ServerUrl = urls.First(); + } ServerUrlsSource .Connect() @@ -86,8 +102,8 @@ public StartupViewModel(IShell shell, ISourcesCacheProvider cacheProvider, IDock .ToCollection() .Subscribe(items => { - Settings.Default.ServerUrl = string.Join(";", items); - Settings.Default.Save(); + config.ServerUrl = string.Join(";", items); + appConfigStorage.Save(config); }); this.WhenAnyValue(x => x.ServerUrl) @@ -96,14 +112,17 @@ public StartupViewModel(IShell shell, ISourcesCacheProvider cacheProvider, IDock // Authorization Tokens - var tokens = Settings.Default.AuthToken + var tokens = config.AuthToken? .Split(';') .Where(x => !string.IsNullOrEmpty(x)) .ToArray(); AuthTokensSource = new SourceList(); - AuthTokensSource.AddRange(tokens); - AuthToken = tokens.FirstOrDefault(); + if (tokens != null) + { + AuthTokensSource.AddRange(tokens); + AuthToken = tokens.First(); + } AuthTokensSource .Connect() @@ -112,8 +131,8 @@ public StartupViewModel(IShell shell, ISourcesCacheProvider cacheProvider, IDock .ToCollection() .Subscribe(items => { - Settings.Default.AuthToken = string.Join(";", items); - Settings.Default.Save(); + config.AuthToken = string.Join(";", items); + appConfigStorage.Save(config); }); this.WhenAnyValue(x => x.AuthToken) @@ -158,7 +177,7 @@ public void UpdateSourcesCache() public string UpdateServerUrl { - get => ServerUrl.ToString(); + get => ServerUrl?.ToString(); set { // will throw if Uri is incorrect @@ -311,15 +330,8 @@ private void ManageGroups() _shell.ShowTool(new ViewRequest("startup")); } - private void UpdateTheme(bool needToSave = true) + private void UpdateTheme() { - if (needToSave) - { - Settings.Default.Accent = Accent; - Settings.Default.IsDark = IsDark; - Settings.Default.Save(); - } - ThemeManager.ChangeTheme(Application.Current, IsDark ? ThemeManager.BaseColorDark : ThemeManager.BaseColorLight, Accent);