Skip to content
This repository has been archived by the owner on Sep 5, 2022. It is now read-only.

Commit

Permalink
Save settings to roaming
Browse files Browse the repository at this point in the history
  • Loading branch information
Liklainy committed Apr 21, 2020
1 parent ad2ee73 commit 0e92488
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 117 deletions.
10 changes: 10 additions & 0 deletions MonikDesktopSolution/MonikDesktop/Common/Config/AppConfig.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
Original file line number Diff line number Diff line change
@@ -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<AppConfig>(data);
}
}
}
74 changes: 0 additions & 74 deletions MonikDesktopSolution/MonikDesktop/Properties/Settings.Designer.cs

This file was deleted.

18 changes: 0 additions & 18 deletions MonikDesktopSolution/MonikDesktop/Properties/Settings.settings

This file was deleted.

62 changes: 37 additions & 25 deletions MonikDesktopSolution/MonikDesktop/ViewModels/StartupViewModel.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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";

Expand All @@ -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<Uri>();
ServerUrlsSource.AddRange(urls);
ServerUrl = urls.FirstOrDefault();
if (urls != null)
{
ServerUrlsSource.AddRange(urls);
ServerUrl = urls.First();
}

ServerUrlsSource
.Connect()
Expand All @@ -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)
Expand All @@ -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<string>();
AuthTokensSource.AddRange(tokens);
AuthToken = tokens.FirstOrDefault();
if (tokens != null)
{
AuthTokensSource.AddRange(tokens);
AuthToken = tokens.First();
}

AuthTokensSource
.Connect()
Expand All @@ -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)
Expand Down Expand Up @@ -158,7 +177,7 @@ public void UpdateSourcesCache()

public string UpdateServerUrl
{
get => ServerUrl.ToString();
get => ServerUrl?.ToString();
set
{
// will throw if Uri is incorrect
Expand Down Expand Up @@ -311,15 +330,8 @@ private void ManageGroups()
_shell.ShowTool<StartupView>(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);
Expand Down

0 comments on commit 0e92488

Please sign in to comment.