fix backwards null checks

This commit is contained in:
Royce551 2021-08-22 09:43:52 -05:00
parent 09f2311c22
commit b7a630697e
14 changed files with 75 additions and 63 deletions

View file

@ -59,7 +59,7 @@ namespace FRESHMusicPlayer.Forms
private void Player_SongChanged(object sender, EventArgs e)
{
ArtistTextBlock.Text = window.CurrentTrack.Artist;
ArtistTextBlock.Text = string.Join(", ", window.CurrentTrack.Artists);
TitleTextBlock.Text = window.CurrentTrack.Title;
ProgressSlider.Maximum = window.Player.CurrentBackend.TotalTime.TotalSeconds;

View file

@ -1,5 +1,6 @@
using ATL;
using DiscordRPC;
using FRESHMusicPlayer.Backends;
using System;
using System.Collections.Generic;
using System.Linq;
@ -24,7 +25,7 @@ namespace FRESHMusicPlayer.Handlers.Integrations
};
}
public void Update(Track track, PlaybackStatus status)
public void Update(IMetadataProvider track, PlaybackStatus status)
{
string activity = string.Empty;
string state = string.Empty;
@ -32,7 +33,7 @@ namespace FRESHMusicPlayer.Handlers.Integrations
{
case PlaybackStatus.Playing:
activity = "play";
state = $"by {track.Artist}";
state = $"by {string.Join(", ", track.Artists)}";
break;
case PlaybackStatus.Paused:
activity = "pause";

View file

@ -1,4 +1,5 @@
using ATL;
using FRESHMusicPlayer.Backends;
namespace FRESHMusicPlayer.Handlers.Integrations
{
@ -12,7 +13,7 @@ namespace FRESHMusicPlayer.Handlers.Integrations
/// </summary>
/// <param name="track">The current track</param>
/// <param name="status">The playback status of the current track</param>
void Update(Track track, PlaybackStatus status);
void Update(IMetadataProvider track, PlaybackStatus status);
/// <summary>
/// Prepares the integration to be closed and set to null (basically Dispose)
/// </summary>

View file

@ -1,4 +1,5 @@
using ATL;
using FRESHMusicPlayer.Backends;
using FRESHMusicPlayer.Utilities;
using System;
using System.Collections.Generic;
@ -37,13 +38,13 @@ namespace FRESHMusicPlayer.Handlers.Integrations
smtc.ButtonPressed += Smtc_ButtonPressed;
}
public void Update(Track track, PlaybackStatus status)
public void Update(IMetadataProvider track, PlaybackStatus status)
{
smtc.PlaybackStatus = (MediaPlaybackStatus)status;
var updater = smtc.DisplayUpdater;
updater.Type = MediaPlaybackType.Music;
updater.MusicProperties.Artist = track.Artist;
updater.MusicProperties.AlbumArtist = track.AlbumArtist;
updater.MusicProperties.Artist = string.Join(", ", track.Artists);
//updater.MusicProperties.AlbumArtist = track.AlbumArtist; // TODO: figure out how to deal with this
updater.MusicProperties.Title = track.Title;
updater.Update();
}

View file

@ -42,11 +42,11 @@ namespace FRESHMusicPlayer.Handlers
Track = new DatabaseTrack
{
Path = window.Player.FilePath,
Artist = window.CurrentTrack.Artist,
Artist = string.Join(", ", window.CurrentTrack.Artists),
Title = window.CurrentTrack.Title,
Album = window.CurrentTrack.Album,
TrackNumber = window.CurrentTrack.TrackNumber,
Length = window.CurrentTrack.Duration
Length = (int)window.Player.TotalTime.TotalSeconds
}
};
TrackingFile.Entries.Add(trackingEntry);

View file

@ -1,4 +1,5 @@
using ATL;
using FRESHMusicPlayer.Backends;
using FRESHMusicPlayer.Forms;
using FRESHMusicPlayer.Forms.Playlists;
using FRESHMusicPlayer.Forms.TagEditor;
@ -58,7 +59,7 @@ namespace FRESHMusicPlayer
public Player Player;
public NotificationHandler NotificationHandler = new NotificationHandler();
public GUILibrary Library;
public Track CurrentTrack;
public IMetadataProvider CurrentTrack;
public PlaytimeTrackingHandler TrackingHandler;
public bool PauseAfterCurrentTrack = false;
@ -82,6 +83,11 @@ namespace FRESHMusicPlayer
};
progressTimer.Tick += ProgressTimer_Tick;
Initialize(initialFile);
}
public async void Initialize(string[] initialFile)
{
LoggingHandler.Log("Reading library...");
LiteDatabase library;
@ -105,14 +111,14 @@ namespace FRESHMusicPlayer
watcher.EnableRaisingEvents = true;
watcher.Changed += (object sender, FileSystemEventArgs args) =>
{
Dispatcher.Invoke(() =>
Dispatcher.Invoke(async () =>
{
var files = File.ReadAllLines(args.FullPath);
if (files.Length != 0) // user wants to play a file
{
player.Queue.Clear();
player.Queue.Add(files);
player.PlayMusic();
Player.Queue.Clear();
Player.Queue.Add(files);
await Player.PlayAsync();
}
else // user might've forgotten fmp is open, let's flash
{
@ -124,11 +130,11 @@ namespace FRESHMusicPlayer
});
};
LoggingHandler.Log("Ready to go!");
if (initialFile != null)
{
Player.Queue.Add(initialFile);
Player.PlayMusic();
await Player.PlayAsync();
}
}
@ -138,13 +144,13 @@ namespace FRESHMusicPlayer
if (!Player.FileLoaded) return;
if (Player.Paused)
{
Player.ResumeMusic();
Player.Resume();
SetIntegrations(PlaybackStatus.Playing);
progressTimer.Start();
}
else
{
Player.PauseMusic();
Player.Pause();
SetIntegrations(PlaybackStatus.Paused);
progressTimer.Stop();
}
@ -153,12 +159,12 @@ namespace FRESHMusicPlayer
public void StopMethod()
{
Player.Queue.Clear();
Player.StopMusic();
Player.Stop();
}
public void NextTrackMethod() => Player.NextSong();
public void PreviousTrackMethod()
public async void NextTrackMethod() => await Player.NextAsync();
public async void PreviousTrackMethod()
{
if (Player.CurrentTime.TotalSeconds <= 5) Player.PreviousSong();
if (Player.CurrentTime.TotalSeconds <= 5) await Player.PreviousAsync();
else
{
if (!Player.FileLoaded) return;
@ -278,7 +284,7 @@ namespace FRESHMusicPlayer
TrackingHandler = null;
}
}
public void HandlePersistence()
public async void HandlePersistence()
{
var persistenceFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "FRESHMusicPlayer", "Configuration", "FMP-WPF", "persistence");
if (File.Exists(persistenceFilePath))
@ -299,7 +305,7 @@ namespace FRESHMusicPlayer
}
if (fields[0] != string.Empty)
{
Player.PlayMusic(fields[0]);
await Player.PlayMusicAsync(fields[0]);
Player.RepositionMusic(int.Parse(fields[1]));
PlayPauseMethod();
ProgressTick();
@ -421,16 +427,16 @@ namespace FRESHMusicPlayer
private void Player_SongChanged(object sender, EventArgs e)
{
CurrentTrack = new Track(Player.FilePath);
Title = $"{CurrentTrack.Artist} - {CurrentTrack.Title} | FRESHMusicPlayer";
CurrentTrack = Player.CurrentBackend.Metadata;
Title = $"{string.Join(", ", CurrentTrack.Artists)} - {CurrentTrack.Title} | FRESHMusicPlayer";
TitleLabel.Text = CurrentTrack.Title;
ArtistLabel.Text = CurrentTrack.Artist == "" ? Properties.Resources.MAINWINDOW_NOARTIST : CurrentTrack.Artist;
ArtistLabel.Text = string.Join(", ", CurrentTrack.Artists) == "" ? Properties.Resources.MAINWINDOW_NOARTIST : string.Join(", ", CurrentTrack.Artists);
ProgressBar.Maximum = Player.CurrentBackend.TotalTime.TotalSeconds;
if (Player.CurrentBackend.TotalTime.TotalSeconds != 0) ProgressIndicator2.Text = Player.CurrentBackend.TotalTime.ToString(@"mm\:ss");
else ProgressIndicator2.Text = "∞";
SetIntegrations(PlaybackStatus.Playing);
UpdatePlayButtonState();
if (CurrentTrack.EmbeddedPictures.Count == 0)
if (CurrentTrack.CoverArt is null)
{
var file = GetCoverArtFromDirectory();
if (file != null)
@ -446,7 +452,7 @@ namespace FRESHMusicPlayer
}
else
{
CoverArtBox.Source = BitmapFrame.Create(new MemoryStream(CurrentTrack.EmbeddedPictures[0].PictureData), BitmapCreateOptions.None, BitmapCacheOption.None);
CoverArtBox.Source = BitmapFrame.Create(new MemoryStream(CurrentTrack.CoverArt), BitmapCreateOptions.None, BitmapCacheOption.None);
SetCoverArtVisibility(true);
}
progressTimer.Start();
@ -458,7 +464,7 @@ namespace FRESHMusicPlayer
LoggingHandler.Log("Changing tracks");
}
private void Player_SongException(object sender, PlaybackExceptionEventArgs e)
private async void Player_SongException(object sender, PlaybackExceptionEventArgs e)
{
NotificationHandler.Add(new Notification
{
@ -467,7 +473,7 @@ namespace FRESHMusicPlayer
DisplayAsToast = true,
Type = NotificationType.Failure
});
Player.NextSong();
await Player.NextAsync();
}
#endregion
#region ControlsBox
@ -566,19 +572,19 @@ namespace FRESHMusicPlayer
PauseAfterCurrentTrack = !PauseAfterCurrentTrack;
UpdatePlayButtonState();
}
private void TrackContextArtist_Click(object sender, RoutedEventArgs e) => ChangeTabs(Menu.Artists, CurrentTrack?.Artist);
private void TrackContextArtist_Click(object sender, RoutedEventArgs e) => ChangeTabs(Menu.Artists, CurrentTrack?.Artists[0]);
private void TrackContextAlbum_Click(object sender, RoutedEventArgs e) => ChangeTabs(Menu.Albums, CurrentTrack?.Album);
private void TrackContextLyrics_Click(object sender, RoutedEventArgs e) => ShowAuxilliaryPane(AuxiliaryPane.Lyrics, openleft: true);
private void TrackContextOpenFile_Click(object sender, RoutedEventArgs e)
private async void TrackContextOpenFile_Click(object sender, RoutedEventArgs e)
{
var dialog = new Forms.FMPTextEntryBox(Properties.Resources.IMPORT_MANUALENTRY);
dialog.ShowDialog();
if (dialog.OK)
{
Player.PlayMusic(dialog.Response);
await Player.PlayMusicAsync(dialog.Response);
}
}
#endregion
@ -694,11 +700,11 @@ namespace FRESHMusicPlayer
e.Effects = DragDropEffects.Copy;
}
private void ControlsBox_Drop(object sender, DragEventArgs e)
private async void ControlsBox_Drop(object sender, DragEventArgs e)
{
Player.Queue.Clear();
InterfaceUtils.DoDragDrop((string[])e.Data.GetData(DataFormats.FileDrop), Player, Library, import: false);
Player.PlayMusic();
await Player.PlayAsync();
}
private void Window_MouseWheel(object sender, MouseWheelEventArgs e)
@ -778,7 +784,7 @@ namespace FRESHMusicPlayer
private void CoverArtBox_ToolTipOpening(object sender, ToolTipEventArgs e)
{
if (CurrentTrack != null)
CoverArtBoxToolTip.Source = BitmapFrame.Create(new MemoryStream(CurrentTrack.EmbeddedPictures[0].PictureData), BitmapCreateOptions.None, BitmapCacheOption.None);
CoverArtBoxToolTip.Source = BitmapFrame.Create(new MemoryStream(CurrentTrack.CoverArt), BitmapCreateOptions.None, BitmapCacheOption.None);
}
private void CoverArtBox_ToolTipClosing(object sender, ToolTipEventArgs e)

View file

@ -34,7 +34,7 @@ namespace FRESHMusicPlayer.Pages
{
window.Player.Queue.Add(dialog.FileName);
await Task.Run(() => window.Library.Import(dialog.FileName));
window.Player.PlayMusic();
await window.Player.PlayAsync();
}
}
@ -62,7 +62,7 @@ namespace FRESHMusicPlayer.Pages
}
window.Player.Queue.Add(reader.FilePaths.ToArray());
await Task.Run(() => window.Library.Import(reader.FilePaths.ToArray()));
window.Player.PlayMusic();
await window.Player.PlayAsync();
}
}
@ -81,7 +81,7 @@ namespace FRESHMusicPlayer.Pages
|| name.EndsWith(".aac")).ToArray();
window.Player.Queue.Add(paths);
await Task.Run(() => window.Library.Import(paths));
window.Player.PlayMusic();
await window.Player.PlayAsync();
}
}
}
@ -96,12 +96,12 @@ namespace FRESHMusicPlayer.Pages
InterfaceUtils.DoDragDrop((string[])e.Data.GetData(DataFormats.FileDrop), window.Player, window.Library, clearqueue: false, enqueue: false);
}
private void TextBoxButton_Click(object sender, RoutedEventArgs e)
private async void TextBoxButton_Click(object sender, RoutedEventArgs e)
{
if (string.IsNullOrEmpty(FilePathBox.Text)) return;
window.Player.Queue.Add(FilePathBox.Text);
window.Library.Import(FilePathBox.Text);
window.Player.PlayMusic();
await window.Player.PlayAsync();
}
}
}

View file

@ -186,11 +186,11 @@ namespace FRESHMusicPlayer.Pages.Library
e.Effects = DragDropEffects.Copy;
}
private void Page_Drop(object sender, DragEventArgs e)
private async void Page_Drop(object sender, DragEventArgs e)
{
window.Player.Queue.Clear();
InterfaceUtils.DoDragDrop((string[])e.Data.GetData(DataFormats.FileDrop), window.Player, window.Library);
window.Player.PlayMusic();
await window.Player.PlayAsync();
var selectedItem = CategoryPanel.SelectedItem;
LoadLibrary();
Thread.Sleep(10);
@ -203,12 +203,12 @@ namespace FRESHMusicPlayer.Pages.Library
window.Player.Queue.Add(tracks);
}
private void PlayAllButton_Click(object sender, RoutedEventArgs e)
private async void PlayAllButton_Click(object sender, RoutedEventArgs e)
{
window.Player.Queue.Clear();
string[] tracks = TracksPanel.Items.OfType<SongEntry>().Select(x => x.FilePath).ToArray(); // avoids firing queue changed event too much
window.Player.Queue.Add(tracks);
window.Player.PlayMusic();
await window.Player.PlayAsync();
}

View file

@ -76,13 +76,13 @@ namespace FRESHMusicPlayer.Pages.Library
}
}
private void PlayAllButton_Click(object sender, RoutedEventArgs e)
private async void PlayAllButton_Click(object sender, RoutedEventArgs e)
{
foreach (SongEntry entry in TracksPanel.Items)
{
window.Player.Queue.Add(entry.FilePath);
}
window.Player.PlayMusic();
await window.Player.PlayAsync();
}
private void Page_Loaded(object sender, RoutedEventArgs e) => Dispatcher.Invoke(() => SearchBox.Focus(), DispatcherPriority.ApplicationIdle);

View file

@ -45,12 +45,12 @@ namespace FRESHMusicPlayer.Pages.Library
PlayButton.Visibility = QueueButton.Visibility = DeleteButton.Visibility = PlayHitbox.Visibility = QueueHitbox.Visibility = DeleteHitbox.Visibility = Visibility.Collapsed;
}
private void PlayButtonClick(object sender, MouseButtonEventArgs e)
private async void PlayButtonClick(object sender, MouseButtonEventArgs e)
{
if (FilePath.StartsWith("http") || File.Exists(FilePath))
{
if (player.FileLoaded) player.Queue.Clear();
player.PlayMusic(FilePath);
await player.PlayMusicAsync(FilePath);
}
else
{
@ -76,12 +76,12 @@ namespace FRESHMusicPlayer.Pages.Library
((ListBox)Parent).Items.Remove(this);
}
private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
private async void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
if (player.FileLoaded) player.Queue.Clear();
player.PlayMusic(FilePath);
await player.PlayMusicAsync(FilePath);
}
}

View file

@ -46,21 +46,21 @@ namespace FRESHMusicPlayer.Pages.Lyrics
{
var track = window.CurrentTrack;
if (track is null) return;
if (track.EmbeddedPictures.Count == 0)
if (track.CoverArt is null)
{
CoverArtBox.Source = null;
CoverArtOverlay.Visibility = Visibility.Hidden;
}
else
{
CoverArtBox.Source = BitmapFrame.Create(new MemoryStream(track.EmbeddedPictures[0].PictureData), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
CoverArtBox.Source = BitmapFrame.Create(new MemoryStream(track.CoverArt), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
CoverArtOverlay.Visibility = Visibility.Visible;
}
}
public void HandleLyrics()
{
LyricsBox.Text = string.Empty;
var track = window.CurrentTrack;
var track = new Track(window.Player.FilePath);
if (track is null) return;
// LRC file present
if (File.Exists(Path.Combine(Path.GetDirectoryName(window.Player.FilePath), Path.GetFileNameWithoutExtension(window.Player.FilePath) + ".lrc")))

View file

@ -37,20 +37,20 @@ namespace FRESHMusicPlayer.Pages
PlayButton.Visibility = DeleteButton.Visibility = Visibility.Collapsed;
}
private void PlayButtonClick(object sender, MouseButtonEventArgs e)
private async void PlayButtonClick(object sender, MouseButtonEventArgs e)
{
player.Queue.Position = Index;
player.PlayMusic();
await player.PlayAsync();
}
private void DeleteButtonClick(object sender, MouseButtonEventArgs e) => player.Queue.Remove(Index);
private void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
private async void UserControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ClickCount == 2)
{
player.Queue.Position = Index;
player.PlayMusic();
await player.PlayAsync();
}
}
}

View file

@ -1,4 +1,5 @@
using ATL;
using FRESHMusicPlayer.Backends;
using FRESHMusicPlayer.Utilities;
using System;
using System.Collections.Generic;
@ -30,25 +31,27 @@ namespace FRESHMusicPlayer.Pages
{
var track = window.CurrentTrack;
if (track is null) return;
if (track.EmbeddedPictures.Count == 0)
if (track.CoverArt is null)
{
CoverArtBox.Source = null;
CoverArtOverlay.Visibility = Visibility.Hidden;
}
else
{
CoverArtBox.Source = BitmapFrame.Create(new MemoryStream(track.EmbeddedPictures[0].PictureData), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
CoverArtBox.Source = BitmapFrame.Create(new MemoryStream(track.CoverArt), BitmapCreateOptions.None, BitmapCacheOption.OnLoad);
CoverArtOverlay.Visibility = Visibility.Visible;
}
InterfaceUtils.SetField(AlbumBox, AlbumLabel, track.Album);
InterfaceUtils.SetField(GenreBox, GenreLabel, track.Genre);
InterfaceUtils.SetField(GenreBox, GenreLabel, string.Join(", ", track.Genres));
InterfaceUtils.SetField(YearBox, YearLabel, track.Year.ToString() == "0" ? null : track.Year.ToString());
InterfaceUtils.SetField(TrackBox, TrackNumberLabel, track.TrackNumber.ToString() == "0" ? null : track.TrackNumber.ToString());
if (track.TrackTotal > 0) TrackBox.Text += "/" + track.TrackTotal;
InterfaceUtils.SetField(DiscBox, DiscNumberLabel, track.DiscNumber.ToString() == "0" ? null : track.DiscNumber.ToString());
if (track.DiscTotal > 0) DiscBox.Text += "/" + track.DiscTotal;
BitrateBox.Text = track.Bitrate + "kbps " + (track.SampleRate / 1000) + "kHz";
if (window.CurrentTrack is FileMetadataProvider file) BitrateBox.Text = file.ATLTrack.Bitrate + "kbps " + (file.ATLTrack.SampleRate / 1000) + "kHz";
else BitrateBox.Text = "Not available"; // TODO: translate
}
private void Player_SongChanged(object sender, EventArgs e) => PopulateFields();