65 lines
1.9 KiB
C#
65 lines
1.9 KiB
C#
using Harmonia.Core.Player;
|
|
using Harmonia.Core.Playlists;
|
|
using System;
|
|
using System.Collections.ObjectModel;
|
|
using System.Threading.Tasks;
|
|
using System.Threading;
|
|
using Avalonia.Media.Imaging;
|
|
using System.Collections.Concurrent;
|
|
using Harmonia.UI.Caching;
|
|
|
|
namespace Harmonia.UI.ViewModels;
|
|
|
|
public class PlaylistViewModel : ViewModelBase
|
|
{
|
|
private readonly IAudioPlayer _audioPlayer;
|
|
private readonly IAudioBitmapCache _audioBitmapImageCache;
|
|
private readonly ConcurrentDictionary<string, Bitmap> _bitmapDictionary = [];
|
|
|
|
public PlaylistSong? PlayingSong => _audioPlayer.PlayingSong;
|
|
|
|
private ObservableCollection<PlaylistSong> _playlistSongs = [];
|
|
public ObservableCollection<PlaylistSong> PlaylistSongs
|
|
{
|
|
get
|
|
{
|
|
return _playlistSongs;
|
|
}
|
|
set
|
|
{
|
|
_playlistSongs = value;
|
|
OnPropertyChanged();
|
|
}
|
|
}
|
|
|
|
public PlaylistViewModel(IAudioPlayer audioPlayer, IAudioBitmapCache audioBitmapImageCache)
|
|
{
|
|
_audioPlayer = audioPlayer;
|
|
_audioPlayer.PlaylistChanged += OnAudioPlayerPlaylistChanged;
|
|
_audioPlayer.PlayingSongChanged += OnAudioPlayerPlayingSongChanged;
|
|
|
|
_audioBitmapImageCache = audioBitmapImageCache;
|
|
}
|
|
|
|
private void OnAudioPlayerPlaylistChanged(object? sender, EventArgs e)
|
|
{
|
|
PlaylistSong[] playlistSongs = _audioPlayer.Playlist?.Songs.ToArray() ?? [];
|
|
|
|
PlaylistSongs = [.. playlistSongs];
|
|
}
|
|
|
|
private void OnAudioPlayerPlayingSongChanged(object? sender, EventArgs e)
|
|
{
|
|
OnPropertyChanged(nameof(PlayingSong));
|
|
}
|
|
|
|
public void PlaySong(PlaylistSong playlistSong)
|
|
{
|
|
_audioPlayer.LoadAsync(playlistSong, PlaybackMode.LoadAndPlay);
|
|
}
|
|
|
|
public async Task<Bitmap?> GetBitmapAsync(PlaylistSong playlistSong, CancellationToken cancellationToken)
|
|
{
|
|
return await _audioBitmapImageCache.GetAsync(playlistSong.Song, cancellationToken);
|
|
}
|
|
} |