use log::{debug, warn};
use self::actions::Actions;
use self::state::AppState;
use crate::app::actions::Action;
use crate::inputs::key::Key;
use crate::inputs::Resource;
pub mod actions;
pub mod state;
pub mod ui;
#[derive(Debug, PartialEq, Eq)]
pub enum AppReturn {
Exit,
Continue,
}
pub struct App {
actions: Actions,
state: AppState,
}
impl App {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
let actions = vec![Action::Quit].into();
let state = AppState::initialized();
Self { actions, state }
}
pub fn do_action(&mut self, key: Key) -> AppReturn {
if let Some(action) = self.actions.find(key) {
debug!("Run action [{:?}]", action);
match action {
Action::Quit => AppReturn::Exit,
}
} else {
warn!("No action accociated to {}", key);
AppReturn::Continue
}
}
pub fn update_on_tick(&mut self) -> AppReturn {
self.state.incr_tick();
AppReturn::Continue
}
pub fn on_fetch_resource(&mut self, resource: Resource) -> AppReturn {
self.state.handle_fetch_resource(resource);
AppReturn::Continue
}
pub fn update_on_new_block_found(&mut self, block_height: u64) -> AppReturn {
self.state.handle_new_block_found(block_height);
AppReturn::Continue
}
pub fn actions(&self) -> &Actions {
&self.actions
}
pub fn state(&self) -> &AppState {
&self.state
}
}