Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tracks_playback_limit config option #219

Merged
merged 2 commits into from
Jul 15, 2023
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
1 change: 1 addition & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ All configuration files should be placed inside the application's configuration
| ------------------------------------ | ----------------------------------------------------------------------------- | ---------------------------------------------------------- |
| `client_id` | the Spotify client's ID | `65b708073fc0480ea92a077233ca87bd` |
| `client_port` | the port that the application's client is running on to handle CLI commands | `8080` |
| `tracks_playback_limit` | the limit for the number of tracks played in a **tracks** playback | `50` |
| `playback_format` | the format of the text in the playback's window | `{track} • {artists}\n{album}\n{metadata}` |
| `notify_format` | the format of a notification (`notify` feature only) | `{ summary = "{track} • {artists}", body = "{album}" }` |
| `copy_command` | the command used to execute a copy-to-clipboard action | `xclip -sel c` (Linux), `pbcopy` (MacOS), `clip` (Windows) |
Expand Down
1 change: 1 addition & 0 deletions examples/app.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
theme = "default"
client_id = "65b708073fc0480ea92a077233ca87bd"
client_port = 8080
tracks_playback_limit = 50
playback_format = "{track} • {artists}\n{album}\n{metadata}"
notify_format = { summary = "{track} • {artists}", body = "{album}" }
# the default `copy_command` is based on the OS
Expand Down
4 changes: 4 additions & 0 deletions spotify_player/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ pub struct AppConfig {
#[cfg(feature = "notify")]
pub notify_format: NotifyFormat,

pub tracks_playback_limit: usize,

// session configs
pub proxy: Option<String>,
pub ap_port: Option<u16>,
Expand Down Expand Up @@ -134,6 +136,8 @@ impl Default for AppConfig {

client_port: 8080,

tracks_playback_limit: 50,

playback_format: String::from("{track} • {artists}\n{album}\n{metadata}"),
#[cfg(feature = "notify")]
notify_format: NotifyFormat {
Expand Down
6 changes: 4 additions & 2 deletions spotify_player/src/event/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,14 @@ pub fn handle_command_for_track_table_window(
let id = rand::thread_rng().gen_range(0..tracks.len());

client_pub.send(ClientRequest::Player(PlayerRequest::StartPlayback(
base_playback.uri_offset(tracks[id].id.uri()),
base_playback
.uri_offset(tracks[id].id.uri(), state.app_config.tracks_playback_limit),
)))?;
}
Command::ChooseSelected => {
client_pub.send(ClientRequest::Player(PlayerRequest::StartPlayback(
base_playback.uri_offset(tracks[id].id.uri()),
base_playback
.uri_offset(tracks[id].id.uri(), state.app_config.tracks_playback_limit),
)))?;
}
Command::ShowActionsOnSelectedItem => {
Expand Down
2 changes: 0 additions & 2 deletions spotify_player/src/state/consant.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
pub use super::*;
use once_cell::sync::Lazy;

pub const PLAYBACK_TRACKS_LIMIT: usize = 200;

pub static USER_TOP_TRACKS_ID: Lazy<TracksId> =
Lazy::new(|| TracksId::new("tracks:user-top-tracks", "Top Tracks"));

Expand Down
10 changes: 5 additions & 5 deletions spotify_player/src/state/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,24 +411,24 @@ impl TracksId {

impl Playback {
/// creates new playback with a specified offset based on the current playback
pub fn uri_offset(&self, uri: String) -> Self {
pub fn uri_offset(&self, uri: String, limit: usize) -> Self {
match self {
Playback::Context(id, _) => {
Playback::Context(id.clone(), Some(rspotify_model::Offset::Uri(uri)))
}
Playback::URIs(ids, _) => {
let ids = if ids.len() < super::PLAYBACK_TRACKS_LIMIT {
let ids = if ids.len() < limit {
ids.clone()
} else {
let pos = ids
.iter()
.position(|id| id.uri() == uri)
.unwrap_or_default();
let l = pos.saturating_sub(super::PLAYBACK_TRACKS_LIMIT / 2);
let r = std::cmp::min(l + super::PLAYBACK_TRACKS_LIMIT, ids.len());
let l = pos.saturating_sub(limit / 2);
let r = std::cmp::min(l + limit, ids.len());
// For a list with too many tracks, to avoid payload limit when making the `start_playback`
// API request, we restrict the range of tracks to be played, which is based on the
// playing track's position (if any) and the application's limit (PLAYBACK_TRACKS_LIMIT).
// playing track's position (if any) and the application's limit (`app_config.tracks_playback_limit`).
// Related issue: https://github.com/aome510/spotify-player/issues/78
ids[l..r].to_vec()
};
Expand Down
Loading