Krolyxon
Commit aca208e

rmptuiaca208e

pl_append functionality for playlists

Author
krolxon
Date
2024-05-13 14:33 +0530
Parent
9bdc76b
Commit
aca208e0d843a43779cac565581bed620f2b08cd

+27 −8

4 files changed

  1. M src/event/pl_append_keys.rs +20 −6
  2. M src/event/pl_rename_keys.rs +1 −1
  3. M src/list.rs +5 −0
  4. M src/ui.rs +1 −1
M src/event/pl_append_keys.rs View
@@ -13,14 +13,12 @@ pub fn hande_pl_append_keys(key_event: KeyEvent, app: &mut App) -> AppResult<()>
1313 KeyCode::Char('k') | KeyCode::Up => app.append_list.prev(),
1414
1515 KeyCode::Enter => {
16− let pl_index = app.append_list.index;
17− let pl_name = &app.append_list.list.get(pl_index).unwrap();
16+ // name of highlighted playlist in append list
17+ let pl_name = &app.append_list.get_item_at_current_index();
1818
19− let s_index: usize;
2019 match app.selected_tab {
2120 SelectedTab::Queue => {
22− s_index = app.queue_list.index;
23− let short_path = &app.queue_list.list.get(s_index).unwrap().file;
21+ let short_path = &app.queue_list.get_item_at_current_index().file;
2422
2523 if let Some(full_path) = app.conn.get_full_path(short_path) {
2624 let song = app.conn.get_song_with_only_filename(&full_path);
@@ -68,7 +66,23 @@ pub fn hande_pl_append_keys(key_event: KeyEvent, app: &mut App) -> AppResult<()>
6866 }
6967 }
7068 }
71− _ => {}
69+
70+ SelectedTab::Playlists => {
71+ let playlist_name = app.pl_list.get_item_at_current_index();
72+ if *pl_name == "Current Playlist" {
73+ app.conn.load_playlist(playlist_name)?;
74+ app.update_queue();
75+ } else {
76+ let songs = app.conn.conn.playlist(playlist_name)?;
77+ for song in songs {
78+ // We ignore the Err() since there could be songs in playlists, which do not exist in the db anymore.
79+ // So instead of panicking, we just ignore if the song does not exists
80+ app.conn
81+ .add_to_playlist(*pl_name, &song)
82+ .unwrap_or_else(|_| {});
83+ }
84+ }
85+ }
7286 }
7387
7488 // hide the playlist popup
M src/event/pl_rename_keys.rs View
@@ -16,7 +16,7 @@ pub fn handle_pl_rename_keys(key_event: KeyEvent, app: &mut App) -> AppResult<()
1616 }
1717 KeyCode::Enter => {
1818 app.conn.conn.pl_rename(
19− app.pl_list.list.get(app.pl_list.index).unwrap(),
19+ app.pl_list.get_item_at_current_index(),
2020 &app.pl_newname_input,
2121 )?;
2222 app.pl_list.list = App::get_playlist(&mut app.conn.conn)?;
M src/list.rs View
@@ -30,6 +30,11 @@ impl<T> ContentList<T> {
3030 pub fn reset_index(&mut self) {
3131 self.index = 0;
3232 }
33+
34+ /// Returns the self.list[index] item
35+ pub fn get_item_at_current_index(&mut self) -> &T {
36+ self.list.get(self.index).unwrap()
37+ }
3338 }
3439
3540 impl<T> Default for ContentList<T> {
M src/ui.rs View
@@ -355,7 +355,7 @@ fn draw_playlist_viewer(frame: &mut Frame, app: &mut App, area: Rect) {
355355 return;
356356 }
357357
358− let pl_name = app.pl_list.list.get(app.pl_list.index).unwrap();
358+ let pl_name = app.pl_list.get_item_at_current_index();
359359 let songs = app.conn.conn.playlist(pl_name).unwrap();
360360 let rows = songs.iter().map(|song| {
361361 let title = song.clone().title.unwrap_or_default().cyan();