| 1 | use crate::browser::FileBrowser; |
| 2 | use crate::connection::Connection; |
| 3 | use crate::list::ContentList; |
| 4 | use crate::ui::InputMode; |
| 5 | use mpd::Client; |
| 6 | |
| 7 | // Application result type |
| 8 | pub type AppResult<T> = std::result::Result<T, Box<dyn std::error::Error>>; |
| 9 | |
| 10 | /// Application |
| 11 | #[derive(Debug)] |
| 12 | pub struct App { |
| 13 | /// check if app is running |
| 14 | pub running: bool, |
| 15 | pub conn: Connection, |
| 16 | pub queue_list: ContentList<String>, |
| 17 | pub pl_list: ContentList<String>, |
| 18 | pub selected_tab: SelectedTab, |
| 19 | pub browser: FileBrowser, |
| 20 | |
| 21 | // Search |
| 22 | pub inputmode: InputMode, |
| 23 | pub search_input: String, |
| 24 | pub cursor_position: usize, |
| 25 | |
| 26 | // add to playlists |
| 27 | pub playlist_popup: bool, |
| 28 | pub append_list: ContentList<String>, |
| 29 | } |
| 30 | |
| 31 | #[derive(Debug, PartialEq, Clone)] |
| 32 | pub enum SelectedTab { |
| 33 | DirectoryBrowser, |
| 34 | Queue, |
| 35 | Playlists, |
| 36 | } |
| 37 | |
| 38 | impl App { |
| 39 | pub fn builder(addrs: &str) -> AppResult<Self> { |
| 40 | let mut conn = Connection::new(addrs).unwrap(); |
| 41 | let mut queue_list = ContentList::new(); |
| 42 | let mut pl_list = ContentList::new(); |
| 43 | pl_list.list = Self::get_playlist(&mut conn.conn)?; |
| 44 | |
| 45 | let append_list = Self::get_append_list(&mut conn.conn)?; |
| 46 | Self::get_queue(&mut conn, &mut queue_list.list); |
| 47 | |
| 48 | let browser = FileBrowser::new(); |
| 49 | Ok(Self { |
| 50 | running: true, |
| 51 | conn, |
| 52 | queue_list, |
| 53 | pl_list, |
| 54 | selected_tab: SelectedTab::DirectoryBrowser, |
| 55 | browser, |
| 56 | inputmode: InputMode::Normal, |
| 57 | search_input: String::new(), |
| 58 | cursor_position: 0, |
| 59 | playlist_popup: false, |
| 60 | append_list, |
| 61 | }) |
| 62 | } |
| 63 | |
| 64 | pub fn tick(&mut self) { |
| 65 | self.conn.update_status(); |
| 66 | self.update_queue(); |
| 67 | // self.browser.update_directory(&mut self.conn).unwrap(); |
| 68 | } |
| 69 | |
| 70 | pub fn quit(&mut self) { |
| 71 | self.running = false; |
| 72 | } |
| 73 | |
| 74 | pub fn get_queue(conn: &mut Connection, vec: &mut Vec<String>) { |
| 75 | // conn.conn.queue().unwrap().into_iter().for_each(|x| { |
| 76 | // if let Some(title) = x.title { |
| 77 | // if let Some(artist) = x.artist { |
| 78 | // vec.push(format!("{} - {}", artist, title)); |
| 79 | // } else { |
| 80 | // vec.push(title) |
| 81 | // } |
| 82 | // } else { |
| 83 | // vec.push(x.file) |
| 84 | // } |
| 85 | // }); |
| 86 | conn.conn.queue().unwrap().into_iter().for_each(|x| { |
| 87 | vec.push(x.file); |
| 88 | }); |
| 89 | } |
| 90 | |
| 91 | pub fn update_queue(&mut self) { |
| 92 | self.queue_list.list.clear(); |
| 93 | Self::get_queue(&mut self.conn, &mut self.queue_list.list); |
| 94 | } |
| 95 | |
| 96 | pub fn get_playlist(conn: &mut Client) -> AppResult<Vec<String>> { |
| 97 | let list: Vec<String> = conn.playlists()?.iter().map(|p| p.clone().name).collect(); |
| 98 | Ok(list) |
| 99 | } |
| 100 | |
| 101 | pub fn get_append_list(conn: &mut Client) -> AppResult<ContentList<String>> { |
| 102 | let mut list = ContentList::new(); |
| 103 | list.list.push("Current Playlist".to_string()); |
| 104 | for item in Self::get_playlist(conn)? { |
| 105 | list.list.push(item.to_string()); |
| 106 | } |
| 107 | |
| 108 | Ok(list) |
| 109 | } |
| 110 | |
| 111 | pub fn update_playlist(&mut self) -> AppResult<()> { |
| 112 | Self::get_playlist(&mut self.conn.conn)?; |
| 113 | Ok(()) |
| 114 | } |
| 115 | |
| 116 | pub fn remove_from_current_playlist(&mut self) { |
| 117 | let mut file = String::new(); |
| 118 | match self.selected_tab { |
| 119 | SelectedTab::DirectoryBrowser => { |
| 120 | let (_, f) = self.browser.filetree.get(self.browser.selected).unwrap(); |
| 121 | file.push_str(f); |
| 122 | } |
| 123 | |
| 124 | SelectedTab::Queue => { |
| 125 | file = self.queue_list.list.get(self.queue_list.index).unwrap().to_string(); |
| 126 | } |
| 127 | |
| 128 | _ => {} |
| 129 | } |
| 130 | |
| 131 | for (i, song) in self.queue_list.list.clone().iter().enumerate() { |
| 132 | if song.contains(&file) { |
| 133 | self.conn.conn.delete(i as u32).unwrap(); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | self.update_queue(); |
| 138 | } |
| 139 | |
| 140 | pub fn cycle_tabls(&mut self) { |
| 141 | self.selected_tab = match self.selected_tab { |
| 142 | SelectedTab::DirectoryBrowser => SelectedTab::Queue, |
| 143 | SelectedTab::Queue => SelectedTab::Playlists, |
| 144 | SelectedTab::Playlists => SelectedTab::DirectoryBrowser, |
| 145 | }; |
| 146 | } |
| 147 | |
| 148 | pub fn search_song(&mut self) -> AppResult<()> { |
| 149 | let filename = self.conn.get_full_path(&self.search_input)?; |
| 150 | let song = self.conn.get_song_with_only_filename(&filename); |
| 151 | self.conn.push(&song)?; |
| 152 | Ok(()) |
| 153 | } |
| 154 | |
| 155 | // Cursor movements |
| 156 | pub fn move_cursor_left(&mut self) { |
| 157 | let cursor_moved_left = self.cursor_position.saturating_sub(1); |
| 158 | self.cursor_position = self.clamp_cursor(cursor_moved_left); |
| 159 | } |
| 160 | |
| 161 | pub fn move_cursor_right(&mut self) { |
| 162 | let cursor_moved_right = self.cursor_position.saturating_add(1); |
| 163 | self.cursor_position = self.clamp_cursor(cursor_moved_right); |
| 164 | } |
| 165 | |
| 166 | pub fn enter_char(&mut self, new_char: char) { |
| 167 | self.search_input.insert(self.cursor_position, new_char); |
| 168 | |
| 169 | self.move_cursor_right(); |
| 170 | } |
| 171 | |
| 172 | pub fn delete_char(&mut self) { |
| 173 | let is_not_cursor_leftmost = self.cursor_position != 0; |
| 174 | if is_not_cursor_leftmost { |
| 175 | // Method "remove" is not used on the saved text for deleting the selected char. |
| 176 | // Reason: Using remove on String works on bytes instead of the chars. |
| 177 | // Using remove would require special care because of char boundaries. |
| 178 | |
| 179 | let current_index = self.cursor_position; |
| 180 | let from_left_to_current_index = current_index - 1; |
| 181 | |
| 182 | // Getting all characters before the selected character. |
| 183 | let before_char_to_delete = self.search_input.chars().take(from_left_to_current_index); |
| 184 | // Getting all characters after selected character. |
| 185 | let after_char_to_delete = self.search_input.chars().skip(current_index); |
| 186 | |
| 187 | // Put all characters together except the selected one. |
| 188 | // By leaving the selected one out, it is forgotten and therefore deleted. |
| 189 | self.search_input = before_char_to_delete.chain(after_char_to_delete).collect(); |
| 190 | self.move_cursor_left(); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | pub fn clamp_cursor(&self, new_cursor_pos: usize) -> usize { |
| 195 | new_cursor_pos.clamp(0, self.search_input.len()) |
| 196 | } |
| 197 | |
| 198 | pub fn reset_cursor(&mut self) { |
| 199 | self.cursor_position = 0; |
| 200 | } |
| 201 | } |