rmptui971b336
fix #8
+32 −13
4 files changed
- M src/app.rs +25 −1
- M src/connection.rs +5 −0
- M src/event/handler.rs +1 −11
- M src/main.rs +1 −1
| @@ -30,6 +30,9 @@ pub struct App { | |||
| 30 | 30 | // used to show playlist popup | |
| 31 | 31 | pub playlist_popup: bool, | |
| 32 | 32 | pub append_list: ContentList<String>, | |
| 33 | + | ||
| 34 | + | // Determines if the database should be updated or not | |
| 35 | + | pub should_update_song_list: bool, | |
| 33 | 36 | } | |
| 34 | 37 | ||
| 35 | 38 | #[derive(Debug, PartialEq, Clone)] | |
| @@ -67,12 +70,33 @@ impl App { | |||
| 67 | 70 | pl_cursor_pos: 0, | |
| 68 | 71 | playlist_popup: false, | |
| 69 | 72 | append_list, | |
| 73 | + | should_update_song_list: false, | |
| 70 | 74 | }) | |
| 71 | 75 | } | |
| 72 | 76 | ||
| 73 | − | pub fn tick(&mut self) { | |
| 77 | + | pub fn tick(&mut self) -> AppResult<()> { | |
| 74 | 78 | self.conn.update_status(); | |
| 75 | 79 | self.update_queue(); | |
| 80 | + | ||
| 81 | + | // Deals with database update | |
| 82 | + | if self.should_update_song_list { | |
| 83 | + | if let None = self.conn.status.updating_db { | |
| 84 | + | // Update the songs list | |
| 85 | + | self.conn.songs_filenames = self | |
| 86 | + | .conn | |
| 87 | + | .conn | |
| 88 | + | .listall()? | |
| 89 | + | .into_iter() | |
| 90 | + | .map(|x| x.file) | |
| 91 | + | .collect(); | |
| 92 | + | ||
| 93 | + | self.browser.update_directory(&mut self.conn)?; | |
| 94 | + | ||
| 95 | + | self.should_update_song_list = false; | |
| 96 | + | } | |
| 97 | + | } | |
| 98 | + | ||
| 99 | + | Ok(()) | |
| 76 | 100 | } | |
| 77 | 101 | ||
| 78 | 102 | pub fn quit(&mut self) { | |
| @@ -18,6 +18,7 @@ pub struct Connection { | |||
| 18 | 18 | pub random: bool, | |
| 19 | 19 | pub current_song: Song, | |
| 20 | 20 | pub stats: mpd::Stats, | |
| 21 | + | pub status: mpd::Status, | |
| 21 | 22 | } | |
| 22 | 23 | ||
| 23 | 24 | impl Connection { | |
| @@ -61,6 +62,7 @@ impl Connection { | |||
| 61 | 62 | random, | |
| 62 | 63 | current_song, | |
| 63 | 64 | stats, | |
| 65 | + | status, | |
| 64 | 66 | }) | |
| 65 | 67 | } | |
| 66 | 68 | ||
| @@ -90,6 +92,9 @@ impl Connection { | |||
| 90 | 92 | .unwrap_or(empty_song); | |
| 91 | 93 | let stats = self.conn.stats().unwrap_or_default(); | |
| 92 | 94 | ||
| 95 | + | // Status | |
| 96 | + | self.status = status.clone(); | |
| 97 | + | ||
| 93 | 98 | // Playback State | |
| 94 | 99 | self.state = match status.state { | |
| 95 | 100 | State::Stop => "Stopped".to_string(), | |
| @@ -141,17 +141,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> { | |||
| 141 | 141 | // Update MPD database | |
| 142 | 142 | KeyCode::Char('U') => { | |
| 143 | 143 | app.conn.conn.rescan()?; | |
| 144 | − | ||
| 145 | − | // Update the songs list | |
| 146 | − | app.conn.songs_filenames = app | |
| 147 | − | .conn | |
| 148 | − | .conn | |
| 149 | − | .listall()? | |
| 150 | − | .into_iter() | |
| 151 | − | .map(|x| x.file) | |
| 152 | − | .collect(); | |
| 153 | − | ||
| 154 | − | app.browser.update_directory(&mut app.conn)?; | |
| 144 | + | app.should_update_song_list = true; | |
| 155 | 145 | } | |
| 156 | 146 | ||
| 157 | 147 | // Search for songs | |
| @@ -33,7 +33,7 @@ fn main() -> AppResult<()> { | |||
| 33 | 33 | while app.running { | |
| 34 | 34 | tui.draw(&mut app)?; | |
| 35 | 35 | match tui.events.next()? { | |
| 36 | − | Event::Tick => app.tick(), | |
| 36 | + | Event::Tick => app.tick()?, | |
| 37 | 37 | Event::Key(key_event) => handler::handle_key_events(key_event, &mut app)?, | |
| 38 | 38 | Event::Mouse(_) => {} | |
| 39 | 39 | Event::Resize(_, _) => {} | |