use std::sync::{Arc, RwLock};
use color_eyre::eyre;
use kimun_core::{NoteVault, VaultConfig};
use crate::{
app_screen::{AppScreen, start::StartScreen},
settings::{AppSettings, SharedSettings},
};
pub struct App {
pub current_screen: Option<Box<dyn AppScreen>>,
pub settings: SharedSettings,
pub vault: Option<Arc<NoteVault>>,
}
impl App {
pub async fn new(config_path: Option<std::path::PathBuf>) -> eyre::Result<Self> {
let loaded_settings = match config_path {
Some(path) => AppSettings::load_from_file(path)?,
None => AppSettings::load_from_disk()?,
};
let settings: SharedSettings = Arc::new(RwLock::new(loaded_settings));
let vault = {
let (workspace_path, cache_path, inbox) = {
let s = settings.read().unwrap();
let path = s.resolve_workspace_path();
let name = s
.workspace_config
.as_ref()
.map(|wc| wc.global.current_workspace.clone())
.filter(|n| !n.is_empty());
let cache = name.as_ref().map(|n| s.cache_path_for(n));
let inbox = s
.workspace_config
.as_ref()
.and_then(|wc| wc.get_current_workspace())
.map(|entry| entry.effective_inbox_path());
(path, cache, inbox)
};
if let Some(workspace) = workspace_path {
let mut config = VaultConfig::new(&workspace);
if let Some(cp) = cache_path {
config = config.with_db_path(cp);
}
NoteVault::new(config).await.ok().map(|mut v| {
if let Some(inbox) = inbox {
v.set_inbox_path(kimun_core::nfs::VaultPath::new(inbox));
}
Arc::new(v)
})
} else {
None
}
};
Ok(Self {
current_screen: Some(Box::new(StartScreen::new(settings.clone(), vault.clone()))),
settings,
vault,
})
}
}