Updated certain events to have more concrete event arguments. Added try-catch block around playlist save logic. Added DispatchQueue where necessary.
This commit is contained in:
@@ -20,9 +20,13 @@ public class AudioPlayer : IAudioPlayer
|
|||||||
}
|
}
|
||||||
protected set
|
protected set
|
||||||
{
|
{
|
||||||
|
Playlist? oldPlaylist = _playlist;
|
||||||
_playlist = value;
|
_playlist = value;
|
||||||
|
|
||||||
NotifyPropertyChanged(nameof(Playlist));
|
NotifyPropertyChanged(nameof(Playlist));
|
||||||
PlaylistChanged?.Invoke(this, new());
|
|
||||||
|
PlaylistChangedEventArgs eventArgs = new(oldPlaylist, value);
|
||||||
|
PlaylistChanged?.Invoke(this, eventArgs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,9 +41,13 @@ public class AudioPlayer : IAudioPlayer
|
|||||||
}
|
}
|
||||||
protected set
|
protected set
|
||||||
{
|
{
|
||||||
|
PlaylistSong? oldSong = _playingSong;
|
||||||
_playingSong = value;
|
_playingSong = value;
|
||||||
|
|
||||||
NotifyPropertyChanged(nameof(PlayingSong));
|
NotifyPropertyChanged(nameof(PlayingSong));
|
||||||
PlayingSongChanged?.Invoke(this, new());
|
|
||||||
|
PlayingSongChangedEventArgs eventArgs = new(oldSong, value);
|
||||||
|
PlayingSongChanged?.Invoke(this, eventArgs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,8 +123,8 @@ public class AudioPlayer : IAudioPlayer
|
|||||||
|
|
||||||
protected virtual int PreviousSongSecondsThreshold => 5;
|
protected virtual int PreviousSongSecondsThreshold => 5;
|
||||||
|
|
||||||
public event EventHandler? PlaylistChanged;
|
public event EventHandler<PlaylistChangedEventArgs>? PlaylistChanged;
|
||||||
public event EventHandler? PlayingSongChanged;
|
public event EventHandler<PlayingSongChangedEventArgs>? PlayingSongChanged;
|
||||||
public event PropertyChangedEventHandler? PropertyChanged;
|
public event PropertyChangedEventHandler? PropertyChanged;
|
||||||
|
|
||||||
public AudioPlayer(IAudioEngine audioEngine, IPlaylistManager playlistManager)
|
public AudioPlayer(IAudioEngine audioEngine, IPlaylistManager playlistManager)
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ public interface IAudioPlayer
|
|||||||
Task PreviousAsync();
|
Task PreviousAsync();
|
||||||
Task NextAsync();
|
Task NextAsync();
|
||||||
|
|
||||||
event EventHandler PlaylistChanged;
|
event EventHandler<PlaylistChangedEventArgs> PlaylistChanged;
|
||||||
event EventHandler PlayingSongChanged;
|
event EventHandler<PlayingSongChangedEventArgs> PlayingSongChanged;
|
||||||
event PropertyChangedEventHandler PropertyChanged;
|
event PropertyChangedEventHandler PropertyChanged;
|
||||||
}
|
}
|
||||||
9
Harmonia.Core/Player/PlayingSongChangedEventArgs.cs
Normal file
9
Harmonia.Core/Player/PlayingSongChangedEventArgs.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using Harmonia.Core.Playlists;
|
||||||
|
|
||||||
|
namespace Harmonia.Core.Player;
|
||||||
|
|
||||||
|
public class PlayingSongChangedEventArgs(PlaylistSong? oldSong, PlaylistSong? newSong) : EventArgs
|
||||||
|
{
|
||||||
|
public PlaylistSong? OldSong { get; } = oldSong;
|
||||||
|
public PlaylistSong? NewSong { get; } = newSong;
|
||||||
|
}
|
||||||
9
Harmonia.Core/Player/PlaylistChangedEventArgs.cs
Normal file
9
Harmonia.Core/Player/PlaylistChangedEventArgs.cs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
using Harmonia.Core.Playlists;
|
||||||
|
|
||||||
|
namespace Harmonia.Core.Player;
|
||||||
|
|
||||||
|
public class PlaylistChangedEventArgs(Playlist? oldPlaylist, Playlist? newPlaylist) : EventArgs
|
||||||
|
{
|
||||||
|
public Playlist? OldPlaylist { get; } = oldPlaylist;
|
||||||
|
public Playlist? NewPlaylist { get; } = newPlaylist;
|
||||||
|
}
|
||||||
@@ -13,4 +13,5 @@ public interface IPlaylistManager
|
|||||||
event EventHandler? CurrentPlaylistChanged;
|
event EventHandler? CurrentPlaylistChanged;
|
||||||
event EventHandler<PlaylistAddedEventArgs> PlaylistAdded;
|
event EventHandler<PlaylistAddedEventArgs> PlaylistAdded;
|
||||||
event EventHandler<PlaylistRemovedEventArgs> PlaylistRemoved;
|
event EventHandler<PlaylistRemovedEventArgs> PlaylistRemoved;
|
||||||
|
event EventHandler<PlaylistSaveFailedEventArgs>? PlaylistSaveFailed;
|
||||||
}
|
}
|
||||||
@@ -25,6 +25,7 @@ public class PlaylistManager : IPlaylistManager
|
|||||||
public event EventHandler? CurrentPlaylistChanged;
|
public event EventHandler? CurrentPlaylistChanged;
|
||||||
public event EventHandler<PlaylistAddedEventArgs>? PlaylistAdded;
|
public event EventHandler<PlaylistAddedEventArgs>? PlaylistAdded;
|
||||||
public event EventHandler<PlaylistRemovedEventArgs>? PlaylistRemoved;
|
public event EventHandler<PlaylistRemovedEventArgs>? PlaylistRemoved;
|
||||||
|
public event EventHandler<PlaylistSaveFailedEventArgs>? PlaylistSaveFailed;
|
||||||
|
|
||||||
public PlaylistManager(IPlaylistRepository playlistRepository)
|
public PlaylistManager(IPlaylistRepository playlistRepository)
|
||||||
{
|
{
|
||||||
@@ -60,7 +61,7 @@ public class PlaylistManager : IPlaylistManager
|
|||||||
_playlistsBySongUid[song.UID] = playlist;
|
_playlistsBySongUid[song.UID] = playlist;
|
||||||
}
|
}
|
||||||
|
|
||||||
await playlistRepository.SaveAsync(playlist);
|
await SavePlaylistAsync(playlist);
|
||||||
|
|
||||||
PlaylistAdded?.Invoke(this, new(playlist));
|
PlaylistAdded?.Invoke(this, new(playlist));
|
||||||
|
|
||||||
@@ -108,6 +109,18 @@ public class PlaylistManager : IPlaylistManager
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
await SavePlaylistAsync(playlist);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task SavePlaylistAsync(Playlist playlist)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
await playlistRepository.SaveAsync(playlist);
|
await playlistRepository.SaveAsync(playlist);
|
||||||
}
|
}
|
||||||
|
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
|
||||||
|
{
|
||||||
|
PlaylistSaveFailed?.Invoke(this, new(playlist, ex));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
7
Harmonia.Core/Playlists/PlaylistSaveFailedEventArgs.cs
Normal file
7
Harmonia.Core/Playlists/PlaylistSaveFailedEventArgs.cs
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
namespace Harmonia.Core.Playlists;
|
||||||
|
|
||||||
|
public class PlaylistSaveFailedEventArgs(Playlist playlist, Exception exception) : EventArgs
|
||||||
|
{
|
||||||
|
public Playlist Playlist { get; } = playlist;
|
||||||
|
public Exception Exception { get; } = exception;
|
||||||
|
}
|
||||||
@@ -229,9 +229,9 @@ public partial class PlayerViewModel : ViewModelBase
|
|||||||
|
|
||||||
#region Event Handlers
|
#region Event Handlers
|
||||||
|
|
||||||
private void OnPlayingSongChanged(object? sender, EventArgs e)
|
private void OnPlayingSongChanged(object? sender, PlayingSongChangedEventArgs e)
|
||||||
{
|
{
|
||||||
Song = _audioPlayer.PlayingSong?.Song;
|
Song = e.NewSong?.Song;
|
||||||
Task.Run(UpdateImage);
|
Task.Run(UpdateImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -52,9 +52,9 @@ public partial class PlayingSongViewModel : ViewModelBase
|
|||||||
_dispatcherQueue = DispatcherQueue.GetForCurrentThread();
|
_dispatcherQueue = DispatcherQueue.GetForCurrentThread();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnAudioPlayerPlayingSongChanged(object? sender, EventArgs e)
|
private void OnAudioPlayerPlayingSongChanged(object? sender, PlayingSongChangedEventArgs e)
|
||||||
{
|
{
|
||||||
Song = _audioPlayer.PlayingSong?.Song;
|
Song = e.NewSong?.Song;
|
||||||
Task.Run(UpdateImage);
|
Task.Run(UpdateImage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -274,7 +274,6 @@ public partial class PlaylistDetailViewModel : ViewModelBase
|
|||||||
private void OnPlaylistChanged(object? sender, EventArgs e)
|
private void OnPlaylistChanged(object? sender, EventArgs e)
|
||||||
{
|
{
|
||||||
Playlist?.PlaylistUpdated -= OnPlaylistUpdated;
|
Playlist?.PlaylistUpdated -= OnPlaylistUpdated;
|
||||||
//Playlist = _audioPlayer.Playlist;
|
|
||||||
Playlist = _playlistManager.CurrentPlaylist;
|
Playlist = _playlistManager.CurrentPlaylist;
|
||||||
Playlist?.PlaylistUpdated += OnPlaylistUpdated;
|
Playlist?.PlaylistUpdated += OnPlaylistUpdated;
|
||||||
|
|
||||||
@@ -318,9 +317,9 @@ public partial class PlaylistDetailViewModel : ViewModelBase
|
|||||||
UpdateFilteredSongs();
|
UpdateFilteredSongs();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnPlayingSongChanged(object? sender, EventArgs e)
|
private void OnPlayingSongChanged(object? sender, PlayingSongChangedEventArgs e)
|
||||||
{
|
{
|
||||||
PlayingSong = _audioPlayer.PlayingSong;
|
PlayingSong = e.NewSong;
|
||||||
|
|
||||||
if (_isUserInitiatingSongChange)
|
if (_isUserInitiatingSongChange)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Harmonia.Core.Player;
|
using Harmonia.Core.Player;
|
||||||
using Harmonia.Core.Playlists;
|
using Harmonia.Core.Playlists;
|
||||||
|
using Microsoft.UI.Dispatching;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.ObjectModel;
|
using System.Collections.ObjectModel;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@@ -10,6 +11,7 @@ public partial class PlaylistsViewModel : ViewModelBase
|
|||||||
{
|
{
|
||||||
private readonly IPlaylistManager _playlistManager;
|
private readonly IPlaylistManager _playlistManager;
|
||||||
private readonly IAudioPlayer _audioPlayer;
|
private readonly IAudioPlayer _audioPlayer;
|
||||||
|
private readonly DispatcherQueue _dispatcherQueue;
|
||||||
|
|
||||||
private ObservableCollection<PlaylistItemViewModel> _playlists = [];
|
private ObservableCollection<PlaylistItemViewModel> _playlists = [];
|
||||||
public ObservableCollection<PlaylistItemViewModel> Playlists
|
public ObservableCollection<PlaylistItemViewModel> Playlists
|
||||||
@@ -63,6 +65,8 @@ public partial class PlaylistsViewModel : ViewModelBase
|
|||||||
_audioPlayer = audioPlayer;
|
_audioPlayer = audioPlayer;
|
||||||
_audioPlayer.PlayingSongChanged += OnPlayingSongChanged;
|
_audioPlayer.PlayingSongChanged += OnPlayingSongChanged;
|
||||||
|
|
||||||
|
_dispatcherQueue = DispatcherQueue.GetForCurrentThread();
|
||||||
|
|
||||||
_playlists = new ObservableCollection<PlaylistItemViewModel>(_playlistManager.Playlists.Select(p => new PlaylistItemViewModel(p)));
|
_playlists = new ObservableCollection<PlaylistItemViewModel>(_playlistManager.Playlists.Select(p => new PlaylistItemViewModel(p)));
|
||||||
_selectedPlaylist = _playlists.FirstOrDefault(pv => pv.Playlist == _playlistManager.CurrentPlaylist);
|
_selectedPlaylist = _playlists.FirstOrDefault(pv => pv.Playlist == _playlistManager.CurrentPlaylist);
|
||||||
}
|
}
|
||||||
@@ -88,7 +92,12 @@ public partial class PlaylistsViewModel : ViewModelBase
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnPlayingSongChanged(object? sender, EventArgs e)
|
private void OnPlayingSongChanged(object? sender, PlayingSongChangedEventArgs e)
|
||||||
|
{
|
||||||
|
_dispatcherQueue.TryEnqueue(UpdateActivePlaylist);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdateActivePlaylist()
|
||||||
{
|
{
|
||||||
ActivePlaylist = Playlists.FirstOrDefault(pv => pv.Playlist == _audioPlayer.Playlist);
|
ActivePlaylist = Playlists.FirstOrDefault(pv => pv.Playlist == _audioPlayer.Playlist);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user