Added playing song info view and view model.

This commit is contained in:
2025-03-03 22:32:17 -05:00
parent 0675131195
commit 9fc8791ad1
11 changed files with 198 additions and 27 deletions

View File

@@ -1,11 +1,61 @@
using Avalonia.Controls;
using Harmonia.Core.Engine;
using Harmonia.Core.Models;
using Harmonia.Core.Player;
using System.ComponentModel;
using System.Linq;
namespace Harmonia.UI.Views;
public partial class MainWindow : Window
{
public MainWindow()
private readonly IAudioPlayer _audioPlayer;
private const string ApplicationTitle = "Harmonia";
public MainWindow(IAudioPlayer audioPlayer)
{
_audioPlayer = audioPlayer;
_audioPlayer.PropertyChanged += OnAudioPlayerPropertyChanged;
Title = ApplicationTitle;
InitializeComponent();
}
private void OnAudioPlayerPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(_audioPlayer.State):
Title = GetUpdatedAppTitle();
//PropertyChanged(nameof(Title));
break;
}
}
private string GetUpdatedAppTitle()
{
return _audioPlayer.State switch
{
AudioPlaybackState.Stopped => ApplicationTitle,
_ => $"{GetSongArtistsAndTitle()} - {ApplicationTitle}"
};
}
private string GetSongArtistsAndTitle()
{
if (_audioPlayer.PlayingSong == null)
return string.Empty;
Song song = _audioPlayer.PlayingSong.Song;
string[] values =
[
string.Join(" / ", song.Artists ?? song.AlbumArtists),
song.Title ?? song.ShortFileName
];
return string.Join(" - ", values.Where(value => string.IsNullOrWhiteSpace(value) == false));
}
}