Added playlist view and initial playlist view logic.

This commit is contained in:
2025-03-10 09:50:05 -04:00
parent 9890236710
commit bd9b30abbd
19 changed files with 619 additions and 20 deletions

View File

@@ -7,6 +7,7 @@ using Harmonia.Core.Models;
using Harmonia.Core.Player;
using Harmonia.Core.Playlists;
using Harmonia.Core.Scanner;
using Harmonia.UI.Models;
using System;
using System.IO;
using System.Linq;
@@ -118,6 +119,57 @@ public partial class PlaybackBarViewModel : ViewModelBase, IDisposable
}
}
public double Volume
{
get
{
return _audioPlayer.Volume;
}
set
{
if (IsMuted)
IsMuted = false;
_audioPlayer.Volume = value;
OnPropertyChanged();
OnPropertyChanged(nameof(VolumeState));
}
}
public bool IsMuted
{
get
{
return _audioPlayer.IsMuted;
}
set
{
_audioPlayer.IsMuted = value;
OnPropertyChanged();
OnPropertyChanged(nameof(VolumeState));
}
}
public VolumeState VolumeState
{
get
{
if (IsMuted)
return VolumeState.Muted;
if (Volume == 0)
return VolumeState.Off;
if (Volume < .33)
return VolumeState.Low;
if (Volume < .66)
return VolumeState.Medium;
return VolumeState.High;
}
}
public bool IsRandom
{
get
@@ -131,11 +183,27 @@ public partial class PlaybackBarViewModel : ViewModelBase, IDisposable
}
}
public RepeatState RepeatState
{
get
{
return _audioPlayer.RepeatState;
}
private set
{
_audioPlayer.RepeatState = value;
OnPropertyChanged();
}
}
public ICommand PlaySongCommand => new RelayCommand(Play);
public ICommand PauseSongCommand => new RelayCommand(Pause);
public ICommand StopSongCommand => new RelayCommand(Stop);
public ICommand PreviousSongCommand => new RelayCommand(Previous);
public ICommand NextSongCommand => new RelayCommand(Next);
public ICommand ToggleMuteCommand => new RelayCommand(ToggleMute);
public ICommand ToggleRandomizerCommand => new RelayCommand(ToggleRandomizer);
public ICommand ToggleRepeatCommand => new RelayCommand(ToggleRepeat);
public PlaybackBarViewModel(IAudioPlayer audioPlayer, IAudioImageCache audioImageCache, IPlaylistRepository playlistRepository, IAudioFileScanner audioFileScanner)
{
@@ -219,6 +287,32 @@ public partial class PlaybackBarViewModel : ViewModelBase, IDisposable
_audioPlayer.NextAsync();
}
public void ToggleMute()
{
IsMuted = !IsMuted;
}
public void ToggleRandomizer()
{
IsRandom = !IsRandom;
}
public void ToggleRepeat()
{
RepeatState = GetNextRepeatState();
}
private RepeatState GetNextRepeatState()
{
return _audioPlayer.RepeatState switch
{
RepeatState.Off => RepeatState.RepeatAll,
RepeatState.RepeatAll => RepeatState.RepeatOne,
RepeatState.RepeatOne => RepeatState.Off,
_ => _audioPlayer.RepeatState,
};
}
public void Dispose()
{
SongImageSource?.Dispose();

View File

@@ -0,0 +1,65 @@
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);
}
}

View File

@@ -12,4 +12,7 @@ public class ViewModelLocator
public static PlayingSongInfoViewModel PlayingSongInfoViewModel
=> App.ServiceProvider.GetRequiredService<PlayingSongInfoViewModel>();
public static PlaylistViewModel PlaylistViewModel
=> App.ServiceProvider.GetRequiredService<PlaylistViewModel>();
}