Broad expectation updates

This commit is contained in:
Wojciech Kozlowski 2025-01-04 20:36:19 +01:00
parent f5644690a4
commit 925063d0e7
8 changed files with 33 additions and 19 deletions

View File

@ -120,6 +120,8 @@ impl<Database: IDatabase, Library> IMusicHoardDatabase for MusicHoard<Database,
Self::sort_albums_and_tracks(self.database_cache.iter_mut()); Self::sort_albums_and_tracks(self.database_cache.iter_mut());
self.collection = self.merge_collections(); self.collection = self.merge_collections();
self.filtered = self.filter_collection();
self.pre_commit = self.collection.clone(); self.pre_commit = self.collection.clone();
Ok(()) Ok(())
@ -364,6 +366,7 @@ pub trait IMusicHoardDatabasePrivate {
impl<Library> IMusicHoardDatabasePrivate for MusicHoard<NoDatabase, Library> { impl<Library> IMusicHoardDatabasePrivate for MusicHoard<NoDatabase, Library> {
fn commit(&mut self) -> Result<(), Error> { fn commit(&mut self) -> Result<(), Error> {
self.collection = self.pre_commit.clone(); self.collection = self.pre_commit.clone();
self.filtered = self.filter_collection();
Ok(()) Ok(())
} }
} }
@ -376,6 +379,7 @@ impl<Database: IDatabase, Library> IMusicHoardDatabasePrivate for MusicHoard<Dat
return Err(err.into()); return Err(err.into());
} }
self.collection = self.pre_commit.clone(); self.collection = self.pre_commit.clone();
self.filtered = self.filter_collection();
} }
Ok(()) Ok(())
} }

View File

@ -45,14 +45,14 @@ impl IAppInteractBrowse for AppMachine<BrowseState> {
fn increment_selection(mut self, delta: Delta) -> Self::APP { fn increment_selection(mut self, delta: Delta) -> Self::APP {
self.inner self.inner
.selection .selection
.increment_selection(self.inner.music_hoard.get_collection(), delta); .increment_selection(self.inner.music_hoard.get_filtered(), delta);
self.into() self.into()
} }
fn decrement_selection(mut self, delta: Delta) -> Self::APP { fn decrement_selection(mut self, delta: Delta) -> Self::APP {
self.inner self.inner
.selection .selection
.decrement_selection(self.inner.music_hoard.get_collection(), delta); .decrement_selection(self.inner.music_hoard.get_filtered(), delta);
self.into() self.into()
} }
@ -68,7 +68,7 @@ impl IAppInteractBrowse for AppMachine<BrowseState> {
let orig = ListSelection::get(&self.inner.selection); let orig = ListSelection::get(&self.inner.selection);
self.inner self.inner
.selection .selection
.reset(self.inner.music_hoard.get_collection()); .reset(self.inner.music_hoard.get_filtered());
AppMachine::search_state(self.inner, orig).into() AppMachine::search_state(self.inner, orig).into()
} }

View File

@ -102,7 +102,7 @@ impl AppMachine<FetchState> {
} }
fn fetch_job(inner: &AppInner, rx: MbApiReceiver) -> Result<SubmitJob, &'static str> { fn fetch_job(inner: &AppInner, rx: MbApiReceiver) -> Result<SubmitJob, &'static str> {
let coll = inner.music_hoard.get_collection(); let coll = inner.music_hoard.get_filtered();
let artist = match inner.selection.state_artist(coll) { let artist = match inner.selection.state_artist(coll) {
Some(artist_state) => &coll[artist_state.index], Some(artist_state) => &coll[artist_state.index],
@ -184,18 +184,22 @@ impl AppMachine<FetchState> {
inner: &mut AppInner, inner: &mut AppInner,
fetch_albums: Vec<AlbumMeta>, fetch_albums: Vec<AlbumMeta>,
) -> Result<(), musichoard::Error> { ) -> 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_state = inner.selection.state_artist(coll).unwrap();
let artist = &coll[artist_state.index]; let artist = &coll[artist_state.index];
let selection = KeySelection::get(coll, &inner.selection); 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() { 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); inner.selection.select_by_id(coll, selection);
Ok(()) Ok(())

View File

@ -173,7 +173,7 @@ impl AppInner {
music_hoard: MH, music_hoard: MH,
musicbrainz: MB, musicbrainz: MB,
) -> Self { ) -> Self {
let selection = Selection::new(music_hoard.get_collection()); let selection = Selection::new(music_hoard.get_filtered());
AppInner { AppInner {
running: true, running: true,
music_hoard: Box::new(music_hoard), music_hoard: Box::new(music_hoard),
@ -186,7 +186,7 @@ impl AppInner {
impl<'a> From<&'a mut AppInner> for AppPublicInner<'a> { impl<'a> From<&'a mut AppInner> for AppPublicInner<'a> {
fn from(inner: &'a mut AppInner) -> Self { fn from(inner: &'a mut AppInner) -> Self {
AppPublicInner { AppPublicInner {
collection: inner.music_hoard.get_collection(), collection: inner.music_hoard.get_filtered(),
selection: &mut inner.selection, selection: &mut inner.selection,
} }
} }
@ -330,7 +330,7 @@ mod tests {
pub fn music_hoard(collection: Collection) -> MockIMusicHoard { pub fn music_hoard(collection: Collection) -> MockIMusicHoard {
let mut music_hoard = MockIMusicHoard::new(); let mut music_hoard = MockIMusicHoard::new();
music_hoard.expect_get_collection().return_const(collection); music_hoard.expect_get_filtered().return_const(collection);
music_hoard music_hoard
} }
@ -594,7 +594,7 @@ mod tests {
.expect_rescan_library() .expect_rescan_library()
.times(1) .times(1)
.return_once(|| Err(musichoard::Error::LibraryError(String::from("get rekt")))); .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()); let app = App::new(music_hoard, mb_job_sender());
assert!(app.is_running()); assert!(app.is_running());

View File

@ -29,7 +29,7 @@ impl IAppInteractReload for AppMachine<ReloadState> {
fn reload_library(mut self) -> Self::APP { fn reload_library(mut self) -> Self::APP {
let previous = KeySelection::get( let previous = KeySelection::get(
self.inner.music_hoard.get_collection(), self.inner.music_hoard.get_filtered(),
&self.inner.selection, &self.inner.selection,
); );
let result = self.inner.music_hoard.rescan_library(); let result = self.inner.music_hoard.rescan_library();
@ -38,7 +38,7 @@ impl IAppInteractReload for AppMachine<ReloadState> {
fn reload_database(mut self) -> Self::APP { fn reload_database(mut self) -> Self::APP {
let previous = KeySelection::get( let previous = KeySelection::get(
self.inner.music_hoard.get_collection(), self.inner.music_hoard.get_filtered(),
&self.inner.selection, &self.inner.selection,
); );
let result = self.inner.music_hoard.reload_database(); let result = self.inner.music_hoard.reload_database();
@ -60,7 +60,7 @@ impl IAppInteractReloadPrivate for AppMachine<ReloadState> {
Ok(()) => { Ok(()) => {
self.inner self.inner
.selection .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() AppMachine::browse_state(self.inner).into()
} }
Err(err) => AppMachine::error_state(self.inner, err.to_string()).into(), Err(err) => AppMachine::error_state(self.inner, err.to_string()).into(),

View File

@ -66,7 +66,7 @@ impl IAppInteractSearch for AppMachine<SearchState> {
} }
fn step_back(mut self) -> Self::APP { 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 let Some(memo) = self.state.memo.pop() {
if memo.char { if memo.char {
self.state.string.pop(); self.state.string.pop();
@ -104,7 +104,7 @@ trait IAppInteractSearchPrivate {
impl IAppInteractSearchPrivate for AppMachine<SearchState> { impl IAppInteractSearchPrivate for AppMachine<SearchState> {
fn incremental_search(&mut self, next: bool) { 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 search = &self.state.string;
let sel = &self.inner.selection; let sel = &self.inner.selection;
@ -121,7 +121,7 @@ impl IAppInteractSearchPrivate for AppMachine<SearchState> {
}; };
if result.is_some() { 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); self.inner.selection.select(collection, result);
} }
} }

View File

@ -18,6 +18,8 @@ use mockall::automock;
pub trait IMusicHoard { pub trait IMusicHoard {
fn rescan_library(&mut self) -> Result<(), musichoard::Error>; fn rescan_library(&mut self) -> Result<(), musichoard::Error>;
fn reload_database(&mut self) -> Result<(), musichoard::Error>; fn reload_database(&mut self) -> Result<(), musichoard::Error>;
fn get_filtered(&self) -> &Collection;
fn get_collection(&self) -> &Collection; fn get_collection(&self) -> &Collection;
fn add_album( fn add_album(
@ -65,6 +67,10 @@ impl<Database: IDatabase, Library: ILibrary> IMusicHoard for MusicHoard<Database
<Self as IMusicHoardDatabase>::reload_database(self) <Self as IMusicHoardDatabase>::reload_database(self)
} }
fn get_filtered(&self) -> &Collection {
<Self as IMusicHoardBase>::get_filtered(self)
}
fn get_collection(&self) -> &Collection { fn get_collection(&self) -> &Collection {
<Self as IMusicHoardBase>::get_collection(self) <Self as IMusicHoardBase>::get_collection(self)
} }

View File

@ -200,7 +200,7 @@ mod tests {
music_hoard.expect_reload_database().returning(|| Ok(())); music_hoard.expect_reload_database().returning(|| Ok(()));
music_hoard.expect_rescan_library().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 music_hoard
} }