1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use crossterm::event::{self, KeyCode};
use crate::error::Error;
use super::*;
impl App {
/// Handle playlists page keys
pub(super) async fn handle_playlists_key(&mut self, key: event::KeyEvent) -> Result<(), Error> {
let mut state = self.state.write().await;
match key.code {
KeyCode::Tab => {
state.playlists.focus = (state.playlists.focus + 1) % 2;
}
KeyCode::Left => {
state.playlists.focus = 0;
}
KeyCode::Right => {
if !state.playlists.songs.is_empty() {
state.playlists.focus = 1;
if state.playlists.selected_song.is_none() {
state.playlists.selected_song = Some(0);
}
}
}
KeyCode::Up | KeyCode::Char('k') => {
if state.playlists.focus == 0 {
// Playlist list
if let Some(sel) = state.playlists.selected_playlist {
if sel > 0 {
state.playlists.selected_playlist = Some(sel - 1);
}
} else if !state.playlists.playlists.is_empty() {
state.playlists.selected_playlist = Some(0);
}
} else {
// Song list
if let Some(sel) = state.playlists.selected_song {
if sel > 0 {
state.playlists.selected_song = Some(sel - 1);
}
} else if !state.playlists.songs.is_empty() {
state.playlists.selected_song = Some(0);
}
}
}
KeyCode::Down | KeyCode::Char('j') => {
if state.playlists.focus == 0 {
let max = state.playlists.playlists.len().saturating_sub(1);
if let Some(sel) = state.playlists.selected_playlist {
if sel < max {
state.playlists.selected_playlist = Some(sel + 1);
}
} else if !state.playlists.playlists.is_empty() {
state.playlists.selected_playlist = Some(0);
}
} else {
let max = state.playlists.songs.len().saturating_sub(1);
if let Some(sel) = state.playlists.selected_song {
if sel < max {
state.playlists.selected_song = Some(sel + 1);
}
} else if !state.playlists.songs.is_empty() {
state.playlists.selected_song = Some(0);
}
}
}
KeyCode::Enter => {
if state.playlists.focus == 0 {
// Load playlist songs
if let Some(idx) = state.playlists.selected_playlist {
if let Some(playlist) = state.playlists.playlists.get(idx) {
let playlist_id = playlist.id.clone();
let playlist_name = playlist.name.clone();
drop(state);
if let Some(ref client) = self.subsonic {
match client.get_playlist(&playlist_id).await {
Ok((_playlist, songs)) => {
let mut state = self.state.write().await;
let count = songs.len();
state.playlists.songs = songs;
state.playlists.selected_song =
if count > 0 { Some(0) } else { None };
state.playlists.focus = 1;
state.notify(format!(
"Loaded playlist: {} ({} songs)",
playlist_name, count
));
}
Err(e) => {
let mut state = self.state.write().await;
state.notify_error(format!(
"Failed to load playlist: {}",
e
));
}
}
}
return Ok(());
}
}
} else {
// Play selected song from playlist
if let Some(idx) = state.playlists.selected_song {
if idx < state.playlists.songs.len() {
let songs = state.playlists.songs.clone();
state.queue.clear();
state.queue.extend(songs);
drop(state);
self.save_queue_sync();
return self.play_queue_position(idx).await;
}
}
}
}
KeyCode::Char('e') => {
// Add to queue
if state.playlists.focus == 1 {
if let Some(idx) = state.playlists.selected_song {
if let Some(song) = state.playlists.songs.get(idx).cloned() {
let title = song.title.clone();
state.queue.push(song);
state.notify(format!("Added to queue: {}", title));
drop(state);
self.save_queue_sync();
}
}
} else {
// Add whole playlist
if !state.playlists.songs.is_empty() {
let count = state.playlists.songs.len();
let songs = state.playlists.songs.clone();
state.queue.extend(songs);
state.notify(format!("Added {} songs to queue", count));
drop(state);
self.save_queue_sync();
}
}
}
KeyCode::Char('n') => {
// Add next
let insert_pos = state.queue_position.map(|p| p + 1).unwrap_or(0);
if state.playlists.focus == 1 {
if let Some(idx) = state.playlists.selected_song {
if let Some(song) = state.playlists.songs.get(idx).cloned() {
let title = song.title.clone();
state.queue.insert(insert_pos, song);
state.notify(format!("Playing next: {}", title));
let queue_position = state.queue_position;
drop(state);
self.save_queue_sync();
if let Some(pos) = queue_position {
let _ = self.mpv.playlist_remove(1); // remove stale preloaded track
self.preload_next_track(pos).await;
}
}
}
}
}
KeyCode::Char('s') => {
// Shuffle play playlist
use rand::seq::SliceRandom;
if !state.playlists.songs.is_empty() {
let mut songs = state.playlists.songs.clone();
songs.shuffle(&mut rand::thread_rng());
state.queue.clear();
state.queue.extend(songs);
drop(state);
self.save_queue_sync();
return self.play_queue_position(0).await;
}
}
_ => {}
}
Ok(())
}
}