Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CmdPal.Ext.Spotify/Commands/PausePlaybackCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using SpotifyAPI.Web;
using CmdPal.Ext.Spotify.Helpers;
using CmdPal.Ext.Spotify.Properties;
using SpotifyAPI.Web;
using System.Threading.Tasks;

namespace CmdPal.Ext.Spotify.Commands;
Expand All @@ -7,6 +9,8 @@ internal sealed partial class PausePlaybackCommand : PlayerCommand<PlayerPausePl
{
public PausePlaybackCommand(SpotifyClient spotifyClient) : base(spotifyClient, new())
{
Name = Resources.ResultPausePlaybackTitle;
Icon = Icons.Pause;
}

protected override async Task InvokeAsync(IPlayerClient player, PlayerPausePlaybackRequest requestParams)
Expand Down
8 changes: 6 additions & 2 deletions CmdPal.Ext.Spotify/Commands/ResumePlaybackCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using SpotifyAPI.Web;
using CmdPal.Ext.Spotify.Helpers;
using CmdPal.Ext.Spotify.Properties;
using SpotifyAPI.Web;
using System.Threading.Tasks;

namespace CmdPal.Ext.Spotify.Commands;
Expand All @@ -7,9 +9,11 @@ internal sealed partial class ResumePlaybackCommand : PlayerCommand<PlayerResume
{
public ResumePlaybackCommand(SpotifyClient spotifyClient, PlayerResumePlaybackRequest requestParams = null) : base(spotifyClient, requestParams ?? new())
{
Name = requestParams == null ? Resources.ResultResumePlaybackTitle : Resources.ResultPlayName;
Icon = Icons.Play;
}

public ResumePlaybackCommand(SpotifyClient spotifyClient, string contextUri) : base(spotifyClient, new PlayerResumePlaybackRequest { ContextUri = contextUri })
public ResumePlaybackCommand(SpotifyClient spotifyClient, string contextUri) : this(spotifyClient, new PlayerResumePlaybackRequest { ContextUri = contextUri })
{
}

Expand Down
11 changes: 10 additions & 1 deletion CmdPal.Ext.Spotify/Commands/SetRepeatCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using SpotifyAPI.Web;
using CmdPal.Ext.Spotify.Helpers;
using CmdPal.Ext.Spotify.Properties;
using SpotifyAPI.Web;
using System.Threading.Tasks;

namespace CmdPal.Ext.Spotify.Commands;
Expand All @@ -7,6 +9,13 @@ internal sealed partial class SetRepeatCommand : PlayerCommand<PlayerSetRepeatRe
{
public SetRepeatCommand(SpotifyClient spotifyClient, PlayerSetRepeatRequest requestParams) : base(spotifyClient, requestParams)
{
Name = requestParams.StateParam switch
{
PlayerSetRepeatRequest.State.Off => Resources.ResultSetRepeatOffTitle,
PlayerSetRepeatRequest.State.Context => Resources.ResultSetRepeatContextTitle,
PlayerSetRepeatRequest.State.Track => Resources.ResultSetRepeatTrackTitle,
};
Icon = Icons.Repeat;
}

protected override async Task InvokeAsync(IPlayerClient player, PlayerSetRepeatRequest requestParams)
Expand Down
10 changes: 9 additions & 1 deletion CmdPal.Ext.Spotify/Commands/SetShuffleCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using SpotifyAPI.Web;
using CmdPal.Ext.Spotify.Helpers;
using CmdPal.Ext.Spotify.Properties;
using SpotifyAPI.Web;
using System.Threading.Tasks;

namespace CmdPal.Ext.Spotify.Commands;
Expand All @@ -7,6 +9,12 @@ internal sealed partial class SetShuffleCommand : PlayerCommand<PlayerShuffleReq
{
public SetShuffleCommand(SpotifyClient spotifyClient, PlayerShuffleRequest requestParams) : base(spotifyClient, requestParams)
{
Name = requestParams.State switch
{
true => Resources.ResultTurnOnShuffleTitle,
false => Resources.ResultTurnOffShuffleTitle,
};
Icon = Icons.Shuffle;
}

protected override async Task InvokeAsync(IPlayerClient player, PlayerShuffleRequest requestParams)
Expand Down
6 changes: 5 additions & 1 deletion CmdPal.Ext.Spotify/Commands/SkipNextCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using SpotifyAPI.Web;
using CmdPal.Ext.Spotify.Helpers;
using CmdPal.Ext.Spotify.Properties;
using SpotifyAPI.Web;
using System.Threading.Tasks;

namespace CmdPal.Ext.Spotify.Commands;
Expand All @@ -7,6 +9,8 @@ internal sealed partial class SkipNextCommand : PlayerCommand<PlayerSkipNextRequ
{
public SkipNextCommand(SpotifyClient spotifyClient) : base(spotifyClient, new())
{
Name = Resources.ResultNextTrackTitle;
Icon = Icons.Next;
}

protected override async Task InvokeAsync(IPlayerClient player, PlayerSkipNextRequest requestParams)
Expand Down
6 changes: 5 additions & 1 deletion CmdPal.Ext.Spotify/Commands/SkipPreviousCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using SpotifyAPI.Web;
using CmdPal.Ext.Spotify.Helpers;
using CmdPal.Ext.Spotify.Properties;
using SpotifyAPI.Web;
using System.Threading.Tasks;

namespace CmdPal.Ext.Spotify.Commands;
Expand All @@ -7,6 +9,8 @@ internal sealed partial class SkipPreviousCommand : PlayerCommand<PlayerSkipPrev
{
public SkipPreviousCommand(SpotifyClient spotifyClient) : base(spotifyClient, new())
{
Name = Resources.ResultPreviousTrackTitle;
Icon = Icons.Previous;
}

protected override async Task InvokeAsync(IPlayerClient player, PlayerSkipPreviousRequest requestParams)
Expand Down
6 changes: 5 additions & 1 deletion CmdPal.Ext.Spotify/Commands/TogglePlaybackCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.CommandPalette.Extensions.Toolkit;
using CmdPal.Ext.Spotify.Helpers;
using CmdPal.Ext.Spotify.Properties;
using Microsoft.CommandPalette.Extensions.Toolkit;
using SpotifyAPI.Web;
using System.Threading.Tasks;

Expand All @@ -8,6 +10,8 @@ internal sealed partial class TogglePlaybackCommand : PlayerCommand<PlayerResume
{
public TogglePlaybackCommand(SpotifyClient spotifyClient) : base(spotifyClient, new())
{
Name = Resources.ResultTogglePlaybackTitle;
Icon = Icons.PlayPause;
}

protected override async Task InvokeAsync(IPlayerClient player, PlayerResumePlaybackRequest requestParams)
Expand Down
139 changes: 67 additions & 72 deletions CmdPal.Ext.Spotify/Pages/SpotifListPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ internal sealed partial class SpotifyListPage : DynamicListPage

public SpotifyListPage(SettingsManager settingsManager)
{
Icon = IconHelpers.FromRelativePath("Assets\\StoreLogo.png");
Title = Resources.ExtensionDisplayName;
Name = Resources.ExtensionDisplayName;
Icon = Icons.Spotify;
Title = Resources.PageTitle;
Name = Resources.PageTitle;

_settingsManager = settingsManager;
_settingsManager.Settings.SettingsChanged += (_, _) => SearchAsync(SearchText);
Expand All @@ -35,8 +35,18 @@ public SpotifyListPage(SettingsManager settingsManager)
_credentialsPath = Path.Combine(appDataPath, "credentials.json");

_items = [.. GetItems(string.Empty).GetAwaiter().GetResult()];

EmptyContent = _defaultEmptyContent;
}

private CommandItem _defaultEmptyContent => new(GetPlaybackCommands()[0])
{
Title = Resources.EmptyContentTitle,
Subtitle = Resources.EmptyContentSubtitle,
Icon = Icons.Spotify,
MoreCommands = GetPlaybackCommands().Skip(1).Select(command => new CommandContextItem(command)).ToArray(),
};

public override IListItem[] GetItems() => [.. _items];

public override void UpdateSearchText(string oldSearch, string newSearch)
Expand Down Expand Up @@ -64,36 +74,56 @@ private async Task<List<ListItem>> GetItems(string search)
var clientId = _settingsManager.ClientId;

if (string.IsNullOrEmpty(clientId))
return [
new ListItem(new NoOpCommand())
{
Title = Resources.ResultMissingClientIdTitle,
Subtitle = Resources.ResultMissingClientIdSubTitle
}
];

{
EmptyContent = new CommandItem()
{
Title = Resources.ResultMissingClientIdTitle,
Subtitle = Resources.ResultMissingClientIdSubTitle,
};
return [];
}

if (!File.Exists(_credentialsPath))
{
var loginCommand = new LoginCommand(clientId, _credentialsPath);
loginCommand.LoggedIn += (_, _) => SearchAsync(search);

return [
new ListItem(loginCommand)
{
Title = Resources.ResultLoginTitle,
Subtitle = Resources.ResultLoginSubTitle
}
];
EmptyContent = new CommandItem(loginCommand)
{
Title = Resources.ResultLoginTitle,
Subtitle = Resources.ResultLoginSubTitle,
Icon = Icons.Spotify,
};
return [];
}

if (_spotifyClient == null)
_spotifyClient = await GetSpotifyClientAsync(clientId);

if (string.IsNullOrEmpty(search.Trim()))
return GetPlayertItems();
{
EmptyContent = _defaultEmptyContent;
return [];
}

return await GetSearchItemsAsync(search);
try
{
var results = await GetSearchItemsAsync(search);
if (results.Count == 0)
{
EmptyContent = new CommandItem()
{
Title = Resources.EmptyResultsTitle,
};
}
return results;
}
catch (Exception ex)
{
EmptyContent = new CommandItem() {
Title = Resources.EmptyErrorTitle,
};
}
return [];
}

private async Task<SpotifyClient> GetSpotifyClientAsync(string clientId)
Expand All @@ -111,58 +141,23 @@ private async Task<SpotifyClient> GetSpotifyClientAsync(string clientId)
}

private List<ListItem> GetPlayertItems()
{
return GetPlaybackCommands().Select(command => new ListItem(command)).ToList();
}

public List<Command> GetPlaybackCommands()
{
return [
new ListItem(new TogglePlaybackCommand(_spotifyClient))
{
Title = Resources.ResultTogglePlaybackTitle,
Icon = Icons.PlayPause,
},
new ListItem(new PausePlaybackCommand(_spotifyClient))
{
Title = Resources.ResultPausePlaybackTitle,
Icon = Icons.Pause,
},
new ListItem(new ResumePlaybackCommand(_spotifyClient))
{
Title = Resources.ResultResumePlaybackTitle,
Icon = Icons.Play,
},
new ListItem(new SkipNextCommand(_spotifyClient))
{
Title = Resources.ResultNextTrackTitle,
Icon = Icons.Next,
},
new ListItem(new SkipPreviousCommand(_spotifyClient))
{
Title = Resources.ResultPreviousTrackTitle,
Icon = Icons.Previous,
},
new ListItem(new SetShuffleCommand(_spotifyClient, new(true)))
{
Title = Resources.ResultTurnOnShuffleTitle,
Icon = Icons.Shuffle,
},
new ListItem(new SetShuffleCommand(_spotifyClient, new(false)))
{
Title = Resources.ResultTurnOffShuffleTitle,
Icon = Icons.Shuffle,
},
new ListItem(new SetRepeatCommand(_spotifyClient, new(PlayerSetRepeatRequest.State.Track)))
{
Title = Resources.ResultSetRepeatTrackTitle,
Icon = Icons.Repeat,
},
new ListItem(new SetRepeatCommand(_spotifyClient, new(PlayerSetRepeatRequest.State.Context)))
{
Title = Resources.ResultSetRepeatContextTitle,
Icon = Icons.Repeat,
},
new ListItem(new SetRepeatCommand(_spotifyClient, new(PlayerSetRepeatRequest.State.Off)))
{
Title = Resources.ResultSetRepeatOffTitle,
Icon = Icons.Repeat,
},
new TogglePlaybackCommand(_spotifyClient),
new PausePlaybackCommand(_spotifyClient),
new ResumePlaybackCommand(_spotifyClient),
new SkipNextCommand(_spotifyClient),
new SkipPreviousCommand(_spotifyClient),
new SetShuffleCommand(_spotifyClient, new(true)),
new SetShuffleCommand(_spotifyClient, new(false)),
new SetRepeatCommand(_spotifyClient, new(PlayerSetRepeatRequest.State.Track)),
new SetRepeatCommand(_spotifyClient, new(PlayerSetRepeatRequest.State.Context)),
new SetRepeatCommand(_spotifyClient, new(PlayerSetRepeatRequest.State.Off)),
];
}

Expand All @@ -182,7 +177,7 @@ private async Task<List<ListItem>> GetSearchItemsAsync(string search)
new ListItem(new ResumePlaybackCommand(_spotifyClient, new PlayerResumePlaybackRequest() { Uris = [track.Uri] }))
{
Title = track.Name,
Subtitle = $"{Resources.ResultSongSubTitle}{(track.Explicit ? $" � {Resources.ResultSongExplicitSubTitle}" : "")} � {Resources.ResultSongBySubTitle} {string.Join(", ", track.Artists.Select(x => x.Name))}",
Subtitle = $"{Resources.ResultSongSubTitle}{(track.Explicit ? $" � {Resources.ResultSongExplicitSubTitle}" : "")} � {Resources.ResultSongBySubTitle} {string.Join(", ", track.Artists.Select(x => x.Name))}",
Icon = new IconInfo(track.Album.Images.OrderBy(x => x.Width * x.Height).FirstOrDefault()?.Url),
})
);
Expand Down
54 changes: 54 additions & 0 deletions CmdPal.Ext.Spotify/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading