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::{ |
| 2 | 2 | config::MainConfig, |
| 3 | 3 | global::{functions::download_all_images, structs::Item, traits::SearchProviderWrapper}, |
| 4 | 4 | }; |
| 5 | | use std::error::Error; |
| 5 | use std::{error::Error, fs}; |
| 6 | use home::home_dir; |
| 6 | 7 | |
| 7 | 8 | pub fn load_playlist(id: &str, mainconfig: &MainConfig) -> Result<Item, Box<dyn Error>> { |
| 8 | 9 | let playlist = |
| @@ -28,3 +29,32 @@ pub fn load_video(id: &str, mainconfig: &MainConfig) -> Result<Item, Box<dyn Err |
| 28 | 29 | |
| 29 | 30 | Ok(video) |
| 30 | 31 | } |
| 32 | |
| 33 | pub 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 { |
| 73 | 73 | Self(items) |
| 74 | 74 | } |
| 75 | 75 | } |
| 76 | |
| 77 | #[derive(Clone, Default, Serialize, Deserialize)] |
| 78 | pub struct ChannelHistory(pub Vec<String>); |
| 79 | |
| 80 | impl Key for ChannelHistory { |
| 81 | type Value = Self; |
| 82 | } |
| 83 | |
| 84 | impl 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( |
| 35 | 35 | [ |
| 36 | 36 | ".cache/youtube-tui/thumbnails/", |
| 37 | 37 | ".cache/youtube-tui/info/", |
| 38 | ".cache/youtube-tui/channels/", |
| 38 | 39 | ".local/share/youtube-tui/thumbnails/", |
| 39 | 40 | ".local/share/youtube-tui/info/", |
| 40 | 41 | ".local/share/youtube-tui/saved/", |
| @@ -63,6 +64,10 @@ pub fn init( |
| 63 | 64 | .data |
| 64 | 65 | .global |
| 65 | 66 | .insert::<WatchHistory>(WatchHistory::load()); |
| 67 | framework |
| 68 | .data |
| 69 | .global |
| 70 | .insert::<ChannelHistory>(ChannelHistory::load()); |
| 66 | 71 | framework |
| 67 | 72 | .data |
| 68 | 73 | .global |
src/items/channeldisplay.rs
+14 −9
| @@ -1,7 +1,7 @@ |
| 1 | 1 | use super::ItemInfo; |
| 2 | 2 | use crate::{ |
| 3 | 3 | config::*, |
| 4 | | global::{functions::*, structs::*, traits::SearchProviderWrapper}, |
| 4 | global::{functions::*, structs::*, traits::{SearchProviderWrapper, CollectionNoId}}, |
| 5 | 5 | }; |
| 6 | 6 | use ratatui::{ |
| 7 | 7 | layout::{Constraint, Rect}, |
| @@ -598,16 +598,12 @@ impl FrameworkItem for ChannelDisplay { |
| 598 | 598 | let mainconfig = framework.data.global.get::<MainConfig>().unwrap(); |
| 599 | 599 | let appearance = framework.data.global.get::<AppearanceConfig>().unwrap(); |
| 600 | 600 | 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); |
| 601 | 603 | |
| 602 | 604 | match page.r#type { |
| 603 | 605 | 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)?; |
| 611 | 607 | let commands = framework |
| 612 | 608 | .data |
| 613 | 609 | .global |
| @@ -615,6 +611,7 @@ impl FrameworkItem for ChannelDisplay { |
| 615 | 611 | .unwrap() |
| 616 | 612 | .channel |
| 617 | 613 | .clone(); |
| 614 | |
| 618 | 615 | *self = Self::Main { |
| 619 | 616 | iteminfo: Box::new(ItemInfo::new(Some(channel.clone()))), |
| 620 | 617 | channel: Box::new(channel), |
| @@ -625,7 +622,7 @@ impl FrameworkItem for ChannelDisplay { |
| 625 | 622 | .border_type(appearance.borders), |
| 626 | 623 | textlist: Self::new_textlist_with_map(commands.clone()), |
| 627 | 624 | commands, |
| 628 | | } |
| 625 | }; |
| 629 | 626 | } |
| 630 | 627 | ChannelDisplayPageType::Videos => { |
| 631 | 628 | let videos = SearchProviderWrapper::channel_videos(&page.id)? |
| @@ -687,6 +684,14 @@ impl FrameworkItem for ChannelDisplay { |
| 687 | 684 | &mut framework.data.state.get_mut::<StateEnvs>().unwrap().0, |
| 688 | 685 | ); |
| 689 | 686 | |
| 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 | |
| 690 | 695 | Ok(()) |
| 691 | 696 | } |
| 692 | 697 | |