// currently 3 copies of the same item is stored in ram // - in localstore // - in watch history // - in the actual state // // this needs to change use std::{ collections::{HashMap, HashSet}, fs::{self, OpenOptions}, io::Write, sync::OnceLock, }; use crate::global::{functions::paths, structs::Item}; static mut LOCALSTORE: OnceLock = OnceLock::new(); pub struct LocalRecord { item: Item, is_new: bool, } /// cached access and write files to ~/.local/share #[derive(Default)] pub struct LocalStore { info: HashMap, downloaded_images: HashSet, } impl LocalStore { pub fn add_image(id: String) { unsafe { LOCALSTORE.get_mut() } .unwrap() .downloaded_images .insert(id); } pub fn init() { unsafe { let _ = LOCALSTORE.set(Self::default()); } } pub fn rm_cache(id: &str) -> bool { let res = unsafe { LOCALSTORE.get_mut() }.unwrap().info.remove(id); unsafe { LOCALSTORE.get_mut() } .unwrap() .downloaded_images .remove(id); let data = paths::data_dir(); let info_path = data.join("info"); let thumbnail_path = data.join("thumbnails"); let _ = fs::remove_file(info_path.join(id).with_extension("json")); let _ = fs::remove_file(thumbnail_path.join(id)); res.is_some() } pub fn get_info(id: &str) -> Option { let localstore = unsafe { LOCALSTORE.get_mut() }.unwrap(); match localstore.info.get(id) { Some(LocalRecord { item, .. }) => Some(item.clone()), None => { let path = paths::data_dir() .join(format!("info/{id}.json")); if path.exists() { serde_json::from_str(&fs::read_to_string(path).ok()?).ok()? } else { None } } } } pub fn set_info(id: String, item: Item, is_new: bool) { let localstore = unsafe { LOCALSTORE.get_mut() }.unwrap(); localstore.info.insert(id, LocalRecord { item, is_new }); } pub fn save_only(ids: &HashSet) { let info_path = paths::data_dir().join("info"); for (id, LocalRecord { item, is_new }) in unsafe { LOCALSTORE.get() }.unwrap().info.iter() { let info = info_path.join(id).with_extension("json"); if *is_new && ids.contains(id) { let mut file = match OpenOptions::new() .write(true) .create(true) .truncate(true) .open(info) { Ok(f) => f, Err(_) => continue, }; let item_string = serde_json::to_string(&item).unwrap(); let _ = file.write_all(item_string.as_bytes()); } } } pub fn list_downloaded_images() -> &'static HashSet { &unsafe { LOCALSTORE.get() }.unwrap().downloaded_images } }