Added playlist view.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Harmonia.Core.Engine;
|
||||
using Harmonia.Core.Models;
|
||||
using Harmonia.Core.Player;
|
||||
using Harmonia.WinUI.Caching;
|
||||
@@ -7,6 +8,7 @@ using Microsoft.UI.Dispatching;
|
||||
using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Media.Imaging;
|
||||
using System;
|
||||
using System.ComponentModel;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Input;
|
||||
@@ -196,6 +198,7 @@ public partial class PlayerViewModel : ViewModelBase
|
||||
{
|
||||
_audioPlayer = audioPlayer;
|
||||
_audioPlayer.PlayingSongChanged += OnPlayingSongChanged;
|
||||
_audioPlayer.PropertyChanged += OnAudioPlayerPropertyChanged;
|
||||
|
||||
_audioBitmapImageCache = audioBitmapCache;
|
||||
|
||||
@@ -232,6 +235,28 @@ public partial class PlayerViewModel : ViewModelBase
|
||||
DispatcherQueue.GetForCurrentThread().TryEnqueue(() => SongImageSource = bitmapImage);
|
||||
}
|
||||
|
||||
private void OnAudioPlayerPropertyChanged(object? sender, PropertyChangedEventArgs e)
|
||||
{
|
||||
switch (e.PropertyName)
|
||||
{
|
||||
case nameof(_audioPlayer.State):
|
||||
UpdateTimer();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateTimer()
|
||||
{
|
||||
if (_audioPlayer.State == AudioPlaybackState.Playing)
|
||||
{
|
||||
_timer.Start();
|
||||
}
|
||||
else
|
||||
{
|
||||
_timer.Stop();
|
||||
}
|
||||
}
|
||||
|
||||
private void TickTock(object? sender, object e)
|
||||
{
|
||||
Position = _audioPlayer.Position;
|
||||
|
||||
@@ -20,6 +20,8 @@ using System.Threading.Tasks;
|
||||
using System.Timers;
|
||||
using System.Windows.Input;
|
||||
using Windows.ApplicationModel.DataTransfer;
|
||||
using Windows.System;
|
||||
using DispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue;
|
||||
using Timer = System.Timers.Timer;
|
||||
|
||||
namespace Harmonia.WinUI.ViewModels;
|
||||
@@ -31,6 +33,7 @@ public partial class PlaylistViewModel : ViewModelBase
|
||||
private readonly IAudioFileScanner _audioFileScanner;
|
||||
private readonly IAudioEngine _audioEngine;
|
||||
private readonly IStorageProvider _storageProvider;
|
||||
private readonly DispatcherQueue _dispatcherQueue;
|
||||
|
||||
private Timer? _filterTimer;
|
||||
|
||||
@@ -121,7 +124,8 @@ public partial class PlaylistViewModel : ViewModelBase
|
||||
IAudioBitmapImageCache audioBitmapImageCache,
|
||||
IAudioFileScanner audioFileScanner,
|
||||
IAudioEngine audioEngine,
|
||||
IStorageProvider storageProvider)
|
||||
IStorageProvider storageProvider,
|
||||
IPlaylistRepository playlistRepository)
|
||||
{
|
||||
_audioPlayer = audioPlayer;
|
||||
_audioPlayer.PlaylistChanged += OnPlaylistChanged;
|
||||
@@ -131,6 +135,23 @@ public partial class PlaylistViewModel : ViewModelBase
|
||||
_audioFileScanner = audioFileScanner;
|
||||
_audioEngine = audioEngine;
|
||||
_storageProvider = storageProvider;
|
||||
_dispatcherQueue = DispatcherQueue.GetForCurrentThread();
|
||||
|
||||
// Testing
|
||||
Task.Run(() => PlayDemoSong(playlistRepository));
|
||||
}
|
||||
|
||||
private async Task PlayDemoSong(IPlaylistRepository playlistRepository)
|
||||
{
|
||||
if (playlistRepository.Get().Count == 0)
|
||||
{
|
||||
playlistRepository.AddPlaylist();
|
||||
}
|
||||
|
||||
Playlist playlist = playlistRepository.Get().First();
|
||||
|
||||
if (playlist.Songs.Count > 0)
|
||||
await _audioPlayer.LoadAsync(playlist.Songs[0], PlaybackMode.LoadOnly);
|
||||
}
|
||||
|
||||
private void OnPlaylistChanged(object? sender, EventArgs e)
|
||||
@@ -161,10 +182,10 @@ public partial class PlaylistViewModel : ViewModelBase
|
||||
switch (e.Action)
|
||||
{
|
||||
case PlaylistUpdateAction.Add:
|
||||
DispatcherQueue.GetForCurrentThread().TryEnqueue(() => AddSongs(e.Songs, e.Index));
|
||||
_dispatcherQueue.TryEnqueue(() => AddSongs(e.Songs, e.Index));
|
||||
break;
|
||||
case PlaylistUpdateAction.Remove:
|
||||
DispatcherQueue.GetForCurrentThread().TryEnqueue(() => RemoveSongsFromCollection(e.Songs));
|
||||
_dispatcherQueue.TryEnqueue(() => RemoveSongsFromCollection(e.Songs));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -232,7 +253,7 @@ public partial class PlaylistViewModel : ViewModelBase
|
||||
_filterTimer.Dispose();
|
||||
_filterTimer = null;
|
||||
|
||||
DispatcherQueue.GetForCurrentThread().TryEnqueue(UpdateFilteredSongs);
|
||||
_dispatcherQueue.TryEnqueue(UpdateFilteredSongs);
|
||||
}
|
||||
|
||||
private void UpdateFilteredSongs()
|
||||
@@ -304,10 +325,12 @@ public partial class PlaylistViewModel : ViewModelBase
|
||||
|
||||
private FilePickerFileType GetAudioFileTypes()
|
||||
{
|
||||
string[] patterns = _audioEngine.SupportedFormats.Select(format => format.Replace("*", "")).ToArray();
|
||||
|
||||
return new()
|
||||
{
|
||||
Name = "Audio Files",
|
||||
Patterns = [.. _audioEngine.SupportedFormats]
|
||||
Patterns = patterns
|
||||
};
|
||||
}
|
||||
|
||||
@@ -316,7 +339,7 @@ public partial class PlaylistViewModel : ViewModelBase
|
||||
if (Playlist == null)
|
||||
return;
|
||||
|
||||
string path = await _storageProvider.GetPathAsync();
|
||||
string? path = await _storageProvider.GetPathAsync();
|
||||
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
return;
|
||||
|
||||
Reference in New Issue
Block a user