Added playing song view. Adding styling to playing song. Fixed caching cancellation issue.
This commit is contained in:
79
Harmonia.WinUI/ViewModels/PlayingSongViewModel.cs
Normal file
79
Harmonia.WinUI/ViewModels/PlayingSongViewModel.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using Harmonia.Core.Models;
|
||||
using Harmonia.Core.Player;
|
||||
using Harmonia.WinUI.Caching;
|
||||
using Microsoft.UI.Dispatching;
|
||||
using Microsoft.UI.Xaml.Media.Imaging;
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Harmonia.WinUI.ViewModels;
|
||||
|
||||
public class PlayingSongViewModel : ViewModelBase
|
||||
{
|
||||
private readonly IAudioPlayer _audioPlayer;
|
||||
private readonly IAudioBitmapImageCache _audioBitmapImageCache;
|
||||
private readonly DispatcherQueue _dispatcherQueue;
|
||||
|
||||
private CancellationTokenSource? _songImageCancellationTokenSource;
|
||||
|
||||
private Song? _song;
|
||||
public Song? Song
|
||||
{
|
||||
get
|
||||
{
|
||||
return _song;
|
||||
}
|
||||
private set
|
||||
{
|
||||
SetProperty(ref _song, value);
|
||||
}
|
||||
}
|
||||
|
||||
private BitmapImage? _songImageSource;
|
||||
public BitmapImage? SongImageSource
|
||||
{
|
||||
get
|
||||
{
|
||||
return _songImageSource;
|
||||
}
|
||||
private set
|
||||
{
|
||||
SetProperty(ref _songImageSource, value);
|
||||
}
|
||||
}
|
||||
|
||||
public PlayingSongViewModel(IAudioPlayer audioPlayer, IAudioBitmapImageCache audioBitmapImageCache)
|
||||
{
|
||||
_audioPlayer = audioPlayer;
|
||||
_audioPlayer.PlayingSongChanged += OnAudioPlayerPlayingSongChanged;
|
||||
|
||||
_audioBitmapImageCache = audioBitmapImageCache;
|
||||
_dispatcherQueue = DispatcherQueue.GetForCurrentThread();
|
||||
}
|
||||
|
||||
private void OnAudioPlayerPlayingSongChanged(object? sender, EventArgs e)
|
||||
{
|
||||
Song = _audioPlayer.PlayingSong?.Song;
|
||||
Task.Run(UpdateImage);
|
||||
}
|
||||
|
||||
private async Task UpdateImage()
|
||||
{
|
||||
// TODO: Show default picture
|
||||
if (Song == null)
|
||||
return;
|
||||
|
||||
if (_songImageCancellationTokenSource != null)
|
||||
await _songImageCancellationTokenSource.CancelAsync();
|
||||
|
||||
_songImageCancellationTokenSource = new();
|
||||
CancellationToken cancellationToken = _songImageCancellationTokenSource.Token;
|
||||
|
||||
_dispatcherQueue.TryEnqueue(async () =>
|
||||
{
|
||||
BitmapImage? bitmapImage = await _audioBitmapImageCache.GetAsync(Song, cancellationToken);
|
||||
SongImageSource = bitmapImage;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -6,9 +6,9 @@ using Harmonia.Core.Playlists;
|
||||
using Harmonia.Core.Scanner;
|
||||
using Harmonia.WinUI.Caching;
|
||||
using Harmonia.WinUI.Storage;
|
||||
using Microsoft.UI.Dispatching;
|
||||
using Microsoft.UI.Xaml.Media.Imaging;
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
@@ -20,8 +20,8 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Timers;
|
||||
using System.Windows.Input;
|
||||
using Windows.ApplicationModel.Contacts;
|
||||
using Windows.ApplicationModel.DataTransfer;
|
||||
using Windows.System;
|
||||
using DispatcherQueue = Microsoft.UI.Dispatching.DispatcherQueue;
|
||||
using Timer = System.Timers.Timer;
|
||||
|
||||
@@ -288,7 +288,38 @@ public partial class PlaylistViewModel : ViewModelBase
|
||||
return;
|
||||
|
||||
List<PlaylistSong> filteredPlaylistSongs = [.. Playlist.Songs.Where(playlistSong => IsFiltered(playlistSong.Song))];
|
||||
FilteredPlaylistSongs = [.. filteredPlaylistSongs];
|
||||
//FilteredPlaylistSongs = [.. filteredPlaylistSongs];
|
||||
|
||||
for (int i = FilteredPlaylistSongs.Count -1; i >= 0; i--)
|
||||
{
|
||||
PlaylistSong playlistSong = FilteredPlaylistSongs[i];
|
||||
|
||||
bool inPlaylist = Playlist.Songs.Contains(playlistSong);
|
||||
bool inFilter = filteredPlaylistSongs.Contains(playlistSong);
|
||||
|
||||
if (!inPlaylist || !inFilter)
|
||||
{
|
||||
FilteredPlaylistSongs.Remove(playlistSong);
|
||||
}
|
||||
}
|
||||
|
||||
int insertionIndex = 0;
|
||||
|
||||
foreach (PlaylistSong playlistSong in Playlist.Songs)
|
||||
{
|
||||
bool inFilter = filteredPlaylistSongs.Contains(playlistSong);
|
||||
bool inCurrentFilteredList = FilteredPlaylistSongs.Contains(playlistSong);
|
||||
|
||||
if (inFilter)
|
||||
{
|
||||
if (!inCurrentFilteredList)
|
||||
{
|
||||
FilteredPlaylistSongs.Insert(insertionIndex, playlistSong);
|
||||
}
|
||||
|
||||
insertionIndex++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private bool IsFiltered(Song song)
|
||||
|
||||
@@ -10,8 +10,8 @@ public class ViewModelLocator
|
||||
public static PlayerViewModel PlayerViewModel
|
||||
=> App.ServiceProvider.GetRequiredService<PlayerViewModel>();
|
||||
|
||||
//public static PlayingSongInfoViewModel PlayingSongInfoViewModel
|
||||
// => App.ServiceProvider.GetRequiredService<PlayingSongInfoViewModel>();
|
||||
public static PlayingSongViewModel PlayingSongViewModel
|
||||
=> App.ServiceProvider.GetRequiredService<PlayingSongViewModel>();
|
||||
|
||||
public static PlaylistViewModel PlaylistViewModel
|
||||
=> App.ServiceProvider.GetRequiredService<PlaylistViewModel>();
|
||||
|
||||
Reference in New Issue
Block a user