From 8f69b3a0628735a0dc2922d1dc0b661c575e9733 Mon Sep 17 00:00:00 2001 From: Simon G Date: Thu, 22 Apr 2021 17:44:52 +0200 Subject: [PATCH] - add dispose for channel and device --- Lib.Audio/Channel.cs | 22 ++++++++++++++++------ Lib.Audio/Device.cs | 6 ++++++ Lib.Audio/Interfaces/IChannel.cs | 1 + Lib.Audio/Interfaces/IDevice.cs | 3 ++- Mystify/App.axaml.cs | 8 +++++--- Mystify/MainModel.cs | 4 +++- 6 files changed, 33 insertions(+), 11 deletions(-) diff --git a/Lib.Audio/Channel.cs b/Lib.Audio/Channel.cs index d7312d1..7fc1fdc 100644 --- a/Lib.Audio/Channel.cs +++ b/Lib.Audio/Channel.cs @@ -57,14 +57,15 @@ namespace Lib.Audio public void MapControllable(IControllable controllable) { Controllable = controllable; + ToggleSelectButtonLed(); + } - ISelectButton? selectButton = Buttons?.OfType().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().FirstOrDefault(); + IMidiMessage? selectMessage = selectButton?.Select(); + + if (selectMessage != null) + _midiCommunication.Send(selectMessage); + } + private void OnFaderPositionChanged(object? sender, int position) { if (sender is not IFader fader) diff --git a/Lib.Audio/Device.cs b/Lib.Audio/Device.cs index b039134..27acda0 100644 --- a/Lib.Audio/Device.cs +++ b/Lib.Audio/Device.cs @@ -47,5 +47,11 @@ namespace Lib.Audio private void OnMidiCommunicationErrorReceived(object? sender, IMidiMessage message) => Log.Write($"Midi Error: Message type: {message.GetType()}, Channel: {message.ChannelNumber}, RawMessage: {message.RawMessage}"); + + public void Dispose() + { + Channels.ForEach(c => c.UnMapControllable()); + _midiCommunication.Close(); + } } } \ No newline at end of file diff --git a/Lib.Audio/Interfaces/IChannel.cs b/Lib.Audio/Interfaces/IChannel.cs index 631c428..e062d96 100644 --- a/Lib.Audio/Interfaces/IChannel.cs +++ b/Lib.Audio/Interfaces/IChannel.cs @@ -18,6 +18,7 @@ namespace Lib.Audio.Interfaces IControllable? Controllable { get; } void MapControllable(IControllable controllable); + void UnMapControllable(); void HandleMessage(IMidiMessage message); void SendAcknowledge(); } diff --git a/Lib.Audio/Interfaces/IDevice.cs b/Lib.Audio/Interfaces/IDevice.cs index eefb24c..f815414 100644 --- a/Lib.Audio/Interfaces/IDevice.cs +++ b/Lib.Audio/Interfaces/IDevice.cs @@ -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 Channels { get; } diff --git a/Mystify/App.axaml.cs b/Mystify/App.axaml.cs index b013527..2936f4b 100644 --- a/Mystify/App.axaml.cs +++ b/Mystify/App.axaml.cs @@ -27,7 +27,8 @@ namespace Mystify private ILoggerInitializer? _loggerInitializer; private INotifyIcon? _notifyIcon; - + + private MainModel? _mainModel; private MainWindow? _mainWindow; private MainWindowViewModel? _mainWindowViewModel; @@ -49,9 +50,9 @@ namespace Mystify await _log.WriteLogHeader(); - MainModel mainModel = _kernel.Resolve(); + _mainModel = _kernel.Resolve(); _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) diff --git a/Mystify/MainModel.cs b/Mystify/MainModel.cs index ec2a9ab..cc9f63c 100644 --- a/Mystify/MainModel.cs +++ b/Mystify/MainModel.cs @@ -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(); } } \ No newline at end of file