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};
33use tui_additions::framework::Framework;
44
55use crate::{
6 CACHED_BEFORE, config::MainConfig, global::{
6 config::MainConfig,
7 global::{
78 structs::*,
89 traits::{Collection, CollectionNoId},
9 }
10 },
11 CACHED_BEFORE,
1012};
1113
1214/// function to run when the app ends
@@ -53,11 +55,16 @@ pub fn exit(framework: &mut Framework) -> Result<(), Box<dyn Error>> {
5355 let thumbnail_path = home_dir().unwrap().join(".local/share/youtube-tui/info/");
5456
5557 // 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 {
5762 let _ = fs::remove_file(info_path.join(deleted).with_extension("json"));
5863 let _ = fs::remove_file(thumbnail_path.join(deleted));
5964 }
6065
66 LocalStore::save_only(&cached_after);
67
6168 // let home_dir = home_dir().unwrap();
6269 // let cache_path = home_dir.join(".cache/youtube-tui/");
6370
src/global/structs/localstore.rs +37 −7
@@ -1,15 +1,24 @@
1use std::{collections::HashMap, fs, sync::OnceLock};
2
3use home::home_dir;
1use std::{
2 collections::{HashMap, HashSet},
3 env::home_dir,
4 fs::{self, OpenOptions},
5 io::Write,
6 sync::OnceLock,
7};
48
59use crate::global::structs::Item;
610
711static mut LOCALSTORE: OnceLock<LocalStore> = OnceLock::new();
812
13pub struct LocalRecord {
14 item: Item,
15 is_new: bool
16}
17
918/// cached access and write files to ~/.local/share
1019#[derive(Default)]
1120pub struct LocalStore {
12 info: HashMap<String, Item>,
21 info: HashMap<String, LocalRecord>,
1322}
1423
1524impl LocalStore {
@@ -23,7 +32,7 @@ impl LocalStore {
2332 let localstore = unsafe { LOCALSTORE.get_mut() }.unwrap();
2433
2534 match localstore.info.get(id) {
26 Some(item) => Some(item.clone()),
35 Some(LocalRecord { item, .. }) => Some(item.clone()),
2736 None => {
2837 let path = home_dir()
2938 .unwrap()
@@ -38,8 +47,29 @@ impl LocalStore {
3847 }
3948 }
4049
41 pub fn set_info(id: String, item: Item) {
50 pub fn set_info(id: String, item: Item, is_new: bool) {
4251 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 }
4474 }
4575}
src/global/traits/collection.rs +2 −6
@@ -61,11 +61,6 @@ where
6161 None => return Ok(()), // TODO make error message
6262 };
6363
64 let info = home_dir()
65 .unwrap()
66 .join(".local/share/youtube-tui/info/")
67 .join(format!("{id}.json"));
68
6964 if let Some(index) = self
7065 .items_mut()
7166 .iter_mut()
@@ -74,7 +69,7 @@ where
7469 self.items_mut().remove(index);
7570 }
7671
77 // TODO use central cache struct
72 /* TODO is this a good approach?
7873 if !info.exists() {
7974 let mut file = OpenOptions::new()
8075 .write(true)
@@ -84,6 +79,7 @@ where
8479 let item_string = serde_json::to_string(&item)?;
8580 file.write_all(item_string.as_bytes())?;
8681 }
82 */
8783
8884 self.items_mut().push(item);
8985
src/items/singleitem.rs +7 −8
@@ -5,7 +5,6 @@ use crate::{
55 config::*,
66 global::{functions::*, structs::*, traits::Collection},
77};
8use home::home_dir;
98use ratatui::{
109 layout::{Constraint, Rect},
1110 style::Style,
@@ -743,9 +742,11 @@ impl FrameworkItem for SingleItem {
743742 let mainconfig = framework.data.global.get::<MainConfig>().unwrap();
744743 // load items using the invidious api
745744 // gets the item that it needs to load from `data.state.Page`
745 let mut is_new = true;
746746 let (item, r#type) = match r#type {
747747 SingleItemPage::Video(id) => {
748748 let video = if let Some(item) = LocalStore::get_info(id) {
749 is_new = false;
749750 item
750751 } else {
751752 load_video(id, mainconfig)?
@@ -760,15 +761,13 @@ impl FrameworkItem for SingleItem {
760761 )
761762 }
762763 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
769767 } else {
770768 load_playlist(id, mainconfig)?
771769 };
770
772771 let r#type = SingleItemType::Playlist(
773772 SinglePlaylistItem::new(
774773 framework.data.global.get::<CommandsConfig>().unwrap(),
@@ -783,7 +782,7 @@ impl FrameworkItem for SingleItem {
783782 }
784783 };
785784
786 LocalStore::set_info(item.id().unwrap().to_string(), item.clone());
785 LocalStore::set_info(item.id().unwrap().to_string(), item.clone(), is_new);
787786
788787 self.item = Some(item);
789788 self.r#type = r#type;