src/app/pages/item_info.rs
544 lines · 20992 bytes
1use std::{collections::LinkedList, error::Error};
2
3use crate::{
4 app::{
5 app::App,
6 config::{Action, PageCommand},
7 pages::{channel::*, global::*},
8 },
9 functions::download_all_thumbnails,
10 structs::{FullPlayList, FullVideo, Item, ListItem, Page, Row, RowItem, WatchHistory},
11 traits::{ItemTrait, PageTrait},
12 widgets::{horizontal_split::HorizontalSplit, item_display::ItemDisplay, text_list::TextList},
13};
14use crossterm::event::KeyCode;
15use tui::{
16 backend::Backend,
17 layout::{Constraint, Rect},
18 style::{Color, Style},
19 Frame,
20};
21
22use super::global::GlobalItem;
23
24#[derive(Debug, Clone)]
25pub enum ItemInfoItem {
26 Unknown,
27 Video(VideoItemInfo),
28 PlayList(PlayListItemInfo),
29}
30
31#[derive(Debug, Clone)]
32pub struct VideoItemInfo {
33 video: FullVideo,
34 list: TextList,
35 mode: Mode,
36 commands: Vec<PageCommand>,
37}
38
39#[derive(Debug, Clone)]
40pub struct PlayListItemInfo {
41 playlist: FullPlayList,
42 video_view_list: TextList,
43 playlist_view_list: TextList,
44 display_view: PlayListDisplayView,
45 mode: Mode,
46 commands: Vec<PageCommand>,
47}
48
49#[derive(Debug, Clone, Copy)]
50pub enum PlayListDisplayView {
51 VideoList,
52 PlayListView,
53}
54
55impl PlayListDisplayView {
56 pub fn toggle(self) -> Self {
57 match self {
58 PlayListDisplayView::VideoList => PlayListDisplayView::PlayListView,
59 PlayListDisplayView::PlayListView => PlayListDisplayView::VideoList,
60 }
61 }
62}
63
64impl ItemTrait for ItemInfoItem {
65 fn select(&mut self, app: App) -> (App, bool) {
66 let selected = true;
67
68 (app, selected)
69 }
70
71 fn selectable(&self) -> bool {
72 true
73 }
74
75 fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App) {
76 let action = match app.config.keybindings.0.get(&key) {
77 Some(action) => *action,
78 None => return (false, app),
79 };
80 match self {
81 ItemInfoItem::Video(videoinfo) => match action {
82 Action::Up => videoinfo.list.up(),
83 Action::Down => videoinfo.list.down(),
84 Action::FirstItem => videoinfo.list.first(),
85 Action::LastItem => videoinfo.list.last(),
86 Action::Select => {
87 let command = &videoinfo.commands[videoinfo.list.selected];
88
89 match command.command.as_str() {
90 "{toggle_mode}" => videoinfo.mode.toggle(),
91 "{goto_channel}" => {
92 let state = Channel::default();
93 let mut history = app.history.clone();
94 history.push(app.into());
95
96 return (
97 false,
98 App {
99 history,
100 page: Page::Channel(
101 ChannelPage::Home,
102 videoinfo.video.channel_id.clone(),
103 ),
104 selectable: App::selectable(&state),
105 state,
106 ..Default::default()
107 },
108 );
109 }
110 _ => {
111 match app.config.commands.0.get(&command.command) {
112 Some(command) => {
113 let mut env = app.config.main.env.clone();
114
115 env.insert(
116 String::from("embed_url"),
117 format!(
118 "{}/embed/{}",
119 match videoinfo.mode {
120 Mode::Invidious => app.client.server.as_str(),
121 Mode::Youtube => "https://youtube.com",
122 },
123 videoinfo.video.video_id
124 ),
125 );
126
127 match command.run_command(&env) {
128 Some(e) => {
129 *app.message.lock().unwrap() =
130 Some(format!("Unknown variable `{}`", e))
131 }
132 None => {
133 *app.message.lock().unwrap() =
134 Some(command.message.clone())
135 }
136 }
137 }
138 None => {
139 *app.message.lock().unwrap() =
140 Some(format!("Unkown command `{}`", &command.command));
141 }
142 };
143 }
144 };
145 }
146 _ => {}
147 },
148
149 ItemInfoItem::PlayList(playlistinfo) => match playlistinfo.display_view {
150 PlayListDisplayView::PlayListView => match action {
151 Action::Up => playlistinfo.playlist_view_list.up(),
152 Action::Down => playlistinfo.playlist_view_list.down(),
153 Action::FirstItem => playlistinfo.playlist_view_list.first(),
154 Action::LastItem => playlistinfo.playlist_view_list.last(),
155 Action::Select => {
156 let command =
157 &playlistinfo.commands[playlistinfo.playlist_view_list.selected];
158
159 match command.command.as_str() {
160 "{toggle_mode}" => playlistinfo.mode.toggle(),
161 "{goto_channel}" => {
162 let state = Channel::default();
163 let mut history = app.history.clone();
164 history.push(app.into());
165
166 return (
167 false,
168 App {
169 history,
170 page: Page::Channel(
171 ChannelPage::Home,
172 playlistinfo.playlist.playlist_id.clone(),
173 ),
174 selectable: App::selectable(&state),
175 state,
176 ..Default::default()
177 },
178 );
179 }
180
181 "{switch_view}" => {
182 playlistinfo.display_view = playlistinfo.display_view.toggle()
183 }
184 _ => {
185 match app.config.commands.0.get(&command.command) {
186 Some(command) => {
187 let mut env = app.config.main.env.clone();
188
189 env.insert(
190 String::from("embed_url"),
191 format!(
192 "{}/playlist?list={}",
193 match playlistinfo.mode {
194 Mode::Invidious => app.client.server.as_str(),
195 Mode::Youtube => "https://youtube.com",
196 },
197 playlistinfo.playlist.playlist_id
198 ),
199 );
200
201 match command.run_command(&env) {
202 Some(e) => {
203 *app.message.lock().unwrap() =
204 Some(format!("Unknown variable `{}`", e))
205 }
206 None => {
207 *app.message.lock().unwrap() =
208 Some(command.message.clone())
209 }
210 }
211 }
212 None => {
213 *app.message.lock().unwrap() =
214 Some(format!("Unkown command `{}`", &command.command));
215 }
216 };
217 }
218 };
219 }
220
221 _ => {}
222 },
223 PlayListDisplayView::VideoList => match action {
224 Action::Up => playlistinfo.video_view_list.up(),
225 Action::Down => playlistinfo.video_view_list.down(),
226 Action::FirstItem => playlistinfo.video_view_list.first(),
227 Action::LastItem => playlistinfo.video_view_list.last(),
228 Action::Select => match playlistinfo.video_view_list.selected {
229 0 => {
230 playlistinfo.display_view = playlistinfo.display_view.toggle();
231 }
232
233 x => {
234 let state = ItemInfo::default();
235 let mut history = app.history.clone();
236 history.push(app.into());
237
238 return (
239 false,
240 App {
241 history,
242 page: Page::ItemDisplay(DisplayItem::Video(
243 playlistinfo.playlist.videos[x - 1].video_id.clone(),
244 )),
245 selectable: App::selectable(&state),
246 state,
247 ..Default::default()
248 },
249 );
250 }
251 },
252
253 _ => {}
254 },
255 },
256 _ => {}
257 }
258
259 (true, app)
260 }
261
262 fn load_item(
263 &self,
264 app: &App,
265 watch_history: &mut WatchHistory,
266 ) -> Result<Item, Box<dyn Error>> {
267 let mut this = self.clone();
268
269 if let ItemInfoItem::Unknown = this {
270 match &app.page {
271 Page::ItemDisplay(display_item) => match display_item {
272 DisplayItem::Video(id) => {
273 let mut list = TextList::default();
274 let video: FullVideo = app.client.video(id, None)?.into();
275 let _ = download_all_thumbnails(LinkedList::from([(
276 video.video_thumbnail.clone(),
277 video.video_id.clone(),
278 )]));
279 let commands = app
280 .config
281 .page_commands
282 .0
283 .get("item_info:video")
284 .unwrap_or(&Vec::new())
285 .clone();
286 list.items(commands.iter().map(|item| item.label.to_string()).collect());
287
288 let iteminfo = VideoItemInfo {
289 video,
290 list,
291 mode: Mode::Youtube,
292 commands,
293 };
294
295 watch_history.push(
296 iteminfo.video.video_id.clone(),
297 ListItem::FullVideo(iteminfo.video.clone()),
298 &app.config,
299 );
300
301 this = ItemInfoItem::Video(iteminfo);
302 }
303
304 DisplayItem::PlayList(id) => {
305 let mut videos_text_list = TextList::default();
306 let mut playlist_text_list = TextList::default();
307 let playlist: FullPlayList = app.client.playlist(id, None)?.into();
308 let _ = download_all_thumbnails(
309 playlist
310 .videos
311 .iter()
312 .map(|v| (v.video_thumbnail.clone(), v.video_id.clone()))
313 .collect(),
314 );
315
316 videos_text_list.items = vec![String::from("Swtich view")];
317 videos_text_list
318 .items
319 .extend(playlist.videos.iter().map(|v| v.title.clone()));
320
321 let commands = app
322 .config
323 .page_commands
324 .0
325 .get("item_info:playlist")
326 .unwrap_or(&Vec::new())
327 .clone();
328 playlist_text_list
329 .items(commands.iter().map(|item| item.label.to_string()).collect());
330
331 let iteminfo = PlayListItemInfo {
332 playlist: playlist,
333 video_view_list: videos_text_list,
334 playlist_view_list: playlist_text_list,
335 mode: Mode::Youtube,
336 display_view: PlayListDisplayView::PlayListView,
337 commands,
338 };
339
340 watch_history.push(
341 iteminfo.playlist.playlist_id.clone(),
342 ListItem::FullPlayList(iteminfo.playlist.clone()),
343 &app.config,
344 );
345
346 this = ItemInfoItem::PlayList(iteminfo);
347 }
348 },
349
350 _ => {}
351 }
352 }
353 Ok(Item::ItemInfo(this))
354 }
355
356 fn render_item<B: Backend>(
357 &mut self,
358 frame: &mut Frame<B>,
359 rect: Rect,
360 app: App,
361 selected: bool,
362 hover: bool,
363 popup_focus: bool,
364 popup_render: bool,
365 ) -> (bool, App) {
366 let out = (false, app);
367
368 if popup_render {
369 return out;
370 }
371
372 match self {
373 ItemInfoItem::Video(videoinfo) => {
374 // list.items[videoinfo.list.items.len() - 1] = format!("Mode: {}", videoinfo.mode);
375
376 let split = HorizontalSplit::default()
377 .percentages(vec![40, 60])
378 .border_style(Style::default().fg(if selected {
379 Color::LightBlue
380 } else if hover {
381 Color::LightRed
382 } else {
383 Color::Reset
384 }));
385
386 let chunks = split.inner(rect);
387
388 videoinfo.list.area(chunks[1]);
389
390 let mut list = videoinfo.list.clone();
391
392 list.items.iter_mut().for_each(|item| match item.as_str() {
393 "{mode}" => *item = format!("Mode: {}", videoinfo.mode),
394 _ => {}
395 });
396 if selected {
397 list.selected_style(Style::default().fg(Color::LightRed));
398 } else {
399 list.selected_style(Style::default().fg(Color::LightYellow));
400 }
401
402 frame.render_widget(split, rect);
403
404 if !popup_focus {
405 let item_display = ItemDisplay {
406 item: ListItem::FullVideo(videoinfo.video.clone()),
407 };
408
409 frame.render_widget(item_display, chunks[0]);
410 }
411
412 frame.render_widget(list, chunks[1]);
413 }
414
415 ItemInfoItem::PlayList(playlistinfo) => {
416 let split = HorizontalSplit::default()
417 .percentages(vec![40, 60])
418 .border_style(Style::default().fg(if selected {
419 Color::LightBlue
420 } else if hover {
421 Color::LightRed
422 } else {
423 Color::Reset
424 }));
425
426 let chunks = split.inner(rect);
427
428 frame.render_widget(split, rect);
429
430 if !popup_focus {
431 let item_display = ItemDisplay {
432 item: ListItem::FullPlayList(playlistinfo.playlist.clone()),
433 };
434
435 frame.render_widget(item_display, chunks[0]);
436 }
437
438 match playlistinfo.display_view {
439 PlayListDisplayView::VideoList => {
440 playlistinfo.video_view_list.area(chunks[1]);
441 let mut list = playlistinfo.video_view_list.clone();
442 list.area(chunks[1]);
443 if selected {
444 list.selected_style(Style::default().fg(Color::LightRed));
445 } else {
446 list.selected_style(Style::default().fg(Color::LightYellow));
447 }
448
449 frame.render_widget(list, chunks[1]);
450 }
451 PlayListDisplayView::PlayListView => {
452 playlistinfo.playlist_view_list.area(chunks[1]);
453 let mut list = playlistinfo.playlist_view_list.clone();
454
455 list.items.iter_mut().for_each(|item| match item.as_str() {
456 "{mode}" => *item = format!("Mode: {}", playlistinfo.mode),
457 _ => {}
458 });
459
460 list.area(chunks[1]);
461
462 if selected {
463 list.selected_style(Style::default().fg(Color::LightRed));
464 } else {
465 list.selected_style(Style::default().fg(Color::LightYellow));
466 }
467
468 frame.render_widget(list, chunks[1]);
469 }
470 }
471 }
472
473 ItemInfoItem::Unknown => {
474 let split = HorizontalSplit::default()
475 .percentages(vec![40, 60])
476 .border_style(Style::default().fg(if selected {
477 Color::LightBlue
478 } else if hover {
479 Color::LightRed
480 } else {
481 Color::Reset
482 }));
483
484 frame.render_widget(split, rect);
485 }
486 }
487 out
488 }
489}
490
491#[derive(Debug, Clone, PartialEq)]
492pub enum DisplayItem {
493 Video(String),
494 PlayList(String),
495}
496
497pub struct ItemInfo;
498
499impl PageTrait for ItemInfo {
500 fn message() -> String {
501 String::from("Loading item info...")
502 }
503
504 fn min() -> (u16, u16) {
505 (21, 12)
506 }
507
508 fn default() -> Vec<Row> {
509 vec![
510 Row {
511 items: vec![
512 RowItem {
513 item: Item::Global(GlobalItem::SearchBar),
514 constraint: Constraint::Min(16),
515 },
516 RowItem {
517 item: Item::Global(GlobalItem::SearchSettings),
518 constraint: Constraint::Length(5),
519 },
520 ],
521 centered: false,
522 height: Constraint::Length(3),
523 },
524 Row {
525 items: vec![RowItem {
526 item: Item::ItemInfo(ItemInfoItem::Unknown),
527 constraint: Constraint::Percentage(100),
528 }],
529 centered: false,
530 height: Constraint::Min(6),
531 },
532 Row {
533 items: vec![RowItem {
534 item: Item::Global(GlobalItem::MessageBar),
535 constraint: Constraint::Percentage(100),
536 }],
537 centered: false,
538 height: Constraint::Length(3),
539 },
540 ]
541 }
542}
543
544impl ItemInfoItem {}