From e5e0c574a7465d0d7db92c2b0b534f7d3bd990df Mon Sep 17 00:00:00 2001 From: Simon G Date: Wed, 14 Apr 2021 19:16:23 +0200 Subject: [PATCH] - continue ui redesign --- Mystify/ViewModels/ChannelViewModel.cs | 31 ++++++++++--- .../ControllableSelectionViewModel.cs | 46 +++++++++++++++++++ Mystify/ViewModels/ControllableViewModel.cs | 36 +++++++++++++++ Mystify/ViewModels/MainWindowViewModel.cs | 2 +- .../Views/ControllableSelectionWindow.axaml | 38 +++++++++++++++ .../ControllableSelectionWindow.axaml.cs | 23 ++++++++++ 6 files changed, 168 insertions(+), 8 deletions(-) create mode 100644 Mystify/ViewModels/ControllableSelectionViewModel.cs create mode 100644 Mystify/ViewModels/ControllableViewModel.cs create mode 100644 Mystify/Views/ControllableSelectionWindow.axaml create mode 100644 Mystify/Views/ControllableSelectionWindow.axaml.cs diff --git a/Mystify/ViewModels/ChannelViewModel.cs b/Mystify/ViewModels/ChannelViewModel.cs index 6d3209e..062a919 100644 --- a/Mystify/ViewModels/ChannelViewModel.cs +++ b/Mystify/ViewModels/ChannelViewModel.cs @@ -2,8 +2,10 @@ // Created: 2021-04-14 // Copyright(c) 2021 SimonG. All Rights Reserved. +using System.Threading.Tasks; using System.Windows.Input; using Lib.Audio.Interfaces; +using Mystify.Views; using ReactiveUI; namespace Mystify.ViewModels @@ -11,24 +13,39 @@ namespace Mystify.ViewModels public class ChannelViewModel : ViewModelBase { private readonly IChannel _channel; + private readonly MainWindow? _mainWindow; + private readonly MainModel _mainModel; - public ChannelViewModel(IChannel channel) => _channel = channel; + public ChannelViewModel(IChannel channel, MainWindow? mainWindow, MainModel mainModel) + { + _channel = channel; + _mainWindow = mainWindow; + _mainModel = mainModel; + } - public IControllable? Controllable + public ControllableViewModel? Controllable { - get => _channel.Controllable; + get => _channel.Controllable == null ? null : new ControllableViewModel(_channel.Controllable); set { if (value != null) - _channel.MapControllable(value); + _channel.MapControllable(value.Controllable); } } public bool IsControllableMapped => Controllable != null; - public bool IsPopupOpen { get; private set; } - public ICommand SelectControllableCommand => ReactiveCommand.Create(SelectControllable); + public ICommand SelectControllableCommand => ReactiveCommand.CreateFromTask(SelectControllable); - private void SelectControllable() => IsPopupOpen = true; + private async Task SelectControllable() + { + ControllableSelectionWindow controllableSelectionWindow = + new() + { + DataContext = new ControllableSelectionViewModel(_mainModel.Controllables) + }; + + Controllable = await controllableSelectionWindow.ShowDialog(_mainWindow); + } } } \ No newline at end of file diff --git a/Mystify/ViewModels/ControllableSelectionViewModel.cs b/Mystify/ViewModels/ControllableSelectionViewModel.cs new file mode 100644 index 0000000..8cc613f --- /dev/null +++ b/Mystify/ViewModels/ControllableSelectionViewModel.cs @@ -0,0 +1,46 @@ +// Author: Simon Gockner +// Created: 2021-04-14 +// Copyright(c) 2021 SimonG. All Rights Reserved. + +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using Lib.Audio.Interfaces; + +namespace Mystify.ViewModels +{ + public class ControllableSelectionViewModel : ViewModelBase + { + public ControllableSelectionViewModel() + { + if (!IsInDesignMode) + throw new InvalidOperationException("Constructor is for design time usage only."); + + Controllables = new ObservableCollection(); + } + + public ControllableSelectionViewModel(List? controllables) + { + Controllables = controllables == null ? new ObservableCollection() : + controllables.Select(c => new ControllableViewModel(c)).ToObservableCollection(); + + foreach (var controllable in Controllables) + controllable.Selected += OnControllableSelected; + } + + public ObservableCollection Controllables { get; } + public ControllableViewModel? SelectedControllable { get; private set; } + + private void OnControllableSelected(object? sender, bool isSelected) + { + if (!isSelected) + return; + + if (sender is not ControllableViewModel controllableViewModel) + return; + + SelectedControllable = controllableViewModel; + } + } +} \ No newline at end of file diff --git a/Mystify/ViewModels/ControllableViewModel.cs b/Mystify/ViewModels/ControllableViewModel.cs new file mode 100644 index 0000000..2a540c9 --- /dev/null +++ b/Mystify/ViewModels/ControllableViewModel.cs @@ -0,0 +1,36 @@ +// Author: Simon Gockner +// Created: 2021-04-14 +// Copyright(c) 2021 SimonG. All Rights Reserved. + +using System; +using System.Windows.Input; +using Lib.Audio.Interfaces; +using ReactiveUI; + +namespace Mystify.ViewModels +{ + public class ControllableViewModel : ViewModelBase + { + private bool _isSelected; + + public ControllableViewModel(IControllable controllable) => Controllable = controllable; + + public IControllable Controllable { get; } + public string Name => Controllable.Name; + public string? IconPath => Controllable.IconPath; + + public bool IsSelected + { + get => _isSelected; + private set + { + _isSelected = value; + Selected?.Invoke(this, IsSelected); + } + } + + public ICommand SelectCommand => ReactiveCommand.Create(() => IsSelected = !IsSelected); + + public event EventHandler? Selected; + } +} \ No newline at end of file diff --git a/Mystify/ViewModels/MainWindowViewModel.cs b/Mystify/ViewModels/MainWindowViewModel.cs index 9df684e..99eab46 100644 --- a/Mystify/ViewModels/MainWindowViewModel.cs +++ b/Mystify/ViewModels/MainWindowViewModel.cs @@ -32,7 +32,7 @@ namespace Mystify.ViewModels _mainWindow = mainWindow; } - public ObservableCollection? Channels => _mainModel?.Channels?.Select(c => new ChannelViewModel(c)).ToObservableCollection(); + public ObservableCollection? Channels => _mainModel?.Channels?.Select(c => new ChannelViewModel(c, _mainWindow, _mainModel)).ToObservableCollection(); public List? Controllables => _mainModel?.Controllables; public IChannel? SelectedChannel diff --git a/Mystify/Views/ControllableSelectionWindow.axaml b/Mystify/Views/ControllableSelectionWindow.axaml new file mode 100644 index 0000000..6b8111c --- /dev/null +++ b/Mystify/Views/ControllableSelectionWindow.axaml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Mystify/Views/ControllableSelectionWindow.axaml.cs b/Mystify/Views/ControllableSelectionWindow.axaml.cs new file mode 100644 index 0000000..47fd7ba --- /dev/null +++ b/Mystify/Views/ControllableSelectionWindow.axaml.cs @@ -0,0 +1,23 @@ +// Author: Simon Gockner +// Created: 2021-04-14 +// Copyright(c) 2021 SimonG. All Rights Reserved. + +using Avalonia; +using Avalonia.Controls; +using Avalonia.Markup.Xaml; + +namespace Mystify.Views +{ + public class ControllableSelectionWindow : Window + { + public ControllableSelectionWindow() + { + InitializeComponent(); +#if DEBUG + this.AttachDevTools(); +#endif + } + + private void InitializeComponent() => AvaloniaXamlLoader.Load(this); + } +} \ No newline at end of file