Generic collection

This commit is contained in:
Wojciech Kozlowski 2023-04-13 14:30:11 +02:00
parent 14a0567fa1
commit 5edcceeae5
5 changed files with 58 additions and 41 deletions

View File

@ -61,7 +61,7 @@ fn main() {
let ui = Ui::new();
let app = App::new(Box::new(collection_manager)).expect("failed to initialise app");
let app = App::new(collection_manager).expect("failed to initialise app");
// Run the TUI application.
Tui::run(terminal, app, ui, handler, listener).expect("failed to run tui");

View File

@ -110,14 +110,14 @@ struct Selection {
artist: Option<ArtistSelection>,
}
pub struct App {
collection_manager: Box<dyn CollectionManager>,
pub struct App<CM> {
collection_manager: CM,
selection: Selection,
running: bool,
}
impl App {
pub fn new(mut collection_manager: Box<dyn CollectionManager>) -> Result<Self, Error> {
impl<CM: CollectionManager> App<CM> {
pub fn new(mut collection_manager: CM) -> Result<Self, Error> {
collection_manager.rescan_library()?;
let selection = Selection {
active: Category::Artist,
@ -516,7 +516,7 @@ mod tests {
.expect_get_collection()
.return_const(COLLECTION.to_owned());
let mut app = App::new(Box::new(collection_manager)).unwrap();
let mut app = App::new(collection_manager).unwrap();
assert!(app.is_running());
app.quit();
@ -535,7 +535,7 @@ mod tests {
.expect_get_collection()
.return_const(COLLECTION.to_owned());
let mut app = App::new(Box::new(collection_manager)).unwrap();
let mut app = App::new(collection_manager).unwrap();
assert!(app.is_running());
assert!(!app.get_artist_ids().is_empty());
@ -640,7 +640,7 @@ mod tests {
.expect_get_collection()
.return_const(collection);
let mut app = App::new(Box::new(collection_manager)).unwrap();
let mut app = App::new(collection_manager).unwrap();
assert!(app.is_running());
assert!(!app.get_artist_ids().is_empty());
@ -682,7 +682,7 @@ mod tests {
.expect_get_collection()
.return_const(collection);
let mut app = App::new(Box::new(collection_manager)).unwrap();
let mut app = App::new(collection_manager).unwrap();
assert!(app.is_running());
assert!(!app.get_artist_ids().is_empty());
@ -736,7 +736,7 @@ mod tests {
.expect_get_collection()
.return_const(collection);
let mut app = App::new(Box::new(collection_manager)).unwrap();
let mut app = App::new(collection_manager).unwrap();
assert!(app.is_running());
assert!(app.get_artist_ids().is_empty());

View File

@ -2,6 +2,7 @@ use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
#[cfg(test)]
use mockall::automock;
use musichoard::collection::CollectionManager;
use super::{
app::App,
@ -9,12 +10,12 @@ use super::{
};
#[cfg_attr(test, automock)]
pub trait EventHandler {
fn handle_next_event(&self, app: &mut App) -> Result<(), EventError>;
pub trait EventHandler<CM> {
fn handle_next_event(&self, app: &mut App<CM>) -> Result<(), EventError>;
}
trait EventHandlerPrivate {
fn handle_key_event(app: &mut App, key_event: KeyEvent);
trait EventHandlerPrivate<CM> {
fn handle_key_event(app: &mut App<CM>, key_event: KeyEvent);
}
pub struct TuiEventHandler {
@ -28,8 +29,8 @@ impl TuiEventHandler {
}
}
impl EventHandler for TuiEventHandler {
fn handle_next_event(&self, app: &mut App) -> Result<(), EventError> {
impl<CM: CollectionManager> EventHandler<CM> for TuiEventHandler {
fn handle_next_event(&self, app: &mut App<CM>) -> Result<(), EventError> {
match self.events.recv()? {
Event::Key(key_event) => Self::handle_key_event(app, key_event),
Event::Mouse(_) => {}
@ -39,8 +40,8 @@ impl EventHandler for TuiEventHandler {
}
}
impl EventHandlerPrivate for TuiEventHandler {
fn handle_key_event(app: &mut App, key_event: KeyEvent) {
impl<CM: CollectionManager> EventHandlerPrivate<CM> for TuiEventHandler {
fn handle_key_event(app: &mut App<CM>, key_event: KeyEvent) {
match key_event.code {
// Exit application on `ESC` or `q`.
KeyCode::Esc | KeyCode::Char('q') => {

View File

@ -1,9 +1,10 @@
use crossterm::event::{DisableMouseCapture, EnableMouseCapture};
use crossterm::terminal::{self, EnterAlternateScreen, LeaveAlternateScreen};
use musichoard::collection;
use musichoard::collection::{self, CollectionManager};
use ratatui::backend::Backend;
use ratatui::Terminal;
use std::io;
use std::marker::PhantomData;
pub mod app;
pub mod event;
@ -43,11 +44,12 @@ impl From<EventError> for Error {
}
}
pub struct Tui<B: Backend> {
pub struct Tui<B: Backend, CM> {
terminal: Terminal<B>,
_phantom: PhantomData<CM>,
}
impl<B: Backend> Tui<B> {
impl<B: Backend, CM: CollectionManager> Tui<B, CM> {
fn init(&mut self) -> Result<(), Error> {
self.terminal.hide_cursor()?;
self.terminal.clear()?;
@ -64,7 +66,12 @@ impl<B: Backend> Tui<B> {
self.exit();
}
fn main_loop(&mut self, mut app: App, ui: Ui, handler: impl EventHandler) -> Result<(), Error> {
fn main_loop(
&mut self,
mut app: App<CM>,
ui: Ui<CM>,
handler: impl EventHandler<CM>,
) -> Result<(), Error> {
while app.is_running() {
self.terminal.draw(|frame| ui.render(&app, frame))?;
handler.handle_next_event(&mut app)?;
@ -75,12 +82,15 @@ impl<B: Backend> Tui<B> {
fn main(
term: Terminal<B>,
app: App,
ui: Ui,
handler: impl EventHandler,
app: App<CM>,
ui: Ui<CM>,
handler: impl EventHandler<CM>,
listener: impl EventListener,
) -> Result<(), Error> {
let mut tui = Tui { terminal: term };
let mut tui = Tui {
terminal: term,
_phantom: PhantomData,
};
tui.init()?;
@ -132,9 +142,9 @@ impl<B: Backend> Tui<B> {
pub fn run(
term: Terminal<B>,
app: App,
ui: Ui,
handler: impl EventHandler,
app: App<CM>,
ui: Ui<CM>,
handler: impl EventHandler<CM>,
listener: impl EventListener,
) -> Result<(), Error> {
Self::enable()?;
@ -174,7 +184,7 @@ mod tests {
Terminal::new(backend).unwrap()
}
pub fn app(collection: Collection) -> App {
pub fn app(collection: Collection) -> App<MockCollectionManager> {
let mut collection_manager = MockCollectionManager::new();
collection_manager
@ -184,7 +194,7 @@ mod tests {
.expect_get_collection()
.return_const(collection);
App::new(Box::new(collection_manager)).unwrap()
App::new(collection_manager).unwrap()
}
fn listener() -> MockEventListener {
@ -198,7 +208,7 @@ mod tests {
listener
}
fn handler() -> MockEventHandler {
fn handler() -> MockEventHandler<MockCollectionManager> {
let mut handler = MockEventHandler::new();
handler.expect_handle_next_event().return_once(|app| {
app.quit();

View File

@ -1,4 +1,6 @@
use musichoard::TrackFormat;
use std::marker::PhantomData;
use musichoard::{collection::CollectionManager, TrackFormat};
use ratatui::{
backend::Backend,
layout::{Alignment, Rect},
@ -57,11 +59,15 @@ struct AppState<'a> {
tracks: TrackState<'a>,
}
pub struct Ui {}
pub struct Ui<CM> {
_phantom: PhantomData<CM>,
}
impl Ui {
impl<CM: CollectionManager> Ui<CM> {
pub fn new() -> Self {
Ui {}
Ui {
_phantom: PhantomData,
}
}
fn construct_areas(frame: Rect) -> FrameAreas {
@ -121,7 +127,7 @@ impl Ui {
}
}
fn construct_artist_list(app: &App) -> ArtistState {
fn construct_artist_list(app: &App<CM>) -> ArtistState {
let artists = app.get_artist_ids();
let list = List::new(
artists
@ -143,7 +149,7 @@ impl Ui {
}
}
fn construct_album_list(app: &App) -> AlbumState {
fn construct_album_list(app: &App<CM>) -> AlbumState {
let albums = app.get_album_ids();
let list = List::new(
albums
@ -176,7 +182,7 @@ impl Ui {
}
}
fn construct_track_list(app: &App) -> TrackState {
fn construct_track_list(app: &App<CM>) -> TrackState {
let tracks = app.get_track_ids();
let list = List::new(
tracks
@ -220,7 +226,7 @@ impl Ui {
}
}
fn construct_app_state(app: &App) -> AppState {
fn construct_app_state(app: &App<CM>) -> AppState {
AppState {
artists: Self::construct_artist_list(app),
albums: Self::construct_album_list(app),
@ -312,7 +318,7 @@ impl Ui {
Self::render_info_widget("Track info", state.info, state.active, area.info, frame);
}
pub fn render<B: Backend>(&self, app: &App, frame: &mut Frame<'_, B>) {
pub fn render<B: Backend>(&self, app: &App<CM>, frame: &mut Frame<'_, B>) {
let areas = Self::construct_areas(frame.size());
let app_state = Self::construct_app_state(app);