using System.Threading.Tasks; using OwlCore.Extensions; using StrixMusic.Sdk.ViewModels; using StrixMusic.Sdk.WinUI.Controls.Collections.Abstract; using StrixMusic.Sdk.WinUI.Controls.Items; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace StrixMusic.Sdk.WinUI.Controls.Collections { /// /// A templated for displaying an . /// /// /// This class temporarily only displays s. /// public partial class AlbumCollection : CollectionControl { /// /// Initializes a new instance of the class. /// public AlbumCollection() { this.DefaultStyleKey = typeof(AlbumCollection); } /// protected override void OnApplyTemplate() { // OnApplyTemplate is often a more appropriate point to deal with // adjustments to the template-created visual tree than is the Loaded event. // The Loaded event might occur before the template is applied, // and the visual tree might be incomplete as of Loaded. base.OnApplyTemplate(); OnCollectionChanged(null, Collection); AttachHandlers(); } /// protected override async Task LoadMore() { if (Collection is not null && !Collection.PopulateMoreAlbumsCommand.IsRunning) await Collection.PopulateMoreAlbumsCommand.ExecuteAsync(25); } /// protected override void CheckAndToggleEmpty() { if (Collection is not null && !Collection.PopulateMoreAlbumsCommand.IsRunning && Collection.TotalAlbumItemsCount == 0) SetEmptyVisibility(Visibility.Visible); } private void AttachHandlers() { Unloaded += AlbumCollection_Unloaded; } private void AlbumCollection_Unloaded(object sender, RoutedEventArgs e) { DetachHandlers(); } private void DetachHandlers() { Unloaded -= AlbumCollection_Unloaded; } /// /// Collection holding the data for /// public IAlbumCollectionViewModel? Collection { get { return (IAlbumCollectionViewModel)GetValue(CollectionProperty); } set { SetValue(CollectionProperty, value); } } /// /// Dependency property for . /// public static readonly DependencyProperty CollectionProperty = DependencyProperty.Register(nameof(Collection), typeof(IAlbumCollectionViewModel), typeof(AlbumCollection), new PropertyMetadata(null, (s, e) => s.Cast().OnCollectionChanged((IAlbumCollectionViewModel?)e.OldValue, (IAlbumCollectionViewModel?)e.NewValue))); /// /// Fires when the property changes. /// protected virtual void OnCollectionChanged(IAlbumCollectionViewModel? oldValue, IAlbumCollectionViewModel? newValue) { if (newValue is not null && !newValue.PopulateMoreAlbumsCommand.IsRunning && newValue.Albums.Count == 0) newValue.PopulateMoreAlbumsCommand.Execute(20); } } }