Added playing song view. Adding styling to playing song. Fixed caching cancellation issue.
This commit is contained in:
@@ -6,9 +6,13 @@ using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using Microsoft.UI.Xaml.Data;
|
||||
using Microsoft.UI.Xaml.Input;
|
||||
using Microsoft.UI.Xaml.Media;
|
||||
using Microsoft.UI.Xaml.Media.Imaging;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Linq;
|
||||
using Windows.UI.Popups;
|
||||
|
||||
namespace Harmonia.WinUI.Views;
|
||||
|
||||
@@ -21,6 +25,8 @@ public sealed partial class PlaylistView : UserControl
|
||||
InitializeComponent();
|
||||
|
||||
_viewModel = (PlaylistViewModel)DataContext;
|
||||
_viewModel.PropertyChanging += OnViewModelPropertyChanging;
|
||||
_viewModel.PropertyChanged += OnViewModelPropertyChanged;
|
||||
_viewModel.PlayingSongChangedAutomatically += OnPlayingSongChangedAutomatically;
|
||||
|
||||
foreach (MenuFlyoutItemBase item in PlaylistListViewMenuFlyout.Items)
|
||||
@@ -29,6 +35,75 @@ public sealed partial class PlaylistView : UserControl
|
||||
}
|
||||
}
|
||||
|
||||
private void OnViewModelPropertyChanging(object? sender, PropertyChangingEventArgs e)
|
||||
{
|
||||
if (e.PropertyName == nameof(_viewModel.PlayingSong))
|
||||
{
|
||||
ListViewItem listViewItem = (ListViewItem)PlaylistListView.ContainerFromItem(_viewModel.PlayingSong);
|
||||
|
||||
if (listViewItem != null)
|
||||
{
|
||||
UpdateListViewItemStyle(listViewItem, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnViewModelPropertyChanged(object? sender, PropertyChangedEventArgs e)
|
||||
{
|
||||
if (e.PropertyName == nameof(_viewModel.PlayingSong))
|
||||
{
|
||||
ListViewItem listViewItem = (ListViewItem)PlaylistListView.ContainerFromItem(_viewModel.PlayingSong);
|
||||
|
||||
UpdateListViewItemStyle(listViewItem, true);
|
||||
}
|
||||
}
|
||||
|
||||
private void UpdateListViewItemStyle(FrameworkElement listViewItem, bool isPlaying)
|
||||
{
|
||||
if (listViewItem == null)
|
||||
return;
|
||||
|
||||
FrameworkElement? frameworkElement;
|
||||
|
||||
if (listViewItem.Name == "PlaylistSongListViewItem")
|
||||
{
|
||||
frameworkElement = listViewItem;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (listViewItem.FindDescendant("PlaylistSongListViewItem") is not FrameworkElement frameworkElement2)
|
||||
return;
|
||||
|
||||
frameworkElement = frameworkElement2;
|
||||
}
|
||||
|
||||
if (frameworkElement == null)
|
||||
return;
|
||||
|
||||
string songItemTitleBrushName = isPlaying ? "PlayingSongTitleTextBlock" : "SongTitleTextBlock";
|
||||
string songItemSubtitleBrushName = isPlaying ? "PlayingSongSubtitleTextBlock" : "SongSubtitleTextBlock";
|
||||
|
||||
UpdateElementStyle(frameworkElement, "SongTitleTextBlock", songItemTitleBrushName);
|
||||
UpdateElementStyle(frameworkElement, "SongArtistsTextBlock", songItemSubtitleBrushName);
|
||||
UpdateElementStyle(frameworkElement, "SongAlbumTextBlock", songItemSubtitleBrushName);
|
||||
}
|
||||
|
||||
private void UpdateElementStyle(FrameworkElement dependencyObject, string elementName, string resourceName)
|
||||
{
|
||||
if (dependencyObject.FindDescendant(elementName) is not FrameworkElement frameworkElement)
|
||||
return;
|
||||
|
||||
Resources.TryGetValue(resourceName, out object? resource);
|
||||
|
||||
if (resource == null)
|
||||
Application.Current.Resources.TryGetValue(resourceName, out resource);
|
||||
|
||||
if (resource is not Style style)
|
||||
return;
|
||||
|
||||
frameworkElement.Style = style;
|
||||
}
|
||||
|
||||
private void OnPlayingSongChangedAutomatically(object? sender, EventArgs e)
|
||||
{
|
||||
BringPlayingSongIntoView();
|
||||
@@ -112,6 +187,16 @@ public sealed partial class PlaylistView : UserControl
|
||||
});
|
||||
}
|
||||
|
||||
private void PlaylistSongListViewItem_DataContextChanged(FrameworkElement sender, DataContextChangedEventArgs args)
|
||||
{
|
||||
if (args.NewValue is not PlaylistSong playlistSong)
|
||||
return;
|
||||
|
||||
bool isPlaying = playlistSong == _viewModel.PlayingSong;
|
||||
|
||||
UpdateListViewItemStyle(sender, isPlaying);
|
||||
}
|
||||
|
||||
private async void PlaylistListViewItem_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
|
||||
{
|
||||
if (sender is not FrameworkElement element)
|
||||
|
||||
Reference in New Issue
Block a user