Add fetch state

This commit is contained in:
Wojciech Kozlowski 2024-09-01 22:14:45 +02:00
parent 8e48412282
commit 9552311d02
6 changed files with 109 additions and 16 deletions

View File

@ -0,0 +1,46 @@
use crate::tui::app::{
machine::{App, AppInner, AppMachine},
AppPublic, AppState, IAppInteractFetch,
};
pub struct AppFetch;
impl AppMachine<AppFetch> {
pub fn fetch(inner: AppInner) -> Self {
AppMachine {
inner,
state: AppFetch,
}
}
}
impl From<AppMachine<AppFetch>> for App {
fn from(machine: AppMachine<AppFetch>) -> Self {
AppState::Fetch(machine)
}
}
impl<'a> From<&'a mut AppMachine<AppFetch>> for AppPublic<'a> {
fn from(machine: &'a mut AppMachine<AppFetch>) -> Self {
AppPublic {
inner: (&mut machine.inner).into(),
state: AppState::Fetch(()),
}
}
}
impl IAppInteractFetch for AppMachine<AppFetch> {
type APP = App;
fn proceed(self) -> Self::APP {
self.into()
}
fn abort(self) -> Self::APP {
AppMachine::browse(self.inner).into()
}
fn no_op(self) -> Self::APP {
self.into()
}
}

View File

@ -1,6 +1,7 @@
mod browse;
mod critical;
mod error;
mod fetch;
mod info;
mod matches;
mod reload;
@ -16,6 +17,7 @@ use crate::tui::{
use browse::AppBrowse;
use critical::AppCritical;
use error::AppError;
use fetch::AppFetch;
use info::AppInfo;
use matches::AppMatches;
use reload::AppReload;
@ -26,6 +28,7 @@ pub type App = AppState<
AppMachine<AppInfo>,
AppMachine<AppReload>,
AppMachine<AppSearch>,
AppMachine<AppFetch>,
AppMachine<AppMatches>,
AppMachine<AppError>,
AppMachine<AppCritical>,
@ -65,9 +68,10 @@ impl App {
match self {
AppState::Browse(browse) => &browse.inner,
AppState::Info(info) => &info.inner,
AppState::Matches(matches) => &matches.inner,
AppState::Reload(reload) => &reload.inner,
AppState::Search(search) => &search.inner,
AppState::Fetch(fetch) => &fetch.inner,
AppState::Matches(matches) => &matches.inner,
AppState::Error(error) => &error.inner,
AppState::Critical(critical) => &critical.inner,
}
@ -77,9 +81,10 @@ impl App {
match self {
AppState::Browse(browse) => &mut browse.inner,
AppState::Info(info) => &mut info.inner,
AppState::Matches(matches) => &mut matches.inner,
AppState::Reload(reload) => &mut reload.inner,
AppState::Search(search) => &mut search.inner,
AppState::Fetch(fetch) => &mut fetch.inner,
AppState::Matches(matches) => &mut matches.inner,
AppState::Error(error) => &mut error.inner,
AppState::Critical(critical) => &mut critical.inner,
}
@ -91,6 +96,7 @@ impl IAppInteract for App {
type IS = AppMachine<AppInfo>;
type RS = AppMachine<AppReload>;
type SS = AppMachine<AppSearch>;
type FS = AppMachine<AppFetch>;
type MS = AppMachine<AppMatches>;
type ES = AppMachine<AppError>;
type CS = AppMachine<AppCritical>;
@ -106,7 +112,8 @@ impl IAppInteract for App {
fn state(
self,
) -> AppState<Self::BS, Self::IS, Self::RS, Self::SS, Self::MS, Self::ES, Self::CS> {
) -> AppState<Self::BS, Self::IS, Self::RS, Self::SS, Self::FS, Self::MS, Self::ES, Self::CS>
{
self
}
}
@ -116,9 +123,10 @@ impl IAppAccess for App {
match self {
AppState::Browse(browse) => browse.into(),
AppState::Info(info) => info.into(),
AppState::Matches(matches) => matches.into(),
AppState::Reload(reload) => reload.into(),
AppState::Search(search) => search.into(),
AppState::Fetch(fetch) => fetch.into(),
AppState::Matches(matches) => matches.into(),
AppState::Error(error) => error.into(),
AppState::Critical(critical) => critical.into(),
}
@ -162,7 +170,7 @@ mod tests {
use super::*;
impl<BS, IS, RS, SS, MS, ES, CS> AppState<BS, IS, RS, SS, MS, ES, CS> {
impl<BS, IS, RS, SS, FS, MS, ES, CS> AppState<BS, IS, RS, SS, FS, MS, ES, CS> {
pub fn unwrap_browse(self) -> BS {
match self {
AppState::Browse(browse) => browse,
@ -191,6 +199,13 @@ mod tests {
}
}
pub fn unwrap_fetch(self) -> FS {
match self {
AppState::Fetch(fetch) => fetch,
_ => panic!(),
}
}
pub fn unwrap_matches(self) -> MS {
match self {
AppState::Matches(matches) => matches,

View File

@ -8,11 +8,12 @@ use musichoard::collection::{album::AlbumMeta, artist::ArtistMeta, Collection};
use crate::tui::lib::interface::musicbrainz::Match;
pub enum AppState<BS, IS, RS, SS, MS, ES, CS> {
pub enum AppState<BS, IS, RS, SS, FS, MS, ES, CS> {
Browse(BS),
Info(IS),
Reload(RS),
Search(SS),
Fetch(FS),
Matches(MS),
Error(ES),
Critical(CS),
@ -23,6 +24,7 @@ pub trait IAppInteract {
type IS: IAppInteractInfo<APP = Self>;
type RS: IAppInteractReload<APP = Self>;
type SS: IAppInteractSearch<APP = Self>;
type FS: IAppInteractFetch<APP = Self>;
type MS: IAppInteractMatches<APP = Self>;
type ES: IAppInteractError<APP = Self>;
type CS: IAppInteractCritical<APP = Self>;
@ -33,7 +35,7 @@ pub trait IAppInteract {
#[allow(clippy::type_complexity)]
fn state(
self,
) -> AppState<Self::BS, Self::IS, Self::RS, Self::SS, Self::MS, Self::ES, Self::CS>;
) -> AppState<Self::BS, Self::IS, Self::RS, Self::SS, Self::FS, Self::MS, Self::ES, Self::CS>;
}
pub trait IAppInteractBrowse {
@ -87,6 +89,15 @@ pub trait IAppInteractSearch {
fn no_op(self) -> Self::APP;
}
pub trait IAppInteractFetch {
type APP: IAppInteract;
fn proceed(self) -> Self::APP;
fn abort(self) -> Self::APP;
fn no_op(self) -> Self::APP;
}
pub trait IAppInteractMatches {
type APP: IAppInteract;
@ -177,9 +188,9 @@ pub struct AppPublicMatches<'app> {
}
pub type AppPublicState<'app> =
AppState<(), (), (), &'app str, AppPublicMatches<'app>, &'app str, &'app str>;
AppState<(), (), (), &'app str, (), AppPublicMatches<'app>, &'app str, &'app str>;
impl<BS, IS, RS, SS, MS, ES, CS> AppState<BS, IS, RS, SS, MS, ES, CS> {
impl<BS, IS, RS, SS, FS, MS, ES, CS> AppState<BS, IS, RS, SS, FS, MS, ES, CS> {
pub fn is_search(&self) -> bool {
matches!(self, AppState::Search(_))
}

View File

@ -6,7 +6,8 @@ use mockall::automock;
use crate::tui::{
app::{
AppState, Delta, IAppInteract, IAppInteractBrowse, IAppInteractCritical, IAppInteractError,
IAppInteractInfo, IAppInteractMatches, IAppInteractReload, IAppInteractSearch,
IAppInteractFetch, IAppInteractInfo, IAppInteractMatches, IAppInteractReload,
IAppInteractSearch,
},
event::{Event, EventError, EventReceiver},
};
@ -22,6 +23,7 @@ trait IEventHandlerPrivate<APP: IAppInteract> {
fn handle_info_key_event(app: <APP as IAppInteract>::IS, 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_fetch_key_event(app: <APP as IAppInteract>::FS, 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;
@ -70,6 +72,9 @@ impl<APP: IAppInteract> IEventHandlerPrivate<APP> for EventHandler {
AppState::Search(search) => {
<Self as IEventHandlerPrivate<APP>>::handle_search_key_event(search, key_event)
}
AppState::Fetch(fetch) => {
<Self as IEventHandlerPrivate<APP>>::handle_fetch_key_event(fetch, key_event)
}
AppState::Matches(matches) => {
<Self as IEventHandlerPrivate<APP>>::handle_matches_key_event(matches, key_event)
}
@ -161,6 +166,15 @@ impl<APP: IAppInteract> IEventHandlerPrivate<APP> for EventHandler {
}
}
fn handle_fetch_key_event(app: <APP as IAppInteract>::FS, key_event: KeyEvent) -> APP {
match key_event.code {
// Abort.
KeyCode::Esc | KeyCode::Char('q') | KeyCode::Char('Q') => app.abort(),
// Othey keys.
_ => app.no_op(),
}
}
fn handle_matches_key_event(app: <APP as IAppInteract>::MS, key_event: KeyEvent) -> APP {
match key_event.code {
// Abort.

View File

@ -27,7 +27,7 @@ impl Minibuffer<'_> {
pub fn new(state: &AppPublicState) -> Self {
let columns = 3;
let mut mb = match state {
AppState::Browse(_) => Minibuffer {
AppState::Browse(()) => Minibuffer {
paragraphs: vec![
Paragraph::new("m: show info overlay"),
Paragraph::new("g: show reload menu"),
@ -36,11 +36,11 @@ impl Minibuffer<'_> {
],
columns,
},
AppState::Info(_) => Minibuffer {
AppState::Info(()) => Minibuffer {
paragraphs: vec![Paragraph::new("m: hide info overlay")],
columns,
},
AppState::Reload(_) => Minibuffer {
AppState::Reload(()) => Minibuffer {
paragraphs: vec![
Paragraph::new("g: hide reload menu"),
Paragraph::new("d: reload database"),
@ -51,12 +51,18 @@ impl Minibuffer<'_> {
AppState::Search(ref s) => Minibuffer {
paragraphs: vec![
Paragraph::new(format!("I-search: {s}")),
Paragraph::new("ctrl+s: search next".to_string()).alignment(Alignment::Center),
Paragraph::new("ctrl+g: cancel search".to_string())
.alignment(Alignment::Center),
Paragraph::new("ctrl+s: search next").alignment(Alignment::Center),
Paragraph::new("ctrl+g: cancel search").alignment(Alignment::Center),
],
columns,
},
AppState::Fetch(()) => Minibuffer {
paragraphs: vec![
Paragraph::new(format!("fetching...")),
Paragraph::new(format!("q: abort")),
],
columns: 2,
},
AppState::Matches(public) => Minibuffer {
paragraphs: vec![
Paragraph::new(UiDisplay::display_matching_info(public.matches)),

View File

@ -203,6 +203,7 @@ mod tests {
AppState::Info(()) => AppState::Info(()),
AppState::Reload(()) => AppState::Reload(()),
AppState::Search(s) => AppState::Search(s),
AppState::Fetch(()) => AppState::Fetch(()),
AppState::Matches(ref mut m) => AppState::Matches(AppPublicMatches {
matches: m.matches,
state: m.state,