Clean up a bit
This commit is contained in:
parent
73aab07d9b
commit
799de9c003
@ -46,6 +46,14 @@ pub enum MbRefOption<T> {
|
||||
}
|
||||
|
||||
impl<T> MbRefOption<T> {
|
||||
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<T>) -> MbRefOption<T> {
|
||||
match (&self, &optb) {
|
||||
(MbRefOption::Some(_), _) | (MbRefOption::CannotHaveMbid, MbRefOption::None) => self,
|
||||
|
@ -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,46 +176,45 @@ impl AppMachine<FetchState> {
|
||||
list: EntityList,
|
||||
) -> Result<(), musichoard::Error> {
|
||||
match list {
|
||||
EntityList::Album(new_albums) => {
|
||||
EntityList::Album(fetch_albums) => Self::apply_album_results(inner, fetch_albums),
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_album_results(
|
||||
inner: &mut AppInner,
|
||||
fetch_albums: Vec<AlbumMeta>,
|
||||
) -> Result<(), musichoard::Error> {
|
||||
let coll = inner.music_hoard.get_collection();
|
||||
|
||||
let artist_state = inner.selection.state_artist(coll).unwrap();
|
||||
|
||||
let artist = &coll[artist_state.index];
|
||||
let artist_id = &coll[artist_state.index].meta.id.clone();
|
||||
let old_albums = &artist.albums;
|
||||
let selection = KeySelection::get(coll, &inner.selection);
|
||||
|
||||
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_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)?;
|
||||
}
|
||||
|
||||
if !exists {
|
||||
to_be_added.push(new);
|
||||
}
|
||||
}
|
||||
|
||||
let previous =
|
||||
KeySelection::get(inner.music_hoard.get_collection(), &inner.selection);
|
||||
|
||||
for new in to_be_added.into_iter() {
|
||||
inner.music_hoard.add_album(&artist_id, new)?;
|
||||
}
|
||||
|
||||
inner
|
||||
.selection
|
||||
.select_by_id(inner.music_hoard.get_collection(), previous);
|
||||
let coll = inner.music_hoard.get_collection();
|
||||
inner.selection.select_by_id(coll, selection);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn new_albums(fetch_albums: Vec<AlbumMeta>, albums: &[Album]) -> Vec<AlbumMeta> {
|
||||
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(
|
||||
inner: AppInner,
|
||||
@ -288,7 +287,7 @@ impl AppMachine<FetchState> {
|
||||
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())
|
||||
})
|
||||
|
Loading…
x
Reference in New Issue
Block a user