diff --git a/src/core/musichoard/database.rs b/src/core/musichoard/database.rs index 0d387aa..990871c 100644 --- a/src/core/musichoard/database.rs +++ b/src/core/musichoard/database.rs @@ -120,6 +120,8 @@ impl IMusicHoardDatabase for MusicHoard IMusicHoardDatabasePrivate for MusicHoard { fn commit(&mut self) -> Result<(), Error> { self.collection = self.pre_commit.clone(); + self.filtered = self.filter_collection(); Ok(()) } } @@ -376,6 +379,7 @@ impl IMusicHoardDatabasePrivate for MusicHoard { fn increment_selection(mut self, delta: Delta) -> Self::APP { self.inner .selection - .increment_selection(self.inner.music_hoard.get_collection(), delta); + .increment_selection(self.inner.music_hoard.get_filtered(), delta); self.into() } fn decrement_selection(mut self, delta: Delta) -> Self::APP { self.inner .selection - .decrement_selection(self.inner.music_hoard.get_collection(), delta); + .decrement_selection(self.inner.music_hoard.get_filtered(), delta); self.into() } @@ -68,7 +68,7 @@ impl IAppInteractBrowse for AppMachine { let orig = ListSelection::get(&self.inner.selection); self.inner .selection - .reset(self.inner.music_hoard.get_collection()); + .reset(self.inner.music_hoard.get_filtered()); AppMachine::search_state(self.inner, orig).into() } diff --git a/src/tui/app/machine/fetch_state.rs b/src/tui/app/machine/fetch_state.rs index 9ce871c..412c9f1 100644 --- a/src/tui/app/machine/fetch_state.rs +++ b/src/tui/app/machine/fetch_state.rs @@ -102,7 +102,7 @@ impl AppMachine { } fn fetch_job(inner: &AppInner, rx: MbApiReceiver) -> Result { - let coll = inner.music_hoard.get_collection(); + let coll = inner.music_hoard.get_filtered(); let artist = match inner.selection.state_artist(coll) { Some(artist_state) => &coll[artist_state.index], @@ -184,18 +184,22 @@ impl AppMachine { inner: &mut AppInner, fetch_albums: Vec, ) -> Result<(), musichoard::Error> { - let coll = inner.music_hoard.get_collection(); + let coll = inner.music_hoard.get_filtered(); let artist_state = inner.selection.state_artist(coll).unwrap(); let artist = &coll[artist_state.index]; let selection = KeySelection::get(coll, &inner.selection); - let artist_id = &artist.meta.id.clone(); + // Find the artist in the full collection to correctly identify already existing albums. + let artist_id = artist.meta.id.clone(); + let coll = inner.music_hoard.get_collection(); + let artist = coll.iter().find(|a| a.meta.id == artist_id).unwrap(); + for new in Self::new_albums(fetch_albums, &artist.albums).into_iter() { - inner.music_hoard.add_album(artist_id, new)?; + inner.music_hoard.add_album(&artist_id, new)?; } - let coll = inner.music_hoard.get_collection(); + let coll = inner.music_hoard.get_filtered(); inner.selection.select_by_id(coll, selection); Ok(()) diff --git a/src/tui/app/machine/mod.rs b/src/tui/app/machine/mod.rs index 2d0322f..6f630a5 100644 --- a/src/tui/app/machine/mod.rs +++ b/src/tui/app/machine/mod.rs @@ -173,7 +173,7 @@ impl AppInner { music_hoard: MH, musicbrainz: MB, ) -> Self { - let selection = Selection::new(music_hoard.get_collection()); + let selection = Selection::new(music_hoard.get_filtered()); AppInner { running: true, music_hoard: Box::new(music_hoard), @@ -186,7 +186,7 @@ impl AppInner { impl<'a> From<&'a mut AppInner> for AppPublicInner<'a> { fn from(inner: &'a mut AppInner) -> Self { AppPublicInner { - collection: inner.music_hoard.get_collection(), + collection: inner.music_hoard.get_filtered(), selection: &mut inner.selection, } } @@ -330,7 +330,7 @@ mod tests { pub fn music_hoard(collection: Collection) -> MockIMusicHoard { let mut music_hoard = MockIMusicHoard::new(); - music_hoard.expect_get_collection().return_const(collection); + music_hoard.expect_get_filtered().return_const(collection); music_hoard } @@ -594,7 +594,7 @@ mod tests { .expect_rescan_library() .times(1) .return_once(|| Err(musichoard::Error::LibraryError(String::from("get rekt")))); - music_hoard.expect_get_collection().return_const(vec![]); + music_hoard.expect_get_filtered().return_const(vec![]); let app = App::new(music_hoard, mb_job_sender()); assert!(app.is_running()); diff --git a/src/tui/app/machine/reload_state.rs b/src/tui/app/machine/reload_state.rs index 25def5b..deea728 100644 --- a/src/tui/app/machine/reload_state.rs +++ b/src/tui/app/machine/reload_state.rs @@ -29,7 +29,7 @@ impl IAppInteractReload for AppMachine { fn reload_library(mut self) -> Self::APP { let previous = KeySelection::get( - self.inner.music_hoard.get_collection(), + self.inner.music_hoard.get_filtered(), &self.inner.selection, ); let result = self.inner.music_hoard.rescan_library(); @@ -38,7 +38,7 @@ impl IAppInteractReload for AppMachine { fn reload_database(mut self) -> Self::APP { let previous = KeySelection::get( - self.inner.music_hoard.get_collection(), + self.inner.music_hoard.get_filtered(), &self.inner.selection, ); let result = self.inner.music_hoard.reload_database(); @@ -60,7 +60,7 @@ impl IAppInteractReloadPrivate for AppMachine { Ok(()) => { self.inner .selection - .select_by_id(self.inner.music_hoard.get_collection(), previous); + .select_by_id(self.inner.music_hoard.get_filtered(), previous); AppMachine::browse_state(self.inner).into() } Err(err) => AppMachine::error_state(self.inner, err.to_string()).into(), diff --git a/src/tui/app/machine/search_state.rs b/src/tui/app/machine/search_state.rs index 736bcec..1e4dc0d 100644 --- a/src/tui/app/machine/search_state.rs +++ b/src/tui/app/machine/search_state.rs @@ -66,7 +66,7 @@ impl IAppInteractSearch for AppMachine { } fn step_back(mut self) -> Self::APP { - let collection = self.inner.music_hoard.get_collection(); + let collection = self.inner.music_hoard.get_filtered(); if let Some(memo) = self.state.memo.pop() { if memo.char { self.state.string.pop(); @@ -104,7 +104,7 @@ trait IAppInteractSearchPrivate { impl IAppInteractSearchPrivate for AppMachine { fn incremental_search(&mut self, next: bool) { - let collection = self.inner.music_hoard.get_collection(); + let collection = self.inner.music_hoard.get_filtered(); let search = &self.state.string; let sel = &self.inner.selection; @@ -121,7 +121,7 @@ impl IAppInteractSearchPrivate for AppMachine { }; if result.is_some() { - let collection = self.inner.music_hoard.get_collection(); + let collection = self.inner.music_hoard.get_filtered(); self.inner.selection.select(collection, result); } } diff --git a/src/tui/lib/mod.rs b/src/tui/lib/mod.rs index c08eb3a..ef67eec 100644 --- a/src/tui/lib/mod.rs +++ b/src/tui/lib/mod.rs @@ -18,6 +18,8 @@ use mockall::automock; pub trait IMusicHoard { fn rescan_library(&mut self) -> Result<(), musichoard::Error>; fn reload_database(&mut self) -> Result<(), musichoard::Error>; + + fn get_filtered(&self) -> &Collection; fn get_collection(&self) -> &Collection; fn add_album( @@ -65,6 +67,10 @@ impl IMusicHoard for MusicHoard::reload_database(self) } + fn get_filtered(&self) -> &Collection { + ::get_filtered(self) + } + fn get_collection(&self) -> &Collection { ::get_collection(self) } diff --git a/src/tui/mod.rs b/src/tui/mod.rs index 2a2fd80..630fcb5 100644 --- a/src/tui/mod.rs +++ b/src/tui/mod.rs @@ -200,7 +200,7 @@ mod tests { music_hoard.expect_reload_database().returning(|| Ok(())); music_hoard.expect_rescan_library().returning(|| Ok(())); - music_hoard.expect_get_collection().return_const(collection); + music_hoard.expect_get_filtered().return_const(collection); music_hoard }