Krolyxon
Commit 971b336

rmptui971b336

fix #8

Author
krolxon
Date
2024-06-01 15:58 +0530
Parent
9922b1c
Commit
971b336feaf17305aca26bfeabc20ad8fd9ef20d

+32 −13

4 files changed

  1. M src/app.rs +25 −1
  2. M src/connection.rs +5 −0
  3. M src/event/handler.rs +1 −11
  4. M src/main.rs +1 −1
M src/app.rs View
@@ -30,6 +30,9 @@ pub struct App {
3030 // used to show playlist popup
3131 pub playlist_popup: bool,
3232 pub append_list: ContentList<String>,
33+
34+ // Determines if the database should be updated or not
35+ pub should_update_song_list: bool,
3336 }
3437
3538 #[derive(Debug, PartialEq, Clone)]
@@ -67,12 +70,33 @@ impl App {
6770 pl_cursor_pos: 0,
6871 playlist_popup: false,
6972 append_list,
73+ should_update_song_list: false,
7074 })
7175 }
7276
73− pub fn tick(&mut self) {
77+ pub fn tick(&mut self) -> AppResult<()> {
7478 self.conn.update_status();
7579 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(())
76100 }
77101
78102 pub fn quit(&mut self) {
M src/connection.rs View
@@ -18,6 +18,7 @@ pub struct Connection {
1818 pub random: bool,
1919 pub current_song: Song,
2020 pub stats: mpd::Stats,
21+ pub status: mpd::Status,
2122 }
2223
2324 impl Connection {
@@ -61,6 +62,7 @@ impl Connection {
6162 random,
6263 current_song,
6364 stats,
65+ status,
6466 })
6567 }
6668
@@ -90,6 +92,9 @@ impl Connection {
9092 .unwrap_or(empty_song);
9193 let stats = self.conn.stats().unwrap_or_default();
9294
95+ // Status
96+ self.status = status.clone();
97+
9398 // Playback State
9499 self.state = match status.state {
95100 State::Stop => "Stopped".to_string(),
M src/event/handler.rs View
@@ -141,17 +141,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
141141 // Update MPD database
142142 KeyCode::Char('U') => {
143143 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;
155145 }
156146
157147 // Search for songs
M src/main.rs View
@@ -33,7 +33,7 @@ fn main() -> AppResult<()> {
3333 while app.running {
3434 tui.draw(&mut app)?;
3535 match tui.events.next()? {
36− Event::Tick => app.tick(),
36+ Event::Tick => app.tick()?,
3737 Event::Key(key_event) => handler::handle_key_events(key_event, &mut app)?,
3838 Event::Mouse(_) => {}
3939 Event::Resize(_, _) => {}