// 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.ObjectModel; using System.Linq; using CommunityToolkit.Diagnostics; using OwlCore.Extensions; using StrixMusic.Sdk.AppModels; using StrixMusic.Sdk.ViewModels.Helpers.Comparers; namespace StrixMusic.Sdk.ViewModels.Helpers { /// /// Helper to perform operations on a collection of playables. /// public static class CollectionSorting { /// /// Sorts the tracks. /// /// The collection to initialize. /// Sort type of the collection. /// The direction to sort the collection. /// The original order of the playlistCollection. public static void SortTracks(ObservableCollection trackCollection, TrackSortingType trackSorting, SortDirection sortDirection, ObservableCollection originalOrder) { if (!originalOrder.Any()) { originalOrder = trackCollection; } if (sortDirection == SortDirection.Unsorted || trackSorting == TrackSortingType.Unsorted) { trackCollection = originalOrder; originalOrder.Clear(); return; } bool isDescending = sortDirection == SortDirection.Descending; switch (trackSorting) { case TrackSortingType.Alphanumerical: trackCollection.Sort(new NameComparer(isDescending)); break; case TrackSortingType.TrackNumber: trackCollection.Sort(new TrackNumberComparer(isDescending)); break; case TrackSortingType.DateAdded: trackCollection.Sort(new AddedAtComparer(isDescending)); break; case TrackSortingType.Duration: trackCollection.Sort(new DurationComparer(isDescending)); break; default: ThrowHelper.ThrowNotSupportedException($"{nameof(TrackSortingType)}: {trackSorting} is not supported."); break; } } /// /// Sorts the albums. /// /// The collection to sort. /// Sort type of the collection. /// The direction to sort the collection. /// The original order of the albumCollection. public static void SortAlbums(ObservableCollection albumCollection, AlbumSortingType albumSorting, SortDirection sortDirection, ObservableCollection originalOrder) { if (!originalOrder.Any()) { originalOrder = albumCollection; } if (sortDirection == SortDirection.Unsorted || albumSorting == AlbumSortingType.Unsorted) { albumCollection = originalOrder; originalOrder.Clear(); return; } bool isDescending = sortDirection == SortDirection.Descending; switch (albumSorting) { case AlbumSortingType.Alphanumerical: albumCollection.Sort(new NameComparer(isDescending)); break; case AlbumSortingType.DateAdded: albumCollection.Sort(new AddedAtComparer(isDescending)); break; case AlbumSortingType.Duration: albumCollection.Sort(new DurationComparer(isDescending)); break; case AlbumSortingType.LastPlayed: albumCollection.Sort(new LastPlayedComparer(isDescending)); break; case AlbumSortingType.DatePublished: throw new NotImplementedException(); // IAlbumCollectionItem don't have DatePublished property. IAlbumBase has it. The potential solution will be is to move those IAlbumBase items to IAlbumCollectionItem. default: ThrowHelper.ThrowNotSupportedException($"{nameof(AlbumSortingType)}: {albumSorting} is not supported."); break; } } /// /// Sorts the artists. /// /// The collection to sort. /// Sort type of the collection. /// The direction to sort the collection. /// The original order of the playlistCollection. public static void SortArtists(ObservableCollection artistCollection, ArtistSortingType artistSorting, SortDirection sortDirection, ObservableCollection originalOrder) { if (!originalOrder.Any()) { originalOrder = artistCollection; } if (sortDirection == SortDirection.Unsorted || artistSorting == ArtistSortingType.Unsorted) { artistCollection = originalOrder; originalOrder.Clear(); return; } bool isDescending = sortDirection == SortDirection.Descending; switch (artistSorting) { case ArtistSortingType.Alphanumerical: artistCollection.Sort(new NameComparer(isDescending)); break; case ArtistSortingType.DateAdded: artistCollection.Sort(new AddedAtComparer(isDescending)); break; case ArtistSortingType.Duration: artistCollection.Sort(new DurationComparer(isDescending)); break; case ArtistSortingType.LastPlayed: artistCollection.Sort(new LastPlayedComparer(isDescending)); break; default: ThrowHelper.ThrowNotSupportedException($"{nameof(ArtistSortingType)}: {artistSorting} is not supported."); break; } } /// /// Sorts the playlists. /// /// The collection to sort. /// Sort type of the collection. /// The direction to sort the collection. /// The original order of the playlistCollection. public static void SortPlaylists(ObservableCollection playlistCollection, PlaylistSortingType playlistSorting, SortDirection sortDirection, ObservableCollection originalOrder) { if (!originalOrder.Any()) { originalOrder = playlistCollection; } if (sortDirection == SortDirection.Unsorted || playlistSorting == PlaylistSortingType.Unsorted) { playlistCollection = originalOrder; originalOrder.Clear(); return; } bool isDescending = sortDirection == SortDirection.Descending; switch (playlistSorting) { case PlaylistSortingType.Alphanumerical: playlistCollection.Sort(new NameComparer(isDescending)); break; case PlaylistSortingType.DateAdded: playlistCollection.Sort(new AddedAtComparer(isDescending)); break; case PlaylistSortingType.Duration: playlistCollection.Sort(new DurationComparer(isDescending)); break; case PlaylistSortingType.LastPlayed: playlistCollection.Sort(new LastPlayedComparer(isDescending)); break; default: ThrowHelper.ThrowNotSupportedException($"{nameof(PlaylistSortingType)}: {playlistSorting} is not supported."); break; } } } }