Some refactoring

This commit is contained in:
Wojciech Kozlowski 2024-08-30 14:06:15 +02:00
parent f24737d5fa
commit fbe6c23013
5 changed files with 51 additions and 31 deletions

View File

@ -257,8 +257,11 @@ mod tests {
let public_matches = public.state.unwrap_matches(); let public_matches = public.state.unwrap_matches();
assert_eq!(public_matches.matching, Some(&album_1)); assert_eq!(public_matches.matches.as_ref().unwrap().matching, &album_1);
assert_eq!(public_matches.matches, Some(matches_1.as_slice())); assert_eq!(
public_matches.matches.as_ref().unwrap().list,
matches_1.as_slice()
);
let mut app = app.unwrap_matches().select(); let mut app = app.unwrap_matches().select();
@ -267,8 +270,11 @@ mod tests {
let public_matches = public.state.unwrap_matches(); let public_matches = public.state.unwrap_matches();
assert_eq!(public_matches.matching, Some(&album_4)); assert_eq!(public_matches.matches.as_ref().unwrap().matching, &album_4);
assert_eq!(public_matches.matches, Some(matches_4.as_slice())); assert_eq!(
public_matches.matches.as_ref().unwrap().list,
matches_4.as_slice()
);
let app = app.unwrap_matches().select(); let app = app.unwrap_matches().select();
app.unwrap_browse(); app.unwrap_browse();

View File

@ -5,7 +5,8 @@ use musichoard::collection::album::Album;
use crate::tui::{ use crate::tui::{
app::{ app::{
machine::{App, AppInner, AppMachine}, machine::{App, AppInner, AppMachine},
AppPublic, AppPublicMatches, AppState, IAppInteractMatches, WidgetState, AppPublic, AppPublicAlbumMatches, AppPublicMatches, AppState, IAppInteractMatches,
WidgetState,
}, },
lib::interface::musicbrainz::Match, lib::interface::musicbrainz::Match,
}; };
@ -52,18 +53,14 @@ impl From<AppMachine<AppMatches>> for App {
impl<'a> From<&'a mut AppMachine<AppMatches>> for AppPublic<'a> { impl<'a> From<&'a mut AppMachine<AppMatches>> for AppPublic<'a> {
fn from(machine: &'a mut AppMachine<AppMatches>) -> Self { fn from(machine: &'a mut AppMachine<AppMatches>) -> Self {
let (matching, matches) = match machine.state.index { let matches = machine.state.index.map(|index| AppPublicAlbumMatches {
Some(index) => ( matching: &machine.state.matches_info_vec[index].matching,
Some(&machine.state.matches_info_vec[index].matching), list: machine.state.matches_info_vec[index].matches.as_slice(),
Some(machine.state.matches_info_vec[index].matches.as_slice()), });
),
None => (None, None),
};
AppPublic { AppPublic {
inner: (&mut machine.inner).into(), inner: (&mut machine.inner).into(),
state: AppState::Matches(AppPublicMatches { state: AppState::Matches(AppPublicMatches {
matching,
matches, matches,
state: &mut machine.state.state, state: &mut machine.state.state,
}), }),
@ -199,7 +196,6 @@ mod tests {
let public = app.get(); let public = app.get();
let public_matches = public.state.unwrap_matches(); let public_matches = public.state.unwrap_matches();
assert_eq!(public_matches.matching, None);
assert_eq!(public_matches.matches, None); assert_eq!(public_matches.matches, None);
assert_eq!(public_matches.state, &widget_state); assert_eq!(public_matches.state, &widget_state);
} }
@ -220,10 +216,13 @@ mod tests {
let public = app.get(); let public = app.get();
let public_matches = public.state.unwrap_matches(); let public_matches = public.state.unwrap_matches();
assert_eq!(public_matches.matching, Some(&matches_info_vec[0].matching));
assert_eq!( assert_eq!(
public_matches.matches, public_matches.matches.as_ref().unwrap().matching,
Some(matches_info_vec[0].matches.as_slice()) &matches_info_vec[0].matching
);
assert_eq!(
public_matches.matches.as_ref().unwrap().list,
matches_info_vec[0].matches.as_slice()
); );
assert_eq!(public_matches.state, &widget_state); assert_eq!(public_matches.state, &widget_state);
} }

View File

@ -129,9 +129,14 @@ pub struct AppPublicInner<'app> {
pub selection: &'app mut Selection, pub selection: &'app mut Selection,
} }
#[derive(Debug, PartialEq, Eq)]
pub struct AppPublicAlbumMatches<'app> {
pub matching: &'app Album,
pub list: &'app [Match<Album>],
}
pub struct AppPublicMatches<'app> { pub struct AppPublicMatches<'app> {
pub matching: Option<&'app Album>, pub matches: Option<AppPublicAlbumMatches<'app>>,
pub matches: Option<&'app [Match<Album>]>,
pub state: &'app mut WidgetState, pub state: &'app mut WidgetState,
} }

View File

@ -59,7 +59,9 @@ impl Minibuffer<'_> {
}, },
AppState::Matches(public) => Minibuffer { AppState::Matches(public) => Minibuffer {
paragraphs: vec![ paragraphs: vec![
Paragraph::new(UiDisplay::display_matching_info(public.matching)), Paragraph::new(UiDisplay::display_matching_info(
public.matches.as_ref().map(|m| m.matching),
)),
Paragraph::new("q: abort"), Paragraph::new("q: abort"),
], ],
columns: 2, columns: 2,

View File

@ -15,7 +15,6 @@ use musichoard::collection::{album::Album, Collection};
use crate::tui::{ use crate::tui::{
app::{AppPublicState, AppState, Category, IAppAccess, Selection, WidgetState}, app::{AppPublicState, AppState, Category, IAppAccess, Selection, WidgetState},
lib::interface::musicbrainz::Match,
ui::{ ui::{
browse::{ browse::{
AlbumArea, AlbumState, ArtistArea, ArtistState, FrameArea, TrackArea, TrackState, AlbumArea, AlbumState, ArtistArea, ArtistState, FrameArea, TrackArea, TrackState,
@ -31,6 +30,8 @@ use crate::tui::{
}, },
}; };
use super::app::AppPublicAlbumMatches;
pub trait IUi { pub trait IUi {
fn render<APP: IAppAccess>(app: &mut APP, frame: &mut Frame); fn render<APP: IAppAccess>(app: &mut APP, frame: &mut Frame);
} }
@ -133,14 +134,17 @@ impl Ui {
} }
fn render_matches_overlay( fn render_matches_overlay(
matching: Option<&Album>, matches: Option<AppPublicAlbumMatches>,
matches: Option<&[Match<Album>]>,
state: &mut WidgetState, state: &mut WidgetState,
frame: &mut Frame, frame: &mut Frame,
) { ) {
let area = OverlayBuilder::default().build(frame.size()); let area = OverlayBuilder::default().build(frame.size());
let (matching, list) = match matches {
Some(m) => (Some(m.matching), Some(m.list)),
None => (None, None),
};
let matching_string = UiDisplay::display_matching_info(matching); let matching_string = UiDisplay::display_matching_info(matching);
let st = AlbumMatchesState::new(matches.unwrap_or_default(), state); let st = AlbumMatchesState::new(list.unwrap_or_default(), state);
UiWidget::render_overlay_list_widget(&matching_string, st.list, st.state, true, area, frame) UiWidget::render_overlay_list_widget(&matching_string, st.list, st.state, true, area, frame)
} }
@ -167,7 +171,7 @@ impl IUi for Ui {
match state { match state {
AppState::Info(_) => Self::render_info_overlay(collection, selection, frame), AppState::Info(_) => Self::render_info_overlay(collection, selection, frame),
AppState::Matches(public) => { AppState::Matches(public) => {
Self::render_matches_overlay(public.matching, public.matches, public.state, frame) Self::render_matches_overlay(public.matches, public.state, frame)
} }
AppState::Reload(_) => Self::render_reload_overlay(frame), AppState::Reload(_) => Self::render_reload_overlay(frame),
AppState::Error(msg) => Self::render_error_overlay("Error", msg, frame), AppState::Error(msg) => Self::render_error_overlay("Error", msg, frame),
@ -185,7 +189,8 @@ mod tests {
}; };
use crate::tui::{ use crate::tui::{
app::{AppPublic, AppPublicInner, AppPublicMatches, Delta}, app::{AppPublic, AppPublicAlbumMatches, AppPublicInner, AppPublicMatches, Delta},
lib::interface::musicbrainz::Match,
testmod::COLLECTION, testmod::COLLECTION,
tests::terminal, tests::terminal,
}; };
@ -206,8 +211,10 @@ mod tests {
AppState::Reload(()) => AppState::Reload(()), AppState::Reload(()) => AppState::Reload(()),
AppState::Search(s) => AppState::Search(s), AppState::Search(s) => AppState::Search(s),
AppState::Matches(ref mut m) => AppState::Matches(AppPublicMatches { AppState::Matches(ref mut m) => AppState::Matches(AppPublicMatches {
matching: m.matching, matches: m.matches.as_ref().map(|matches| AppPublicAlbumMatches {
matches: m.matches, matching: matches.matching,
list: matches.list,
}),
state: m.state, state: m.state,
}), }),
AppState::Error(s) => AppState::Error(s), AppState::Error(s) => AppState::Error(s),
@ -256,8 +263,10 @@ mod tests {
widget_state.list.select(Some(0)); widget_state.list.select(Some(0));
app.state = AppState::Matches(AppPublicMatches { app.state = AppState::Matches(AppPublicMatches {
matching: Some(&album), matches: Some(AppPublicAlbumMatches {
matches: Some(&album_matches), matching: &album,
list: &album_matches,
}),
state: &mut widget_state, state: &mut widget_state,
}); });
terminal.draw(|frame| Ui::render(&mut app, frame)).unwrap(); terminal.draw(|frame| Ui::render(&mut app, frame)).unwrap();
@ -265,7 +274,6 @@ mod tests {
let mut widget_state = WidgetState::default(); let mut widget_state = WidgetState::default();
app.state = AppState::Matches(AppPublicMatches { app.state = AppState::Matches(AppPublicMatches {
matching: None,
matches: None, matches: None,
state: &mut widget_state, state: &mut widget_state,
}); });