| 1 | use crate::app::{App, AppResult, SelectedTab}; |
| 2 | use crate::browser::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 | let pl_index = app.append_list.index; |
| 17 | let pl_name = &app.append_list.list.get(pl_index).unwrap(); |
| 18 | |
| 19 | let s_index: usize; |
| 20 | match app.selected_tab { |
| 21 | SelectedTab::Queue => { |
| 22 | s_index = app.queue_list.index; |
| 23 | let short_path = &app.queue_list.list.get(s_index).unwrap().file; |
| 24 | |
| 25 | if let Some(full_path) = app.conn.get_full_path(short_path) { |
| 26 | let song = app.conn.get_song_with_only_filename(&full_path); |
| 27 | |
| 28 | if *pl_name == "Current Playlist" { |
| 29 | app.conn.conn.push(&song)?; |
| 30 | app.update_queue(); |
| 31 | } else { |
| 32 | app.conn.add_to_playlist(pl_name, &song)?; |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | SelectedTab::DirectoryBrowser => { |
| 38 | let (t, f) = app.browser.filetree.get(app.browser.selected).unwrap(); |
| 39 | if t == "file" { |
| 40 | let short_path = f; |
| 41 | if let Some(full_path) = app.conn.get_full_path(short_path) { |
| 42 | let song = app.conn.get_song_with_only_filename(&full_path); |
| 43 | |
| 44 | if *pl_name == "Current Playlist" { |
| 45 | app.conn.conn.push(&song)?; |
| 46 | app.update_queue(); |
| 47 | } else { |
| 48 | app.conn.add_to_playlist(pl_name, &song)?; |
| 49 | } |
| 50 | } |
| 51 | } else if t == "directory" { |
| 52 | for (t, f) in app.conn.conn.listfiles(f)?.iter() { |
| 53 | // dir_vec.push((t, f)); |
| 54 | if t == "file" |
| 55 | && Path::new(&f).has_extension(&[ |
| 56 | "mp3", "ogg", "flac", "m4a", "wav", "aac", "opus", "ape", |
| 57 | "wma", "mpc", "aiff", "dff", "mp2", "mka", |
| 58 | ]) |
| 59 | { |
| 60 | let full_path = app.conn.get_full_path(f).unwrap_or_default(); |
| 61 | let song = app.conn.get_song_with_only_filename(&full_path); |
| 62 | if *pl_name == "Current Playlist" { |
| 63 | app.conn.conn.push(&song)?; |
| 64 | } else { |
| 65 | app.conn.add_to_playlist(pl_name, &song)?; |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | _ => {} |
| 72 | } |
| 73 | |
| 74 | // hide the playlist popup |
| 75 | app.playlist_popup = false; |
| 76 | app.append_list.index = 0; |
| 77 | } |
| 78 | _ => {} |
| 79 | } |
| 80 | |
| 81 | Ok(()) |
| 82 | } |