Skip to content

Commit

Permalink
Implemented Player feature
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderDotH committed Apr 21, 2023
1 parent 1a0f060 commit d76ef5a
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

namespace OpenLyricsClient.Backend.Events.EventHandler;

public delegate void TimeUpdatedEventHandler(Object sender);
public delegate void SongUpdatedEventHandler(Object sender);
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public async Task<bool> TestConnection()
try
{
CurrentlyPlayingContext currentlyPlayingContext = await new SpotifyClient(GetAccessToken()).Player.GetCurrentPlayback();
return DataValidator.ValidateData(currentlyPlayingContext) && currentlyPlayingContext.IsPlaying;
return DataValidator.ValidateData(currentlyPlayingContext);
}
catch (Exception e)
{
Expand Down
17 changes: 17 additions & 0 deletions OpenLyricsClient/Backend/Handler/Song/SongHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Runtime.InteropServices.ComTypes;
using System.Threading.Tasks;
using Avalonia.Threading;
using DevBase.Async.Task;
using DevBase.Generics;
using OpenLyricsClient.Backend.Collector.Song;
Expand Down Expand Up @@ -34,6 +36,7 @@ public class SongHandler : IHandler
private SongCollector _songCollector;

public event SongChangedEventHandler SongChanged;
public event SongUpdatedEventHandler SongUpdated;

public SongHandler()
{
Expand Down Expand Up @@ -156,6 +159,11 @@ public ISongProvider GetSongProvider(EnumSongProvider enumSongProvider)
return this._songProviders.FindEntry(enumSongProvider);
}

public EnumSongProvider SongProvider
{
get => this._songProviderChooser.GetSongProvider();
}

private Structure.Song.Song GetCurrentSong()
{
if (DataValidator.ValidateData(this._songProviderChooser))
Expand All @@ -182,6 +190,15 @@ protected virtual void SongChangedEvent(SongChangedEventArgs songChangedEventArg
songChangedEventHandler?.Invoke(this, songChangedEventArgs);
}

public async Task SongUpdatedEvent()
{
await Dispatcher.UIThread.InvokeAsync(() =>
{
SongUpdatedEventHandler songUpdatedEvent = SongUpdated;
songUpdatedEvent?.Invoke(this);
});
}

public Structure.Song.Song CurrentSong
{
get => GetCurrentSong();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,8 @@ public static Structure.Song.Song ValidateUpdatePlayBack(Structure.Song.Song son
public static Structure.Song.Song UpdatePlayBack(Structure.Song.Song song, CurrentlyPlayingContext playbackContext)
{
song.Paused = !playbackContext.IsPlaying;
if (!song.Paused)
{
song.TimeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
song.ProgressMs = playbackContext.ProgressMs;
}

song.TimeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
song.ProgressMs = playbackContext.ProgressMs;
return song;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ private async Task UpdateSongDataTask()
if (!this._service.IsConnected())
continue;

if (!DataValidator.ValidateData(this._currentSong))
{
await UpdateCurrentPlaybackTrack();
}

if (DataValidator.ValidateData(this._spotifyClient) &&
DataValidator.ValidateData(this._currentSong))
{
Expand All @@ -126,6 +131,7 @@ private async Task UpdateSongDataTask()
SpotifyDataMerger.ValidateUpdateAndMerge(this._currentSong, currentTrack);
this._currentSong =
SpotifyDataMerger.ValidateUpdatePlayBack(this._currentSong, currentTrack);
await Core.INSTANCE.SongHandler.SongUpdatedEvent();
}
}
catch (Exception e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ public SolidColorBrush UiBackground
{
get
{
if (Core.INSTANCE.SettingManager.Settings.DisplayPreferences.ArtworkBackground)
if (Core.INSTANCE.SettingManager.Settings?.DisplayPreferences?.ArtworkBackground == true)
return App.Current.FindResource("PrimaryThemeColorBrush") as SolidColorBrush;

return App.Current.FindResource("PrimaryBackgroundBrush") as SolidColorBrush;
Expand Down
117 changes: 65 additions & 52 deletions OpenLyricsClient/Frontend/Models/Pages/LyricsPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using OpenLyricsClient.Backend;
using OpenLyricsClient.Backend.Events.EventArgs;
using OpenLyricsClient.Backend.Handler.Services.Services.Spotify;
using OpenLyricsClient.Backend.Handler.Song.SongProvider;
using OpenLyricsClient.Backend.Structure.Artwork;
using OpenLyricsClient.Backend.Structure.Enum;
using OpenLyricsClient.Backend.Structure.Song;
Expand All @@ -40,18 +41,20 @@ public class LyricsPageViewModel : INotifyPropertyChanged
private string _artwork;

public ReactiveCommand<Unit, Unit> UpdatePlaybackCommand { get; }

public ReactiveCommand<Unit, Unit> PreviousSongCommand { get; }
public ReactiveCommand<Unit, Unit> NextSongCommand { get; }

public LyricsPageViewModel()
{
UpdatePlaybackCommand = ReactiveCommand.CreateFromTask(UpdatePlayback);
PreviousSongCommand = ReactiveCommand.CreateFromTask(()=>SkipSong(EnumPlayback.PREVOUS_TRACK));
NextSongCommand = ReactiveCommand.CreateFromTask(()=>SkipSong(EnumPlayback.NEXT_TRACK));


Core.INSTANCE.TickHandler += OnTickHandler;
Core.INSTANCE.SongHandler.SongChanged += SongHandlerOnSongChanged;
Core.INSTANCE.SettingManager.SettingsChanged += SettingManagerOnSettingsChanged;
Core.INSTANCE.ArtworkHandler.ArtworkAppliedHandler += ArtworkHandlerOnArtworkAppliedHandler;
Core.INSTANCE.LyricHandler.LyricsFound += LyricHandlerOnLyricsFound;
Core.INSTANCE.SongHandler.SongUpdated += SongHandlerOnSongUpdated;

this._currentSongName = string.Empty;
this._currentArtists = string.Empty;
Expand All @@ -63,71 +66,76 @@ public LyricsPageViewModel()
this._time = 0;
}

private void SongHandlerOnSongUpdated(object sender)
{
OnPropertyChanged("SongName");
OnPropertyChanged("Artists");
/*OnPropertyChanged("Album");*/
OnPropertyChanged("IsSongPlaying");
OnPropertyChanged("CurrentTime");
OnPropertyChanged("Percentage");
OnPropertyChanged("CurrentMaxTime");
OnPropertyChanged("IsPlayerAvailable");
}

public async Task UpdatePlayback()
{
Song song = Core.INSTANCE.SongHandler?.CurrentSong!;
SpotifyService service = (SpotifyService)Core.INSTANCE.ServiceHandler.GetServiceByName("Spotify");

if (song.Paused)
try
{
await service.UpdatePlayback(EnumPlayback.RESUME);
Song song = Core.INSTANCE.SongHandler?.CurrentSong!;
SpotifyService service = (SpotifyService)Core.INSTANCE.ServiceHandler.GetServiceByName("Spotify");

if (song.Paused)
{
await service.UpdatePlayback(EnumPlayback.RESUME);
}
else
{
await service.UpdatePlayback(EnumPlayback.PAUSE);
}

await Dispatcher.UIThread.InvokeAsync(() => OnPropertyChanged("IsSongPlaying"));
}
else
catch (Exception e) { }
}

public async Task SkipSong(EnumPlayback playback)
{
try
{
await service.UpdatePlayback(EnumPlayback.PAUSE);
SpotifyService service = (SpotifyService)Core.INSTANCE.ServiceHandler.GetServiceByName("Spotify");
await service.UpdatePlayback(playback);
}
catch (Exception e) { }
}

private void LyricHandlerOnLyricsFound(object sender)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("AiBadge"));
OnPropertyChanged("AiBadge");
}

private void ArtworkHandlerOnArtworkAppliedHandler(object sender, ArtworkAppliedEventArgs args)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Artwork"));
OnPropertyChanged("Artwork");
}

private void SettingManagerOnSettingsChanged(object sender, SettingsChangedEventArgs settingschangedeventargs)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("UiBackground"));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("UiForeground"));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SelectedColor"));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("UnSelectedColor"));
OnPropertyChanged("UiBackground");
OnPropertyChanged("UiForeground");
OnPropertyChanged("SelectedColor");
OnPropertyChanged("UnSelectedColor");
}

private void SongHandlerOnSongChanged(object sender, SongChangedEventArgs songchangedevent)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SongName"));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Artists"));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Album"));
OnPropertyChanged("SongName");
OnPropertyChanged("Artists");
OnPropertyChanged("Album");

this._time = 0;
}

// RECODE NEEDED My eyes are bleeding
private void OnTickHandler(object sender)
{
Song song = Core.INSTANCE.SongHandler.CurrentSong;

if (!DataValidator.ValidateData(song))
return;

if (!this._time.Equals(song.Time))
{
Percentage = song.GetPercentage();
this._time = song.Time;
}



if (!this._currentTime.Equals(song.ProgressString))
CurrentTime = song.ProgressString;

if (!this._currentMaxTime.Equals(song.MaxProgressString))
CurrentMaxTime = song.MaxProgressString;
}

public string? SongName
{
get => Core.INSTANCE?.SongHandler?.CurrentSong?.SongMetadata?.Name!;
Expand All @@ -137,6 +145,11 @@ public bool IsSongPlaying
{
get => Core.INSTANCE.SongHandler?.CurrentSong?.Paused! == false;
}

public bool IsPlayerAvailable
{
get => Core.INSTANCE.SongHandler?.SongProvider! == EnumSongProvider.SPOTIFY;
}

public bool IsSongPaused
{
Expand Down Expand Up @@ -165,11 +178,16 @@ public string Artists

public double Percentage
{
get => this._currentPercentage;
set
get
{
this._currentPercentage = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Percentage"));
Song song = Core.INSTANCE.SongHandler.CurrentSong;

if (DataValidator.ValidateData(song))
{
return song.GetPercentage();
}

return 0;
}
}

Expand All @@ -185,12 +203,7 @@ public string CurrentTime

public string CurrentMaxTime
{
get => this._currentMaxTime;
set
{
this._currentMaxTime = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("CurrentMaxTime"));
}
get => Core.INSTANCE.SongHandler?.CurrentSong?.MaxProgressString!;
}

public string Artwork
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
</Grid>

<TextBlock VerticalAlignment="Center"
TextWrapping="Wrap"
Text="{Binding Text}"/>
</StackPanel>
</DataTemplate>
Expand Down
10 changes: 7 additions & 3 deletions OpenLyricsClient/Frontend/View/Pages/LyricsPage.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
HorizontalAlignment="Left"
VerticalAlignment="Top"
FontWeight="Bold"
FontSize="20"></TextBlock>
FontSize="20"/>

<Border Grid.Column="1"
CornerRadius="7"
Expand Down Expand Up @@ -143,11 +143,14 @@
</StackPanel>

<StackPanel Orientation="Horizontal"
HorizontalAlignment="Center" Spacing="20">
HorizontalAlignment="Center"
IsVisible="{Binding IsPlayerAvailable}"
Spacing="20">
<Button Background="{DynamicResource LightBackgroundBrush}"
CornerRadius="20"
Width="30"
Height="30"
Command="{Binding PreviousSongCommand}"
assists:ShadowAssist.ShadowDepth="Depth2">
<avalonia:MaterialIcon Kind="StepBackward"/>
</Button>
Expand All @@ -160,14 +163,15 @@
assists:ShadowAssist.ShadowDepth="Depth2">
<Panel>
<avalonia:MaterialIcon Kind="Pause" IsVisible="{Binding IsSongPlaying}"/>
<avalonia:MaterialIcon Kind="Resume" IsVisible="{Binding !IsSongPlaying}"/>
<avalonia:MaterialIcon Kind="Play" IsVisible="{Binding !IsSongPlaying}"/>
</Panel>
</Button>

<Button Background="{DynamicResource LightBackgroundBrush}"
CornerRadius="20"
Width="30"
Height="30"
Command="{Binding NextSongCommand}"
assists:ShadowAssist.ShadowDepth="Depth2">
<avalonia:MaterialIcon Kind="StepForward"/>
</Button>
Expand Down
2 changes: 1 addition & 1 deletion OpenLyricsClient/Frontend/View/Pages/LyricsPage.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private void DataContextOnPropertyChanged(object? sender, PropertyChangedEventAr

if (DataValidator.ValidateData(this._lyricsPageViewModel.Artwork))
{
if (e.PropertyName!.Equals("Artwork") &&
if (e.PropertyName?.Equals("Artwork") == true &&
!this._oldImagePath.Equals(this._lyricsPageViewModel.Artwork))
{
Dispatcher.UIThread.InvokeAsync(() =>
Expand Down

0 comments on commit d76ef5a

Please sign in to comment.