Added playlists view context menu for rename, lock/unlock, and delete.

This commit is contained in:
2026-08-09 17:30:38 -04:00
parent cf8a40ee52
commit 91e4d88643
6 changed files with 154 additions and 15 deletions

View File

@@ -1,9 +1,12 @@
using Harmonia.Core.Player;
using CommunityToolkit.Mvvm.Input;
using Harmonia.Core.Player;
using Harmonia.Core.Playlists;
using Harmonia.WinUI.Messaging;
using Microsoft.UI.Dispatching;
using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
namespace Harmonia.WinUI.ViewModels;
@@ -11,6 +14,7 @@ public partial class PlaylistsViewModel : ViewModelBase
{
private readonly IPlaylistManager _playlistManager;
private readonly IAudioPlayer _audioPlayer;
private readonly IMessageService _messageService;
private readonly DispatcherQueue _dispatcherQueue;
private ObservableCollection<PlaylistItemViewModel> _playlists = [];
@@ -55,12 +59,31 @@ public partial class PlaylistsViewModel : ViewModelBase
}
}
public PlaylistsViewModel(IPlaylistManager playlistManager, IAudioPlayer audioPlayer)
private PlaylistItemViewModel? _flyoutPlaylist = null;
public PlaylistItemViewModel? FlyoutPlaylist
{
get
{
return _flyoutPlaylist;
}
set
{
SetProperty(ref _flyoutPlaylist, value);
}
}
public IAsyncRelayCommand<PlaylistItemViewModel?> RenamePlaylistCommand { get; }
public IRelayCommand<PlaylistItemViewModel?> LockPlaylistCommand { get; }
public IRelayCommand<PlaylistItemViewModel?> UnlockPlaylistCommand { get; }
public IAsyncRelayCommand<PlaylistItemViewModel?> DeletePlaylistCommand { get; }
public PlaylistsViewModel(IPlaylistManager playlistManager, IAudioPlayer audioPlayer, IMessageService messageService)
{
_playlistManager = playlistManager;
_playlistManager.PlaylistAdded += OnPlaylistAdded;
_playlistManager.PlaylistRemoved += OnPlaylistRemoved;
_playlistManager.CurrentPlaylistChanged += OnCurrentPlaylistChanged;
_messageService = messageService;
_audioPlayer = audioPlayer;
_audioPlayer.PlayingSongChanged += OnPlayingSongChanged;
@@ -70,6 +93,11 @@ public partial class PlaylistsViewModel : ViewModelBase
_playlists = new ObservableCollection<PlaylistItemViewModel>(_playlistManager.Playlists.Select(p => new PlaylistItemViewModel(p)));
_selectedPlaylist = _playlists.FirstOrDefault(pv => pv.Playlist == _playlistManager.CurrentPlaylist);
_activePlaylist = _playlists.FirstOrDefault(pv => pv.Playlist == _audioPlayer.Playlist);
RenamePlaylistCommand = new AsyncRelayCommand<PlaylistItemViewModel?>(RenamePlaylist);
LockPlaylistCommand = new RelayCommand<PlaylistItemViewModel?>(LockPlaylist);
UnlockPlaylistCommand = new RelayCommand<PlaylistItemViewModel?>(UnlockPlaylist);
DeletePlaylistCommand = new AsyncRelayCommand<PlaylistItemViewModel?>(DeletePlaylistAsync);
}
private void OnCurrentPlaylistChanged(object? sender, EventArgs e)
@@ -102,4 +130,57 @@ public partial class PlaylistsViewModel : ViewModelBase
{
ActivePlaylist = Playlists.FirstOrDefault(pv => pv.Playlist == _audioPlayer.Playlist);
}
private async Task RenamePlaylist(PlaylistItemViewModel? playlistItem)
{
if (playlistItem is null || playlistItem.Playlist is null)
return;
Playlist playlist = playlistItem.Playlist;
string newName = await _messageService.InputTextAsync(
$"Rename Playlist - {playlist.Name}",
"Enter a new name for the playlist:",
playlist.Name ?? string.Empty);
if (string.IsNullOrWhiteSpace(newName))
return;
playlist.SetName(newName);
}
private void LockPlaylist(PlaylistItemViewModel? playlistItem)
{
if (playlistItem is null || playlistItem.Playlist is null)
return;
Playlist playlist = playlistItem.Playlist;
playlist.Lock();
}
private void UnlockPlaylist(PlaylistItemViewModel? playlistItem)
{
if (playlistItem is null || playlistItem.Playlist is null)
return;
Playlist playlist = playlistItem.Playlist;
playlist.Unlock();
}
private async Task DeletePlaylistAsync(PlaylistItemViewModel? playlistItem)
{
if (playlistItem is null || playlistItem.Playlist is null)
return;
Playlist playlist = playlistItem.Playlist;
bool confirmed = await _messageService.ConfirmAsync($"Delete Playlist - {playlist.Name}", "Are you sure you want to delete this playlist?");
if (!confirmed)
return;
_playlistManager.RemovePlaylist(playlist);
}
}