From a96d4281d28f7829732748d48ad0d553bd6b7704 Mon Sep 17 00:00:00 2001 From: freezy Date: Sat, 21 Nov 2015 22:35:31 +0100 Subject: [PATCH] Fixed more init bugs. --- Application/Settings.cs | 1 + Application/SettingsManager.cs | 13 ++++++---- ViewModels/AppViewModel.cs | 6 ++++- ViewModels/MainViewModel.cs | 2 +- ViewModels/Settings/SettingsViewModel.cs | 30 ++++++++++++++---------- Vpdb/VpdbClient.cs | 2 +- 6 files changed, 34 insertions(+), 20 deletions(-) diff --git a/Application/Settings.cs b/Application/Settings.cs index a574ddf..d988348 100644 --- a/Application/Settings.cs +++ b/Application/Settings.cs @@ -151,6 +151,7 @@ public async Task WriteToStorage(IBlobCache storage) await storage.InsertObject("DownloadOrientationFallback", DownloadOrientationFallback); await storage.InsertObject("DownloadLighting", DownloadLighting); await storage.InsertObject("DownloadLightingFallback", DownloadLightingFallback); + await storage.InsertObject("IsFirstRun", false); IsFirstRun = false; } diff --git a/Application/SettingsManager.cs b/Application/SettingsManager.cs index a952fe9..a0b7bb4 100644 --- a/Application/SettingsManager.cs +++ b/Application/SettingsManager.cs @@ -81,8 +81,8 @@ public interface ISettingsManager /// Only call this after , since it doesn't validate on its own! /// /// Settings to save - /// This instance - ISettingsManager Save(Settings settings); + /// An observable that returns one value when settings are saved and completes. + IObservable Save(Settings settings); /// /// Persists internal (non-validated) settings. @@ -209,11 +209,12 @@ private static Dictionary HandleApiError(Dictionary Save(Settings settings) { if (!settings.IsValidated) { throw new InvalidOperationException("Settings must be validated before saved."); } + var result = new Subject(); Task.Run(async () => { Settings.Copy(settings, Settings); @@ -236,12 +237,14 @@ public ISettingsManager Save(Settings settings) if (_registryKey.GetValue("VPDB Agent") != null) { _registryKey.DeleteValue("VPDB Agent"); } - } + + result.OnNext(Settings); + result.OnCompleted(); }); CanCancel = true; - return this; + return result; } public IObservable SaveInternal(Settings settings) diff --git a/ViewModels/AppViewModel.cs b/ViewModels/AppViewModel.cs index 8de749f..b3c43c9 100644 --- a/ViewModels/AppViewModel.cs +++ b/ViewModels/AppViewModel.cs @@ -70,7 +70,11 @@ public AppViewModel(IMutableDependencyResolver dependencyResolver = null, Routin if (settings.IsFirstRun || string.IsNullOrEmpty(settings.ApiKey)) { System.Windows.Application.Current.MainWindow = new MainWindow(this); System.Windows.Application.Current.MainWindow.Show(); - Router.Navigate.Execute(new SettingsViewModel(this, Locator.Current.GetService(), Locator.Current.GetService())); + Router.Navigate.Execute(new SettingsViewModel(this, + Locator.Current.GetService(), + Locator.Current.GetService(), + Locator.Current.GetService()) + ); } else if (!options.Minimized) { // start the initialization diff --git a/ViewModels/MainViewModel.cs b/ViewModels/MainViewModel.cs index 2e8bd02..c0878e9 100644 --- a/ViewModels/MainViewModel.cs +++ b/ViewModels/MainViewModel.cs @@ -52,7 +52,7 @@ public MainViewModel(IScreen screen, ISettingsManager settingsManager, IVersionM Games = new GamesViewModel(Locator.Current.GetService(), Locator.Current.GetService()); Downloads = new DownloadsViewModel(); Messsages = new MessagesViewModel(); - GotoSettings = ReactiveCommand.CreateAsyncObservable(_ => screen.Router.Navigate.ExecuteAsync(new SettingsViewModel(screen, settingsManager, versionManager))); + GotoSettings = ReactiveCommand.CreateAsyncObservable(_ => screen.Router.Navigate.ExecuteAsync(new SettingsViewModel(screen, settingsManager, versionManager, Locator.Current.GetService()))); // login status settingsManager.WhenAnyValue(sm => sm.AuthenticatedUser) diff --git a/ViewModels/Settings/SettingsViewModel.cs b/ViewModels/Settings/SettingsViewModel.cs index 807ad55..70edefb 100644 --- a/ViewModels/Settings/SettingsViewModel.cs +++ b/ViewModels/Settings/SettingsViewModel.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Diagnostics.Eventing; using System.Linq; +using System.Reactive.Concurrency; using System.Reactive.Linq; using System.Threading.Tasks; using System.Windows.Forms; @@ -20,6 +21,7 @@ public class SettingsViewModel : ReactiveObject, IRoutableViewModel private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private readonly ISettingsManager _settingsManager; private readonly IVersionManager _versionManager; + private readonly IGameManager _gameManager; // setting props public string ApiKey { get { return _apiKey; } set { this.RaiseAndSetIfChanged(ref _apiKey, value); } } @@ -77,11 +79,12 @@ public class SettingsViewModel : ReactiveObject, IRoutableViewModel private readonly ObservableAsPropertyHelper _isFirstRun; private readonly ObservableAsPropertyHelper _canCancel; - public SettingsViewModel(IScreen screen, ISettingsManager settingsManager, IVersionManager versionManager) + public SettingsViewModel(IScreen screen, ISettingsManager settingsManager, IVersionManager versionManager, IGameManager gameManager) { HostScreen = screen; _settingsManager = settingsManager; _versionManager = versionManager; + _gameManager = gameManager; ApiKey = _settingsManager.Settings.ApiKey; AuthUser = _settingsManager.Settings.AuthUser; @@ -132,7 +135,7 @@ public SettingsViewModel(IScreen screen, ISettingsManager settingsManager, IVers } - public SettingsViewModel(IScreen screen, ISettingsManager settingsManager, IVersionManager versionManager, Dictionary errors) : this(screen, settingsManager, versionManager) + public SettingsViewModel(IScreen screen, ISettingsManager settingsManager, IVersionManager versionManager, IGameManager gameManager, Dictionary errors) : this(screen, settingsManager, versionManager, gameManager) { Errors = errors; } @@ -174,16 +177,19 @@ private async Task> Save() var errors = await _settingsManager.Validate(settings); if (settings.IsValidated) { - - _settingsManager.Save(settings); - Logger.Info("Settings saved."); - - if (HostScreen.Router.NavigationStack.Count == 1) { - HostScreen.Router.NavigateAndReset.Execute(new MainViewModel(HostScreen, _settingsManager, _versionManager)); - } else { - HostScreen.Router.NavigateBack.Execute(null); - } - + _settingsManager.Save(settings).SubscribeOn(Scheduler.CurrentThread).ObserveOn(Scheduler.CurrentThread).Subscribe(_ => + { + Logger.Info("Settings saved."); + System.Windows.Application.Current.Dispatcher.Invoke(delegate { + if (HostScreen.Router.NavigationStack.Count == 1) { + _gameManager.Initialize(); + HostScreen.Router.NavigateAndReset.Execute(new MainViewModel(HostScreen, _settingsManager, _versionManager)); + + } else { + HostScreen.Router.NavigateBack.Execute(null); + } + }); + }); } else { Errors = errors; if (errors.ContainsKey("Auth")) { diff --git a/Vpdb/VpdbClient.cs b/Vpdb/VpdbClient.cs index 366bf06..349f896 100644 --- a/Vpdb/VpdbClient.cs +++ b/Vpdb/VpdbClient.cs @@ -157,7 +157,7 @@ public void HandleApiError(Exception e, string origin) var apiException = e as ApiException; if (apiException?.StatusCode == HttpStatusCode.Unauthorized) { var errors = _settingsManager.OnApiFailed(apiException); - _screen.Router.Navigate.Execute(new SettingsViewModel(_screen, _settingsManager, _versionManager, errors)); + _screen.Router.Navigate.Execute(new SettingsViewModel(_screen, _settingsManager, _versionManager, null, errors)); } _logger.Error(e, "API error while {0}:", origin); }