Commit 9c63221f6b
9c63221f6b25c7d4a162c5c5765b4b4e91e05123
parent: 95700f4c6f
Unsigned
Siriusmart <sirius@siri.ws> · 2025-11-08 11:31 UTC
uses central local store
Layout: unified · split
src/exit.rs
+10 −3
| @@ -3,10 +3,12 @@ use std::{collections::HashSet, error::Error, fs}; |
| 3 | 3 | use tui_additions::framework::Framework; |
| 4 | 4 | |
| 5 | 5 | use crate::{ |
| 6 | | CACHED_BEFORE, config::MainConfig, global::{ |
| 6 | config::MainConfig, |
| 7 | global::{ |
| 7 | 8 | structs::*, |
| 8 | 9 | traits::{Collection, CollectionNoId}, |
| 9 | | } |
| 10 | }, |
| 11 | CACHED_BEFORE, |
| 10 | 12 | }; |
| 11 | 13 | |
| 12 | 14 | /// function to run when the app ends |
| @@ -53,11 +55,16 @@ pub fn exit(framework: &mut Framework) -> Result<(), Box<dyn Error>> { |
| 53 | 55 | let thumbnail_path = home_dir().unwrap().join(".local/share/youtube-tui/info/"); |
| 54 | 56 | |
| 55 | 57 | // remove cache that no longer exists |
| 56 | | for deleted in cached_before.iter().filter(|id| !cached_after.contains(*id)) { |
| 58 | for deleted in cached_before |
| 59 | .iter() |
| 60 | .filter(|id| !cached_after.contains(*id)) |
| 61 | { |
| 57 | 62 | let _ = fs::remove_file(info_path.join(deleted).with_extension("json")); |
| 58 | 63 | let _ = fs::remove_file(thumbnail_path.join(deleted)); |
| 59 | 64 | } |
| 60 | 65 | |
| 66 | LocalStore::save_only(&cached_after); |
| 67 | |
| 61 | 68 | // let home_dir = home_dir().unwrap(); |
| 62 | 69 | // let cache_path = home_dir.join(".cache/youtube-tui/"); |
| 63 | 70 | |
src/global/structs/localstore.rs
+37 −7
| @@ -1,15 +1,24 @@ |
| 1 | | use std::{collections::HashMap, fs, sync::OnceLock}; |
| 2 | | |
| 3 | | use home::home_dir; |
| 1 | use std::{ |
| 2 | collections::{HashMap, HashSet}, |
| 3 | env::home_dir, |
| 4 | fs::{self, OpenOptions}, |
| 5 | io::Write, |
| 6 | sync::OnceLock, |
| 7 | }; |
| 4 | 8 | |
| 5 | 9 | use crate::global::structs::Item; |
| 6 | 10 | |
| 7 | 11 | static mut LOCALSTORE: OnceLock<LocalStore> = OnceLock::new(); |
| 8 | 12 | |
| 13 | pub struct LocalRecord { |
| 14 | item: Item, |
| 15 | is_new: bool |
| 16 | } |
| 17 | |
| 9 | 18 | /// cached access and write files to ~/.local/share |
| 10 | 19 | #[derive(Default)] |
| 11 | 20 | pub struct LocalStore { |
| 12 | | info: HashMap<String, Item>, |
| 21 | info: HashMap<String, LocalRecord>, |
| 13 | 22 | } |
| 14 | 23 | |
| 15 | 24 | impl LocalStore { |
| @@ -23,7 +32,7 @@ impl LocalStore { |
| 23 | 32 | let localstore = unsafe { LOCALSTORE.get_mut() }.unwrap(); |
| 24 | 33 | |
| 25 | 34 | match localstore.info.get(id) { |
| 26 | | Some(item) => Some(item.clone()), |
| 35 | Some(LocalRecord { item, .. }) => Some(item.clone()), |
| 27 | 36 | None => { |
| 28 | 37 | let path = home_dir() |
| 29 | 38 | .unwrap() |
| @@ -38,8 +47,29 @@ impl LocalStore { |
| 38 | 47 | } |
| 39 | 48 | } |
| 40 | 49 | |
| 41 | | pub fn set_info(id: String, item: Item) { |
| 50 | pub fn set_info(id: String, item: Item, is_new: bool) { |
| 42 | 51 | let localstore = unsafe { LOCALSTORE.get_mut() }.unwrap(); |
| 43 | | localstore.info.insert(id, item); |
| 52 | localstore.info.insert(id, LocalRecord { item, is_new }); |
| 53 | } |
| 54 | |
| 55 | pub fn save_only(ids: &HashSet<String>) { |
| 56 | let info_path = home_dir().unwrap().join(".local/share/youtube-tui/info/"); |
| 57 | |
| 58 | for (id, LocalRecord { item, is_new }) in unsafe { LOCALSTORE.get() }.unwrap().info.iter() { |
| 59 | let info = info_path.join(id).with_extension("json"); |
| 60 | if *is_new && ids.contains(id) { |
| 61 | let mut file = match OpenOptions::new() |
| 62 | .write(true) |
| 63 | .create(true) |
| 64 | .truncate(true) |
| 65 | .open(info) |
| 66 | { |
| 67 | Ok(f) => f, |
| 68 | Err(_) => continue, |
| 69 | }; |
| 70 | let item_string = serde_json::to_string(&item).unwrap(); |
| 71 | let _ = file.write_all(item_string.as_bytes()); |
| 72 | } |
| 73 | } |
| 44 | 74 | } |
| 45 | 75 | } |
src/global/traits/collection.rs
+2 −6
| @@ -61,11 +61,6 @@ where |
| 61 | 61 | None => return Ok(()), // TODO make error message |
| 62 | 62 | }; |
| 63 | 63 | |
| 64 | | let info = home_dir() |
| 65 | | .unwrap() |
| 66 | | .join(".local/share/youtube-tui/info/") |
| 67 | | .join(format!("{id}.json")); |
| 68 | | |
| 69 | 64 | if let Some(index) = self |
| 70 | 65 | .items_mut() |
| 71 | 66 | .iter_mut() |
| @@ -74,7 +69,7 @@ where |
| 74 | 69 | self.items_mut().remove(index); |
| 75 | 70 | } |
| 76 | 71 | |
| 77 | | // TODO use central cache struct |
| 72 | /* TODO is this a good approach? |
| 78 | 73 | if !info.exists() { |
| 79 | 74 | let mut file = OpenOptions::new() |
| 80 | 75 | .write(true) |
| @@ -84,6 +79,7 @@ where |
| 84 | 79 | let item_string = serde_json::to_string(&item)?; |
| 85 | 80 | file.write_all(item_string.as_bytes())?; |
| 86 | 81 | } |
| 82 | */ |
| 87 | 83 | |
| 88 | 84 | self.items_mut().push(item); |
| 89 | 85 | |
src/items/singleitem.rs
+7 −8
| @@ -5,7 +5,6 @@ use crate::{ |
| 5 | 5 | config::*, |
| 6 | 6 | global::{functions::*, structs::*, traits::Collection}, |
| 7 | 7 | }; |
| 8 | | use home::home_dir; |
| 9 | 8 | use ratatui::{ |
| 10 | 9 | layout::{Constraint, Rect}, |
| 11 | 10 | style::Style, |
| @@ -743,9 +742,11 @@ impl FrameworkItem for SingleItem { |
| 743 | 742 | let mainconfig = framework.data.global.get::<MainConfig>().unwrap(); |
| 744 | 743 | // load items using the invidious api |
| 745 | 744 | // gets the item that it needs to load from `data.state.Page` |
| 745 | let mut is_new = true; |
| 746 | 746 | let (item, r#type) = match r#type { |
| 747 | 747 | SingleItemPage::Video(id) => { |
| 748 | 748 | let video = if let Some(item) = LocalStore::get_info(id) { |
| 749 | is_new = false; |
| 749 | 750 | item |
| 750 | 751 | } else { |
| 751 | 752 | load_video(id, mainconfig)? |
| @@ -760,15 +761,13 @@ impl FrameworkItem for SingleItem { |
| 760 | 761 | ) |
| 761 | 762 | } |
| 762 | 763 | SingleItemPage::Playlist(id) => { |
| 763 | | let path = home_dir() |
| 764 | | .unwrap() |
| 765 | | .join(format!(".local/share/youtube-tui/info/{id}.json")); |
| 766 | | |
| 767 | | let playlist = if path.exists() { |
| 768 | | serde_json::from_str(&fs::read_to_string(path)?)? |
| 764 | let playlist = if let Some(item) = LocalStore::get_info(id) { |
| 765 | is_new = false; |
| 766 | item |
| 769 | 767 | } else { |
| 770 | 768 | load_playlist(id, mainconfig)? |
| 771 | 769 | }; |
| 770 | |
| 772 | 771 | let r#type = SingleItemType::Playlist( |
| 773 | 772 | SinglePlaylistItem::new( |
| 774 | 773 | framework.data.global.get::<CommandsConfig>().unwrap(), |
| @@ -783,7 +782,7 @@ impl FrameworkItem for SingleItem { |
| 783 | 782 | } |
| 784 | 783 | }; |
| 785 | 784 | |
| 786 | | LocalStore::set_info(item.id().unwrap().to_string(), item.clone()); |
| 785 | LocalStore::set_info(item.id().unwrap().to_string(), item.clone(), is_new); |
| 787 | 786 | |
| 788 | 787 | self.item = Some(item); |
| 789 | 788 | self.r#type = r#type; |