diff --git a/Mystify/App.axaml.cs b/Mystify/App.axaml.cs index 3d20ab1..382fd4d 100644 --- a/Mystify/App.axaml.cs +++ b/Mystify/App.axaml.cs @@ -31,10 +31,10 @@ namespace Mystify _kernel = bootstrapper.BootstrapKernel(); MainModel mainModel = _kernel.Resolve(); - _mainWindow = new MainWindow - { - DataContext = new MainWindowViewModel(mainModel) - }; + _mainWindow = new MainWindow(); + + MainWindowViewModel mainWindowViewModel = new (mainModel, _mainWindow); + _mainWindow.DataContext = mainWindowViewModel; if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopStyleApplicationLifetime) { diff --git a/Mystify/MainModel.cs b/Mystify/MainModel.cs index 65711be..8eba761 100644 --- a/Mystify/MainModel.cs +++ b/Mystify/MainModel.cs @@ -2,26 +2,46 @@ // Created: 2021-04-09 // Copyright(c) 2021 SimonG. All Rights Reserved. +using System; using System.Collections.Generic; using Lib.Audio.Factories; using Lib.Audio.Interfaces; +using Lib.Driver.Factories; +using Lib.Driver.Interfaces; namespace Mystify { public class MainModel { - private readonly IDevice _device; - private readonly IControllableCollector _controllableCollector; + private readonly IDriverLoader _driverLoader; + private readonly IDeviceFactory _deviceFactory; + private readonly IControllableCollectorFactory _controllableCollectorFactory; + + private IDevice? _device; + private IControllableCollector? _controllableCollector; - public MainModel(IDeviceFactory deviceFactory, IControllableCollectorFactory controllableCollectorFactory) + public MainModel(IDriverLoaderFactory driverLoaderFactory, IDeviceFactory deviceFactory, IControllableCollectorFactory controllableCollectorFactory) + { + _driverLoader = driverLoaderFactory.Create(); + _deviceFactory = deviceFactory; + _controllableCollectorFactory = controllableCollectorFactory; + } + + public List? Channels => _device?.Channels; + public List? Controllables => _controllableCollector?.Controllables; + public string? DeviceName => _device?.Name; + + public void LoadDriverAndDevice(string driverPath) { - _device = deviceFactory.Create(); - _controllableCollector = controllableCollectorFactory.Create(); + IDriver? driver = _driverLoader.Load(driverPath); + + if (driver == null) + throw new Exception("Driver could not be loaded."); + + _device = _deviceFactory.Create(driver); + _controllableCollector = _controllableCollectorFactory.Create(); _device.StartCommunication(); } - - public List Channels => _device.Channels; - public List Controllables => _controllableCollector.Controllables; } } \ No newline at end of file diff --git a/Mystify/ViewModels/MainWindowViewModel.cs b/Mystify/ViewModels/MainWindowViewModel.cs index 96d985f..19f31fe 100644 --- a/Mystify/ViewModels/MainWindowViewModel.cs +++ b/Mystify/ViewModels/MainWindowViewModel.cs @@ -5,7 +5,10 @@ using System; using System.Collections.Generic; using System.Windows.Input; +using Avalonia.Controls; using Lib.Audio.Interfaces; +using Lib.Driver; +using Mystify.Views; using ReactiveUI; namespace Mystify.ViewModels @@ -13,6 +16,7 @@ namespace Mystify.ViewModels public class MainWindowViewModel : ViewModelBase { private readonly MainModel? _mainModel; + private readonly MainWindow? _mainWindow; public MainWindowViewModel() { @@ -20,7 +24,11 @@ namespace Mystify.ViewModels throw new InvalidOperationException("Constructor is for design time usage only."); } - public MainWindowViewModel(MainModel mainModel) => _mainModel = mainModel; + public MainWindowViewModel(MainModel mainModel, MainWindow? mainWindow) + { + _mainModel = mainModel; + _mainWindow = mainWindow; + } public List? Channels => _mainModel?.Channels; public List? Controllables => _mainModel?.Controllables; @@ -28,6 +36,19 @@ namespace Mystify.ViewModels public IChannel? SelectedChannel { get; set; } public IControllable? SelectedControllable { get; set; } + public string SelectedDeviceText => $"Selected Device: {_mainModel?.DeviceName ?? "-"}"; + + public ICommand SelectDriverCommand => ReactiveCommand.CreateFromTask(async () => + { + OpenFileDialog openFileDialog = new() + { + Filters = new List {new() {Extensions = new List {DriverLoader.DRIVER_FILE_EXTENSION}, Name = "Mystify Driver"}} + }; + + await openFileDialog.ShowAsync(_mainWindow); + + _mainModel?.LoadDriverAndDevice(openFileDialog.Directory); + }); public ICommand AllocateControllableCommand => ReactiveCommand.Create(() => { diff --git a/Mystify/Views/MainWindow.axaml b/Mystify/Views/MainWindow.axaml index d79e60f..2dd95b9 100644 --- a/Mystify/Views/MainWindow.axaml +++ b/Mystify/Views/MainWindow.axaml @@ -14,7 +14,12 @@ - + + +