From 090d5b76e8a1424e167383b2cf2fadf3e3743ebb Mon Sep 17 00:00:00 2001 From: Robert Date: Sat, 20 Aug 2022 17:45:30 +0200 Subject: [PATCH] Startup wizard - Implemented auto-run and update settings --- .../Settings/Tabs/GeneralTabView.axaml | 2 +- .../Settings/Tabs/GeneralTabViewModel.cs | 8 +- .../StartupWizard/StartupWizardView.axaml | 4 +- .../StartupWizard/StartupWizardViewModel.cs | 86 ++++++++++- .../StartupWizard/Steps/SettingsStep.axaml | 137 +++++++++++------- 5 files changed, 176 insertions(+), 61 deletions(-) diff --git a/src/Artemis.UI/Screens/Settings/Tabs/GeneralTabView.axaml b/src/Artemis.UI/Screens/Settings/Tabs/GeneralTabView.axaml index 5db51aec5..012f083ad 100644 --- a/src/Artemis.UI/Screens/Settings/Tabs/GeneralTabView.axaml +++ b/src/Artemis.UI/Screens/Settings/Tabs/GeneralTabView.axaml @@ -122,7 +122,7 @@ - + Updating diff --git a/src/Artemis.UI/Screens/Settings/Tabs/GeneralTabViewModel.cs b/src/Artemis.UI/Screens/Settings/Tabs/GeneralTabViewModel.cs index 6dbdc1b11..12baea870 100644 --- a/src/Artemis.UI/Screens/Settings/Tabs/GeneralTabViewModel.cs +++ b/src/Artemis.UI/Screens/Settings/Tabs/GeneralTabViewModel.cs @@ -32,6 +32,7 @@ namespace Artemis.UI.Screens.Settings private readonly IWindowService _windowService; private readonly IUpdateService _updateService; private readonly IAutoRunProvider? _autoRunProvider; + private bool _startupWizardOpen; public GeneralTabViewModel(IKernel kernel, ISettingsService settingsService, @@ -156,7 +157,6 @@ namespace Artemis.UI.Screens.Settings Utilities.OpenFolder(Constants.LogsFolder); } - private async Task ExecuteCheckForUpdate(CancellationToken cancellationToken) { await _updateService.ManualUpdate(); @@ -164,7 +164,9 @@ namespace Artemis.UI.Screens.Settings private async Task ExecuteShowSetupWizard() { + _startupWizardOpen = true; await _windowService.ShowDialogAsync(); + _startupWizardOpen = false; } private void ExecuteShowDebugger() @@ -179,7 +181,7 @@ namespace Artemis.UI.Screens.Settings private async Task ApplyAutoRun() { - if (_autoRunProvider == null) + if (_autoRunProvider == null || _startupWizardOpen) return; try @@ -202,7 +204,7 @@ namespace Artemis.UI.Screens.Settings private async void UIAutoRunDelayOnSettingChanged(object? sender, EventArgs e) { - if (_autoRunProvider == null || !UIAutoRun.Value) + if (_autoRunProvider == null || !UIAutoRun.Value || _startupWizardOpen) return; try diff --git a/src/Artemis.UI/Screens/StartupWizard/StartupWizardView.axaml b/src/Artemis.UI/Screens/StartupWizard/StartupWizardView.axaml index 1b0f0b157..819035d92 100644 --- a/src/Artemis.UI/Screens/StartupWizard/StartupWizardView.axaml +++ b/src/Artemis.UI/Screens/StartupWizard/StartupWizardView.axaml @@ -17,8 +17,8 @@ - - + + diff --git a/src/Artemis.UI/Screens/StartupWizard/StartupWizardViewModel.cs b/src/Artemis.UI/Screens/StartupWizard/StartupWizardViewModel.cs index 0bcc1a28f..2c509c971 100644 --- a/src/Artemis.UI/Screens/StartupWizard/StartupWizardViewModel.cs +++ b/src/Artemis.UI/Screens/StartupWizard/StartupWizardViewModel.cs @@ -1,31 +1,44 @@ -using System.Collections.Generic; +using System; using System.Collections.ObjectModel; using System.Linq; using System.Reactive; +using System.Reactive.Disposables; using System.Reflection; +using System.Threading.Tasks; using Artemis.Core; using Artemis.Core.DeviceProviders; using Artemis.Core.Services; using Artemis.UI.Ninject.Factories; using Artemis.UI.Screens.Plugins; +using Artemis.UI.Services.Interfaces; using Artemis.UI.Shared; +using Artemis.UI.Shared.Providers; +using Artemis.UI.Shared.Services; +using Ninject; using ReactiveUI; namespace Artemis.UI.Screens.StartupWizard; public class StartupWizardViewModel : DialogViewModelBase { + private readonly IAutoRunProvider? _autoRunProvider; private readonly IRgbService _rgbService; private readonly ISettingsService _settingsService; + private readonly IUpdateService _updateService; + private readonly IWindowService _windowService; private int _currentStep; private bool _showContinue; - private bool _showGoBack; private bool _showFinish; + private bool _showGoBack; - public StartupWizardViewModel(ISettingsService settingsService, IRgbService rgbService, IPluginManagementService pluginManagementService, ISettingsVmFactory settingsVmFactory) + public StartupWizardViewModel(IKernel kernel, ISettingsService settingsService, IRgbService rgbService, IPluginManagementService pluginManagementService, IWindowService windowService, + IUpdateService updateService, ISettingsVmFactory settingsVmFactory) { _settingsService = settingsService; _rgbService = rgbService; + _windowService = windowService; + _updateService = updateService; + _autoRunProvider = kernel.TryGet(); Continue = ReactiveCommand.Create(ExecuteContinue); GoBack = ReactiveCommand.Create(ExecuteGoBack); @@ -43,9 +56,22 @@ public class StartupWizardViewModel : DialogViewModelBase CurrentStep = 1; SetupButtons(); + + this.WhenActivated(d => + { + UIAutoRun.SettingChanged += UIAutoRunOnSettingChanged; + UIAutoRunDelay.SettingChanged += UIAutoRunDelayOnSettingChanged; + + Disposable.Create(() => + { + UIAutoRun.SettingChanged -= UIAutoRunOnSettingChanged; + UIAutoRunDelay.SettingChanged -= UIAutoRunDelayOnSettingChanged; + + _settingsService.SaveAllSettings(); + }).DisposeWith(d); + }); } - public ReactiveCommand Continue { get; } public ReactiveCommand GoBack { get; } public ReactiveCommand SkipOrFinishWizard { get; } @@ -53,11 +79,15 @@ public class StartupWizardViewModel : DialogViewModelBase public string Version { get; } public ObservableCollection DeviceProviders { get; } - + + public bool IsAutoRunSupported => _autoRunProvider != null; + public bool IsUpdatingSupported => _updateService.UpdatingSupported; + public PluginSetting UIAutoRun => _settingsService.GetSetting("UI.AutoRun", false); public PluginSetting UIAutoRunDelay => _settingsService.GetSetting("UI.AutoRunDelay", 15); public PluginSetting UIShowOnStartup => _settingsService.GetSetting("UI.ShowOnStartup", true); public PluginSetting UICheckForUpdates => _settingsService.GetSetting("UI.CheckForUpdates", true); + public PluginSetting UIAutoUpdate => _settingsService.GetSetting("UI.AutoUpdate", false); public int CurrentStep { @@ -88,6 +118,10 @@ public class StartupWizardViewModel : DialogViewModelBase if (CurrentStep > 1) CurrentStep--; + // Skip the settings step if none of it's contents are supported + if (CurrentStep == 4 && !IsAutoRunSupported && !IsUpdatingSupported) + CurrentStep--; + SetupButtons(); } @@ -96,6 +130,10 @@ public class StartupWizardViewModel : DialogViewModelBase if (CurrentStep < 5) CurrentStep++; + // Skip the settings step if none of it's contents are supported + if (CurrentStep == 4 && !IsAutoRunSupported && !IsUpdatingSupported) + CurrentStep++; + SetupButtons(); } @@ -123,4 +161,42 @@ public class StartupWizardViewModel : DialogViewModelBase ExecuteContinue(); } + + private async Task ApplyAutoRun() + { + if (_autoRunProvider == null) + return; + + try + { + if (UIAutoRun.Value) + await _autoRunProvider.EnableAutoRun(false, UIAutoRunDelay.Value); + else + await _autoRunProvider.DisableAutoRun(); + } + catch (Exception exception) + { + _windowService.ShowExceptionDialog("Failed to apply auto-run", exception); + } + } + + private async void UIAutoRunOnSettingChanged(object? sender, EventArgs e) + { + await ApplyAutoRun(); + } + + private async void UIAutoRunDelayOnSettingChanged(object? sender, EventArgs e) + { + if (_autoRunProvider == null || !UIAutoRun.Value) + return; + + try + { + await _autoRunProvider.EnableAutoRun(true, UIAutoRunDelay.Value); + } + catch (Exception exception) + { + _windowService.ShowExceptionDialog("Failed to apply auto-run", exception); + } + } } \ No newline at end of file diff --git a/src/Artemis.UI/Screens/StartupWizard/Steps/SettingsStep.axaml b/src/Artemis.UI/Screens/StartupWizard/Steps/SettingsStep.axaml index 04775f5eb..59b270b13 100644 --- a/src/Artemis.UI/Screens/StartupWizard/Steps/SettingsStep.axaml +++ b/src/Artemis.UI/Screens/StartupWizard/Steps/SettingsStep.axaml @@ -3,6 +3,9 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:startupWizard="clr-namespace:Artemis.UI.Screens.StartupWizard" + xmlns:controls="clr-namespace:FluentAvalonia.UI.Controls;assembly=FluentAvalonia" + xmlns:behaviors="clr-namespace:Artemis.UI.Shared.Behaviors;assembly=Artemis.UI.Shared" + xmlns:shared="clr-namespace:Artemis.UI.Shared;assembly=Artemis.UI.Shared" mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="Artemis.UI.Screens.StartupWizard.Steps.SettingsStep" x:DataType="startupWizard:StartupWizardViewModel"> @@ -15,60 +18,94 @@ Below you can find a few relevant settings, many more can be changed later on the settings page. - - - - - Auto-run on startup - - - - + + + + Auto-run + + + + + + + Auto-run on startup + + + + - - - Hide window on auto-run - - - - - - + + + Hide window on auto-run + + + + + + - - - Startup delay - - Set the amount of seconds to wait before auto-running Artemis. - - - If some devices don't work because Artemis starts before the manufacturer's software, try increasing this value. - + + + Startup delay + + Set the amount of seconds to wait before auto-running Artemis. + + + If some devices don't work because Artemis starts before the manufacturer's software, try increasing this value. + + + + + + + + + sec + + - - - sec - - - + + + - - - - Check for updates - - - If enabled, we'll check for updates on startup and periodically while running. - - - - - - - - + + + + Updating + + + + + + + Check for updates + + + If enabled, we'll check for updates on startup and periodically while running. + + + + + + + + + + + + Auto-install updates + + + If enabled, new updates will automatically be installed. + + + + + + + + + - - \ No newline at end of file