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.

70 lines
2.4 KiB

// Author: Gockner, Simon
// 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;
using Mystify.Settings.Interfaces;
namespace Mystify
{
public class MainModel : IDisposable
{
private readonly IDriverLoader _driverLoader;
private readonly IDeviceFactory _deviceFactory;
private readonly IControllableCollectorFactory _controllableCollectorFactory;
private readonly ISettings _settings;
private IDevice? _device;
private IControllableCollector? _controllableCollector;
public MainModel(IDriverLoaderFactory driverLoaderFactory,
ISettingsManager settingsManager,
IDeviceFactory deviceFactory,
IControllableCollectorFactory controllableCollectorFactory,
IControllableFactory controllableFactory)
{
_driverLoader = driverLoaderFactory.Create();
_deviceFactory = deviceFactory;
_controllableCollectorFactory = controllableCollectorFactory;
ControllableFactory = controllableFactory;
_settings = settingsManager.Settings;
if (!string.IsNullOrEmpty(_settings.LastDriverPath))
LoadDriverAndDevice(_settings.LastDriverPath);
}
public string? DeviceName => _device?.Name;
public List<IChannel>? Channels => _device?.Channels;
public List<IControllable>? Controllables => _controllableCollector?.Controllables;
public bool UseMidiView { get; set; }
public IControllableFactory ControllableFactory { get; }
public void LoadDriverAndDevice(string driverPath)
{
_device?.StopCommunication();
IDriver? driver = _driverLoader.Load(driverPath);
if (driver == null)
throw new Exception("Driver could not be loaded.");
_settings.LastDriverPath = driverPath;
_controllableCollector = _controllableCollectorFactory.Create();
_device = _deviceFactory.Create(driver, _controllableCollector);
_device.StartCommunication(UseMidiView);
}
public void Dispose() => _device?.Dispose();
}
}