Provide a keyboard shortcut to sync all existing albums with MusicBrainz #167
@ -1,3 +1,11 @@
|
||||
use std::{thread, time};
|
||||
|
||||
use musichoard::{
|
||||
collection::musicbrainz::IMusicBrainzRef,
|
||||
external::musicbrainz::api::{client::MusicBrainzApiClient, MusicBrainzApi},
|
||||
interface::musicbrainz::IMusicBrainz,
|
||||
};
|
||||
|
||||
use crate::tui::{
|
||||
app::{
|
||||
machine::{App, AppInner, AppMachine},
|
||||
@ -81,6 +89,48 @@ impl<MH: IMusicHoard> IAppInteractBrowse for AppMachine<MH, AppBrowse> {
|
||||
AppMachine::search(self.inner, orig).into()
|
||||
}
|
||||
|
||||
fn fetch_musicbrainz(self) -> Self::APP {
|
||||
const USER_AGENT: &str = concat!(
|
||||
"MusicHoard/",
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
" ( musichoard@thenineworlds.net )"
|
||||
);
|
||||
|
||||
let client = MusicBrainzApiClient::new(USER_AGENT).expect("failed to create API client");
|
||||
let mut api = MusicBrainzApi::new(client);
|
||||
|
||||
let coll = self.inner.music_hoard.get_collection();
|
||||
let artist = match self.inner.selection.state_artist(coll) {
|
||||
Some(artist_state) => &coll[artist_state.index],
|
||||
None => {
|
||||
return AppMachine::error(self.inner, "cannot fetch: no artist selected").into()
|
||||
}
|
||||
};
|
||||
|
||||
let arid = match artist.musicbrainz {
|
||||
Some(ref mbid) => mbid.mbid(),
|
||||
None => {
|
||||
return AppMachine::error(self.inner, "cannot fetch: missing artist MBID").into()
|
||||
}
|
||||
};
|
||||
|
||||
if artist.albums.is_empty() {
|
||||
return AppMachine::error(self.inner, "cannot fetch: this artist has no albums").into();
|
||||
}
|
||||
|
||||
let mut artist_album_matches = vec![];
|
||||
for album in artist.albums.iter() {
|
||||
match api.search_release_group(arid, album) {
|
||||
Ok(matches) => artist_album_matches.push(matches),
|
||||
Err(err) => return AppMachine::error(self.inner, err.to_string()).into(),
|
||||
}
|
||||
|
||||
thread::sleep(time::Duration::from_secs(1));
|
||||
}
|
||||
|
||||
AppMachine::matches(self.inner, artist_album_matches).into()
|
||||
}
|
||||
|
||||
fn no_op(self) -> Self::APP {
|
||||
self.into()
|
||||
}
|
||||
|
@ -1,13 +1,7 @@
|
||||
use musichoard::{
|
||||
collection::{album::Album, artist::Artist, musicbrainz::IMusicBrainzRef, Collection},
|
||||
external::musicbrainz::api::{client::MusicBrainzApiClient, MusicBrainzApi},
|
||||
interface::musicbrainz::IMusicBrainz,
|
||||
};
|
||||
|
||||
use crate::tui::{
|
||||
app::{
|
||||
machine::{App, AppInner, AppMachine},
|
||||
AppPublic, AppState, Category, IAppInteractInfo,
|
||||
AppPublic, AppState, IAppInteractInfo,
|
||||
},
|
||||
lib::IMusicHoard,
|
||||
};
|
||||
@ -45,48 +39,6 @@ impl<MH: IMusicHoard> IAppInteractInfo for AppMachine<MH, AppInfo> {
|
||||
AppMachine::browse(self.inner).into()
|
||||
}
|
||||
|
||||
fn fetch_musicbrainz(self) -> Self::APP {
|
||||
const USER_AGENT: &str = concat!(
|
||||
"MusicHoard/",
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
" ( musichoard@thenineworlds.net )"
|
||||
);
|
||||
|
||||
let client = MusicBrainzApiClient::new(USER_AGENT).expect("failed to create API client");
|
||||
let mut api = MusicBrainzApi::new(client);
|
||||
|
||||
if self.inner.selection.category() == Category::Artist {
|
||||
return AppMachine::error(self.inner, "artist fetch not yet supported").into();
|
||||
}
|
||||
|
||||
let coll: &Collection = self.inner.music_hoard.get_collection();
|
||||
let artist: &Artist = match self.inner.selection.state_artist(coll) {
|
||||
Some(artist_state) => &coll[artist_state.index],
|
||||
None => {
|
||||
return AppMachine::error(self.inner, "cannot fetch: no artist selected").into()
|
||||
}
|
||||
};
|
||||
|
||||
let arid = match artist.musicbrainz {
|
||||
Some(ref mbid) => mbid.mbid(),
|
||||
None => {
|
||||
return AppMachine::error(self.inner, "cannot fetch: missing artist MBID").into()
|
||||
}
|
||||
};
|
||||
|
||||
let album: &Album = match self.inner.selection.state_album(coll) {
|
||||
Some(album_state) => &artist.albums[album_state.index],
|
||||
None => {
|
||||
return AppMachine::error(self.inner, "cannot fetch: no album selected").into()
|
||||
}
|
||||
};
|
||||
|
||||
match api.search_release_group(arid, album) {
|
||||
Ok(matches) => AppMachine::matches(self.inner, matches).into(),
|
||||
Err(err) => AppMachine::error(self.inner, err.to_string()).into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn no_op(self) -> Self::APP {
|
||||
self.into()
|
||||
}
|
||||
|
@ -11,19 +11,27 @@ use crate::tui::{
|
||||
};
|
||||
|
||||
pub struct AppMatches {
|
||||
matches: Vec<Match<Album>>,
|
||||
matches: Vec<Vec<Match<Album>>>,
|
||||
index: usize,
|
||||
state: WidgetState,
|
||||
}
|
||||
|
||||
impl<MH: IMusicHoard> AppMachine<MH, AppMatches> {
|
||||
pub fn matches(inner: AppInner<MH>, matches: Vec<Match<Album>>) -> Self {
|
||||
pub fn matches(inner: AppInner<MH>, matches: Vec<Vec<Match<Album>>>) -> Self {
|
||||
assert!(!matches.is_empty());
|
||||
|
||||
let mut state = WidgetState::default();
|
||||
if !matches.is_empty() {
|
||||
if !matches[0].is_empty() {
|
||||
state.list.select(Some(0));
|
||||
}
|
||||
|
||||
AppMachine {
|
||||
inner,
|
||||
state: AppMatches { matches, state },
|
||||
state: AppMatches {
|
||||
matches,
|
||||
index: 0,
|
||||
state,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -39,7 +47,7 @@ impl<'a, MH: IMusicHoard> From<&'a mut AppMachine<MH, AppMatches>> for AppPublic
|
||||
AppPublic {
|
||||
inner: (&mut machine.inner).into(),
|
||||
state: AppState::Matches(AppPublicMatches {
|
||||
matches: &machine.state.matches,
|
||||
matches: &machine.state.matches[machine.state.index],
|
||||
state: &mut machine.state.state,
|
||||
}),
|
||||
}
|
||||
@ -61,15 +69,28 @@ impl<MH: IMusicHoard> IAppInteractMatches for AppMachine<MH, AppMatches> {
|
||||
fn next_match(mut self) -> Self::APP {
|
||||
if let Some(index) = self.state.state.list.selected() {
|
||||
let result = index.saturating_add(1);
|
||||
let to = cmp::min(result, self.state.matches.len() - 1);
|
||||
let to = cmp::min(result, self.state.matches[self.state.index].len() - 1);
|
||||
self.state.state.list.select(Some(to));
|
||||
}
|
||||
|
||||
self.into()
|
||||
}
|
||||
|
||||
fn select(mut self) -> Self::APP {
|
||||
self.state.index = self.state.index.saturating_add(1);
|
||||
if self.state.index < self.state.matches.len() {
|
||||
self.state.state = WidgetState::default();
|
||||
if !self.state.matches[self.state.index].is_empty() {
|
||||
self.state.state.list.select(Some(0));
|
||||
}
|
||||
self.into()
|
||||
} else {
|
||||
AppMachine::browse(self.inner).into()
|
||||
}
|
||||
}
|
||||
|
||||
fn abort(self) -> Self::APP {
|
||||
AppMachine::info(self.inner).into()
|
||||
AppMachine::browse(self.inner).into()
|
||||
}
|
||||
|
||||
fn no_op(self) -> Self::APP {
|
||||
|
@ -22,9 +22,9 @@ use search::AppSearch;
|
||||
pub type App<MH> = AppState<
|
||||
AppMachine<MH, AppBrowse>,
|
||||
AppMachine<MH, AppInfo>,
|
||||
AppMachine<MH, AppMatches>,
|
||||
AppMachine<MH, AppReload>,
|
||||
AppMachine<MH, AppSearch>,
|
||||
AppMachine<MH, AppMatches>,
|
||||
AppMachine<MH, AppError>,
|
||||
AppMachine<MH, AppCritical>,
|
||||
>;
|
||||
@ -83,9 +83,9 @@ impl<MH: IMusicHoard> App<MH> {
|
||||
impl<MH: IMusicHoard> IAppInteract for App<MH> {
|
||||
type BS = AppMachine<MH, AppBrowse>;
|
||||
type IS = AppMachine<MH, AppInfo>;
|
||||
type MS = AppMachine<MH, AppMatches>;
|
||||
type RS = AppMachine<MH, AppReload>;
|
||||
type SS = AppMachine<MH, AppSearch>;
|
||||
type MS = AppMachine<MH, AppMatches>;
|
||||
type ES = AppMachine<MH, AppError>;
|
||||
type CS = AppMachine<MH, AppCritical>;
|
||||
|
||||
@ -100,7 +100,7 @@ impl<MH: IMusicHoard> IAppInteract for App<MH> {
|
||||
|
||||
fn state(
|
||||
self,
|
||||
) -> AppState<Self::BS, Self::IS, Self::MS, Self::RS, Self::SS, Self::ES, Self::CS> {
|
||||
) -> AppState<Self::BS, Self::IS, Self::RS, Self::SS, Self::MS, Self::ES, Self::CS> {
|
||||
self
|
||||
}
|
||||
}
|
||||
@ -150,7 +150,7 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
impl<BS, IS, MS, RS, SS, ES, CS> AppState<BS, IS, MS, RS, SS, ES, CS> {
|
||||
impl<BS, IS, RS, SS, MS, ES, CS> AppState<BS, IS, RS, SS, MS, ES, CS> {
|
||||
pub fn unwrap_browse(self) -> BS {
|
||||
match self {
|
||||
AppState::Browse(browse) => browse,
|
||||
|
@ -9,12 +9,12 @@ use musichoard::{
|
||||
interface::musicbrainz::Match,
|
||||
};
|
||||
|
||||
pub enum AppState<BS, IS, MS, RS, SS, ES, CS> {
|
||||
pub enum AppState<BS, IS, RS, SS, MS, ES, CS> {
|
||||
Browse(BS),
|
||||
Info(IS),
|
||||
Matches(MS),
|
||||
Reload(RS),
|
||||
Search(SS),
|
||||
Matches(MS),
|
||||
Error(ES),
|
||||
Critical(CS),
|
||||
}
|
||||
@ -22,9 +22,9 @@ pub enum AppState<BS, IS, MS, RS, SS, ES, CS> {
|
||||
pub trait IAppInteract {
|
||||
type BS: IAppInteractBrowse<APP = Self>;
|
||||
type IS: IAppInteractInfo<APP = Self>;
|
||||
type MS: IAppInteractMatches<APP = Self>;
|
||||
type RS: IAppInteractReload<APP = Self>;
|
||||
type SS: IAppInteractSearch<APP = Self>;
|
||||
type MS: IAppInteractMatches<APP = Self>;
|
||||
type ES: IAppInteractError<APP = Self>;
|
||||
type CS: IAppInteractCritical<APP = Self>;
|
||||
|
||||
@ -34,7 +34,7 @@ pub trait IAppInteract {
|
||||
#[allow(clippy::type_complexity)]
|
||||
fn state(
|
||||
self,
|
||||
) -> AppState<Self::BS, Self::IS, Self::MS, Self::RS, Self::SS, Self::ES, Self::CS>;
|
||||
) -> AppState<Self::BS, Self::IS, Self::RS, Self::SS, Self::MS, Self::ES, Self::CS>;
|
||||
}
|
||||
|
||||
pub trait IAppInteractBrowse {
|
||||
@ -53,6 +53,8 @@ pub trait IAppInteractBrowse {
|
||||
|
||||
fn begin_search(self) -> Self::APP;
|
||||
|
||||
fn fetch_musicbrainz(self) -> Self::APP;
|
||||
|
||||
fn no_op(self) -> Self::APP;
|
||||
}
|
||||
|
||||
@ -60,18 +62,6 @@ pub trait IAppInteractInfo {
|
||||
type APP: IAppInteract;
|
||||
|
||||
fn hide_info_overlay(self) -> Self::APP;
|
||||
fn fetch_musicbrainz(self) -> Self::APP;
|
||||
|
||||
fn no_op(self) -> Self::APP;
|
||||
}
|
||||
|
||||
pub trait IAppInteractMatches {
|
||||
type APP: IAppInteract;
|
||||
|
||||
fn prev_match(self) -> Self::APP;
|
||||
fn next_match(self) -> Self::APP;
|
||||
|
||||
fn abort(self) -> Self::APP;
|
||||
|
||||
fn no_op(self) -> Self::APP;
|
||||
}
|
||||
@ -98,6 +88,18 @@ pub trait IAppInteractSearch {
|
||||
fn no_op(self) -> Self::APP;
|
||||
}
|
||||
|
||||
pub trait IAppInteractMatches {
|
||||
type APP: IAppInteract;
|
||||
|
||||
fn prev_match(self) -> Self::APP;
|
||||
fn next_match(self) -> Self::APP;
|
||||
fn select(self) -> Self::APP;
|
||||
|
||||
fn abort(self) -> Self::APP;
|
||||
|
||||
fn no_op(self) -> Self::APP;
|
||||
}
|
||||
|
||||
pub trait IAppInteractError {
|
||||
type APP: IAppInteract;
|
||||
|
||||
@ -134,9 +136,9 @@ pub struct AppPublicMatches<'app> {
|
||||
}
|
||||
|
||||
pub type AppPublicState<'app> =
|
||||
AppState<(), (), AppPublicMatches<'app>, (), &'app str, &'app str, &'app str>;
|
||||
AppState<(), (), (), &'app str, AppPublicMatches<'app>, &'app str, &'app str>;
|
||||
|
||||
impl<BS, IS, MS, RS, SS, ES, CS> AppState<BS, IS, MS, RS, SS, ES, CS> {
|
||||
impl<BS, IS, RS, SS, MS, ES, CS> AppState<BS, IS, RS, SS, MS, ES, CS> {
|
||||
pub fn is_search(&self) -> bool {
|
||||
matches!(self, AppState::Search(_))
|
||||
}
|
||||
|
@ -20,9 +20,9 @@ trait IEventHandlerPrivate<APP: IAppInteract> {
|
||||
fn handle_key_event(app: APP, key_event: KeyEvent) -> APP;
|
||||
fn handle_browse_key_event(app: <APP as IAppInteract>::BS, key_event: KeyEvent) -> APP;
|
||||
fn handle_info_key_event(app: <APP as IAppInteract>::IS, key_event: KeyEvent) -> APP;
|
||||
fn handle_matches_key_event(app: <APP as IAppInteract>::MS, key_event: KeyEvent) -> APP;
|
||||
fn handle_reload_key_event(app: <APP as IAppInteract>::RS, key_event: KeyEvent) -> APP;
|
||||
fn handle_search_key_event(app: <APP as IAppInteract>::SS, key_event: KeyEvent) -> APP;
|
||||
fn handle_matches_key_event(app: <APP as IAppInteract>::MS, key_event: KeyEvent) -> APP;
|
||||
fn handle_error_key_event(app: <APP as IAppInteract>::ES, key_event: KeyEvent) -> APP;
|
||||
fn handle_critical_key_event(app: <APP as IAppInteract>::CS, key_event: KeyEvent) -> APP;
|
||||
}
|
||||
@ -64,15 +64,15 @@ impl<APP: IAppInteract> IEventHandlerPrivate<APP> for EventHandler {
|
||||
AppState::Info(info) => {
|
||||
<Self as IEventHandlerPrivate<APP>>::handle_info_key_event(info, key_event)
|
||||
}
|
||||
AppState::Matches(matches) => {
|
||||
<Self as IEventHandlerPrivate<APP>>::handle_matches_key_event(matches, key_event)
|
||||
}
|
||||
AppState::Reload(reload) => {
|
||||
<Self as IEventHandlerPrivate<APP>>::handle_reload_key_event(reload, key_event)
|
||||
}
|
||||
AppState::Search(search) => {
|
||||
<Self as IEventHandlerPrivate<APP>>::handle_search_key_event(search, key_event)
|
||||
}
|
||||
AppState::Matches(matches) => {
|
||||
<Self as IEventHandlerPrivate<APP>>::handle_matches_key_event(matches, key_event)
|
||||
}
|
||||
AppState::Error(error) => {
|
||||
<Self as IEventHandlerPrivate<APP>>::handle_error_key_event(error, key_event)
|
||||
}
|
||||
@ -106,6 +106,7 @@ impl<APP: IAppInteract> IEventHandlerPrivate<APP> for EventHandler {
|
||||
app.no_op()
|
||||
}
|
||||
}
|
||||
KeyCode::Char('f') | KeyCode::Char('F') => app.fetch_musicbrainz(),
|
||||
// Othey keys.
|
||||
_ => app.no_op(),
|
||||
}
|
||||
@ -119,19 +120,6 @@ impl<APP: IAppInteract> IEventHandlerPrivate<APP> for EventHandler {
|
||||
| KeyCode::Char('Q')
|
||||
| KeyCode::Char('m')
|
||||
| KeyCode::Char('M') => app.hide_info_overlay(),
|
||||
KeyCode::Char('f') | KeyCode::Char('F') => app.fetch_musicbrainz(),
|
||||
// Othey keys.
|
||||
_ => app.no_op(),
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_matches_key_event(app: <APP as IAppInteract>::MS, key_event: KeyEvent) -> APP {
|
||||
match key_event.code {
|
||||
// Abort.
|
||||
KeyCode::Esc | KeyCode::Char('q') | KeyCode::Char('Q') => app.abort(),
|
||||
// Select.
|
||||
KeyCode::Up => app.prev_match(),
|
||||
KeyCode::Down => app.next_match(),
|
||||
// Othey keys.
|
||||
_ => app.no_op(),
|
||||
}
|
||||
@ -173,6 +161,19 @@ impl<APP: IAppInteract> IEventHandlerPrivate<APP> for EventHandler {
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_matches_key_event(app: <APP as IAppInteract>::MS, key_event: KeyEvent) -> APP {
|
||||
match key_event.code {
|
||||
// Abort.
|
||||
KeyCode::Esc | KeyCode::Char('q') | KeyCode::Char('Q') => app.abort(),
|
||||
// Select.
|
||||
KeyCode::Up => app.prev_match(),
|
||||
KeyCode::Down => app.next_match(),
|
||||
KeyCode::Enter => app.select(),
|
||||
// Othey keys.
|
||||
_ => app.no_op(),
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_error_key_event(app: <APP as IAppInteract>::ES, _key_event: KeyEvent) -> APP {
|
||||
// Any key dismisses the error.
|
||||
app.dismiss_error()
|
||||
|
@ -504,18 +504,12 @@ impl Minibuffer<'_> {
|
||||
Paragraph::new("m: show info overlay"),
|
||||
Paragraph::new("g: show reload menu"),
|
||||
Paragraph::new("ctrl+s: search artist"),
|
||||
],
|
||||
columns,
|
||||
},
|
||||
AppState::Info(_) => Minibuffer {
|
||||
paragraphs: vec![
|
||||
Paragraph::new("m: hide info overlay"),
|
||||
Paragraph::new("f: fetch musicbrainz"),
|
||||
],
|
||||
columns,
|
||||
},
|
||||
AppState::Matches(_) => Minibuffer {
|
||||
paragraphs: vec![],
|
||||
AppState::Info(_) => Minibuffer {
|
||||
paragraphs: vec![Paragraph::new("m: hide info overlay")],
|
||||
columns,
|
||||
},
|
||||
AppState::Reload(_) => Minibuffer {
|
||||
@ -535,6 +529,13 @@ impl Minibuffer<'_> {
|
||||
],
|
||||
columns,
|
||||
},
|
||||
AppState::Matches(_) => Minibuffer {
|
||||
paragraphs: vec![
|
||||
Paragraph::new("enter: apply highlighted"),
|
||||
Paragraph::new("q: abort"),
|
||||
],
|
||||
columns,
|
||||
},
|
||||
AppState::Error(_) => Minibuffer {
|
||||
paragraphs: vec![Paragraph::new(
|
||||
"Press any key to dismiss the error message...",
|
||||
@ -908,12 +909,12 @@ mod tests {
|
||||
state: match self.state {
|
||||
AppState::Browse(()) => AppState::Browse(()),
|
||||
AppState::Info(()) => AppState::Info(()),
|
||||
AppState::Reload(()) => AppState::Reload(()),
|
||||
AppState::Search(s) => AppState::Search(s),
|
||||
AppState::Matches(ref mut m) => AppState::Matches(AppPublicMatches {
|
||||
matches: &m.matches,
|
||||
state: &mut m.state,
|
||||
}),
|
||||
AppState::Reload(()) => AppState::Reload(()),
|
||||
AppState::Search(s) => AppState::Search(s),
|
||||
AppState::Error(s) => AppState::Error(s),
|
||||
AppState::Critical(s) => AppState::Critical(s),
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user