src/app/pages/channel.rs
462 lines · 17253 bytes
1use std::{collections::LinkedList, error::Error};
2
3use crossterm::event::KeyCode;
4use serde::{Deserialize, Serialize};
5use tui::{
6 backend::Backend,
7 layout::{Alignment, Rect},
8 style::{Color, Style},
9 widgets::{Block, BorderType, Borders, Paragraph},
10 Frame,
11};
12
13use crate::{
14 app::{
15 app::{App, AppNoState},
16 config::{
17 Action, ChannelItemTransitional, ConstraintTransitional, ItemTransitional,
18 LayoutConfig, RowItemTransitional, RowTransitional,
19 },
20 },
21 functions::download_all_thumbnails,
22 structs::{FullChannel, Item, ListItem, MiniPlayList, MiniVideo, Page, WatchHistory},
23 traits::{ItemTrait, PageTrait},
24 widgets::{horizontal_split::HorizontalSplit, item_display::ItemDisplay, text_list::TextList},
25};
26
27use super::{
28 global::GlobalItem,
29 item_info::{DisplayItem, ItemInfo},
30};
31
32#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
33pub enum ChannelPage {
34 Home,
35 Playlists,
36 Videos,
37}
38
39#[derive(Debug, Clone)]
40pub enum ChannelItem {
41 InfoDisplay(ChannelDisplayItem),
42 SelectItems(ChannelPage),
43}
44
45#[derive(Debug, Clone)]
46pub enum ChannelDisplayItem {
47 Playlists(Vec<MiniPlayList>, TextList),
48 Videos(Vec<MiniVideo>, TextList),
49 Home(FullChannel),
50 Unknown,
51}
52
53impl ItemTrait for ChannelItem {
54 fn load_item(&self, app: &App, _: &mut WatchHistory) -> Result<Item, Box<dyn Error>> {
55 let mut this = self.clone();
56
57 if let Page::Channel(channel_page, id) = &app.page {
58 if let ChannelItem::InfoDisplay(displayitem) = &mut this {
59 match channel_page {
60 ChannelPage::Home => {
61 let channel = app.client.channel(id, None)?;
62 let mut linked_list = LinkedList::new();
63 linked_list.push_back((
64 channel.authorThumbnails[4].url.clone(),
65 channel.authorId.clone(),
66 ));
67 let _ = download_all_thumbnails(linked_list);
68
69 *displayitem = ChannelDisplayItem::Home(channel.into());
70 }
71 ChannelPage::Playlists => {
72 let mut textlist = TextList::default();
73 let playlists = app
74 .client
75 .channel_playlists(id, None)?
76 .playlists
77 .into_iter()
78 .map(|item| {
79 textlist.items.push(item.title.clone());
80 item.into()
81 })
82 .collect::<Vec<MiniPlayList>>();
83
84 let mut linked_list = LinkedList::new();
85 for item in playlists.iter() {
86 if let Some(thumbnail) = &item.thumbnail {
87 linked_list
88 .push_back((thumbnail.clone(), item.playlist_id.clone()));
89 }
90 }
91 let _ = download_all_thumbnails(linked_list);
92
93 *displayitem = ChannelDisplayItem::Playlists(playlists, textlist);
94 }
95
96 ChannelPage::Videos => {
97 let mut textlist = TextList::default();
98 let videos = app
99 .client
100 .channel_videos(id, None)?
101 .videos
102 .into_iter()
103 .map(|item| {
104 textlist.items.push(item.title.clone());
105 item.into()
106 })
107 .collect::<Vec<MiniVideo>>();
108
109 let mut linked_list = LinkedList::new();
110 for item in videos.iter() {
111 linked_list
112 .push_back((item.video_thumbnail.clone(), item.video_id.clone()));
113 }
114 let _ = download_all_thumbnails(linked_list);
115
116 *displayitem = ChannelDisplayItem::Videos(videos, textlist);
117 }
118 }
119 }
120 }
121
122 Ok(Item::Channel(this))
123 }
124
125 fn render_item<B: Backend>(
126 &mut self,
127 frame: &mut Frame<B>,
128 rect: Rect,
129 app: AppNoState,
130 selected: bool,
131 hover: bool,
132 popup_focus: bool,
133 _: bool,
134 ) -> (bool, AppNoState) {
135 let mut out = (false, app);
136
137 let mut style = Style::default().fg(if selected {
138 Color::LightBlue
139 } else if hover {
140 Color::LightRed
141 } else {
142 Color::Reset
143 });
144
145 match self {
146 ChannelItem::InfoDisplay(displayitem) => match displayitem {
147 ChannelDisplayItem::Unknown => {
148 let block = Block::default()
149 .border_type(BorderType::Rounded)
150 .borders(Borders::ALL)
151 .border_style(style);
152
153 frame.render_widget(block, rect);
154 }
155 ChannelDisplayItem::Home(channel) => {
156 let block = Block::default()
157 .border_type(BorderType::Rounded)
158 .borders(Borders::ALL)
159 .border_style(style);
160
161 let inner = block.inner(rect);
162 frame.render_widget(block, rect);
163 frame.render_widget(
164 ItemDisplay {
165 item: ListItem::FullChannel(channel.clone()),
166 },
167 inner,
168 );
169 }
170 ChannelDisplayItem::Videos(videos, textlist) => {
171 let split = HorizontalSplit::default()
172 .percentages(vec![60, 40])
173 .border_style(Style::default().fg(if selected {
174 Color::LightBlue
175 } else if hover {
176 Color::LightRed
177 } else {
178 Color::Reset
179 }));
180
181 let chunks = split.inner(rect);
182
183 frame.render_widget(split, rect);
184
185 match &mut textlist.area {
186 Some(area) if *area != chunks[0] => {
187 *area = chunks[0];
188 out.0 = true;
189 }
190 None => {
191 textlist.area = Some(chunks[0]);
192 out.0 = true;
193 }
194
195 _ => {}
196 };
197
198 textlist.area(chunks[0]);
199
200 let mut textlist = textlist.clone();
201
202 if selected {
203 textlist.selected_style(Style::default().fg(Color::LightRed));
204 } else {
205 textlist.selected_style(Style::default().fg(Color::LightYellow));
206 }
207
208 if let Some(item) = videos.iter().nth(textlist.selected) {
209 if !popup_focus {
210 frame.render_widget(
211 ItemDisplay {
212 item: ListItem::MiniVideo(item.clone()),
213 },
214 chunks[1],
215 );
216 }
217 }
218
219 frame.render_widget(textlist, chunks[0]);
220 }
221 ChannelDisplayItem::Playlists(playlists, textlist) => {
222 let split = HorizontalSplit::default()
223 .percentages(vec![60, 40])
224 .border_style(Style::default().fg(if selected {
225 Color::LightBlue
226 } else if hover {
227 Color::LightRed
228 } else {
229 Color::Reset
230 }));
231
232 let chunks = split.inner(rect);
233
234 frame.render_widget(split, rect);
235
236 textlist.area(chunks[0]);
237
238 let mut textlist = textlist.clone();
239
240 if selected {
241 textlist.selected_style(Style::default().fg(Color::LightRed));
242 } else {
243 textlist.selected_style(Style::default().fg(Color::LightYellow));
244 }
245
246 if let Some(item) = playlists.iter().nth(textlist.selected) {
247 if !popup_focus {
248 frame.render_widget(
249 ItemDisplay {
250 item: ListItem::MiniPlayList(item.clone()),
251 },
252 chunks[1],
253 );
254 }
255 }
256
257 frame.render_widget(textlist, chunks[0]);
258 }
259 },
260 ChannelItem::SelectItems(selected_page) => {
261 if !hover {
262 if let Page::Channel(channel_page, _) = &out.1.page {
263 if *selected_page == *channel_page {
264 style = style.fg(Color::LightYellow);
265 }
266 }
267 }
268
269 let block = Block::default()
270 .border_type(BorderType::Rounded)
271 .borders(Borders::ALL)
272 .border_style(style);
273 let paragraph = Paragraph::new(format!("{:?}", selected_page))
274 .block(block)
275 .alignment(Alignment::Center);
276
277 frame.render_widget(paragraph, rect);
278 }
279 }
280 out
281 }
282
283 fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App) {
284 let action = match app.config.keybindings.0.get(&key) {
285 Some(action) => action,
286 None => return (false, app),
287 };
288
289 match self {
290 ChannelItem::InfoDisplay(displayitem) => match displayitem {
291 ChannelDisplayItem::Videos(videos, textlist) => match action {
292 Action::Up => {
293 textlist.up();
294 }
295 Action::Down => {
296 textlist.down();
297 }
298 Action::FirstItem => textlist.first(),
299 Action::LastItem => textlist.last(),
300 Action::Select => {
301 let state = app.config.layouts.item_info.clone().into();
302 let mut history = app.history.clone();
303 history.push(app.into());
304
305 return (
306 false,
307 App {
308 history,
309 page: Page::ItemDisplay(DisplayItem::Video(
310 videos[textlist.selected].video_id.clone(),
311 )),
312 selectable: App::selectable(&state),
313 state,
314 ..Default::default()
315 },
316 );
317 }
318 _ => {}
319 },
320
321 ChannelDisplayItem::Playlists(playlists, textlist) => match action {
322 Action::Up => {
323 textlist.up();
324 }
325 Action::Down => {
326 textlist.down();
327 }
328 Action::FirstItem => textlist.first(),
329 Action::LastItem => textlist.last(),
330 Action::Select => {
331 let state = app.config.layouts.item_info.clone().into();
332 let mut history = app.history.clone();
333 history.push(app.into());
334
335 return (
336 false,
337 App {
338 history,
339 page: Page::ItemDisplay(DisplayItem::PlayList(
340 playlists[textlist.selected].playlist_id.clone(),
341 )),
342 selectable: App::selectable(&state),
343 state,
344 ..Default::default()
345 },
346 );
347 }
348 _ => {}
349 },
350
351 _ => unreachable!(),
352 },
353 _ => {
354 unreachable!()
355 }
356 }
357
358 (true, app)
359 }
360
361 fn select(&mut self, app: App) -> (App, bool) {
362 let cloned = self.clone();
363 match &cloned {
364 ChannelItem::InfoDisplay(_) => (app, true),
365 ChannelItem::SelectItems(tab) => {
366 if let Page::Channel(channel_page, id) = &app.page.clone() {
367 if *tab == *channel_page {
368 return (app, false);
369 }
370
371 let state = app.config.layouts.channel.clone().into();
372 let mut history = app.history.clone();
373 history.push(app.into());
374
375 return (
376 App {
377 history,
378 page: Page::Channel(tab.clone(), id.clone()),
379 selectable: App::selectable(&state),
380 state,
381 ..Default::default()
382 },
383 false,
384 );
385 } else {
386 (app, false)
387 }
388 }
389 }
390 }
391
392 fn selectable(&self) -> bool {
393 true
394 }
395}
396
397pub struct Channel;
398
399impl PageTrait for Channel {
400 fn default() -> LayoutConfig {
401 LayoutConfig {
402 layout: vec![
403 RowTransitional {
404 items: vec![
405 RowItemTransitional {
406 item: ItemTransitional::Global(GlobalItem::SearchBar),
407 constraint: ConstraintTransitional::Min(16),
408 },
409 RowItemTransitional {
410 item: ItemTransitional::Global(GlobalItem::SearchSettings),
411 constraint: ConstraintTransitional::Length(5),
412 },
413 ],
414 centered: false,
415 height: ConstraintTransitional::Length(3),
416 },
417 RowTransitional {
418 items: vec![
419 RowItemTransitional {
420 item: ItemTransitional::Channel(ChannelItemTransitional::SelectItems(
421 ChannelPage::Home,
422 )),
423 constraint: ConstraintTransitional::Length(15),
424 },
425 RowItemTransitional {
426 item: ItemTransitional::Channel(ChannelItemTransitional::SelectItems(
427 ChannelPage::Videos,
428 )),
429 constraint: ConstraintTransitional::Length(15),
430 },
431 RowItemTransitional {
432 item: ItemTransitional::Channel(ChannelItemTransitional::SelectItems(
433 ChannelPage::Playlists,
434 )),
435 constraint: ConstraintTransitional::Length(15),
436 },
437 ],
438 centered: true,
439 height: ConstraintTransitional::Length(3),
440 },
441 RowTransitional {
442 items: vec![RowItemTransitional {
443 item: ItemTransitional::Channel(ChannelItemTransitional::InfoDisplay),
444 constraint: ConstraintTransitional::Percentage(100),
445 }],
446 centered: false,
447 height: ConstraintTransitional::Min(6),
448 },
449 RowTransitional {
450 items: vec![RowItemTransitional {
451 item: ItemTransitional::Global(GlobalItem::MessageBar),
452 constraint: ConstraintTransitional::Percentage(100),
453 }],
454 centered: false,
455 height: ConstraintTransitional::Length(3),
456 },
457 ],
458 min: (46, 15),
459 message: String::from("Loading channel info..."),
460 }
461 }
462}