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