- add load driver functionality

master
Simon G 5 years ago
parent 4f4d9e9f4b
commit b996ad9b30
  1. 8
      Mystify/App.axaml.cs
  2. 36
      Mystify/MainModel.cs
  3. 23
      Mystify/ViewModels/MainWindowViewModel.cs
  4. 7
      Mystify/Views/MainWindow.axaml

@ -31,10 +31,10 @@ namespace Mystify
_kernel = bootstrapper.BootstrapKernel(); _kernel = bootstrapper.BootstrapKernel();
MainModel mainModel = _kernel.Resolve<MainModel>(); MainModel mainModel = _kernel.Resolve<MainModel>();
_mainWindow = new MainWindow _mainWindow = new MainWindow();
{
DataContext = new MainWindowViewModel(mainModel) MainWindowViewModel mainWindowViewModel = new (mainModel, _mainWindow);
}; _mainWindow.DataContext = mainWindowViewModel;
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopStyleApplicationLifetime) if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopStyleApplicationLifetime)
{ {

@ -2,26 +2,46 @@
// Created: 2021-04-09 // Created: 2021-04-09
// Copyright(c) 2021 SimonG. All Rights Reserved. // Copyright(c) 2021 SimonG. All Rights Reserved.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using Lib.Audio.Factories; using Lib.Audio.Factories;
using Lib.Audio.Interfaces; using Lib.Audio.Interfaces;
using Lib.Driver.Factories;
using Lib.Driver.Interfaces;
namespace Mystify namespace Mystify
{ {
public class MainModel public class MainModel
{ {
private readonly IDevice _device; private readonly IDriverLoader _driverLoader;
private readonly IControllableCollector _controllableCollector; private readonly IDeviceFactory _deviceFactory;
private readonly IControllableCollectorFactory _controllableCollectorFactory;
public MainModel(IDeviceFactory deviceFactory, IControllableCollectorFactory controllableCollectorFactory) private IDevice? _device;
private IControllableCollector? _controllableCollector;
public MainModel(IDriverLoaderFactory driverLoaderFactory, IDeviceFactory deviceFactory, IControllableCollectorFactory controllableCollectorFactory)
{
_driverLoader = driverLoaderFactory.Create();
_deviceFactory = deviceFactory;
_controllableCollectorFactory = controllableCollectorFactory;
}
public List<IChannel>? Channels => _device?.Channels;
public List<IControllable>? Controllables => _controllableCollector?.Controllables;
public string? DeviceName => _device?.Name;
public void LoadDriverAndDevice(string driverPath)
{ {
_device = deviceFactory.Create(); IDriver? driver = _driverLoader.Load(driverPath);
_controllableCollector = controllableCollectorFactory.Create();
if (driver == null)
throw new Exception("Driver could not be loaded.");
_device = _deviceFactory.Create(driver);
_controllableCollector = _controllableCollectorFactory.Create();
_device.StartCommunication(); _device.StartCommunication();
} }
public List<IChannel> Channels => _device.Channels;
public List<IControllable> Controllables => _controllableCollector.Controllables;
} }
} }

@ -5,7 +5,10 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Windows.Input; using System.Windows.Input;
using Avalonia.Controls;
using Lib.Audio.Interfaces; using Lib.Audio.Interfaces;
using Lib.Driver;
using Mystify.Views;
using ReactiveUI; using ReactiveUI;
namespace Mystify.ViewModels namespace Mystify.ViewModels
@ -13,6 +16,7 @@ namespace Mystify.ViewModels
public class MainWindowViewModel : ViewModelBase public class MainWindowViewModel : ViewModelBase
{ {
private readonly MainModel? _mainModel; private readonly MainModel? _mainModel;
private readonly MainWindow? _mainWindow;
public MainWindowViewModel() public MainWindowViewModel()
{ {
@ -20,7 +24,11 @@ namespace Mystify.ViewModels
throw new InvalidOperationException("Constructor is for design time usage only."); 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<IChannel>? Channels => _mainModel?.Channels; public List<IChannel>? Channels => _mainModel?.Channels;
public List<IControllable>? Controllables => _mainModel?.Controllables; public List<IControllable>? Controllables => _mainModel?.Controllables;
@ -28,6 +36,19 @@ namespace Mystify.ViewModels
public IChannel? SelectedChannel { get; set; } public IChannel? SelectedChannel { get; set; }
public IControllable? SelectedControllable { 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<FileDialogFilter> {new() {Extensions = new List<string> {DriverLoader.DRIVER_FILE_EXTENSION}, Name = "Mystify Driver"}}
};
await openFileDialog.ShowAsync(_mainWindow);
_mainModel?.LoadDriverAndDevice(openFileDialog.Directory);
});
public ICommand AllocateControllableCommand => public ICommand AllocateControllableCommand =>
ReactiveCommand.Create(() => ReactiveCommand.Create(() =>
{ {

@ -14,7 +14,12 @@
<vm:MainWindowViewModel/> <vm:MainWindowViewModel/>
</Design.DataContext> </Design.DataContext>
<StackPanel> <StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal" Margin="10">
<Button Margin="5" Content="Select MIDI Device Driver" Command="{Binding SelectDriverCommand}"/>
<TextBlock Text="{Binding SelectedDeviceText}" Margin="10,5"/>
</StackPanel>
<StackPanel Orientation="Horizontal" Margin="10"> <StackPanel Orientation="Horizontal" Margin="10">
<TextBlock Text="Select Channel:" Margin="12"/> <TextBlock Text="Select Channel:" Margin="12"/>
<ComboBox Items="{Binding Channels}" <ComboBox Items="{Binding Channels}"

Loading…
Cancel
Save