src/app/pages/global.rs
157 lines · 4616 bytes
1use crossterm::event::KeyCode;
2use invidious::structs::hidden::{PopularItem, TrendingVideo};
3use tui::{style::{Color, Style}, widgets::{Borders, Paragraph, BorderType, Block}, layout::{Alignment, Rect}, Frame, backend::Backend};
4
5use crate::{
6 app::app::App,
7 traits::{KeyInput, SelectItem},
8};
9
10#[derive(Debug, Clone)]
11pub enum GlobalItem {
12 SearchBar(String),
13 MessageBar,
14}
15
16impl SelectItem for GlobalItem {
17 fn select(&mut self, app: App) -> (App, bool) {
18 let selected = match self {
19 GlobalItem::SearchBar(_) => true,
20 _ => false,
21 };
22
23 (app, selected)
24 }
25
26 fn selectable(&self) -> bool {
27 match self {
28 GlobalItem::SearchBar(_) => true,
29 _ => false,
30 }
31 }
32}
33
34impl KeyInput for GlobalItem {
35 fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App) {
36 match self {
37 GlobalItem::SearchBar(search_bar) => match key {
38 KeyCode::Char(c) => {
39 search_bar.push(c);
40 }
41 KeyCode::Backspace => {
42 search_bar.pop();
43 }
44 KeyCode::Enter => {}
45 _ => {}
46 },
47 _ => {}
48 }
49
50 (true, app)
51 }
52}
53
54#[derive(Debug, Clone)]
55pub enum ListItem {
56 Video(MiniVideo),
57 PageTurner(bool), // true: +1 | false: -1
58}
59
60impl ListItem {
61 pub fn id(&self) -> String {
62 match self {
63 ListItem::Video(video) => video.video_id.clone(),
64 _ => String::new(),
65 }
66 }
67}
68
69#[derive(Debug, Clone)]
70pub struct MiniVideo {
71 pub title: String,
72 pub video_id: String,
73 pub video_thumbnail: String,
74 pub length: String,
75 pub view_count: u64,
76 pub author: String,
77 pub author_url: String,
78 pub published: String,
79 pub description: String,
80}
81
82impl From<TrendingVideo> for MiniVideo {
83 fn from(original: TrendingVideo) -> Self {
84 MiniVideo {
85 title: original.title,
86 video_id: original.videoId,
87 video_thumbnail: original.videoThumbnails[4].url.clone(),
88 length: hrtime::from_sec(original.lengthSeconds as u64),
89 view_count: original.viewCount,
90 author: original.author,
91 author_url: original.authorUrl,
92 published: original.publishedText,
93 description: original.description,
94 }
95 }
96}
97
98impl From<PopularItem> for MiniVideo {
99 fn from(original: PopularItem) -> Self {
100 MiniVideo {
101 title: original.title,
102 video_id: original.videoId,
103 video_thumbnail: original.videoThumbnails[5].url.clone(),
104 length: hrtime::from_sec(original.lengthSeconds as u64),
105 view_count: original.viewCount,
106 author: original.author,
107 author_url: original.authorUrl,
108 published: original.publishedText,
109 description: String::new(),
110 }
111 }
112}
113
114impl GlobalItem {
115 pub fn render_item<B: Backend>(
116 &self,
117 frame: &mut Frame<B>,
118 rect: Rect,
119 selected: bool,
120 hover: bool,
121 message: &Option<String>,
122 ) {
123 match self {
124 GlobalItem::SearchBar(search) => {
125 let block = Block::default()
126 .border_type(BorderType::Rounded)
127 .borders(Borders::ALL)
128 .border_style(tui::style::Style::default().fg(if selected {
129 Color::LightBlue
130 } else if hover {
131 Color::LightRed
132 } else {
133 Color::Reset
134 }))
135 .title("Search YouTube")
136 .title_alignment(Alignment::Center);
137 let paragraph = Paragraph::new(search.clone()).block(block);
138 frame.render_widget(paragraph, rect);
139 }
140 GlobalItem::MessageBar => {
141 // let color = Color::LightYellow;
142 let block = Block::default()
143 .border_type(BorderType::Rounded)
144 .borders(Borders::ALL)
145 .border_style(Style::default().fg(if message.is_some() {
146 Color::LightRed
147 } else {
148 Color::Reset
149 }));
150 let paragraph =
151 Paragraph::new(message.clone().unwrap_or(String::from("All good :)")))
152 .block(block);
153 frame.render_widget(paragraph, rect);
154 }
155 }
156 }
157}