// Copyright (c) Arlo Godfrey. All Rights Reserved.
// Licensed under the GNU Lesser General Public License, Version 3.0 with additional terms.
// See the LICENSE, LICENSE.LESSER and LICENSE.ADDITIONAL files in the project root for more information.
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using OwlCore.Events;
using OwlCore.Extensions;
using StrixMusic.Sdk.AppModels;
using StrixMusic.Sdk.MediaPlayback;
using StrixMusic.Sdk.Plugins.Model;
namespace StrixMusic.Sdk.Plugins.PlaybackHandler;
///
/// Adds a playback device using the provided
///
public class StrixDataRootPlaybackHandlerPlugin : StrixDataRootPluginBase
{
private readonly IPlaybackHandlerService _playbackHandler;
private readonly List _devices = new();
///
/// Initializes a new instance of the class.
///
/// Contains metadata for a plugin. Used to identify a plugin before instantiation.
/// An implementation which member access is delegated to, unless the member is overridden in a derived class which changes the behavior.
/// An instance of that should be used when playback commands are issued.
protected internal StrixDataRootPlaybackHandlerPlugin(ModelPluginMetadata registration, IStrixDataRoot inner, IPlaybackHandlerService playbackHandler)
: base(registration, inner)
{
_playbackHandler = playbackHandler;
_devices.Add(_playbackHandler.LocalDevice);
_devices.AddRange(base.Devices);
DevicesChanged?.Invoke(this, new List>(new CollectionChangedItem(_playbackHandler.LocalDevice, 0).IntoList()), new List>());
AttachEvents();
}
///
public override event CollectionChangedEventHandler? DevicesChanged;
///
public override IReadOnlyList Devices => _devices;
private void AttachEvents() => Inner.DevicesChanged += OnDevicesChanged;
private void DetachEvents() => Inner.DevicesChanged -= OnDevicesChanged;
private void OnDevicesChanged(object sender, IReadOnlyList> addedItems, IReadOnlyList> removedItems)
{
// We're injecting a single device, so shift indices +1
var wrappedAdded = addedItems.Select(x => new CollectionChangedItem(x.Data, x.Index + 1)).ToList();
var wrappedRemoved = removedItems.Select(x => new CollectionChangedItem(x.Data, x.Index + 1)).ToList();
_devices.ChangeCollection(wrappedAdded, wrappedRemoved);
DevicesChanged?.Invoke(sender, addedItems, removedItems);
}
///
public override ValueTask DisposeAsync()
{
DetachEvents();
return base.DisposeAsync();
}
}