1
0
mirror of https://github.com/Artemis-RGB/Artemis synced 2025-12-13 05:48:35 +00:00

UI - Implemented plugin feature VMs

This commit is contained in:
Robert Beekman 2021-11-08 17:13:06 +01:00
parent 8a7f8cff96
commit c178fc6cf8
21 changed files with 222 additions and 274 deletions

View File

@ -76,15 +76,11 @@ namespace Artemis.UI.Avalonia.Shared.Services.Builders
return this;
}
public ContentDialogBuilder WithViewModel<T>(params (string name, object value)[] parameters)
public ContentDialogBuilder WithViewModel<T>(ref T viewModel, params (string name, object value)[] parameters)
{
if (parameters.Length != 0)
{
IParameter[] paramsArray = parameters.Select(kv => new ConstructorArgument(kv.name, kv.value)).Cast<IParameter>().ToArray();
_contentDialog.Content = _kernel.Get<T>(paramsArray);
}
else
_contentDialog.Content = _kernel.Get<T>();
IParameter[] paramsArray = parameters.Select(kv => new ConstructorArgument(kv.name, kv.value)).Cast<IParameter>().ToArray();
viewModel = _kernel.Get<T>(paramsArray);
_contentDialog.Content = _kernel.Get<T>();
return this;
}

View File

@ -4,6 +4,7 @@ using Artemis.UI.Avalonia.Shared.Utilities;
using Avalonia.Controls;
using Avalonia.Threading;
using FluentAvalonia.UI.Controls;
using ReactiveUI;
using Button = Avalonia.Controls.Button;
namespace Artemis.UI.Avalonia.Shared.Services.Builders
@ -106,7 +107,7 @@ namespace Artemis.UI.Avalonia.Shared.Services.Builders
public IControl Build()
{
return _action != null
? new Button {Content = _text, Command = new DelegateCommand(_ => _action())}
? new Button {Content = _text, Command = ReactiveCommand.Create(() => _action)}
: new Button {Content = _text};
}
}

View File

@ -8,11 +8,11 @@ namespace Artemis.UI.Avalonia.Shared.Services.Interfaces
public interface IWindowService : IArtemisSharedUIService
{
/// <summary>
/// Creates a view model instance of type <typeparamref name="T" /> and shows its corresponding View as a window
/// Creates a view model instance of type <typeparamref name="TViewModel" /> and shows its corresponding View as a window
/// </summary>
/// <typeparam name="T">The type of view model to create</typeparam>
/// <typeparam name="TViewModel">The type of view model to create</typeparam>
/// <returns>The created view model</returns>
T ShowWindow<T>();
TViewModel ShowWindow<TViewModel>(params (string name, object value)[] parameters);
/// <summary>
/// Given a ViewModel, show its corresponding View as a window
@ -28,12 +28,20 @@ namespace Artemis.UI.Avalonia.Shared.Services.Interfaces
void ShowExceptionDialog(string title, Exception exception);
/// <summary>
/// Given a ViewModel, show its corresponding View as a Dialog
/// Given an existing ViewModel, show its corresponding View as a Dialog
/// </summary>
/// <typeparam name="T">The return type</typeparam>
/// <typeparam name="TResult">The return type</typeparam>
/// <param name="viewModel">ViewModel to show the View for</param>
/// <returns>A task containing the return value of type <typeparamref name="T" /></returns>
Task<T> ShowDialogAsync<T>(object viewModel);
/// <returns>A task containing the return value of type <typeparamref name="TResult" /></returns>
Task<TResult> ShowDialogAsync<TResult>(DialogViewModelBase<TResult> viewModel);
/// <summary>
/// Creates a view model instance of type <typeparamref name="TViewModel"/> and shows its corresponding View as a Dialog
/// </summary>
/// <typeparam name="TViewModel">The view model type</typeparam>
/// <typeparam name="TResult">The return type</typeparam>
/// <returns>A task containing the return value of type <typeparamref name="TResult" /></returns>
Task<TResult> ShowDialogAsync<TViewModel, TResult>(params (string name, object value)[] parameters) where TViewModel : DialogViewModelBase<TResult>;
/// <summary>
/// Creates an open file dialog, use the fluent API to configure it
@ -49,6 +57,8 @@ namespace Artemis.UI.Avalonia.Shared.Services.Interfaces
ContentDialogBuilder CreateContentDialog();
ConfirmDialogBuilder CreateConfirmDialog();
Window GetCurrentWindow();
}
}

View File

@ -2,7 +2,7 @@
namespace Artemis.UI.Avalonia.Shared.Services
{
public class ExceptionDialogViewModel : ActivatableViewModelBase
public class ExceptionDialogViewModel : DialogViewModelBase<object>
{
public ExceptionDialogViewModel(string title, Exception exception)
{

View File

@ -8,6 +8,7 @@ using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Ninject;
using Ninject.Parameters;
namespace Artemis.UI.Avalonia.Shared.Services
{
@ -21,9 +22,10 @@ namespace Artemis.UI.Avalonia.Shared.Services
_kernel = kernel;
}
public T ShowWindow<T>()
public T ShowWindow<T>(params (string name, object value)[] parameters)
{
T viewModel = _kernel.Get<T>()!;
IParameter[] paramsArray = parameters.Select(kv => new ConstructorArgument(kv.name, kv.value)).Cast<IParameter>().ToArray();
T viewModel = _kernel.Get<T>(paramsArray)!;
ShowWindow(viewModel);
return viewModel;
}
@ -34,49 +36,69 @@ namespace Artemis.UI.Avalonia.Shared.Services
Type? type = viewModel.GetType().Assembly.GetType(name);
if (type == null)
{
throw new ArtemisSharedUIException($"Failed to find a window named {name}.");
}
if (!type.IsAssignableTo(typeof(Window)))
{
throw new ArtemisSharedUIException($"Type {name} is not a window.");
}
Window window = (Window) Activator.CreateInstance(type)!;
window.DataContext = viewModel;
window.Show();
}
/// <inheritdoc />
public void ShowExceptionDialog(string title, Exception exception)
public async Task<TResult> ShowDialogAsync<TViewModel, TResult>(params (string name, object value)[] parameters) where TViewModel : DialogViewModelBase<TResult>
{
if (_exceptionDialogOpen)
return;
try
{
_exceptionDialogOpen = true;
ShowDialogAsync<object>(new ExceptionDialogViewModel(title, exception)).GetAwaiter().GetResult();
}
finally
{
_exceptionDialogOpen = false;
}
IParameter[] paramsArray = parameters.Select(kv => new ConstructorArgument(kv.name, kv.value)).Cast<IParameter>().ToArray();
TViewModel viewModel = _kernel.Get<TViewModel>(paramsArray)!;
return await ShowDialogAsync(viewModel);
}
public async Task<T> ShowDialogAsync<T>(object viewModel)
public async Task<TResult> ShowDialogAsync<TResult>(DialogViewModelBase<TResult> viewModel)
{
if (Application.Current.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime classic)
throw new ArtemisSharedUIException($"Can't show a dialog when application lifetime is not IClassicDesktopStyleApplicationLifetime.");
{
throw new ArtemisSharedUIException("Can't show a dialog when application lifetime is not IClassicDesktopStyleApplicationLifetime.");
}
string name = viewModel.GetType().FullName!.Split('`')[0].Replace("ViewModel", "View");
Type? type = viewModel.GetType().Assembly.GetType(name);
if (type == null)
{
throw new ArtemisSharedUIException($"Failed to find a window named {name}.");
}
if (!type.IsAssignableTo(typeof(Window)))
{
throw new ArtemisSharedUIException($"Type {name} is not a window.");
}
Window window = (Window) Activator.CreateInstance(type)!;
window.DataContext = viewModel;
Window parent = classic.Windows.FirstOrDefault(w => w.IsActive) ?? classic.MainWindow;
return await window.ShowDialog<T>(parent);
return await window.ShowDialog<TResult>(parent);
}
public void ShowExceptionDialog(string title, Exception exception)
{
if (_exceptionDialogOpen)
{
return;
}
try
{
_exceptionDialogOpen = true;
ShowDialogAsync(new ExceptionDialogViewModel(title, exception)).GetAwaiter().GetResult();
}
finally
{
_exceptionDialogOpen = false;
}
}
public ContentDialogBuilder CreateContentDialog()
@ -97,7 +119,9 @@ namespace Artemis.UI.Avalonia.Shared.Services
public Window GetCurrentWindow()
{
if (Application.Current.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime classic)
{
throw new ArtemisSharedUIException("Can't show a dialog when application lifetime is not IClassicDesktopStyleApplicationLifetime.");
}
Window parent = classic.Windows.FirstOrDefault(w => w.IsActive) ?? classic.MainWindow;
return parent;

View File

@ -1,60 +0,0 @@
using System;
using System.Windows.Input;
namespace Artemis.UI.Avalonia.Shared.Utilities
{
/// <summary>
/// Provides a command that simply calls a delegate when invoked
/// </summary>
public class DelegateCommand : ICommand
{
private readonly Predicate<object?>? _canExecute;
private readonly Action<object?> _execute;
/// <summary>
/// Creates a new instance of the <see cref="DelegateCommand" /> class
/// </summary>
/// <param name="execute">The delegate to execute</param>
public DelegateCommand(Action<object?> execute) : this(execute, null)
{
}
/// <summary>
/// Creates a new instance of the <see cref="DelegateCommand" /> class with a predicate indicating whether the command
/// can be executed
/// </summary>
/// <param name="execute">The delegate to execute</param>
/// <param name="canExecute">The predicate that determines whether the command can execute</param>
public DelegateCommand(Action<object?> execute, Predicate<object?>? canExecute)
{
_execute = execute;
_canExecute = canExecute;
}
/// <summary>
/// Invokes the <see cref="CanExecuteChanged" /> event
/// </summary>
public void RaiseCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, EventArgs.Empty);
}
/// <inheritdoc />
public event EventHandler? CanExecuteChanged;
/// <inheritdoc />
public bool CanExecute(object? parameter)
{
if (_canExecute == null)
return true;
return _canExecute(parameter);
}
/// <inheritdoc />
public void Execute(object? parameter)
{
_execute(parameter);
}
}
}

View File

@ -6,14 +6,14 @@ using ReactiveUI;
namespace Artemis.UI.Avalonia.Shared
{
/// <summary>
/// Represents the base class for Artemis view models
/// Represents the base class for Artemis view models
/// </summary>
public abstract class ViewModelBase : ReactiveObject
{
private string? _displayName;
/// <summary>
/// Gets or sets the display name of the view model
/// Gets or sets the display name of the view model
/// </summary>
public string? DisplayName
{
@ -23,7 +23,7 @@ namespace Artemis.UI.Avalonia.Shared
}
/// <summary>
/// Represents the base class for Artemis view models that are interested in the activated event
/// Represents the base class for Artemis view models that are interested in the activated event
/// </summary>
public abstract class ActivatableViewModelBase : ViewModelBase, IActivatableViewModel, IDisposable
{
@ -34,17 +34,14 @@ namespace Artemis.UI.Avalonia.Shared
}
/// <summary>
/// Releases the unmanaged resources used by the object and optionally releases the managed resources.
/// Releases the unmanaged resources used by the object and optionally releases the managed resources.
/// </summary>
/// <param name="disposing">
/// <see langword="true" /> to release both managed and unmanaged resources;
/// <see langword="false" /> to release only unmanaged resources.
/// <see langword="true" /> to release both managed and unmanaged resources;
/// <see langword="false" /> to release only unmanaged resources.
/// </param>
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
}
}
/// <inheritdoc />
@ -59,11 +56,10 @@ namespace Artemis.UI.Avalonia.Shared
}
/// <summary>
/// Represents the base class for Artemis view models used to drive dialogs
/// Represents the base class for Artemis view models used to drive dialogs
/// </summary>
public abstract class DialogViewModelBase<TResult> : ActivatableViewModelBase
{
/// <inheritdoc />
protected DialogViewModelBase()
{
@ -71,9 +67,8 @@ namespace Artemis.UI.Avalonia.Shared
Cancel = ReactiveCommand.Create(() => { });
}
/// <summary>
/// Closes the dialog with a given result
/// Closes the dialog with a given result
/// </summary>
public ReactiveCommand<TResult, TResult> Close { get; }

View File

@ -2,6 +2,7 @@
using Artemis.Core;
using Artemis.UI.Avalonia.Screens.Device;
using Artemis.UI.Avalonia.Screens.Device.Tabs.ViewModels;
using Artemis.UI.Avalonia.Screens.Plugins.ViewModels;
using Artemis.UI.Avalonia.Screens.Root.ViewModels;
using Artemis.UI.Avalonia.Screens.SurfaceEditor.ViewModels;
using ReactiveUI;
@ -33,4 +34,9 @@ namespace Artemis.UI.Avalonia.Ninject.Factories
SurfaceDeviceViewModel SurfaceDeviceViewModel(ArtemisDevice device);
ListDeviceViewModel ListDeviceViewModel(ArtemisDevice device);
}
public interface IPrerequisitesVmFactory : IVmFactory
{
PluginPrerequisiteViewModel PluginPrerequisiteViewModel(PluginPrerequisite pluginPrerequisite, bool uninstall);
}
}

View File

@ -6,11 +6,12 @@ using System.Linq;
using System.Threading.Tasks;
using Artemis.Core;
using Artemis.Core.Services;
using Artemis.UI.Avalonia.Shared;
using Artemis.UI.Avalonia.Shared.Services.Builders;
using Artemis.UI.Avalonia.Shared.Services.Interfaces;
using ReactiveUI;
namespace Artemis.UI.Avalonia.Screens.Settings.Tabs.Plugins.ViewModels
namespace Artemis.UI.Avalonia.Screens.Plugins.ViewModels
{
public class PluginFeatureViewModel : ActivatableViewModelBase
{

View File

@ -1,10 +1,7 @@
using System;
using System.ComponentModel;
using Artemis.Core;
using Artemis.UI.Avalonia;
using Artemis.Core;
using Artemis.UI.Avalonia.Shared;
namespace Artemis.UI.Screens.Plugins
namespace Artemis.UI.Avalonia.Screens.Plugins.ViewModels
{
public class PluginPrerequisiteActionViewModel : ViewModelBase
{

View File

@ -1,26 +1,51 @@
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Artemis.Core;
using Artemis.Core.Services;
using Artemis.UI.Shared.Services;
using Stylet;
using Artemis.UI.Avalonia.Shared;
using DynamicData;
using ReactiveUI;
namespace Artemis.UI.Screens.Plugins
namespace Artemis.UI.Avalonia.Screens.Plugins.ViewModels
{
public class PluginPrerequisiteViewModel : Conductor<PluginPrerequisiteActionViewModel>.Collection.OneActive
public class PluginPrerequisiteViewModel : ActivatableViewModelBase
{
private readonly bool _uninstall;
private readonly ObservableAsPropertyHelper<bool> _busy;
private readonly ObservableAsPropertyHelper<int> _activeStepNumber;
private bool _installing;
private bool _uninstalling;
private bool _isMet;
private PluginPrerequisiteActionViewModel? _activeAction;
public PluginPrerequisiteViewModel(PluginPrerequisite pluginPrerequisite, bool uninstall)
{
_uninstall = uninstall;
PluginPrerequisite = pluginPrerequisite;
Actions = new ObservableCollection<PluginPrerequisiteActionViewModel>(!_uninstall
? PluginPrerequisite.InstallActions.Select(a => new PluginPrerequisiteActionViewModel(a))
: PluginPrerequisite.UninstallActions.Select(a => new PluginPrerequisiteActionViewModel(a)));
this.WhenAnyValue(x => x.Installing, x => x.Uninstalling, (i, u) => i || u).ToProperty(this, x => x.Busy, out _busy);
this.WhenAnyValue(x => x.ActiveAction, a => Actions.IndexOf(a)).ToProperty(this, x => x.ActiveStepNumber, out _activeStepNumber);
PluginPrerequisite.PropertyChanged += PluginPrerequisiteOnPropertyChanged;
// Could be slow so take it off of the UI thread
Task.Run(() => IsMet = PluginPrerequisite.IsMet());
}
public ObservableCollection<PluginPrerequisiteActionViewModel> Actions { get; }
public PluginPrerequisiteActionViewModel? ActiveAction
{
get => _activeAction;
set => this.RaiseAndSetIfChanged(ref _activeAction , value);
}
public PluginPrerequisite PluginPrerequisite { get; }
@ -28,32 +53,24 @@ namespace Artemis.UI.Screens.Plugins
public bool Installing
{
get => _installing;
set
{
SetAndNotify(ref _installing, value);
NotifyOfPropertyChange(nameof(Busy));
}
set => this.RaiseAndSetIfChanged(ref _installing, value);
}
public bool Uninstalling
{
get => _uninstalling;
set
{
SetAndNotify(ref _uninstalling, value);
NotifyOfPropertyChange(nameof(Busy));
}
set => this.RaiseAndSetIfChanged(ref _uninstalling, value);
}
public bool IsMet
{
get => _isMet;
set => SetAndNotify(ref _isMet, value);
set => this.RaiseAndSetIfChanged(ref _isMet, value);
}
public bool Busy => Installing || Uninstalling;
public int ActiveStemNumber => Items.IndexOf(ActiveItem) + 1;
public bool HasMultipleActions => Items.Count > 1;
public bool Busy => _busy.Value;
public int ActiveStepNumber => _activeStepNumber.Value;
public bool HasMultipleActions => Actions.Count > 1;
public async Task Install(CancellationToken cancellationToken)
{
@ -89,7 +106,7 @@ namespace Artemis.UI.Screens.Plugins
}
}
private void PluginPrerequisiteOnPropertyChanged(object sender, PropertyChangedEventArgs e)
private void PluginPrerequisiteOnPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(PluginPrerequisite.CurrentAction))
ActivateCurrentAction();
@ -97,37 +114,27 @@ namespace Artemis.UI.Screens.Plugins
private void ActivateCurrentAction()
{
PluginPrerequisiteActionViewModel newActiveItem = Items.FirstOrDefault(i => i.Action == PluginPrerequisite.CurrentAction);
if (newActiveItem == null)
PluginPrerequisiteActionViewModel? activeAction = Actions.FirstOrDefault(i => i.Action == PluginPrerequisite.CurrentAction);
if (activeAction == null)
return;
ActiveItem = newActiveItem;
NotifyOfPropertyChange(nameof(ActiveStemNumber));
ActiveAction = activeAction;
}
#region Overrides of Screen
#region IDisposable
/// <inheritdoc />
protected override void OnClose()
protected override void Dispose(bool disposing)
{
PluginPrerequisite.PropertyChanged -= PluginPrerequisiteOnPropertyChanged;
base.OnClose();
}
if (disposing)
{
PluginPrerequisite.PropertyChanged -= PluginPrerequisiteOnPropertyChanged;
}
/// <inheritdoc />
protected override void OnInitialActivate()
{
PluginPrerequisite.PropertyChanged += PluginPrerequisiteOnPropertyChanged;
// Could be slow so take it off of the UI thread
Task.Run(() => IsMet = PluginPrerequisite.IsMet());
Items.AddRange(!_uninstall
? PluginPrerequisite.InstallActions.Select(a => new PluginPrerequisiteActionViewModel(a))
: PluginPrerequisite.UninstallActions.Select(a => new PluginPrerequisiteActionViewModel(a)));
base.OnInitialActivate();
base.Dispose(disposing);
}
#endregion
}
}

View File

@ -5,10 +5,12 @@ using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Artemis.Core;
using Artemis.UI.Avalonia.Ninject.Factories;
using Artemis.UI.Avalonia.Shared;
using Artemis.UI.Avalonia.Shared.Services.Interfaces;
using ReactiveUI;
namespace Artemis.UI.Screens.Plugins
namespace Artemis.UI.Avalonia.Screens.Plugins.ViewModels
{
public class PluginPrerequisitesInstallDialogViewModel : DialogViewModelBase<bool>
{
@ -20,14 +22,14 @@ namespace Artemis.UI.Screens.Plugins
private bool _showProgress;
private CancellationTokenSource? _tokenSource;
public PluginPrerequisitesInstallDialogViewModel(List<IPrerequisitesSubject> subjects, IPrerequisitesVmFactory prerequisitesVmFactory, IDialogService dialogService)
public PluginPrerequisitesInstallDialogViewModel(List<IPrerequisitesSubject> subjects, IPrerequisitesVmFactory prerequisitesVmFactory)
{
Prerequisites = new ObservableCollection<PluginPrerequisiteViewModel>();
foreach (IPrerequisitesSubject prerequisitesSubject in subjects)
Prerequisites.AddRange(prerequisitesSubject.Prerequisites.Select(p => prerequisitesVmFactory.PluginPrerequisiteViewModel(p, false)));
foreach (PluginPrerequisite prerequisite in subjects.SelectMany(prerequisitesSubject => prerequisitesSubject.Prerequisites))
Prerequisites.Add(prerequisitesVmFactory.PluginPrerequisiteViewModel(prerequisite, false));
foreach (PluginPrerequisiteViewModel pluginPrerequisiteViewModel in Prerequisites)
pluginPrerequisiteViewModel.ConductWith(this);
CanInstall = false;
Task.Run(() => CanInstall = Prerequisites.Any(p => !p.PluginPrerequisite.IsMet()));
}
public ObservableCollection<PluginPrerequisiteViewModel> Prerequisites { get; }
@ -68,7 +70,7 @@ namespace Artemis.UI.Screens.Plugins
set => this.RaiseAndSetIfChanged(ref _canInstall, value);
}
public async void Install()
public async Task Install()
{
CanInstall = false;
ShowFailed = false;
@ -116,13 +118,12 @@ namespace Artemis.UI.Screens.Plugins
public void Accept()
{
Result = true;
Close.Execute();
Close.Execute(true);
}
public static Task<object> Show(IDialogService dialogService, List<IPrerequisitesSubject> subjects)
public static async Task<bool> Show(IWindowService windowService, List<IPrerequisitesSubject> subjects)
{
return dialogService.ShowDialog<PluginPrerequisitesInstallDialogViewModel>(new Dictionary<string, object> {{"subjects", subjects}});
return await windowService.ShowDialogAsync<PluginPrerequisitesInstallDialogViewModel, bool>(("subjects", subjects));
}
/// <inheritdoc />
@ -136,18 +137,5 @@ namespace Artemis.UI.Screens.Plugins
base.Dispose(disposing);
}
#region Overrides of Screen
/// <inheritdoc />
protected override void OnInitialActivate()
{
CanInstall = false;
Task.Run(() => CanInstall = Prerequisites.Any(p => !p.PluginPrerequisite.IsMet()));
base.OnInitialActivate();
}
#endregion
}
}

View File

@ -1,76 +1,79 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Artemis.Core;
using Artemis.Core.Services;
using Artemis.UI.Ninject.Factories;
using Artemis.UI.Shared.Services;
using MaterialDesignThemes.Wpf;
using Stylet;
using Artemis.UI.Avalonia.Ninject.Factories;
using Artemis.UI.Avalonia.Shared;
using Artemis.UI.Avalonia.Shared.Services.Interfaces;
using DynamicData;
using ReactiveUI;
namespace Artemis.UI.Screens.Plugins
namespace Artemis.UI.Avalonia.Screens.Plugins.ViewModels
{
public class PluginPrerequisitesUninstallDialogViewModel : DialogViewModelBase
public class PluginPrerequisitesUninstallDialogViewModel : DialogViewModelBase<bool>
{
private readonly IDialogService _dialogService;
private readonly IPluginManagementService _pluginManagementService;
private readonly List<IPrerequisitesSubject> _subjects;
private PluginPrerequisiteViewModel _activePrerequisite;
private readonly IWindowService _windowService;
private bool _canUninstall;
private bool _isFinished;
private CancellationTokenSource _tokenSource;
private PluginPrerequisiteViewModel? _activePrerequisite;
private CancellationTokenSource? _tokenSource;
public PluginPrerequisitesUninstallDialogViewModel(List<IPrerequisitesSubject> subjects, string cancelLabel, IPrerequisitesVmFactory prerequisitesVmFactory,
IDialogService dialogService, IPluginManagementService pluginManagementService)
public PluginPrerequisitesUninstallDialogViewModel(List<IPrerequisitesSubject> subjects, string cancelLabel, IPrerequisitesVmFactory prerequisitesVmFactory, IWindowService windowService,
IPluginManagementService pluginManagementService)
{
_subjects = subjects;
_dialogService = dialogService;
_windowService = windowService;
_pluginManagementService = pluginManagementService;
CancelLabel = cancelLabel;
Prerequisites = new BindableCollection<PluginPrerequisiteViewModel>();
foreach (IPrerequisitesSubject prerequisitesSubject in subjects)
Prerequisites.AddRange(prerequisitesSubject.Prerequisites.Select(p => prerequisitesVmFactory.PluginPrerequisiteViewModel(p, true)));
Prerequisites = new ObservableCollection<PluginPrerequisiteViewModel>();
foreach (PluginPrerequisite prerequisite in subjects.SelectMany(prerequisitesSubject => prerequisitesSubject.Prerequisites))
Prerequisites.Add(prerequisitesVmFactory.PluginPrerequisiteViewModel(prerequisite, true));
foreach (PluginPrerequisiteViewModel pluginPrerequisiteViewModel in Prerequisites)
pluginPrerequisiteViewModel.ConductWith(this);
// Could be slow so take it off of the UI thread
Task.Run(() => CanUninstall = Prerequisites.Any(p => p.PluginPrerequisite.IsMet()));
}
public string CancelLabel { get; }
public BindableCollection<PluginPrerequisiteViewModel> Prerequisites { get; }
public ObservableCollection<PluginPrerequisiteViewModel> Prerequisites { get; }
public PluginPrerequisiteViewModel ActivePrerequisite
public PluginPrerequisiteViewModel? ActivePrerequisite
{
get => _activePrerequisite;
set => SetAndNotify(ref _activePrerequisite, value);
set => this.RaiseAndSetIfChanged(ref _activePrerequisite, value);
}
public bool CanUninstall
{
get => _canUninstall;
set => SetAndNotify(ref _canUninstall, value);
set => this.RaiseAndSetIfChanged(ref _canUninstall, value);
}
public bool IsFinished
{
get => _isFinished;
set => SetAndNotify(ref _isFinished, value);
set => this.RaiseAndSetIfChanged(ref _isFinished, value);
}
#region Overrides of DialogViewModelBase
/// <inheritdoc />
public override void OnDialogClosed(object sender, DialogClosingEventArgs e)
protected override void Dispose(bool disposing)
{
_tokenSource?.Cancel();
base.OnDialogClosed(sender, e);
if (disposing)
{
_tokenSource?.Cancel();
_tokenSource?.Dispose();
}
base.Dispose(disposing);
}
#endregion
public async void Uninstall()
public async Task Uninstall()
{
CanUninstall = false;
@ -78,20 +81,28 @@ namespace Artemis.UI.Screens.Plugins
foreach (IPrerequisitesSubject prerequisitesSubject in _subjects)
{
if (prerequisitesSubject is PluginInfo pluginInfo)
{
_pluginManagementService.DisablePlugin(pluginInfo.Plugin, true);
}
}
// Disable all subjects that are features if still required
foreach (IPrerequisitesSubject prerequisitesSubject in _subjects)
{
if (prerequisitesSubject is not PluginFeatureInfo featureInfo)
if (prerequisitesSubject is not PluginFeatureInfo featureInfo)
{
continue;
}
// Disable the parent plugin if the feature is AlwaysEnabled
if (featureInfo.AlwaysEnabled)
{
_pluginManagementService.DisablePlugin(featureInfo.Plugin, true);
else if (featureInfo.Instance != null)
}
else if (featureInfo.Instance != null)
{
_pluginManagementService.DisablePluginFeature(featureInfo.Instance, true);
}
}
_tokenSource = new CancellationTokenSource();
@ -102,14 +113,18 @@ namespace Artemis.UI.Screens.Plugins
{
pluginPrerequisiteViewModel.IsMet = pluginPrerequisiteViewModel.PluginPrerequisite.IsMet();
if (!pluginPrerequisiteViewModel.IsMet)
{
continue;
}
ActivePrerequisite = pluginPrerequisiteViewModel;
await ActivePrerequisite.Uninstall(_tokenSource.Token);
// Wait after the task finished for the user to process what happened
if (pluginPrerequisiteViewModel != Prerequisites.Last())
{
await Task.Delay(1000);
}
}
if (Prerequisites.All(p => !p.IsMet))
@ -120,14 +135,12 @@ namespace Artemis.UI.Screens.Plugins
// This shouldn't be happening and the experience isn't very nice for the user (too lazy to make a nice UI for such an edge case)
// but at least give some feedback
Session?.Close(false);
await _dialogService.ShowConfirmDialog(
"Plugin prerequisites",
"The plugin was not able to fully remove all prerequisites. \r\nPlease try again or contact the plugin creator.",
"Confirm",
""
);
await Show(_dialogService, _subjects);
Close.Execute(false);
await _windowService.CreateContentDialog()
.WithTitle("Plugin prerequisites")
.WithContent("The plugin was not able to fully remove all prerequisites. \r\nPlease try again or contact the plugin creator.")
.ShowAsync();
await Show(_windowService, _subjects);
}
catch (OperationCanceledException)
{
@ -143,30 +156,12 @@ namespace Artemis.UI.Screens.Plugins
public void Accept()
{
Session?.Close(true);
Close.Execute(true);
}
public static Task<object> Show(IDialogService dialogService, List<IPrerequisitesSubject> subjects, string cancelLabel = "CANCEL")
public static async Task<object> Show(IWindowService windowService, List<IPrerequisitesSubject> subjects, string cancelLabel = "Cancel")
{
return dialogService.ShowDialog<PluginPrerequisitesUninstallDialogViewModel>(new Dictionary<string, object>
{
{"subjects", subjects},
{"cancelLabel", cancelLabel},
});
return await windowService.ShowDialogAsync<PluginPrerequisitesUninstallDialogViewModel, bool>(("subjects", subjects), ("cancelLabel", cancelLabel));
}
#region Overrides of Screen
/// <inheritdoc />
protected override void OnInitialActivate()
{
CanUninstall = false;
// Could be slow so take it off of the UI thread
Task.Run(() => CanUninstall = Prerequisites.Any(p => p.PluginPrerequisite.IsMet()));
base.OnInitialActivate();
}
#endregion
}
}

View File

@ -1,28 +1,23 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Artemis.Core;
using Artemis.Core.Services;
using Artemis.UI.Avalonia.Screens.Settings.Tabs.Plugins.ViewModels;
using Artemis.UI.Ninject.Factories;
using Artemis.UI.Screens.Plugins;
using Artemis.UI.Shared.Services;
using Artemis.UI.Avalonia.Shared;
using Ninject;
using Stylet;
using ReactiveUI;
namespace Artemis.UI.Screens.Settings.Tabs.Plugins
namespace Artemis.UI.Avalonia.Screens.Plugins.ViewModels
{
public class PluginSettingsViewModel : Conductor<PluginFeatureViewModel>.Collection.AllActive
public class PluginSettingsViewModel : ActivatableViewModelBase
{
private readonly ICoreService _coreService;
private readonly IDialogService _dialogService;
private readonly IMessageService _messageService;
private readonly IPluginManagementService _pluginManagementService;
private readonly ISettingsVmFactory _settingsVmFactory;
private readonly IWindowManager _windowManager;
private bool _canInstallPrerequisites;
private bool _canRemovePrerequisites;
private bool _enabling;
@ -32,31 +27,27 @@ namespace Artemis.UI.Screens.Settings.Tabs.Plugins
public PluginSettingsViewModel(Plugin plugin,
ISettingsVmFactory settingsVmFactory,
ICoreService coreService,
IWindowManager windowManager,
IDialogService dialogService,
IPluginManagementService pluginManagementService,
IMessageService messageService)
IPluginManagementService pluginManagementService)
{
Plugin = plugin;
_settingsVmFactory = settingsVmFactory;
_coreService = coreService;
_windowManager = windowManager;
_dialogService = dialogService;
_pluginManagementService = pluginManagementService;
_messageService = messageService;
}
public ObservableCollection<PluginFeatureViewModel> PluginFeatures { get; }
public Plugin Plugin
{
get => _plugin;
set => SetAndNotify(ref _plugin, value);
set => this.RaiseAndSetIfChanged(ref _plugin, value);
}
public bool Enabling
{
get => _enabling;
set => SetAndNotify(ref _enabling, value);
set => this.RaiseAndSetIfChanged(ref _enabling, value);
}
public string Type => Plugin.GetType().BaseType?.Name ?? Plugin.GetType().Name;
@ -73,7 +64,7 @@ namespace Artemis.UI.Screens.Settings.Tabs.Plugins
get => _isSettingsPopupOpen;
set
{
if (!SetAndNotify(ref _isSettingsPopupOpen, value)) return;
if (!this.RaiseAndSetIfChanged(ref _isSettingsPopupOpen, value)) return;
CheckPrerequisites();
}
}
@ -81,13 +72,13 @@ namespace Artemis.UI.Screens.Settings.Tabs.Plugins
public bool CanInstallPrerequisites
{
get => _canInstallPrerequisites;
set => SetAndNotify(ref _canInstallPrerequisites, value);
set => this.RaiseAndSetIfChanged(ref _canInstallPrerequisites, value);
}
public bool CanRemovePrerequisites
{
get => _canRemovePrerequisites;
set => SetAndNotify(ref _canRemovePrerequisites, value);
set => this.RaiseAndSetIfChanged(ref _canRemovePrerequisites, value);
}
public void OpenSettings()

View File

@ -1,8 +1,7 @@
using System;
using Artemis.Core;
using Stylet;
namespace Artemis.UI.Screens.Settings.Tabs.Plugins
namespace Artemis.UI.Avalonia.Screens.Plugins.ViewModels
{
public class PluginSettingsWindowViewModel : Conductor<PluginConfigurationViewModel>
{

View File

@ -3,6 +3,6 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Artemis.UI.Avalonia.Screens.Settings.Tabs.Plugins.Views.PluginFeatureView">
x:Class="Artemis.UI.Avalonia.Screens.Plugins.Views.PluginFeatureView">
Welcome to Avalonia!
</UserControl>

View File

@ -1,8 +1,7 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace Artemis.UI.Avalonia.Screens.Settings.Tabs.Plugins.Views
namespace Artemis.UI.Avalonia.Screens.Plugins.Views
{
public partial class PluginFeatureView : UserControl
{

View File

@ -3,6 +3,6 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Artemis.UI.Avalonia.Screens.Settings.Tabs.Plugins.Views.PluginSettingsView">
x:Class="Artemis.UI.Avalonia.Screens.Plugins.Views.PluginSettingsView">
Welcome to Avalonia!
</UserControl>

View File

@ -1,8 +1,7 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace Artemis.UI.Avalonia.Screens.Settings.Tabs.Plugins.Views
namespace Artemis.UI.Avalonia.Screens.Plugins.Views
{
public partial class PluginSettingsView : UserControl
{

View File

@ -3,7 +3,7 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
x:Class="Artemis.UI.Avalonia.Screens.Settings.Tabs.Plugins.Views.PluginSettingsWindowView"
x:Class="Artemis.UI.Avalonia.Screens.Plugins.Views.PluginSettingsWindowView"
Title="PluginSettingsWindowView">
Welcome to Avalonia!
</Window>

View File

@ -2,7 +2,7 @@ using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
namespace Artemis.UI.Avalonia.Screens.Settings.Tabs.Plugins.Views
namespace Artemis.UI.Avalonia.Screens.Plugins.Views
{
public partial class PluginSettingsWindowView : Window
{