Reformat code
This commit is contained in:
53
src/app.rs
53
src/app.rs
@@ -1,16 +1,16 @@
|
||||
use std::any::{Any};
|
||||
use crate::event::{AppEvent, EventHandler};
|
||||
use ratatui::{DefaultTerminal, Frame};
|
||||
use std::time::Duration;
|
||||
use color_eyre::Result;
|
||||
use crossterm::event::{Event, KeyEvent};
|
||||
use crossterm::event::Event as CrosstermEvent;
|
||||
use diesel::{Connection, SqliteConnection};
|
||||
use rat_cursor::HasScreenCursor;
|
||||
use crate::config::types::ApplicationConfig;
|
||||
use crate::constants::{APP_CONFIG_DIR, APP_CONIFG_FILE_PATH, APP_DATA_DIR};
|
||||
use crate::widgets::views::{View};
|
||||
use crate::event::{AppEvent, EventHandler};
|
||||
use crate::widgets::views::MainView;
|
||||
use crate::widgets::views::View;
|
||||
use color_eyre::Result;
|
||||
use crossterm::event::Event as CrosstermEvent;
|
||||
use crossterm::event::{Event, KeyEvent};
|
||||
use diesel::{Connection, SqliteConnection};
|
||||
use rat_cursor::HasScreenCursor;
|
||||
use ratatui::{DefaultTerminal, Frame};
|
||||
use std::any::Any;
|
||||
use std::time::Duration;
|
||||
|
||||
pub(crate) struct App {
|
||||
events: EventHandler,
|
||||
@@ -25,17 +25,21 @@ struct AppState {
|
||||
|
||||
impl App {
|
||||
pub async fn create() -> Self {
|
||||
let app_conf =
|
||||
if APP_CONIFG_FILE_PATH.exists() { ApplicationConfig::from_file(&APP_CONIFG_FILE_PATH).unwrap() }
|
||||
else { ApplicationConfig::new() };
|
||||
let app_conf = if APP_CONIFG_FILE_PATH.exists() {
|
||||
ApplicationConfig::from_file(&APP_CONIFG_FILE_PATH).unwrap()
|
||||
} else {
|
||||
ApplicationConfig::new()
|
||||
};
|
||||
Self::initialize_folders();
|
||||
let db_conn = Self::establish_db_connection(app_conf.clone());
|
||||
let state = AppState { view: Some(Box::new(MainView::new(&app_conf))) };
|
||||
let state = AppState {
|
||||
view: Some(Box::new(MainView::new(&app_conf))),
|
||||
};
|
||||
Self {
|
||||
events: EventHandler::new(Duration::from_millis(app_conf.basic_config.tick_rate)),
|
||||
db_connection: db_conn,
|
||||
app_config: app_conf,
|
||||
state
|
||||
state,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,10 +63,11 @@ impl App {
|
||||
terminal.draw(|frame| self.draw(frame))?;
|
||||
let event = self.events.next().await?;
|
||||
self.update(event)?;
|
||||
if let Some(view) = self.state.view.as_mut() &&
|
||||
let Some(main_view) = view.downcast_ref::<MainView>() &&
|
||||
!main_view.is_running() {
|
||||
break Ok(())
|
||||
if let Some(view) = self.state.view.as_mut()
|
||||
&& let Some(main_view) = view.downcast_ref::<MainView>()
|
||||
&& !main_view.is_running()
|
||||
{
|
||||
break Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,7 +84,7 @@ impl App {
|
||||
}
|
||||
|
||||
fn handle_key_event(&mut self, key: &KeyEvent) -> Result<()> {
|
||||
if let Some(any) = self.state.view .as_mut() {
|
||||
if let Some(any) = self.state.view.as_mut() {
|
||||
if let Some(main_view) = any.downcast_mut::<MainView>() {
|
||||
main_view.handle_key_input(key)?;
|
||||
}
|
||||
@@ -99,11 +104,15 @@ impl App {
|
||||
fn draw(&mut self, frame: &mut Frame) {
|
||||
if let Some(view) = self.state.view.as_mut() {
|
||||
if let Some(main_view) = view.downcast_mut::<MainView>() {
|
||||
frame.render_stateful_widget(MainView::new(&self.app_config), frame.area(), &mut main_view.state);
|
||||
frame.render_stateful_widget(
|
||||
MainView::new(&self.app_config),
|
||||
frame.area(),
|
||||
&mut main_view.state,
|
||||
);
|
||||
if let Some(pos) = main_view.screen_cursor() {
|
||||
frame.set_cursor_position(pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user