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

@@ -12,6 +12,8 @@ using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using Windows.System;
using DispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue;
namespace Harmonia.WinUI.ViewModels;
@@ -20,6 +22,7 @@ public partial class PlayerViewModel : ViewModelBase
private readonly IAudioPlayer _audioPlayer;
private readonly IAudioBitmapImageCache _audioBitmapImageCache;
private readonly DispatcherTimer _timer;
private readonly DispatcherQueue _dispatcherQueue;
private CancellationTokenSource? _songImageCancellationTokenSource;
@@ -185,6 +188,18 @@ public partial class PlayerViewModel : ViewModelBase
}
}
public bool CanUpdatePosition
{
get
{
return _audioPlayer.State != AudioPlaybackState.Stopped;
}
private set
{
OnPropertyChanged();
}
}
public ICommand PlaySongCommand => new RelayCommand(Play);
public ICommand PauseSongCommand => new RelayCommand(Pause);
public ICommand StopSongCommand => new RelayCommand(Stop);
@@ -208,6 +223,8 @@ public partial class PlayerViewModel : ViewModelBase
};
_timer.Tick += TickTock;
_dispatcherQueue = DispatcherQueue.GetForCurrentThread();
}
#region Event Handlers
@@ -230,9 +247,13 @@ public partial class PlayerViewModel : ViewModelBase
_songImageCancellationTokenSource = new();
CancellationToken cancellationToken = _songImageCancellationTokenSource.Token;
BitmapImage? bitmapImage = await _audioBitmapImageCache.GetAsync(Song, cancellationToken);
//BitmapImage? bitmapImage = await _audioBitmapImageCache.GetAsync(Song, cancellationToken);
DispatcherQueue.GetForCurrentThread().TryEnqueue(() => SongImageSource = bitmapImage);
_dispatcherQueue.TryEnqueue(async () =>
{
BitmapImage? bitmapImage = await _audioBitmapImageCache.GetAsync(Song, cancellationToken);
SongImageSource = bitmapImage;
});
}
private void OnAudioPlayerPropertyChanged(object? sender, PropertyChangedEventArgs e)
@@ -241,6 +262,7 @@ public partial class PlayerViewModel : ViewModelBase
{
case nameof(_audioPlayer.State):
UpdateTimer();
OnPropertyChanged(nameof(CanUpdatePosition));
break;
}
}

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)