Add basic dlsite crawler
Reformat application config
This commit is contained in:
54
src/app.rs
54
src/app.rs
@@ -1,5 +1,5 @@
|
||||
use crate::config::types::ApplicationConfig;
|
||||
use crate::constants::{APP_CONFIG_DIR, APP_CONIFG_FILE_PATH, APP_DATA_DIR};
|
||||
use crate::constants::{APP_CONFIG_DIR, APP_DATA_DIR};
|
||||
use crate::event::{AppEvent, EventHandler};
|
||||
use crate::widgets::views::MainView;
|
||||
use crate::widgets::views::View;
|
||||
@@ -11,11 +11,12 @@ use rat_cursor::HasScreenCursor;
|
||||
use ratatui::{DefaultTerminal, Frame};
|
||||
use std::any::Any;
|
||||
use std::time::Duration;
|
||||
use tokio::fs;
|
||||
use crate::crawler::DLSITE_IMG_FOLDER;
|
||||
|
||||
pub(crate) struct App {
|
||||
events: EventHandler,
|
||||
db_connection: SqliteConnection,
|
||||
app_config: ApplicationConfig,
|
||||
state: AppState,
|
||||
}
|
||||
|
||||
@@ -24,36 +25,22 @@ 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()
|
||||
};
|
||||
Self::initialize_folders();
|
||||
let db_conn = Self::establish_db_connection(app_conf.clone());
|
||||
pub async fn create() -> Result<Self> {
|
||||
let config = ApplicationConfig::get_config()?;
|
||||
let db_conn = Self::establish_db_connection(&config);
|
||||
let state = AppState {
|
||||
view: Some(Box::new(MainView::new(&app_conf))),
|
||||
view: Some(Box::new(MainView::new())),
|
||||
};
|
||||
Self {
|
||||
events: EventHandler::new(Duration::from_millis(app_conf.basic_config.tick_rate)),
|
||||
let app = Self {
|
||||
events: EventHandler::new(Duration::from_millis(config.basic_config.tick_rate)),
|
||||
db_connection: db_conn,
|
||||
app_config: app_conf,
|
||||
state,
|
||||
}
|
||||
};
|
||||
Ok(app)
|
||||
}
|
||||
|
||||
fn initialize_folders() {
|
||||
if !APP_CONFIG_DIR.exists() {
|
||||
std::fs::create_dir_all(APP_CONFIG_DIR.as_path()).unwrap();
|
||||
}
|
||||
if !APP_DATA_DIR.exists() {
|
||||
std::fs::create_dir_all(APP_DATA_DIR.as_path()).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
fn establish_db_connection(application_config: ApplicationConfig) -> SqliteConnection {
|
||||
let database_url = application_config.basic_config.db_path;
|
||||
fn establish_db_connection(application_config: &ApplicationConfig) -> SqliteConnection {
|
||||
let database_url = application_config.clone().basic_config.db_path;
|
||||
SqliteConnection::establish(&database_url)
|
||||
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
|
||||
}
|
||||
@@ -105,7 +92,7 @@ impl App {
|
||||
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),
|
||||
MainView::new(),
|
||||
frame.area(),
|
||||
&mut main_view.state,
|
||||
);
|
||||
@@ -116,3 +103,16 @@ impl App {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn initialize_folders() -> Result<()> {
|
||||
if !APP_CONFIG_DIR.exists() {
|
||||
fs::create_dir_all(APP_CONFIG_DIR.as_path()).await?;
|
||||
}
|
||||
if !APP_DATA_DIR.exists() {
|
||||
fs::create_dir_all(APP_DATA_DIR.as_path()).await?;
|
||||
}
|
||||
if !DLSITE_IMG_FOLDER.exists() {
|
||||
fs::create_dir_all(DLSITE_IMG_FOLDER.as_path()).await?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user