Various updates.

This commit is contained in:
2025-03-30 23:18:40 -04:00
parent b7ba8341a1
commit f7cacf0bbb
11 changed files with 322 additions and 57 deletions

View File

@@ -9,6 +9,7 @@ using Harmonia.WinUI.Storage;
using Microsoft.UI.Dispatching;
using Microsoft.UI.Xaml.Media.Imaging;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
@@ -34,6 +35,7 @@ public partial class PlaylistViewModel : ViewModelBase
private readonly IAudioEngine _audioEngine;
private readonly IStorageProvider _storageProvider;
private readonly DispatcherQueue _dispatcherQueue;
private readonly ConcurrentDictionary<int, CancellationTokenSource> _imageCancellationTokens = [];
private Timer? _filterTimer;
@@ -118,6 +120,9 @@ public partial class PlaylistViewModel : ViewModelBase
public ICommand RemoveDuplicateSongsCommand => new RelayCommand(RemoveDuplicateSongs);
public bool IsUserUpdating { get; set; }
private bool _isUserInitiatingSongChange;
public event EventHandler? PlayingSongChangedAutomatically;
public PlaylistViewModel(
IAudioPlayer audioPlayer,
@@ -216,13 +221,34 @@ public partial class PlaylistViewModel : ViewModelBase
private void OnPlayingSongChanged(object? sender, EventArgs e)
{
PlayingSong = _audioPlayer.PlayingSong;
if (_isUserInitiatingSongChange)
{
_isUserInitiatingSongChange = false;
}
else
{
PlayingSongChangedAutomatically?.Invoke(this, EventArgs.Empty);
}
}
public async Task PlaySongAsync(PlaylistSong playlistSong)
{
_isUserInitiatingSongChange = true;
await _audioPlayer.LoadAsync(playlistSong, PlaybackMode.LoadAndPlay);
}
public async Task<BitmapImage?> GetBitmapImageAsync(int hashCode, PlaylistSong playlistSong)
{
_imageCancellationTokens.TryGetValue(hashCode, out CancellationTokenSource? cancellationTokenSource);
cancellationTokenSource?.Cancel();
cancellationTokenSource = new();
_imageCancellationTokens.AddOrUpdate(hashCode, cancellationTokenSource, (_,_) => cancellationTokenSource);
return await _audioBitmapImageCache.GetAsync(playlistSong.Song, cancellationTokenSource.Token);
}
public async Task<BitmapImage?> GetBitmapAsync(PlaylistSong playlistSong, CancellationToken cancellationToken)
{
return await _audioBitmapImageCache.GetAsync(playlistSong.Song, cancellationToken);
@@ -412,6 +438,9 @@ public partial class PlaylistViewModel : ViewModelBase
private bool CanPasteSongs()
{
if (Playlist == null || SelectedPlaylistSongs.Count == 0)
return false;
DataPackageView dataPackageView = Clipboard.GetContent();
if (dataPackageView == null)