Show total songs and total duration for each playlist.
This commit is contained in:
47
Harmonia.WinUI/ViewModels/PlaylistItemViewModel.cs
Normal file
47
Harmonia.WinUI/ViewModels/PlaylistItemViewModel.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using Harmonia.Core.Playlists;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace Harmonia.WinUI.ViewModels;
|
||||
|
||||
public partial class PlaylistItemViewModel : ObservableObject
|
||||
{
|
||||
public Playlist Playlist { get; private set; }
|
||||
|
||||
private string _summary = string.Empty;
|
||||
public string Summary
|
||||
{
|
||||
get
|
||||
{
|
||||
return _summary;
|
||||
}
|
||||
set
|
||||
{
|
||||
SetProperty(ref _summary, value);
|
||||
}
|
||||
}
|
||||
|
||||
public PlaylistItemViewModel(Playlist playlist)
|
||||
{
|
||||
Playlist = playlist;
|
||||
Playlist.PlaylistUpdated += OnPlaylistUpdated;
|
||||
UpdateMetaData();
|
||||
}
|
||||
|
||||
public void Unsubscribe()
|
||||
{
|
||||
Playlist.PlaylistUpdated -= OnPlaylistUpdated;
|
||||
}
|
||||
|
||||
private void OnPlaylistUpdated(object? sender, EventArgs e)
|
||||
{
|
||||
UpdateMetaData();
|
||||
}
|
||||
|
||||
private void UpdateMetaData()
|
||||
{
|
||||
TimeSpan total = TimeSpan.FromSeconds(Playlist.Songs.Sum(s => s.Song.Length.TotalSeconds));
|
||||
Summary = $"{Playlist.Songs.Count} songs / {total:h\\:mm\\:ss}";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user