Cross Platform Application to allow control with a MIDI controller
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.4 KiB

// Author: Gockner, Simon
// Created: 2021-04-06
// Copyright(c) 2021 SimonG. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Input;
using Avalonia.Controls;
using Lib.Driver;
using Lib.Logging;
using Lib.Tools;
using Mystify.Views;
using ReactiveUI;
namespace Mystify.ViewModels
{
public class MainWindowViewModel : ViewModelBase
{
private readonly MainModel? _mainModel;
private readonly MainWindow? _mainWindow;
public MainWindowViewModel()
{
if (!IsInDesignMode)
throw new InvalidOperationException("Constructor is for design time usage only.");
}
public MainWindowViewModel(MainModel mainModel, MainWindow? mainWindow)
{
_mainModel = mainModel;
_mainWindow = mainWindow;
}
public ObservableCollection<ChannelViewModel>? Channels => _mainModel?.Channels?.Select(c => new ChannelViewModel(c, _mainWindow, _mainModel)).ToObservableCollection();
public bool UseMidiView
{
get => _mainModel is {UseMidiView: true};
set
{
if (_mainModel != null)
_mainModel.UseMidiView = value;
}
}
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"}}
};
string[] selectedPaths = await openFileDialog.ShowAsync(_mainWindow);
if (selectedPaths.Length == 0)
{
await Log.Write<MainWindowViewModel>("No Driver selected.");
return;
}
string driverPath = selectedPaths[0];
if (!string.IsNullOrEmpty(driverPath))
_mainModel?.LoadDriverAndDevice(driverPath);
RaisePropertyChanged(() => SelectedDeviceText);
RaisePropertyChanged(() => Channels);
});
public void UpdateChannels() => RaisePropertyChanged(() => Channels);
}
}