- add dispose for channel and device

master
Simon G 5 years ago
parent a58f112ea9
commit 8f69b3a062
  1. 22
      Lib.Audio/Channel.cs
  2. 6
      Lib.Audio/Device.cs
  3. 1
      Lib.Audio/Interfaces/IChannel.cs
  4. 3
      Lib.Audio/Interfaces/IDevice.cs
  5. 6
      Mystify/App.axaml.cs
  6. 4
      Mystify/MainModel.cs

@ -57,14 +57,15 @@ namespace Lib.Audio
public void MapControllable(IControllable controllable)
{
Controllable = controllable;
ToggleSelectButtonLed();
}
ISelectButton? selectButton = Buttons?.OfType<ISelectButton>().FirstOrDefault();
IMidiMessage? selectMessage = selectButton?.Select();
if (selectMessage != null)
_midiCommunication.Send(selectMessage);
public void UnMapControllable()
{
if (Controllable == null)
return;
ToggleSelectButtonLed();
}
public void HandleMessage(IMidiMessage message)
@ -108,6 +109,15 @@ namespace Lib.Audio
_acknowledgeMessage = null;
}
private void ToggleSelectButtonLed()
{
ISelectButton? selectButton = Buttons?.OfType<ISelectButton>().FirstOrDefault();
IMidiMessage? selectMessage = selectButton?.Select();
if (selectMessage != null)
_midiCommunication.Send(selectMessage);
}
private void OnFaderPositionChanged(object? sender, int position)
{
if (sender is not IFader fader)

@ -47,5 +47,11 @@ namespace Lib.Audio
private void OnMidiCommunicationErrorReceived(object? sender, IMidiMessage message) =>
Log.Write<Device>($"Midi Error: Message type: {message.GetType()}, Channel: {message.ChannelNumber}, RawMessage: {message.RawMessage}");
public void Dispose()
{
Channels.ForEach(c => c.UnMapControllable());
_midiCommunication.Close();
}
}
}

@ -18,6 +18,7 @@ namespace Lib.Audio.Interfaces
IControllable? Controllable { get; }
void MapControllable(IControllable controllable);
void UnMapControllable();
void HandleMessage(IMidiMessage message);
void SendAcknowledge();
}

@ -2,11 +2,12 @@
// Created: 2021-04-07
// Copyright(c) 2021 SimonG. All Rights Reserved.
using System;
using System.Collections.Generic;
namespace Lib.Audio.Interfaces
{
public interface IDevice
public interface IDevice : IDisposable
{
string Name { get; }
List<IChannel> Channels { get; }

@ -28,6 +28,7 @@ namespace Mystify
private INotifyIcon? _notifyIcon;
private MainModel? _mainModel;
private MainWindow? _mainWindow;
private MainWindowViewModel? _mainWindowViewModel;
@ -49,9 +50,9 @@ namespace Mystify
await _log.WriteLogHeader<App>();
MainModel mainModel = _kernel.Resolve<MainModel>();
_mainModel = _kernel.Resolve<MainModel>();
_mainWindow = new MainWindow();
_mainWindowViewModel = new MainWindowViewModel(mainModel, _mainWindow);
_mainWindowViewModel = new MainWindowViewModel(_mainModel, _mainWindow);
_mainWindow.DataContext = _mainWindowViewModel;
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktopStyleApplicationLifetime)
@ -89,6 +90,7 @@ namespace Mystify
private async void OnExit(object? sender, ControlledApplicationLifetimeExitEventArgs args) //TODO: return Task?
{
_mainWindow?.Close();
_mainModel?.Dispose();
_notifyIcon?.Remove();
if (_log != null)

@ -11,7 +11,7 @@ using Lib.Driver.Interfaces;
namespace Mystify
{
public class MainModel
public class MainModel : IDisposable
{
private readonly IDriverLoader _driverLoader;
private readonly IDeviceFactory _deviceFactory;
@ -45,5 +45,7 @@ namespace Mystify
_device.StartCommunication(UseMidiView);
}
public void Dispose() => _device?.Dispose();
}
}
Loading…
Cancel
Save