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