Skip to content

Commit

Permalink
feat(seek): Added the ability to seek.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderDotH committed Jun 6, 2023
1 parent 1feb6fe commit a6cb88b
Show file tree
Hide file tree
Showing 14 changed files with 382 additions and 274 deletions.
34 changes: 30 additions & 4 deletions OpenLyricsClient/Backend/Handler/Services/ServiceHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ServiceHandler()

public bool IsConnected(string serviceName)
{
return GetServiceByName(serviceName).IsConnected();
return GetServiceByName(serviceName).Connected;
}

public string GetAccessToken(IService service)
Expand All @@ -35,9 +35,35 @@ public string GetAccessToken(IService service)
if (!DataValidator.ValidateData(s))
return null;

return s.GetAccessToken();
return s.AccessToken;
}

public void MarkServiceAsActive(IService service)
{
for (int i = 0; i < this._services.Length; i++)
{
IService s = this._services.Get(i);

s.Active = false;

if (s.Equals(service))
s.Active = true;
}
}

public IService GetActiveService()
{
for (int i = 0; i < this._services.Length; i++)
{
IService s = this._services.Get(i);

if (s.Active)
return s;
}

return null;
}

public string GetAccessToken(string serviceName)
{
return GetAccessToken(GetServiceByName(serviceName));
Expand All @@ -53,7 +79,7 @@ public IService GetServiceByName(string serviceName)
for (int i = 0; i < this._services.Length; i++)
{
IService s = this._services.Get(i);
if (s.ServiceName().Equals(serviceName))
if (s.Name.Equals(serviceName))
{
return s;
}
Expand All @@ -66,7 +92,7 @@ public bool IsAnyConnected()
{
for (int i = 0; i < this._services.Length; i++)
{
if (this._services.Get(i).IsConnected())
if (this._services.Get(i).Connected)
return true;
}

Expand Down
14 changes: 7 additions & 7 deletions OpenLyricsClient/Backend/Handler/Services/Services/IService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ namespace OpenLyricsClient.Backend.Handler.Services.Services
{
interface IService
{
string ServiceName();
Task StartAuthorization();

string GetAccessToken();
bool IsConnected();
Task<bool> TestConnection();

bool CanSeek();
Task<bool> Seek(long position);
void Dispose();

string ProcessName();
string Name { get; }
string ProcessName { get; }
string AccessToken { get; }
bool Connected { get; }
bool Active { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private async Task RefreshToken()
await this._refreshTokenSuspensionToken.WaitForRelease();
await Task.Delay(1000);

if (!IsConnected())
if (!Connected)
continue;

long now = DateTimeOffset.Now.ToUnixTimeMilliseconds();
Expand All @@ -64,22 +64,19 @@ private async Task RefreshToken()
}
}

public bool IsConnected()
{
return Core.INSTANCE.SettingsHandler.Settings<SpotifySection>().GetValue<bool>("IsSpotifyConnected") == true;
}
public bool Connected => Core.INSTANCE.SettingsHandler.Settings<SpotifySection>().GetValue<bool>("IsSpotifyConnected");

public async Task<bool> TestConnection()
{
if (!IsConnected())
if (!Connected)
return false;

if (!DataValidator.ValidateData(GetAccessToken()))
if (!DataValidator.ValidateData(this.AccessToken))
return false;

try
{
CurrentlyPlayingContext currentlyPlayingContext = await new SpotifyClient(GetAccessToken()).Player.GetCurrentPlayback();
CurrentlyPlayingContext currentlyPlayingContext = await new SpotifyClient(this.AccessToken).Player.GetCurrentPlayback();
return DataValidator.ValidateData(currentlyPlayingContext);
}
catch (Exception e)
Expand All @@ -88,9 +85,35 @@ public async Task<bool> TestConnection()
}
}

public bool CanSeek()
{
return true;
}

public async Task<bool> Seek(long position)
{
if (!CanSeek())
return false;

if (!Connected)
return false;

if (!DataValidator.ValidateData(this.AccessToken))
return false;

try
{
return await new SpotifyClient(this.AccessToken).Player.SeekTo(new PlayerSeekToRequest(position));
}
catch (Exception e)
{
return false;
}
}

public async Task<SpotifyStatistics> GetStatistics(string accessToken = "")
{
SpotifyClient client = new SpotifyClient(accessToken.Equals(String.Empty) ? GetAccessToken() : accessToken);
SpotifyClient client = new SpotifyClient(accessToken.Equals(String.Empty) ? this.AccessToken : accessToken);

PersonalizationTopRequest topRequest = new PersonalizationTopRequest
{
Expand All @@ -114,9 +137,8 @@ public async Task<SpotifyStatistics> GetStatistics(string accessToken = "")

public async Task UpdatePlayback(EnumPlayback playback)
{
SpotifyClient spotifyClient = new SpotifyClient(GetAccessToken());
Shared.Structure.Song.Song song = Core.INSTANCE.SongHandler?.CurrentSong!;

SpotifyClient spotifyClient = new SpotifyClient(this.AccessToken);

switch (playback)
{
case EnumPlayback.PREVOUS_TRACK:
Expand All @@ -141,16 +163,8 @@ public async Task UpdatePlayback(EnumPlayback playback)
}
}
}

public string GetAccessToken()
{
// Task.Factory.StartNew(async() =>
// {
// await RefreshTokenRequest();
//
// }).Wait(Core.INSTANCE.CancellationTokenSource.Token);
return Core.INSTANCE.SettingsHandler.Settings<SpotifySection>().GetValue<string>("AccessToken");
}

public string AccessToken => Core.INSTANCE.SettingsHandler.Settings<SpotifySection>().GetValue<string>("AccessToken");

private async Task RefreshTokenRequest()
{
Expand Down Expand Up @@ -247,16 +261,12 @@ public async Task StartAuthorization()
await Core.INSTANCE.SettingsHandler.TriggerEvent(typeof(SpotifySection), "SpotifyExpireTime");
await Core.INSTANCE.SettingsHandler.TriggerEvent(typeof(SpotifySection), "IsSpotifyConnected");
}

public bool Active { get; set; }

string IService.ServiceName()
{
return "Spotify";
}
public string Name => "Spotify";

public string ProcessName()
{
return "Spotify";
}
string IService.ProcessName => "Spotify";

public void Dispose()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,16 @@ private void OnTickHandler(object sender)
IService tidalService = Core.INSTANCE.ServiceHandler.GetServiceByName("Tidal");

if (IsInUse(tidalService))
{
songProvider = EnumSongProvider.TIDAL;
Core.INSTANCE.ServiceHandler.MarkServiceAsActive(tidalService);
}

if (IsInUse(spotifyService))
{
songProvider = EnumSongProvider.SPOTIFY;
Core.INSTANCE.ServiceHandler.MarkServiceAsActive(spotifyService);
}

if (songProvider == EnumSongProvider.NONE)
return;
Expand All @@ -81,67 +87,23 @@ private void OnTickHandler(object sender)
}
}

// private async Task SongProviderChooserTask()
// {
// while (!this._disposed)
// {
// await this._taskSuspensionToken.WaitForRelease();
//
// if (!DataValidator.ValidateData(Core.INSTANCE.WindowLogger))
// continue;
//
// EnumSongProvider songProvider = EnumSongProvider.NONE;
//
// IService spotifyService = Core.INSTANCE.ServiceHandler.GetServiceByName("Spotify");
// IService tidalService = Core.INSTANCE.ServiceHandler.GetServiceByName("Tidal");
//
// if (await IsInUse(tidalService))
// songProvider = EnumSongProvider.TIDAL;
//
// if (await IsInUse(spotifyService))
// songProvider = EnumSongProvider.SPOTIFY;
//
// if (songProvider == EnumSongProvider.NONE)
// continue;
//
// if (!songProvider.Equals(this._currentSongProvider))
// {
// this._debugger.Write("SongProvider has been changed to: " + new AString(songProvider.ToString()).CapitalizeFirst(), DebugType.INFO);
// this._currentSongProvider = songProvider;
//
// if (songProvider == EnumSongProvider.SPOTIFY)
// {
// ResumeProvider(EnumSongProvider.SPOTIFY);
// SuspendProvider(EnumSongProvider.TIDAL);
// }
// else if (songProvider == EnumSongProvider.TIDAL)
// {
// ResumeProvider(EnumSongProvider.TIDAL);
// SuspendProvider(EnumSongProvider.SPOTIFY);
// }
// }
//
// await Task.Delay(3000);
// }
// }

private bool IsInUse(IService service)
{
if (!DataValidator.ValidateData(service))
return false;

if (!service.IsConnected())
if (!service.Connected)
return false;

AList<string> lastWindows = Core.INSTANCE.WindowLogger.LastWindows(service.ProcessName());
AList<string> foundProcesses = ProcessUtils.GetRunningProcesses(service.ProcessName());
AList<string> lastWindows = Core.INSTANCE.WindowLogger.LastWindows(service.ProcessName);
AList<string> foundProcesses = ProcessUtils.GetRunningProcesses(service.ProcessName);

if (!lastWindows.IsEmpty())
if (lastWindows.Get(0).Equals(service.ProcessName()))
if (lastWindows.Get(0).Equals(service.ProcessName))
return true;

if (!foundProcesses.IsEmpty())
if (foundProcesses.Get(0).Equals(service.ProcessName()))
if (foundProcesses.Get(0).Equals(service.ProcessName))
return true;

if (service.TestConnection().GetAwaiter().GetResult())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private async Task TimeSyncTask()
await Task.Delay(20);
await this._timeSyncSuspensionToken.WaitForRelease();

if (!this._service.IsConnected())
if (!this._service.Connected)
continue;

if (DataValidator.ValidateData(this._currentSong) &&
Expand Down Expand Up @@ -109,7 +109,7 @@ private async Task UpdateSongDataTask()
await Task.Delay(750);
await this._updateSongDataSuspensionToken.WaitForRelease();

if (!this._service.IsConnected())
if (!this._service.Connected)
continue;

if (!DataValidator.ValidateData(this._currentSong))
Expand Down Expand Up @@ -145,7 +145,7 @@ private async Task UpdateSongDataTask()
//Song changed -> get new song
public async Task<Shared.Structure.Song.Song> UpdateCurrentPlaybackTrack()
{
if (!this._service.IsConnected())
if (!this._service.Connected)
return null;

if (DataValidator.ValidateData(this._spotifyClient))
Expand Down Expand Up @@ -174,7 +174,7 @@ private async Task UpdateSongDataTask()

private SpotifyClient GetPlayerApi()
{
return new SpotifyClient(this._service.GetAccessToken());
return new SpotifyClient(this._service.AccessToken);
}

public void Dispose()
Expand Down
22 changes: 22 additions & 0 deletions OpenLyricsClient/Frontend/Extensions/ColorBrushExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using Avalonia.Media;

namespace OpenLyricsClient.Frontend.Extensions;

public static class ColorBrushExtension
{
public static SolidColorBrush AdjustBrightness(this SolidColorBrush solidColorBrush, double percentage)
{
Color color = solidColorBrush.Color;

byte r = (byte)((color.R * 0.01) * percentage);
byte g = (byte)((color.G * 0.01) * percentage);
byte b = (byte)((color.B * 0.01) * percentage);

r = Math.Clamp(r, (byte)0, (byte)255);
g = Math.Clamp(g, (byte)0, (byte)255);
b = Math.Clamp(b, (byte)0, (byte)255);

return new SolidColorBrush(new Color(color.A, r, g, b));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public Vector Offset
{
Vector diff = vector - this._oldOffsetValue;

if (diff.Y < 0 && ScrollDirection == ScrollDirection.DOWN)
if (diff.Y < 0 && ScrollDirection == ScrollDirection.DOWN && !this.BypassScrollDirectionCheck)
{
this.Offset = this._oldOffsetValue;
}
Expand All @@ -306,6 +306,8 @@ public ScrollDirection ScrollDirection
}
}

public bool BypassScrollDirectionCheck { get; set; } = false;

/// <summary>
/// Gets the size of the viewport on the scrollable content.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
IsVisible="False">
<ItemsRepeater.ItemTemplate>
<DataTemplate DataType="lyrics:LyricPart">
<tile:LyricsTile LyricPart="{Binding}"/>
<tile:LyricsTile LyricPart="{Binding}"
Headless="True"/>
</DataTemplate>
</ItemsRepeater.ItemTemplate>
</ItemsRepeater>
Expand Down
Loading

0 comments on commit a6cb88b

Please sign in to comment.