| 1 | use std::time::Duration; |
| 2 | |
| 3 | use crate::connection::Connection; |
| 4 | use crate::list::ContentList; |
| 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 song_list: ContentList<String>, |
| 17 | pub queue_list: ContentList<String>, |
| 18 | pub pl_list: ContentList<String>, |
| 19 | pub selected_tab: SelectedTab, |
| 20 | } |
| 21 | |
| 22 | #[derive(Debug, PartialEq, Clone)] |
| 23 | pub enum SelectedTab { |
| 24 | SongList, |
| 25 | Queue, |
| 26 | Playlists, |
| 27 | } |
| 28 | |
| 29 | impl SelectedTab { |
| 30 | fn as_usize(&self) { |
| 31 | match self { |
| 32 | SelectedTab::SongList => 0, |
| 33 | SelectedTab::Queue => 1, |
| 34 | SelectedTab::Playlists => 2, |
| 35 | }; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl App { |
| 40 | pub fn builder(addrs: &str) -> AppResult<Self> { |
| 41 | let mut conn = Connection::new(addrs).unwrap(); |
| 42 | let mut queue_list = ContentList::new(); |
| 43 | let mut pl_list = ContentList::new(); |
| 44 | pl_list.list = Self::get_playlist(&mut conn.conn)?; |
| 45 | |
| 46 | Self::get_queue(&mut conn, &mut queue_list.list); |
| 47 | |
| 48 | let mut song_list = ContentList::new(); |
| 49 | song_list.list = conn.songs_filenames.clone(); |
| 50 | |
| 51 | Ok(Self { |
| 52 | running: true, |
| 53 | conn, |
| 54 | song_list, |
| 55 | queue_list, |
| 56 | pl_list, |
| 57 | selected_tab: SelectedTab::SongList, |
| 58 | }) |
| 59 | } |
| 60 | |
| 61 | pub fn tick(&mut self) { |
| 62 | self.conn.update_state(); |
| 63 | self.conn.update_progress(); |
| 64 | self.update_queue(); |
| 65 | } |
| 66 | |
| 67 | pub fn quit(&mut self) { |
| 68 | self.running = false; |
| 69 | } |
| 70 | |
| 71 | pub fn get_queue(conn: &mut Connection, vec: &mut Vec<String>) { |
| 72 | // conn.conn.queue().unwrap().into_iter().for_each(|x| { |
| 73 | // if let Some(title) = x.title { |
| 74 | // if let Some(artist) = x.artist { |
| 75 | // vec.push(format!("{} - {}", artist, title)); |
| 76 | // } else { |
| 77 | // vec.push(title) |
| 78 | // } |
| 79 | // } else { |
| 80 | // vec.push(x.file) |
| 81 | // } |
| 82 | // }); |
| 83 | conn.conn.queue().unwrap().into_iter().for_each(|x| { |
| 84 | vec.push(x.file); |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | pub fn update_queue(&mut self) { |
| 89 | self.queue_list.list.clear(); |
| 90 | Self::get_queue(&mut self.conn, &mut self.queue_list.list); |
| 91 | } |
| 92 | |
| 93 | pub fn get_playlist(conn: &mut Client) -> AppResult<Vec<String>> { |
| 94 | let list: Vec<String> = conn.playlists()?.iter().map(|p| p.clone().name).collect(); |
| 95 | |
| 96 | Ok(list) |
| 97 | } |
| 98 | |
| 99 | pub fn update_playlist(&mut self) -> AppResult<()> { |
| 100 | Self::get_playlist(&mut self.conn.conn)?; |
| 101 | Ok(()) |
| 102 | } |
| 103 | |
| 104 | pub fn cycle_tabls(&mut self) { |
| 105 | self.selected_tab = match self.selected_tab { |
| 106 | SelectedTab::SongList => SelectedTab::Queue, |
| 107 | SelectedTab::Queue => SelectedTab::Playlists, |
| 108 | SelectedTab::Playlists => SelectedTab::SongList, |
| 109 | }; |
| 110 | } |
| 111 | } |