diff --git a/src/core/collection/musicbrainz.rs b/src/core/collection/musicbrainz.rs index 575778a..3b24af5 100644 --- a/src/core/collection/musicbrainz.rs +++ b/src/core/collection/musicbrainz.rs @@ -46,6 +46,14 @@ pub enum MbRefOption { } impl MbRefOption { + pub fn is_some(&self) -> bool { + matches!(self, MbRefOption::Some(_)) + } + + pub fn is_none(&self) -> bool { + matches!(self, MbRefOption::None) + } + pub fn or(self, optb: MbRefOption) -> MbRefOption { match (&self, &optb) { (MbRefOption::Some(_), _) | (MbRefOption::CannotHaveMbid, MbRefOption::None) => self, diff --git a/src/tui/app/machine/fetch_state.rs b/src/tui/app/machine/fetch_state.rs index 1015d44..f10e15a 100644 --- a/src/tui/app/machine/fetch_state.rs +++ b/src/tui/app/machine/fetch_state.rs @@ -5,7 +5,7 @@ use std::{ }; use musichoard::collection::{ - album::{Album, AlbumId}, + album::{Album, AlbumId, AlbumMeta}, artist::{Artist, ArtistId, ArtistMeta}, musicbrainz::{IMusicBrainzRef, MbArtistRef, MbRefOption, Mbid}, }; @@ -176,45 +176,44 @@ impl AppMachine { list: EntityList, ) -> Result<(), musichoard::Error> { match list { - EntityList::Album(new_albums) => { - let coll = inner.music_hoard.get_collection(); - let artist_state = inner.selection.state_artist(coll).unwrap(); + EntityList::Album(fetch_albums) => Self::apply_album_results(inner, fetch_albums), + } + } - let artist = &coll[artist_state.index]; - let artist_id = &coll[artist_state.index].meta.id.clone(); - let old_albums = &artist.albums; + fn apply_album_results( + inner: &mut AppInner, + fetch_albums: Vec, + ) -> Result<(), musichoard::Error> { + let coll = inner.music_hoard.get_collection(); - let mut to_be_added = vec![]; - for new in new_albums.into_iter() { - let mut exists = false; - for old in old_albums.iter() { - if matches!(old.meta.info.musicbrainz, MbRefOption::Some(_)) { - if new.info.musicbrainz == old.meta.info.musicbrainz { - exists = true; - break; - } - } - } + let artist_state = inner.selection.state_artist(coll).unwrap(); + let artist = &coll[artist_state.index]; + let selection = KeySelection::get(coll, &inner.selection); - if !exists { - to_be_added.push(new); - } - } + let artist_id = &artist.meta.id.clone(); + for new in Self::new_albums(fetch_albums, &artist.albums).into_iter() { + inner.music_hoard.add_album(artist_id, new)?; + } - let previous = - KeySelection::get(inner.music_hoard.get_collection(), &inner.selection); + let coll = inner.music_hoard.get_collection(); + inner.selection.select_by_id(coll, selection); - for new in to_be_added.into_iter() { - inner.music_hoard.add_album(&artist_id, new)?; - } + Ok(()) + } - inner - .selection - .select_by_id(inner.music_hoard.get_collection(), previous); - - Ok(()) + fn new_albums(fetch_albums: Vec, albums: &[Album]) -> Vec { + let mut new_albums = vec![]; + for alb in fetch_albums.into_iter() { + let existing = albums.iter().find(|old| Self::album_match(&old.meta, &alb)); + if existing.is_none() { + new_albums.push(alb); } } + new_albums + } + + fn album_match(old: &AlbumMeta, new: &AlbumMeta) -> bool { + old.info.musicbrainz.is_some() && (old.info.musicbrainz == new.info.musicbrainz) } pub fn app_lookup_artist( @@ -288,7 +287,7 @@ impl AppMachine { let arid = arid.mbid(); albums .iter() - .filter(|album| matches!(album.meta.info.musicbrainz, MbRefOption::None)) + .filter(|album| album.meta.info.musicbrainz.is_none()) .map(|album| { MbParams::search_release_group(artist.clone(), arid.clone(), album.meta.clone()) })