Commit 5d1367deaf
Unsigned
Layout: unified · split
src/global/functions/singleitem_load.rs +31 −1
| @@ -2,7 +2,8 @@ use crate::{ | |||
| 2 | config::MainConfig, | 2 | config::MainConfig, |
| 3 | global::{functions::download_all_images, structs::Item, traits::SearchProviderWrapper}, | 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 | pub fn load_playlist(id: &str, mainconfig: &MainConfig) -> Result<Item, Box<dyn Error>> { | 8 | pub fn load_playlist(id: &str, mainconfig: &MainConfig) -> Result<Item, Box<dyn Error>> { |
| 8 | let playlist = | 9 | let playlist = |
| @@ -28,3 +29,32 @@ pub fn load_video(id: &str, mainconfig: &MainConfig) -> Result<Item, Box<dyn Err | |||
| 28 | 29 | ||
| 29 | Ok(video) | 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 | Self(items) | 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 | ".cache/youtube-tui/thumbnails/", | 36 | ".cache/youtube-tui/thumbnails/", |
| 37 | ".cache/youtube-tui/info/", | 37 | ".cache/youtube-tui/info/", |
| 38 | ".cache/youtube-tui/channels/", | ||
| 38 | ".local/share/youtube-tui/thumbnails/", | 39 | ".local/share/youtube-tui/thumbnails/", |
| 39 | ".local/share/youtube-tui/info/", | 40 | ".local/share/youtube-tui/info/", |
| 40 | ".local/share/youtube-tui/saved/", | 41 | ".local/share/youtube-tui/saved/", |
| @@ -63,6 +64,10 @@ pub fn init( | |||
| 63 | .data | 64 | .data |
| 64 | .global | 65 | .global |
| 65 | .insert::<WatchHistory>(WatchHistory::load()); | 66 | .insert::<WatchHistory>(WatchHistory::load()); |
| 67 | framework | ||
| 68 | .data | ||
| 69 | .global | ||
| 70 | .insert::<ChannelHistory>(ChannelHistory::load()); | ||
| 66 | framework | 71 | framework |
| 67 | .data | 72 | .data |
| 68 | .global | 73 | .global |
src/items/channeldisplay.rs +14 −9
| @@ -1,7 +1,7 @@ | |||
| 1 | use super::ItemInfo; | 1 | use super::ItemInfo; |
| 2 | use crate::{ | 2 | use crate::{ |
| 3 | config::*, | 3 | config::*, |
| 4 | global::{functions::*, structs::*, traits::SearchProviderWrapper}, | 4 | global::{functions::*, structs::*, traits::{SearchProviderWrapper, CollectionNoId}}, |
| 5 | }; | 5 | }; |
| 6 | use ratatui::{ | 6 | use ratatui::{ |
| 7 | layout::{Constraint, Rect}, | 7 | layout::{Constraint, Rect}, |
| @@ -598,16 +598,12 @@ impl FrameworkItem for ChannelDisplay { | |||
| 598 | let mainconfig = framework.data.global.get::<MainConfig>().unwrap(); | 598 | let mainconfig = framework.data.global.get::<MainConfig>().unwrap(); |
| 599 | let appearance = framework.data.global.get::<AppearanceConfig>().unwrap(); | 599 | let appearance = framework.data.global.get::<AppearanceConfig>().unwrap(); |
| 600 | let page = framework.data.state.get::<Page>().unwrap().channeldisplay(); | 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 | match page.r#type { | 604 | match page.r#type { |
| 603 | ChannelDisplayPageType::Main => { | 605 | ChannelDisplayPageType::Main => { |
| 604 | let channel = Item::from_full_channel( | 606 | let channel = load_channel(&page.id, mainconfig)?; |
| 605 | SearchProviderWrapper::channel(&page.id)?, | ||
| 606 | mainconfig.image_index, | ||
| 607 | ); | ||
| 608 | if mainconfig.images.display() { | ||
| 609 | download_all_images(vec![(&channel).into()]); | ||
| 610 | } | ||
| 611 | let commands = framework | 607 | let commands = framework |
| 612 | .data | 608 | .data |
| 613 | .global | 609 | .global |
| @@ -615,6 +611,7 @@ impl FrameworkItem for ChannelDisplay { | |||
| 615 | .unwrap() | 611 | .unwrap() |
| 616 | .channel | 612 | .channel |
| 617 | .clone(); | 613 | .clone(); |
| 614 | |||
| 618 | *self = Self::Main { | 615 | *self = Self::Main { |
| 619 | iteminfo: Box::new(ItemInfo::new(Some(channel.clone()))), | 616 | iteminfo: Box::new(ItemInfo::new(Some(channel.clone()))), |
| 620 | channel: Box::new(channel), | 617 | channel: Box::new(channel), |
| @@ -625,7 +622,7 @@ impl FrameworkItem for ChannelDisplay { | |||
| 625 | .border_type(appearance.borders), | 622 | .border_type(appearance.borders), |
| 626 | textlist: Self::new_textlist_with_map(commands.clone()), | 623 | textlist: Self::new_textlist_with_map(commands.clone()), |
| 627 | commands, | 624 | commands, |
| 628 | } | 625 | }; |
| 629 | } | 626 | } |
| 630 | ChannelDisplayPageType::Videos => { | 627 | ChannelDisplayPageType::Videos => { |
| 631 | let videos = SearchProviderWrapper::channel_videos(&page.id)? | 628 | let videos = SearchProviderWrapper::channel_videos(&page.id)? |
| @@ -687,6 +684,14 @@ impl FrameworkItem for ChannelDisplay { | |||
| 687 | &mut framework.data.state.get_mut::<StateEnvs>().unwrap().0, | 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 | Ok(()) | 695 | Ok(()) |
| 691 | } | 696 | } |
| 692 | 697 | ||