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