Skip to content

Commit

Permalink
Fixed more init bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
freezy committed Nov 21, 2015
1 parent fdfd645 commit a96d428
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 20 deletions.
1 change: 1 addition & 0 deletions Application/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
13 changes: 8 additions & 5 deletions Application/SettingsManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public interface ISettingsManager
/// Only call this after <see cref="Validate"/>, since it doesn't validate on its own!
/// </remarks>
/// <param name="settings">Settings to save</param>
/// <returns>This instance</returns>
ISettingsManager Save(Settings settings);
/// <returns>An observable that returns one value when settings are saved and completes.</returns>
IObservable<Settings> Save(Settings settings);

/// <summary>
/// Persists internal (non-validated) settings.
Expand Down Expand Up @@ -209,11 +209,12 @@ private static Dictionary<string, string> HandleApiError(Dictionary<string, stri
return errors;
}

public ISettingsManager Save(Settings settings)
public IObservable<Settings> Save(Settings settings)
{
if (!settings.IsValidated) {
throw new InvalidOperationException("Settings must be validated before saved.");
}
var result = new Subject<Settings>();

Task.Run(async () => {
Settings.Copy(settings, Settings);
Expand All @@ -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<Settings> SaveInternal(Settings settings)
Expand Down
6 changes: 5 additions & 1 deletion ViewModels/AppViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ISettingsManager>(), Locator.Current.GetService<IVersionManager>()));
Router.Navigate.Execute(new SettingsViewModel(this,
Locator.Current.GetService<ISettingsManager>(),
Locator.Current.GetService<IVersionManager>(),
Locator.Current.GetService<IGameManager>())
);

} else if (!options.Minimized) {
// start the initialization
Expand Down
2 changes: 1 addition & 1 deletion ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public MainViewModel(IScreen screen, ISettingsManager settingsManager, IVersionM
Games = new GamesViewModel(Locator.Current.GetService<IGameManager>(), Locator.Current.GetService<IPlatformManager>());
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<IGameManager>())));

// login status
settingsManager.WhenAnyValue(sm => sm.AuthenticatedUser)
Expand Down
30 changes: 18 additions & 12 deletions ViewModels/Settings/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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); } }
Expand Down Expand Up @@ -77,11 +79,12 @@ public class SettingsViewModel : ReactiveObject, IRoutableViewModel
private readonly ObservableAsPropertyHelper<bool> _isFirstRun;
private readonly ObservableAsPropertyHelper<bool> _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;
Expand Down Expand Up @@ -132,7 +135,7 @@ public SettingsViewModel(IScreen screen, ISettingsManager settingsManager, IVers

}

public SettingsViewModel(IScreen screen, ISettingsManager settingsManager, IVersionManager versionManager, Dictionary<string, string> errors) : this(screen, settingsManager, versionManager)
public SettingsViewModel(IScreen screen, ISettingsManager settingsManager, IVersionManager versionManager, IGameManager gameManager, Dictionary<string, string> errors) : this(screen, settingsManager, versionManager, gameManager)
{
Errors = errors;
}
Expand Down Expand Up @@ -174,16 +177,19 @@ private async Task<Dictionary<string, string>> 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")) {
Expand Down
2 changes: 1 addition & 1 deletion Vpdb/VpdbClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit a96d428

Please sign in to comment.