#[cfg(not(any(target_os = "android", target_os = "ios")))] use anyhow::Context; #[cfg(target_os = "android")] use dioxus::core::bail; use dioxus::logger::tracing::info; use crate::{ appstate::{settings::Settings, state::State, storage::Storage}, components::settings::SettingsTab, }; use std::{ collections::HashMap, fs::{self, File}, path::PathBuf, }; #[derive(PartialEq, Clone, Debug)] pub struct App { pub settings: Settings, pub storage: Storage, pub state: State, } impl App { pub fn load() -> anyhow::Result { let (storage_path, settings_path) = Self::get_paths()?; let storage = if !storage_path.exists() { Storage::default() } else { let storage_file = File::open(storage_path)?; // if (storage_file.read) let storage = Storage::load(&storage_file)?; info!("Loaded storage file from storage"); storage }; let settings = if !settings_path.exists() { Settings::default() } else { let settings_file = File::open(settings_path)?; let settings = Settings::load(&settings_file)?; info!("Loaded settings file from storage"); settings }; Ok(App { settings, storage, state: State { selected_chat: None, models: HashMap::new(), settings_shown: false, selected_settings: SettingsTab::default(), }, }) } pub fn save(&self) -> anyhow::Result<()> { info!("Saving data..."); let (storage_path, settings_path) = Self::get_paths()?; let mut storage_file = File::create(storage_path)?; let mut settings_file = File::create(settings_path)?; self.storage.save(&mut storage_file)?; self.settings.save(&mut settings_file)?; Ok(()) } fn get_paths() -> anyhow::Result<(PathBuf, PathBuf)> { #[cfg(not(any(target_os = "android", target_os = "ios")))] let mut data_dir = dirs::data_dir().context("Couldn't find this system's data dir!")?; let mut config_dir = dirs::config_dir().context("Couldn't find this system's config dir!")?; #[cfg(target_os = "android")] bail!("Unable to handle this OS.. for now"); data_dir.push("llm-river"); config_dir.push("llm-river"); fs::create_dir_all(&data_dir)?; fs::create_dir_all(&config_dir)?; let storage_path = { let mut settings_file = data_dir.clone(); settings_file.push("storage.json"); settings_file }; let settings_path = { let mut settings_file = config_dir.clone(); settings_file.push("settings.toml"); settings_file }; Ok((storage_path, settings_path)) } }