Commit 5d1367deaf

5d1367deafdcad3f60b9a7661f5593c17e7a949a

parent: 89296f2325

Unsigned

Vaibhav Mattoo <vaibhavmattoo1@gmail.com> · 2025-08-16 23:12 UTC

feature: added channel caching

Layout: unified · split

src/global/functions/singleitem_load.rs +31 −1
@@ -2,7 +2,8 @@ use crate::{
22 config::MainConfig,
33 global::{functions::download_all_images, structs::Item, traits::SearchProviderWrapper},
44};
5use std::error::Error;
5use std::{error::Error, fs};
6use home::home_dir;
67
78pub fn load_playlist(id: &str, mainconfig: &MainConfig) -> Result<Item, Box<dyn Error>> {
89 let playlist =
@@ -28,3 +29,32 @@ pub fn load_video(id: &str, mainconfig: &MainConfig) -> Result<Item, Box<dyn Err
2829
2930 Ok(video)
3031}
32
33pub fn load_channel(id: &str, mainconfig: &MainConfig) -> Result<Item, Box<dyn Error>> {
34 let home_dir = home_dir().unwrap();
35 let cache_path = home_dir.join(format!(".cache/youtube-tui/channels/{id}.json"));
36
37 if cache_path.exists() {
38 if let Ok(content) = fs::read_to_string(&cache_path) {
39 if let Ok(item) = serde_json::from_str::<Item>(&content) {
40 return Ok(item);
41 }
42 }
43 }
44
45 let channel = Item::from_full_channel(
46 SearchProviderWrapper::channel(id)?,
47 mainconfig.image_index,
48 );
49
50 if let Some(parent) = cache_path.parent() {
51 fs::create_dir_all(parent)?;
52 }
53 fs::write(&cache_path, serde_json::to_string(&channel)?)?;
54
55 if mainconfig.images.display() {
56 download_all_images(vec![(&channel).into()]);
57 }
58
59 Ok(channel)
60}
src/global/structs/history.rs +23
@@ -73,3 +73,26 @@ impl CollectionNoId<String> for CommandHistory {
7373 Self(items)
7474 }
7575}
76
77#[derive(Clone, Default, Serialize, Deserialize)]
78pub struct ChannelHistory(pub Vec<String>);
79
80impl Key for ChannelHistory {
81 type Value = Self;
82}
83
84impl CollectionNoId<String> for ChannelHistory {
85 const INDEX_PATH: &'static str = ".local/share/youtube-tui/channel_history.json";
86
87 fn items(&self) -> &Vec<String> {
88 &self.0
89 }
90
91 fn items_mut(&mut self) -> &mut Vec<String> {
92 &mut self.0
93 }
94
95 fn from_items(items: Vec<String>) -> Self {
96 Self(items)
97 }
98}
src/init.rs +5
@@ -35,6 +35,7 @@ pub fn init(
3535 [
3636 ".cache/youtube-tui/thumbnails/",
3737 ".cache/youtube-tui/info/",
38 ".cache/youtube-tui/channels/",
3839 ".local/share/youtube-tui/thumbnails/",
3940 ".local/share/youtube-tui/info/",
4041 ".local/share/youtube-tui/saved/",
@@ -63,6 +64,10 @@ pub fn init(
6364 .data
6465 .global
6566 .insert::<WatchHistory>(WatchHistory::load());
67 framework
68 .data
69 .global
70 .insert::<ChannelHistory>(ChannelHistory::load());
6671 framework
6772 .data
6873 .global
src/items/channeldisplay.rs +14 −9
@@ -1,7 +1,7 @@
11use super::ItemInfo;
22use crate::{
33 config::*,
4 global::{functions::*, structs::*, traits::SearchProviderWrapper},
4 global::{functions::*, structs::*, traits::{SearchProviderWrapper, CollectionNoId}},
55};
66use ratatui::{
77 layout::{Constraint, Rect},
@@ -598,16 +598,12 @@ impl FrameworkItem for ChannelDisplay {
598598 let mainconfig = framework.data.global.get::<MainConfig>().unwrap();
599599 let appearance = framework.data.global.get::<AppearanceConfig>().unwrap();
600600 let page = framework.data.state.get::<Page>().unwrap().channeldisplay();
601 let page_id = page.id.clone();
602 let is_main_page = matches!(page.r#type, ChannelDisplayPageType::Main);
601603
602604 match page.r#type {
603605 ChannelDisplayPageType::Main => {
604 let channel = Item::from_full_channel(
605 SearchProviderWrapper::channel(&page.id)?,
606 mainconfig.image_index,
607 );
608 if mainconfig.images.display() {
609 download_all_images(vec![(&channel).into()]);
610 }
606 let channel = load_channel(&page.id, mainconfig)?;
611607 let commands = framework
612608 .data
613609 .global
@@ -615,6 +611,7 @@ impl FrameworkItem for ChannelDisplay {
615611 .unwrap()
616612 .channel
617613 .clone();
614
618615 *self = Self::Main {
619616 iteminfo: Box::new(ItemInfo::new(Some(channel.clone()))),
620617 channel: Box::new(channel),
@@ -625,7 +622,7 @@ impl FrameworkItem for ChannelDisplay {
625622 .border_type(appearance.borders),
626623 textlist: Self::new_textlist_with_map(commands.clone()),
627624 commands,
628 }
625 };
629626 }
630627 ChannelDisplayPageType::Videos => {
631628 let videos = SearchProviderWrapper::channel_videos(&page.id)?
@@ -687,6 +684,14 @@ impl FrameworkItem for ChannelDisplay {
687684 &mut framework.data.state.get_mut::<StateEnvs>().unwrap().0,
688685 );
689686
687 if is_main_page {
688 let channel_history = framework.data.global.get_mut::<ChannelHistory>().unwrap();
689 if !channel_history.0.contains(&page_id) {
690 channel_history.0.push(page_id);
691 let _ = channel_history.save();
692 }
693 }
694
690695 Ok(())
691696 }
692697