Skip to content

Commit

Permalink
feat: 197 use commands not actions
Browse files Browse the repository at this point in the history
use commands instead of track actions to move playlist items
  • Loading branch information
cobbinma committed Jul 28, 2023
1 parent 33b61fe commit ffa6ca9
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 165 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,8 @@ List of supported commands:
| `SortTrackByDuration` | sort the track table (if any) by track's duration | `s d` |
| `SortTrackByAddedDate` | sort the track table (if any) by track's added date | `s D` |
| `ReverseOrder` | reverse the order of the track table (if any) | `s r` |
| `MovePlaylistItemUp` | move playlist item up one position | `C-k` |
| `MovePlaylistItemDown` | move playlist item down one position | `C-j` |
To add new shortcuts or modify the default shortcuts, please refer to the [keymaps section](docs/config.md#keymaps) in the configuration documentation.
Expand Down
7 changes: 5 additions & 2 deletions spotify_player/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ pub enum Command {
SortTrackByDuration,
SortTrackByAddedDate,
ReverseTrackOrder,

MovePlaylistItemUp,
MovePlaylistItemDown,
}

#[derive(Debug, Copy, Clone, PartialEq)]
Expand All @@ -78,8 +81,6 @@ pub enum TrackAction {
AddToQueue,
AddToPlaylist,
DeleteFromCurrentPlaylist,
MoveUpInCurrentPlaylist,
MoveDownInCurrentPlaylist,
AddToLikedTracks,
DeleteFromLikedTracks,
CopyTrackLink,
Expand Down Expand Up @@ -198,6 +199,8 @@ impl Command {
Self::SortTrackByDuration => "sort the track table (if any) by track's duration",
Self::SortTrackByAddedDate => "sort the track table (if any) by track's added date",
Self::ReverseTrackOrder => "reverse the order of the track table (if any)",
Self::MovePlaylistItemUp => "move playlist item up one position",
Self::MovePlaylistItemDown => "move playlist item down one position",
}
}
}
8 changes: 8 additions & 0 deletions spotify_player/src/config/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,14 @@ impl Default for KeymapConfig {
key_sequence: "s r".into(),
command: Command::ReverseTrackOrder,
},
Keymap {
key_sequence: "C-k".into(),
command: Command::MovePlaylistItemUp,
},
Keymap {
key_sequence: "C-j".into(),
command: Command::MovePlaylistItemDown,
},
],
}
}
Expand Down
60 changes: 0 additions & 60 deletions spotify_player/src/event/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -612,66 +612,6 @@ fn handle_nth_action(
}
ui.popup = None;
}
TrackAction::MoveUpInCurrentPlaylist => {
if let PageState::Context {
id: Some(ContextId::Playlist(playlist_id)),
state: Some(ContextPageUIState::Playlist { track_table }),
..
} = ui.current_page()
{
if let (Some(_), Some(selected)) = (
state
.data
.read()
.user_data
.playlists
.iter()
.find(|playlist| &playlist.id == playlist_id),
track_table.selected(),
) {
let insert_before = selected - 1;
client_pub.send(ClientRequest::ReorderPlaylistItems {
playlist_id: playlist_id.clone_static(),
insert_before,
range_start: selected,
range_length: None,
snapshot_id: None,
})?;
ui.current_page_mut().select(insert_before);
};
}
ui.popup = None;
}
TrackAction::MoveDownInCurrentPlaylist => {
if let PageState::Context {
id: Some(ContextId::Playlist(playlist_id)),
state: Some(ContextPageUIState::Playlist { track_table }),
..
} = ui.current_page()
{
if let (Some(_), Some(selected)) = (
state
.data
.read()
.user_data
.playlists
.iter()
.find(|playlist| &playlist.id == playlist_id),
track_table.selected(),
) {
let insert_before = selected + 1;
client_pub.send(ClientRequest::ReorderPlaylistItems {
playlist_id: playlist_id.clone_static(),
insert_before,
range_start: selected,
range_length: None,
snapshot_id: None,
})?;
ui.current_page_mut().select(insert_before);
};
}
ui.popup = None;
}
},
ActionListItem::Album(album, actions) => match actions[n] {
AlbumAction::GoToArtist => {
Expand Down
165 changes: 62 additions & 103 deletions spotify_player/src/event/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,23 +174,8 @@ pub fn handle_command_for_track_table_window(
}
Command::ShowActionsOnSelectedItem => {
let mut actions = command::construct_track_actions(tracks[id], data);
if let ContextId::Playlist(playlist_id) = context_id {
if let ContextId::Playlist(_) = context_id {
actions.push(TrackAction::DeleteFromCurrentPlaylist);

if let (Some(Context::Playlist { tracks, playlist }), Some(user_id)) = (
state.data.read().caches.context.get(&playlist_id.uri()),
state
.data
.read()
.user_data
.user
.as_ref()
.map(|user| &user.id),
) {
actions.append(&mut track_actions_for_playlist_owner(
tracks, id, user_id, playlist,
));
}
}
ui.popup = Some(PopupState::ActionList(
ActionListItem::Track(tracks[id].clone(), actions),
Expand All @@ -200,6 +185,66 @@ pub fn handle_command_for_track_table_window(
Command::AddSelectedItemToQueue => {
client_pub.send(ClientRequest::AddTrackToQueue(tracks[id].id.clone()))?;
}
Command::MovePlaylistItemUp => {
if let PageState::Context {
id: Some(ContextId::Playlist(playlist_id)),
..
} = ui.current_page()
{
if id > 0
&& data
.user_data
.playlists
.iter()
.find(|playlist| &playlist.id == playlist_id)
.is_some_and(|playlist| {
Some(&playlist.owner.1)
== data.user_data.user.as_ref().map(|user| &user.id)
})
{
let insert_before = id - 1;
client_pub.send(ClientRequest::ReorderPlaylistItems {
playlist_id: playlist_id.clone_static(),
insert_before,
range_start: id,
range_length: None,
snapshot_id: None,
})?;
ui.current_page_mut().select(insert_before);
};
}
ui.popup = None;
}
Command::MovePlaylistItemDown => {
if let PageState::Context {
id: Some(ContextId::Playlist(playlist_id)),
..
} = ui.current_page()
{
let insert_before = id + 1;
if insert_before < tracks.len()
&& data
.user_data
.playlists
.iter()
.find(|playlist| &playlist.id == playlist_id)
.is_some_and(|playlist| {
Some(&playlist.owner.1)
== data.user_data.user.as_ref().map(|user| &user.id)
})
{
client_pub.send(ClientRequest::ReorderPlaylistItems {
playlist_id: playlist_id.clone_static(),
insert_before,
range_start: id,
range_length: None,
snapshot_id: None,
})?;
ui.current_page_mut().select(insert_before);
};
}
ui.popup = None;
}
_ => return Ok(false),
}
Ok(true)
Expand All @@ -213,6 +258,7 @@ pub fn handle_command_for_track_list_window(
mut ui: UIStateGuard,
state: &SharedState,
) -> Result<bool> {
tracing::info!("here 0");
let id = ui.current_page_mut().selected().unwrap_or_default();
if id >= tracks.len() {
return Ok(false);
Expand Down Expand Up @@ -390,90 +436,3 @@ pub fn handle_command_for_playlist_list_window(
}
Ok(true)
}

fn track_actions_for_playlist_owner(
tracks: &[Track],
track_index: usize,
user_id: &UserId<'_>,
playlist: &Playlist,
) -> Vec<TrackAction> {
if &playlist.owner.1 != user_id {
return vec![];
};

let mut actions = vec![];
if track_index > 0 {
actions.push(TrackAction::MoveUpInCurrentPlaylist);
}
if track_index + 1 < tracks.len() {
actions.push(TrackAction::MoveDownInCurrentPlaylist);
}

actions
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_track_actions_for_playlist_owner() {
fn create_dummy_track(track_id: &str) -> Track {
Track {
id: TrackId::from_id(track_id.to_owned()).unwrap(),
name: "".to_string(),
artists: vec![],
album: None,
duration: chrono::Duration::minutes(1),
added_at: 0,
}
}

let user_id = "jhgDSLJahsgd";
let tracks = vec![
create_dummy_track("37BTh5g05cxBIRYMbw8g2T"),
create_dummy_track("4cOdK2wGLETKBW3PvgPWqT"),
create_dummy_track("6tASfEUyB7lE2r6DLzURji"),
];

let playlist = Playlist {
id: PlaylistId::from_id(user_id.to_owned()).unwrap(),
collaborative: false,
name: "".to_string(),
owner: (
user_id.to_string(),
UserId::from_id(user_id.to_owned()).unwrap(),
),
};

// test when the track index is neither first or last
let actions = track_actions_for_playlist_owner(
&tracks,
1,
&UserId::from_id(user_id).unwrap(),
&playlist,
);
assert!(actions.contains(&TrackAction::MoveUpInCurrentPlaylist));
assert!(actions.contains(&TrackAction::MoveDownInCurrentPlaylist));

// test when the track index is first
let actions = track_actions_for_playlist_owner(
&tracks,
0,
&UserId::from_id(user_id).unwrap(),
&playlist,
);
assert!(!actions.contains(&TrackAction::MoveUpInCurrentPlaylist));
assert!(actions.contains(&TrackAction::MoveDownInCurrentPlaylist));

// test when the track index is last
let actions = track_actions_for_playlist_owner(
&tracks,
2,
&UserId::from_id(user_id).unwrap(),
&playlist,
);
assert!(actions.contains(&TrackAction::MoveUpInCurrentPlaylist));
assert!(!actions.contains(&TrackAction::MoveDownInCurrentPlaylist));
}
}

0 comments on commit ffa6ca9

Please sign in to comment.