// 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; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using StrixMusic.Sdk.AdapterModels; using StrixMusic.Sdk.AppModels; using StrixMusic.Sdk.CoreModels; using StrixMusic.Sdk.Plugins.Model; namespace StrixMusic.Sdk.PluginModels; /// /// Wraps an instance of with the provided plugins. /// public class SearchPluginWrapper : ISearch, IPluginWrapper { private readonly ISearch _search; /// /// Initializes a new instance of the class. /// /// The instance to wrap around and apply plugins to. /// The plugins that are applied to items returned from or emitted by this collection. public SearchPluginWrapper(ISearch search, params SdkModelPlugin[] plugins) { foreach (var item in plugins) ActivePlugins.Import(item); ActivePlugins = GlobalModelPluginConnector.Create(ActivePlugins); _search = search; if (search.SearchHistory is not null) SearchHistory = new SearchHistoryPluginWrapper(search.SearchHistory, plugins); AttachEvents(_search); } private void AttachEvents(ISearch search) { search.SourcesChanged += OnSourcesChanged; } private void DetachEvents(ISearch search) { search.SourcesChanged -= OnSourcesChanged; } /// public SdkModelPlugin ActivePlugins { get; } = new(PluginModelWrapperInfo.Metadata); private void OnSourcesChanged(object sender, EventArgs e) => SourcesChanged?.Invoke(sender, e); /// public event EventHandler? SourcesChanged; /// public IAsyncEnumerable GetSearchAutoCompleteAsync(string query, CancellationToken cancellationToken = default) => _search.GetSearchAutoCompleteAsync(query, cancellationToken); /// public bool Equals(ICoreSearch other) => _search.Equals(other); /// public IReadOnlyList Sources => _search.Sources; /// public Task GetSearchResultsAsync(string query, CancellationToken cancellationToken = default) => _search.GetSearchResultsAsync(query, cancellationToken); /// public IAsyncEnumerable GetRecentSearchQueries(CancellationToken cancellationToken = default) => _search.GetRecentSearchQueries(cancellationToken); /// public ISearchHistory? SearchHistory { get; } /// public ValueTask DisposeAsync() { DetachEvents(_search); return _search.DisposeAsync(); } }