Skip to content

Commit

Permalink
small cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
diogotr7 committed Nov 24, 2023
1 parent 5aee6a2 commit f6c9c91
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 35,206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="SpotifyAPI.Web" Version="7.0.0" />
<PackageReference Include="SpotifyAPI.Web.Auth" Version="7.0.0" />
<PackageReference Include="SpotifyAPI.Web" Version="7.0.2" />
<PackageReference Include="SpotifyAPI.Web.Auth" Version="7.0.2" />
</ItemGroup>

<ItemGroup>
<Content Include="plugin.json" CopyToOutputDirectory="PreserveNewest" />
<Content Include="Spotify.json" CopyToOutputDirectory="PreserveNewest" />
<Content Include="Spotify.zip" CopyToOutputDirectory="PreserveNewest" />
<Content Include="no-user.png" CopyToOutputDirectory="PreserveNewest" />
<PackageReference Include="ArtemisRGB.Plugins.BuildTask" Version="2.0.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
15 changes: 5 additions & 10 deletions src/Artemis.Plugins.Modules.Spotify/DataModels/SpotifyDataModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,16 @@ public enum Key
{
None = -1,
C = 0,
[Description("C#")]
Cs = 1,
[Description("C#")] Cs = 1,
D = 2,
[Description("D#")]
Ds = 3,
[Description("D#")] Ds = 3,
E = 4,
F = 5,
[Description("F#")]
Fs = 6,
[Description("F#")] Fs = 6,
G = 7,
[Description("G#")]
Gs = 8,
[Description("G#")] Gs = 8,
A = 9,
[Description("A#")]
As = 10,
[Description("A#")] As = 10,
B = 11,
}

Expand Down
35,157 changes: 0 additions & 35,157 deletions src/Artemis.Plugins.Modules.Spotify/Spotify.json

This file was deleted.

Binary file added src/Artemis.Plugins.Modules.Spotify/Spotify.zip
Binary file not shown.
64 changes: 28 additions & 36 deletions src/Artemis.Plugins.Modules.Spotify/SpotifyModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Artemis.Core.Services;

namespace Artemis.Plugins.Modules.Spotify;

Expand All @@ -19,31 +20,28 @@ public class SpotifyModule : Module<SpotifyDataModel>
{
public override List<IModuleActivationRequirement> ActivationRequirements { get; } = new();

#region Constructor and readonly fields
private readonly ILogger _logger;
private readonly PluginSetting<PKCETokenResponse> _token;
private readonly PluginSetting<Dictionary<string, ColorSwatch>> _cache;
private readonly HttpClient _httpClient;
private readonly ILogger _logger;
private readonly PluginSetting<PKCETokenResponse> _token;
private readonly PluginSetting<Dictionary<string, ColorSwatch>> _cache;
private readonly HttpClient _httpClient;

public SpotifyModule(PluginSettings settings, ILogger logger)
public SpotifyModule(PluginSettings settings, ILogger logger)
{
_logger = logger;
_token = settings.GetSetting<PKCETokenResponse>(Constants.SPOTIFY_AUTH_SETTING);
_cache = settings.GetSetting<Dictionary<string, ColorSwatch>>("AlbumArtCache", new());
_httpClient = new HttpClient
{
_logger = logger;
_token = settings.GetSetting<PKCETokenResponse>(Constants.SPOTIFY_AUTH_SETTING);
_cache = settings.GetSetting<Dictionary<string, ColorSwatch>>("AlbumArtCache", new());
_httpClient = new HttpClient
{
Timeout = TimeSpan.FromSeconds(2)
};
}
#endregion
Timeout = TimeSpan.FromSeconds(2)
};
}

private SpotifyClient? _spotify;
private string? _trackId;
private string? _contextId;
private string? _albumArtUrl;
private TrackAudioAnalysis? _analysis;

#region Plugin Methods
public override void Enable()
{
try
Expand All @@ -54,14 +52,15 @@ public override void Enable()
}
catch (Exception e)
{
_logger.Error("Failed spotify authentication, login in the settings dialog:" + e.ToString());
_logger.Error(e, "Failed spotify authentication, login in the settings dialog");
}

AddTimedUpdate(TimeSpan.FromSeconds(2), UpdatePlayback, nameof(UpdatePlayback));
AddDefaultProfile(DefaultCategoryName.Applications, Plugin.ResolveRelativePath("Spotify.json"));
AddDefaultProfile(DefaultCategoryName.Applications, Plugin.ResolveRelativePath("Spotify.zip"));
}
catch (Exception e)
{
_logger.Error("Failed spotify authentication, login in the settings dialog:" + e.ToString());
_logger.Error(e, "Failed spotify authentication, login in the settings dialog");
}
}

Expand Down Expand Up @@ -98,9 +97,7 @@ public override void Update(double deltaTime)
DataModel.Track.Analysis.CurrentSegment = currentSegment;
}
}
#endregion

#region DataModel update methods
private async Task UpdatePlayback(double deltaTime)
{
//this will be null before authentication
Expand All @@ -119,7 +116,7 @@ private async Task UpdatePlayback(double deltaTime)
}

if (playing is null || DataModel is null)
return;//weird
return; //weird

try
{
Expand All @@ -142,16 +139,15 @@ private async Task UpdateTrackInfo(FullTrack track)
{
if (_spotify is null)
return;

string trackId = track.Uri.Split(':').Last();
if (trackId == _trackId)

if (track.Id == _trackId)
return;

UpdateBasicTrackInfo(track);

try
{
TrackAudioFeatures features = await _spotify.Tracks.GetAudioFeatures(trackId);
TrackAudioFeatures features = await _spotify.Tracks.GetAudioFeatures(track.Id);
UpdateTrackFeatures(features);
}
catch (Exception e)
Expand All @@ -161,7 +157,7 @@ private async Task UpdateTrackInfo(FullTrack track)

try
{
_analysis = await _spotify.Tracks.GetAudioAnalysis(trackId);
_analysis = await _spotify.Tracks.GetAudioAnalysis(track.Id);
}
catch (Exception e)
{
Expand All @@ -175,14 +171,14 @@ private async Task UpdateTrackInfo(FullTrack track)
_albumArtUrl = image.Url;
}

_trackId = trackId;
_trackId = track.Id;
}

private async Task UpdatePlayerInfo(CurrentlyPlayingContext playing)
{
if (_spotify is null)
return;

DataModel.Device.Name = playing.Device.Name;
DataModel.Device.Type = playing.Device.Type;
DataModel.Player.Shuffle = playing.ShuffleState;
Expand Down Expand Up @@ -279,9 +275,6 @@ private void UpdateTrackFeatures(TrackAudioFeatures features)
DataModel.Track.Features.TimeSignature = features.TimeSignature;
}

#endregion

#region VM interaction
internal bool LoggedIn => _spotify != null;

internal void Login()
Expand Down Expand Up @@ -309,7 +302,7 @@ internal void Logout()
{
if (_spotify is null)
return null;

try
{
return await _spotify.UserProfile.Current();
Expand All @@ -319,5 +312,4 @@ internal void Logout()
return null;
}
}
#endregion
}
}

0 comments on commit f6c9c91

Please sign in to comment.