src/app/pages/global.rs
86 lines · 2091 bytes
1use crossterm::event::KeyCode;
2use invidious::structs::{hidden::TrendingVideo, video::Video};
3
4use crate::{
5 app::app::App,
6 traits::{KeyInput, SelectItem},
7};
8
9#[derive(Debug, Clone)]
10pub enum GlobalItem {
11 SearchBar(String),
12 MessageBar,
13}
14
15impl SelectItem for GlobalItem {
16 fn select(&mut self, app: App) -> (App, bool) {
17 let selected = match self {
18 GlobalItem::SearchBar(_) => true,
19 _ => false,
20 };
21
22 (app, selected)
23 }
24
25 fn selectable(&self) -> bool {
26 match self {
27 GlobalItem::SearchBar(_) => true,
28 _ => false,
29 }
30 }
31}
32
33impl KeyInput for GlobalItem {
34 fn key_input(&mut self, key: KeyCode, app: App) -> App {
35 match self {
36 GlobalItem::SearchBar(search_bar) => match key {
37 KeyCode::Char(c) => {
38 search_bar.push(c);
39 }
40 KeyCode::Backspace => {
41 search_bar.pop();
42 }
43 KeyCode::Enter => {}
44 _ => {}
45 },
46 _ => {}
47 }
48
49 app
50 }
51}
52
53#[derive(Debug, Clone)]
54pub enum ListItem {
55 Video(MiniVideo),
56 PageTurner(bool), // true: +1 | false: -1
57}
58
59#[derive(Debug, Clone)]
60pub struct MiniVideo {
61 pub title: String,
62 pub video_id: String,
63 pub video_thumbnail: String,
64 pub length: String,
65 pub view_count: u64,
66 pub author: String,
67 pub author_url: String,
68 pub published: String,
69 pub description: String,
70}
71
72impl From<TrendingVideo> for MiniVideo {
73 fn from(original: TrendingVideo) -> Self {
74 MiniVideo {
75 title: original.title,
76 video_id: original.videoId,
77 video_thumbnail: original.videoThumbnails[0].url.clone(),
78 length: hrtime::from_sec(original.lengthSeconds as u64),
79 view_count: original.viewCount,
80 author: original.author,
81 author_url: original.authorUrl,
82 published: original.publishedText,
83 description: original.description,
84 }
85 }
86}