Feature implemented
This commit is contained in:
parent
e22068e461
commit
011a999f28
@ -1,5 +1,6 @@
|
|||||||
use std::{
|
use std::{
|
||||||
collections::VecDeque,
|
collections::VecDeque,
|
||||||
|
slice,
|
||||||
sync::mpsc::{self, TryRecvError},
|
sync::mpsc::{self, TryRecvError},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -12,7 +13,7 @@ use musichoard::collection::{
|
|||||||
use crate::tui::{
|
use crate::tui::{
|
||||||
app::{
|
app::{
|
||||||
machine::{match_state::MatchState, App, AppInner, AppMachine},
|
machine::{match_state::MatchState, App, AppInner, AppMachine},
|
||||||
AppPublicState, AppState, IAppEventFetch, IAppInteractFetch,
|
AppPublicState, AppState, Category, IAppEventFetch, IAppInteractFetch,
|
||||||
},
|
},
|
||||||
lib::interface::musicbrainz::daemon::{
|
lib::interface::musicbrainz::daemon::{
|
||||||
Error as DaemonError, IMbJobSender, MbApiResult, MbParams, ResultSender,
|
Error as DaemonError, IMbJobSender, MbApiResult, MbParams, ResultSender,
|
||||||
@ -68,16 +69,42 @@ impl AppMachine<FetchState> {
|
|||||||
|
|
||||||
fn app_fetch_new(inner: AppInner) -> App {
|
fn app_fetch_new(inner: AppInner) -> App {
|
||||||
let coll = inner.music_hoard.get_collection();
|
let coll = inner.music_hoard.get_collection();
|
||||||
|
|
||||||
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],
|
||||||
None => {
|
None => {
|
||||||
return AppMachine::error_state(inner, "cannot fetch: no artist selected").into()
|
let err = "cannot fetch artist: no artist selected";
|
||||||
|
return AppMachine::error_state(inner, err).into();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let (fetch_tx, fetch_rx) = mpsc::channel::<MbApiResult>();
|
let (fetch_tx, fetch_rx) = mpsc::channel::<MbApiResult>();
|
||||||
let fetch = FetchState::new(fetch_rx);
|
let fetch = FetchState::new(fetch_rx);
|
||||||
match Self::submit_fetch_job(&*inner.musicbrainz, fetch_tx, artist) {
|
|
||||||
|
let mb = &*inner.musicbrainz;
|
||||||
|
let result = match inner.selection.category() {
|
||||||
|
Category::Artist => Self::submit_search_artist_job(mb, fetch_tx, artist),
|
||||||
|
_ => {
|
||||||
|
let arid = match artist.meta.info.musicbrainz {
|
||||||
|
MbRefOption::Some(ref mbref) => mbref,
|
||||||
|
_ => {
|
||||||
|
let err = "cannot fetch album: artist has no MBID";
|
||||||
|
return AppMachine::error_state(inner, err).into();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let album = match inner.selection.state_album(coll) {
|
||||||
|
Some(album_state) => &artist.albums[album_state.index],
|
||||||
|
None => {
|
||||||
|
let err = "cannot fetch album: no album selected";
|
||||||
|
return AppMachine::error_state(inner, err).into();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let artist_id = &artist.meta.id;
|
||||||
|
Self::submit_search_release_group_job(mb, fetch_tx, artist_id, arid, album)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
match result {
|
||||||
Ok(()) => AppMachine::fetch_state(inner, fetch).into(),
|
Ok(()) => AppMachine::fetch_state(inner, fetch).into(),
|
||||||
Err(FetchError::NothingToFetch) => AppMachine::browse_state(inner).into(),
|
Err(FetchError::NothingToFetch) => AppMachine::browse_state(inner).into(),
|
||||||
Err(FetchError::SubmitError(daemon_err)) => {
|
Err(FetchError::SubmitError(daemon_err)) => {
|
||||||
@ -144,17 +171,17 @@ impl AppMachine<FetchState> {
|
|||||||
Self::app_fetch_next(inner, fetch)
|
Self::app_fetch_next(inner, fetch)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn submit_fetch_job(
|
fn submit_search_artist_job(
|
||||||
musicbrainz: &dyn IMbJobSender,
|
musicbrainz: &dyn IMbJobSender,
|
||||||
result_sender: ResultSender,
|
result_sender: ResultSender,
|
||||||
artist: &Artist,
|
artist: &Artist,
|
||||||
) -> Result<(), FetchError> {
|
) -> Result<(), FetchError> {
|
||||||
let requests = match artist.meta.info.musicbrainz {
|
let requests = match artist.meta.info.musicbrainz {
|
||||||
MbRefOption::Some(ref arid) => {
|
MbRefOption::Some(ref arid) => {
|
||||||
Self::fetch_albums_requests(&artist.meta.id, arid, &artist.albums)
|
Self::search_albums_requests(&artist.meta.id, arid, &artist.albums)
|
||||||
}
|
}
|
||||||
MbRefOption::CannotHaveMbid => VecDeque::new(),
|
MbRefOption::CannotHaveMbid => VecDeque::new(),
|
||||||
MbRefOption::None => Self::fetch_artist_request(&artist.meta),
|
MbRefOption::None => Self::search_artist_request(&artist.meta),
|
||||||
};
|
};
|
||||||
if requests.is_empty() {
|
if requests.is_empty() {
|
||||||
return Err(FetchError::NothingToFetch);
|
return Err(FetchError::NothingToFetch);
|
||||||
@ -162,7 +189,22 @@ impl AppMachine<FetchState> {
|
|||||||
Ok(musicbrainz.submit_background_job(result_sender, requests)?)
|
Ok(musicbrainz.submit_background_job(result_sender, requests)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fetch_albums_requests(
|
fn submit_search_release_group_job(
|
||||||
|
musicbrainz: &dyn IMbJobSender,
|
||||||
|
result_sender: ResultSender,
|
||||||
|
artist_id: &ArtistId,
|
||||||
|
artist_mbid: &MbArtistRef,
|
||||||
|
album: &Album,
|
||||||
|
) -> Result<(), FetchError> {
|
||||||
|
if !matches!(album.meta.info.musicbrainz, MbRefOption::None) {
|
||||||
|
return Err(FetchError::NothingToFetch);
|
||||||
|
}
|
||||||
|
let requests =
|
||||||
|
Self::search_albums_requests(&artist_id, &artist_mbid, slice::from_ref(album));
|
||||||
|
Ok(musicbrainz.submit_background_job(result_sender, requests)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn search_albums_requests(
|
||||||
artist: &ArtistId,
|
artist: &ArtistId,
|
||||||
arid: &MbArtistRef,
|
arid: &MbArtistRef,
|
||||||
albums: &[Album],
|
albums: &[Album],
|
||||||
@ -177,7 +219,7 @@ impl AppMachine<FetchState> {
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fetch_artist_request(meta: &ArtistMeta) -> VecDeque<MbParams> {
|
fn search_artist_request(meta: &ArtistMeta) -> VecDeque<MbParams> {
|
||||||
VecDeque::from([MbParams::search_artist(meta.clone())])
|
VecDeque::from([MbParams::search_artist(meta.clone())])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user