Split states into own structs

This commit is contained in:
Wojciech Kozlowski 2024-02-17 22:53:33 +01:00
parent 001bc81b0d
commit 6b7e3ad232
2 changed files with 47 additions and 33 deletions

View File

@ -18,15 +18,23 @@ struct AppInner<MH: IMusicHoard> {
} }
pub type App<MH> = AppState< pub type App<MH> = AppState<
AppGenericState<MH>, AppBrowseState<MH>,
AppGenericState<MH>, AppInfoState<MH>,
AppGenericState<MH>, AppReloadState<MH>,
AppSearchState<MH>, AppSearchState<MH>,
AppErrorState<MH>, AppErrorState<MH>,
AppErrorState<MH>, AppCriticalState<MH>,
>; >;
pub struct AppGenericState<MH: IMusicHoard> { pub struct AppBrowseState<MH: IMusicHoard> {
inner: AppInner<MH>,
}
pub struct AppInfoState<MH: IMusicHoard> {
inner: AppInner<MH>,
}
pub struct AppReloadState<MH: IMusicHoard> {
inner: AppInner<MH>, inner: AppInner<MH>,
} }
@ -47,6 +55,11 @@ pub struct AppErrorState<MH: IMusicHoard> {
string: String, string: String,
} }
pub struct AppCriticalState<MH: IMusicHoard> {
inner: AppInner<MH>,
string: String,
}
impl<MH: IMusicHoard> App<MH> { impl<MH: IMusicHoard> App<MH> {
pub fn new(mut music_hoard: MH) -> Self { pub fn new(mut music_hoard: MH) -> Self {
let init_result = Self::init(&mut music_hoard); let init_result = Self::init(&mut music_hoard);
@ -57,8 +70,8 @@ impl<MH: IMusicHoard> App<MH> {
selection, selection,
}; };
match init_result { match init_result {
Ok(()) => Self::Browse(AppGenericState { inner }), Ok(()) => Self::Browse(AppBrowseState { inner }),
Err(err) => Self::Critical(AppErrorState { Err(err) => Self::Critical(AppCriticalState {
inner, inner,
string: err.to_string(), string: err.to_string(),
}), }),
@ -73,32 +86,34 @@ impl<MH: IMusicHoard> App<MH> {
fn inner_ref(&self) -> &AppInner<MH> { fn inner_ref(&self) -> &AppInner<MH> {
match self { match self {
AppState::Browse(generic) | AppState::Info(generic) | AppState::Reload(generic) => { AppState::Browse(browse) => &browse.inner,
&generic.inner AppState::Info(info) => &info.inner,
} AppState::Reload(reload) => &reload.inner,
AppState::Search(search) => &search.inner, AppState::Search(search) => &search.inner,
AppState::Error(error) | AppState::Critical(error) => &error.inner, AppState::Error(error) => &error.inner,
AppState::Critical(critical) => &critical.inner,
} }
} }
fn inner_mut(&mut self) -> &mut AppInner<MH> { fn inner_mut(&mut self) -> &mut AppInner<MH> {
match self { match self {
AppState::Browse(generic) | AppState::Info(generic) | AppState::Reload(generic) => { AppState::Browse(browse) => &mut browse.inner,
&mut generic.inner AppState::Info(info) => &mut info.inner,
} AppState::Reload(reload) => &mut reload.inner,
AppState::Search(search) => &mut search.inner, AppState::Search(search) => &mut search.inner,
AppState::Error(error) | AppState::Critical(error) => &mut error.inner, AppState::Error(error) => &mut error.inner,
AppState::Critical(critical) => &mut critical.inner,
} }
} }
} }
impl<MH: IMusicHoard> IAppInteract for App<MH> { impl<MH: IMusicHoard> IAppInteract for App<MH> {
type BS = AppGenericState<MH>; type BS = AppBrowseState<MH>;
type IS = AppGenericState<MH>; type IS = AppInfoState<MH>;
type RS = AppGenericState<MH>; type RS = AppReloadState<MH>;
type SS = AppSearchState<MH>; type SS = AppSearchState<MH>;
type ES = AppErrorState<MH>; type ES = AppErrorState<MH>;
type CS = AppErrorState<MH>; type CS = AppCriticalState<MH>;
fn is_running(&self) -> bool { fn is_running(&self) -> bool {
self.inner_ref().running self.inner_ref().running
@ -114,7 +129,7 @@ impl<MH: IMusicHoard> IAppInteract for App<MH> {
} }
} }
impl<MH: IMusicHoard> IAppInteractBrowse for AppGenericState<MH> { impl<MH: IMusicHoard> IAppInteractBrowse for AppBrowseState<MH> {
type APP = App<MH>; type APP = App<MH>;
fn save_and_quit(mut self) -> Self::APP { fn save_and_quit(mut self) -> Self::APP {
@ -155,11 +170,11 @@ impl<MH: IMusicHoard> IAppInteractBrowse for AppGenericState<MH> {
} }
fn show_info_overlay(self) -> Self::APP { fn show_info_overlay(self) -> Self::APP {
AppState::Info(self) AppState::Info(AppInfoState { inner: self.inner })
} }
fn show_reload_menu(self) -> Self::APP { fn show_reload_menu(self) -> Self::APP {
AppState::Reload(self) AppState::Reload(AppReloadState { inner: self.inner })
} }
fn begin_search(mut self) -> Self::APP { fn begin_search(mut self) -> Self::APP {
@ -180,11 +195,11 @@ impl<MH: IMusicHoard> IAppInteractBrowse for AppGenericState<MH> {
} }
} }
impl<MH: IMusicHoard> IAppInteractInfo for AppGenericState<MH> { impl<MH: IMusicHoard> IAppInteractInfo for AppInfoState<MH> {
type APP = App<MH>; type APP = App<MH>;
fn hide_info_overlay(self) -> Self::APP { fn hide_info_overlay(self) -> Self::APP {
AppState::Browse(AppGenericState { inner: self.inner }) AppState::Browse(AppBrowseState { inner: self.inner })
} }
fn no_op(self) -> Self::APP { fn no_op(self) -> Self::APP {
@ -192,7 +207,7 @@ impl<MH: IMusicHoard> IAppInteractInfo for AppGenericState<MH> {
} }
} }
impl<MH: IMusicHoard> IAppInteractReload for AppGenericState<MH> { impl<MH: IMusicHoard> IAppInteractReload for AppReloadState<MH> {
type APP = App<MH>; type APP = App<MH>;
fn reload_library(mut self) -> Self::APP { fn reload_library(mut self) -> Self::APP {
@ -214,7 +229,7 @@ impl<MH: IMusicHoard> IAppInteractReload for AppGenericState<MH> {
} }
fn hide_reload_menu(self) -> Self::APP { fn hide_reload_menu(self) -> Self::APP {
AppState::Browse(AppGenericState { inner: self.inner }) AppState::Browse(AppBrowseState { inner: self.inner })
} }
fn no_op(self) -> Self::APP { fn no_op(self) -> Self::APP {
@ -226,14 +241,14 @@ trait IAppInteractReloadPrivate<MH: IMusicHoard> {
fn refresh(self, previous: IdSelection, result: Result<(), musichoard::Error>) -> App<MH>; fn refresh(self, previous: IdSelection, result: Result<(), musichoard::Error>) -> App<MH>;
} }
impl<MH: IMusicHoard> IAppInteractReloadPrivate<MH> for AppGenericState<MH> { impl<MH: IMusicHoard> IAppInteractReloadPrivate<MH> for AppReloadState<MH> {
fn refresh(mut self, previous: IdSelection, result: Result<(), musichoard::Error>) -> App<MH> { fn refresh(mut self, previous: IdSelection, result: Result<(), musichoard::Error>) -> App<MH> {
match result { match result {
Ok(()) => { Ok(()) => {
self.inner self.inner
.selection .selection
.select_by_id(self.inner.music_hoard.get_collection(), previous); .select_by_id(self.inner.music_hoard.get_collection(), previous);
AppState::Browse(AppGenericState { inner: self.inner }) AppState::Browse(AppBrowseState { inner: self.inner })
} }
Err(err) => AppState::Error(AppErrorState { Err(err) => AppState::Error(AppErrorState {
inner: self.inner, inner: self.inner,
@ -281,12 +296,12 @@ impl<MH: IMusicHoard> IAppInteractSearch for AppSearchState<MH> {
} }
fn finish_search(self) -> Self::APP { fn finish_search(self) -> Self::APP {
AppState::Browse(AppGenericState { inner: self.inner }) AppState::Browse(AppBrowseState { inner: self.inner })
} }
fn cancel_search(mut self) -> Self::APP { fn cancel_search(mut self) -> Self::APP {
self.inner.selection.select_by_list(self.orig); self.inner.selection.select_by_list(self.orig);
AppState::Browse(AppGenericState { inner: self.inner }) AppState::Browse(AppBrowseState { inner: self.inner })
} }
fn no_op(self) -> Self::APP { fn no_op(self) -> Self::APP {
@ -298,11 +313,11 @@ impl<MH: IMusicHoard> IAppInteractError for AppErrorState<MH> {
type APP = App<MH>; type APP = App<MH>;
fn dismiss_error(self) -> Self::APP { fn dismiss_error(self) -> Self::APP {
AppState::Browse(AppGenericState { inner: self.inner }) AppState::Browse(AppBrowseState { inner: self.inner })
} }
} }
impl<MH: IMusicHoard> IAppInteractCritical for AppErrorState<MH> { impl<MH: IMusicHoard> IAppInteractCritical for AppCriticalState<MH> {
type APP = App<MH>; type APP = App<MH>;
fn no_op(self) -> Self::APP { fn no_op(self) -> Self::APP {

View File

@ -29,7 +29,6 @@ pub trait IAppInteract {
fn state(self) -> AppState<Self::BS, Self::IS, Self::RS, Self::SS, Self::ES, Self::CS>; fn state(self) -> AppState<Self::BS, Self::IS, Self::RS, Self::SS, Self::ES, Self::CS>;
} }
// FIXME: move to returning specific states - for errors, return Result
pub trait IAppInteractBrowse { pub trait IAppInteractBrowse {
type APP: IAppInteract; type APP: IAppInteract;