| 1 | use crate::app::{App, AppResult, SelectedTab}; |
| 2 | use crate::utils::FileExtension; |
| 3 | use crossterm::event::{KeyCode, KeyEvent}; |
| 4 | use std::path::Path; |
| 5 | |
| 6 | pub fn hande_pl_append_keys(key_event: KeyEvent, app: &mut App) -> AppResult<()> { |
| 7 | match key_event.code { |
| 8 | KeyCode::Char('q') | KeyCode::Esc => { |
| 9 | app.playlist_popup = false; |
| 10 | } |
| 11 | |
| 12 | KeyCode::Char('j') | KeyCode::Down => app.append_list.next(), |
| 13 | KeyCode::Char('k') | KeyCode::Up => app.append_list.prev(), |
| 14 | |
| 15 | KeyCode::Enter => { |
| 16 | // name of highlighted playlist in append list |
| 17 | let pl_name = &app.append_list.get_item_at_current_index(); |
| 18 | |
| 19 | match app.selected_tab { |
| 20 | SelectedTab::Queue => { |
| 21 | // Just exit out the menu if no item is selected in the Queue |
| 22 | if app.queue_list.list.is_empty() { |
| 23 | app.playlist_popup = false; |
| 24 | return Ok(()); |
| 25 | } |
| 26 | |
| 27 | if let Ok(songs) = app.conn.conn.songs(app.queue_list.index as u32) { |
| 28 | let option_song = songs.first(); |
| 29 | if let Some(song) = option_song { |
| 30 | if *pl_name == "Current Playlist" { |
| 31 | app.conn.conn.push(&song)?; |
| 32 | app.update_queue(); |
| 33 | } else { |
| 34 | app.conn.add_to_playlist(pl_name, &song)?; |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | SelectedTab::DirectoryBrowser => { |
| 41 | let (t, f) = app.browser.filetree.get(app.browser.selected).unwrap(); |
| 42 | if t == "file" { |
| 43 | let short_path = f; |
| 44 | if let Some(full_path) = app.conn.get_full_path(short_path) { |
| 45 | let song = app.conn.get_song_with_only_filename(&full_path); |
| 46 | |
| 47 | if *pl_name == "Current Playlist" { |
| 48 | app.conn.conn.push(&song)?; |
| 49 | app.update_queue(); |
| 50 | } else { |
| 51 | app.conn.add_to_playlist(pl_name, &song)?; |
| 52 | } |
| 53 | } |
| 54 | } else if t == "directory" { |
| 55 | for (t, f) in app.conn.conn.listfiles(f)?.iter() { |
| 56 | // dir_vec.push((t, f)); |
| 57 | if t == "file" |
| 58 | && Path::new(&f).has_extension(&[ |
| 59 | "mp3", "ogg", "flac", "m4a", "wav", "aac", "opus", "ape", |
| 60 | "wma", "mpc", "aiff", "dff", "mp2", "mka", |
| 61 | ]) |
| 62 | { |
| 63 | let full_path = app.conn.get_full_path(f).unwrap_or_default(); |
| 64 | let song = app.conn.get_song_with_only_filename(&full_path); |
| 65 | if *pl_name == "Current Playlist" { |
| 66 | app.conn.conn.push(&song)?; |
| 67 | } else { |
| 68 | app.conn.add_to_playlist(pl_name, &song)?; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | SelectedTab::Playlists => { |
| 76 | let playlist_name = app.pl_list.get_item_at_current_index(); |
| 77 | if *pl_name == "Current Playlist" { |
| 78 | app.conn.load_playlist(playlist_name)?; |
| 79 | app.update_queue(); |
| 80 | } else { |
| 81 | let songs = app.conn.conn.playlist(playlist_name)?; |
| 82 | for song in songs { |
| 83 | // We ignore the Err() since there could be songs in playlists, which do not exist in the db anymore. |
| 84 | // So instead of panicking, we just ignore if the song does not exists |
| 85 | app.conn |
| 86 | .add_to_playlist(*pl_name, &song) |
| 87 | .unwrap_or_else(|_| {}); |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // hide the playlist popup |
| 94 | app.playlist_popup = false; |
| 95 | app.append_list.index = 0; |
| 96 | } |
| 97 | _ => {} |
| 98 | } |
| 99 | |
| 100 | Ok(()) |
| 101 | } |