<commit_before>﻿using Crystal3.Model;
using Neptunium.Data.History;
using Neptunium.Media;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Navigation;
using Neptunium.Data;
using Crystal3.Navigation;
using Neptunium.ViewModel;
using Windows.UI.Notifications;
using Microsoft.Toolkit.Uwp.Notifications;
using Crystal3;
using Windows.Storage;
using Crystal3.Core;
using Windows.Data.Xml.Dom;
using Hqub.MusicBrainz.API.Entities;
using Hqub.MusicBrainz.API;
using Windows.Media.Playback;
using Windows.Storage.Streams;
using Crystal3.UI.Commands;
using Windows.System;
using System.Diagnostics;
using Neptunium.Managers;
using Microsoft.HockeyApp.DataContracts;
using Neptunium.Managers.Songs;

namespace Neptunium.ViewModel
{
    public class NowPlayingViewViewModel : UIViewModelBase
    {
        public NowPlayingViewViewModel()
        {
            ViewAlbumOnMusicBrainzCommand = new ManualRelayCommand(x =>
            {
                //await Launcher.LaunchUriAsync(new Uri("https://musicbrainz.org/release/" + CurrentSongAlbumData.AlbumID));
            });

            PlayPauseCommand = new RelayCommand(item =>
            {
                if (StationMediaPlayer.IsPlaying)
                    StationMediaPlayer.Pause();
                else
                    StationMediaPlayer.Play();
            });

            NextStationCommand = new RelayCommand(item => { });
            PreviousStationCommand = new RelayCommand(item => { });
        }

        private async void ShoutcastStationMediaPlayer_BackgroundAudioError(object sender, EventArgs e)
        {
            await Crystal3.CrystalApplication.Dispatcher.RunAsync(() =>
            {
                CurrentArtist = "";
                CurrentSong = "";
                CurrentStation = null;
                CurrentAlbum = null;
            });

        }

        private async void ShoutcastStationMediaPlayer_CurrentStationChanged(object sender, EventArgs e)
        {
            await App.Dispatcher.RunWhenIdleAsync(() =>
            {
                IsBusy = true;

                if (StationMediaPlayer.CurrentStation != null)
                {
                    CurrentStation = StationMediaPlayer.CurrentStation;
                    NowPlayingBackgroundImage = StationMediaPlayer.CurrentStation.Logo.ToString();
                }

                HistoryItems?.Clear();

                IsBusy = false;
            });
        }

        protected override async void OnNavigatedTo(object sender, CrystalNavigationEventArgs e)
        {
            CurrentStation = StationMediaPlayer.CurrentStation;

            StationMediaPlayer.CurrentStationChanged += ShoutcastStationMediaPlayer_CurrentStationChanged;
            StationMediaPlayer.BackgroundAudioError += ShoutcastStationMediaPlayer_BackgroundAudioError;

            if (Neptunium.Managers.Songs.SongManager.CurrentSong != null)
            {
                var song = Neptunium.Managers.Songs.SongManager.CurrentSong;

                CurrentSong = song.Track;
                CurrentArtist = song.Artist;

                SongMetadata = song.Track + " by " + song.Artist;

                UpdateCoverImage(song);
            }

            SongManager.PreSongChanged += SongManager_PreSongChanged;
            SongManager.SongChanged += SongManager_SongChanged;
        }

        private void UpdateCoverImage(SongMetadata song)
        {
            var albumUrl = song.MBData?.Album?.AlbumCoverUrl;
            if (string.IsNullOrWhiteSpace(albumUrl))
                albumUrl = song.ITunesData?.Album?.AlbumCoverUrl;

            if (!string.IsNullOrWhiteSpace(albumUrl))
                CoverImage = new Uri(albumUrl);
            else
                CoverImage = new Uri(CurrentStation.Logo);
        }

        protected override void OnNavigatedFrom(object sender, CrystalNavigationEventArgs e)
        {
            SongManager.SongChanged -= SongManager_SongChanged;
            SongManager.PreSongChanged -= SongManager_PreSongChanged;
            StationMediaPlayer.CurrentStationChanged -= ShoutcastStationMediaPlayer_CurrentStationChanged;<commit_msg>Reduce flickering when changing cover art on the now playing pages.<commit_after>﻿using Crystal3.Model;
using Neptunium.Data.History;
using Neptunium.Media;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml.Navigation;
using Neptunium.Data;
using Crystal3.Navigation;
using Neptunium.ViewModel;
using Windows.UI.Notifications;
using Microsoft.Toolkit.Uwp.Notifications;
using Crystal3;
using Windows.Storage;
using Crystal3.Core;
using Windows.Data.Xml.Dom;
using Hqub.MusicBrainz.API.Entities;
using Hqub.MusicBrainz.API;
using Windows.Media.Playback;
using Windows.Storage.Streams;
using Crystal3.UI.Commands;
using Windows.System;
using System.Diagnostics;
using Neptunium.Managers;
using Microsoft.HockeyApp.DataContracts;
using Neptunium.Managers.Songs;

namespace Neptunium.ViewModel
{
    public class NowPlayingViewViewModel : UIViewModelBase
    {
        public NowPlayingViewViewModel()
        {
            ViewAlbumOnMusicBrainzCommand = new ManualRelayCommand(x =>
            {
                //await Launcher.LaunchUriAsync(new Uri("https://musicbrainz.org/release/" + CurrentSongAlbumData.AlbumID));
            });

            PlayPauseCommand = new RelayCommand(item =>
            {
                if (StationMediaPlayer.IsPlaying)
                    StationMediaPlayer.Pause();
                else
                    StationMediaPlayer.Play();
            });

            NextStationCommand = new RelayCommand(item => { });
            PreviousStationCommand = new RelayCommand(item => { });
        }

        private async void ShoutcastStationMediaPlayer_BackgroundAudioError(object sender, EventArgs e)
        {
            await Crystal3.CrystalApplication.Dispatcher.RunAsync(() =>
            {
                CurrentArtist = "";
                CurrentSong = "";
                CurrentStation = null;
                CurrentAlbum = null;
            });

        }

        private async void ShoutcastStationMediaPlayer_CurrentStationChanged(object sender, EventArgs e)
        {
            await App.Dispatcher.RunWhenIdleAsync(() =>
            {
                IsBusy = true;

                if (StationMediaPlayer.CurrentStation != null)
                {
                    CurrentStation = StationMediaPlayer.CurrentStation;
                    NowPlayingBackgroundImage = StationMediaPlayer.CurrentStation.Logo.ToString();
                }

                HistoryItems?.Clear();

                IsBusy = false;
            });
        }

        protected override async void OnNavigatedTo(object sender, CrystalNavigationEventArgs e)
        {
            CurrentStation = StationMediaPlayer.CurrentStation;

            StationMediaPlayer.CurrentStationChanged += ShoutcastStationMediaPlayer_CurrentStationChanged;
            StationMediaPlayer.BackgroundAudioError += ShoutcastStationMediaPlayer_BackgroundAudioError;

            if (Neptunium.Managers.Songs.SongManager.CurrentSong != null)
            {
                var song = Neptunium.Managers.Songs.SongManager.CurrentSong;

                CurrentSong = song.Track;
                CurrentArtist = song.Artist;

                SongMetadata = song.Track + " by " + song.Artist;

                UpdateCoverImage(song);
            }

            SongManager.PreSongChanged += SongManager_PreSongChanged;
            SongManager.SongChanged += SongManager_SongChanged;
        }

        private void UpdateCoverImage(SongMetadata song)
        {
            var albumUrl = song.MBData?.Album?.AlbumCoverUrl;
            if (string.IsNullOrWhiteSpace(albumUrl))
                albumUrl = song.ITunesData?.Album?.AlbumCoverUrl;

            if (!string.IsNullOrWhiteSpace(albumUrl))
            {
                var albumUri = new Uri(albumUrl);

                if (CoverImage != albumUri)
                    CoverImage = albumUri;
            }
            else
            {
                var stationLogo = new Uri(CurrentStation.Logo);

                if (CoverImage != stationLogo)
                    CoverImage = stationLogo;
            }
        }

        protected override void OnNavigatedFrom(object sender, CrystalNavigationEventArgs e)
        {
            SongManager.SongChanged -= SongManager_SongChanged;
            SongManager.PreSongChanged -= SongManager_PreSongChanged;
            StationMediaPlayer.CurrentStationChanged -= ShoutcastStationMediaPlayer_CurrentStationChanged;
{'fraction_non_alphanumeric': 0.07718386258334275, 'fraction_numerical': 0.001356085433382303, 'mean_word_length': 2.227571115973742, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1784524', 'n_tokens_mistral': 2352, 'n_tokens_neox': 2152, 'n_words': 403}