src/app/pages/main_menu.rs
204 lines · 6467 bytes
1use std::{collections::LinkedList, error::Error};
2
3use futures::{executor::block_on, future::join_all};
4
5use crate::{
6 app::{
7 app::{App, Item, Page, Row, RowItem},
8 pages::global::{GlobalItem, ListItem},
9 },
10 traits::{KeyInput, LoadItem, SelectItem},
11 widgets::text_list::TextList,
12};
13use crossterm::event::KeyCode;
14use tui::layout::Constraint;
15#[derive(Debug, Clone)]
16pub enum MainMenuItem {
17 SeletorTab(MainMenuSelector),
18 VideoList(Option<(LinkedList<ListItem>, TextList, Option<usize>)>), // Videos, List, page
19}
20
21// app.client.trending(None)
22
23impl SelectItem for MainMenuItem {
24 fn select(&mut self, mut app: App) -> (App, bool) {
25 let selected = match self {
26 MainMenuItem::SeletorTab(selector) => {
27 if app.page == (Page::MainMenu { tab: *selector }) {
28 return (app, false);
29 }
30 app.page = Page::MainMenu { tab: *selector };
31 app.load = true;
32 app.message = Some(String::from("Loading videos..."));
33 false
34 }
35
36 _ => true,
37 };
38
39 (app, selected)
40 }
41
42 fn selectable(&self) -> bool {
43 true
44 }
45}
46
47impl KeyInput for MainMenuItem {
48 fn key_input(&mut self, key: KeyCode, app: App) -> App {
49 match self {
50 MainMenuItem::VideoList(Some((_, list, _))) => match key {
51 KeyCode::Up => list.up(),
52 KeyCode::Down => list.down(),
53 KeyCode::PageUp => list.selected = 0,
54 KeyCode::PageDown => list.selected = list.items.len() - 1,
55 _ => {}
56 },
57 _ => {}
58 }
59
60 app
61 }
62}
63
64impl LoadItem for MainMenuItem {
65 fn load_item(&self, app: &App) -> Result<Box<Self>, Box<dyn Error>> {
66 let mut this = self.clone();
67
68 match &mut this {
69 MainMenuItem::VideoList(enum_items) => match app.page {
70 Page::MainMenu { tab } => match tab {
71 MainMenuSelector::Trending => {
72 let list: LinkedList<ListItem> = app
73 .client
74 .trending(None)?
75 .videos
76 .into_iter()
77 .map(|t| ListItem::Video(t.into()))
78 .collect();
79
80 let mut text_list = TextList::default();
81
82 if let Some((video_list, text_list, _)) = enum_items {
83 *text_list = TextList::default();
84 text_list.items(textlist_from_video_list(&list));
85
86 *video_list = list;
87 } else {
88 text_list.items(textlist_from_video_list(&list));
89 *enum_items = Some((list, text_list, None));
90 };
91 }
92
93 MainMenuSelector::Popular => {
94 let list: LinkedList<ListItem> = app
95 .client
96 .popular(None)?
97 .items
98 .into_iter()
99 .map(|t| ListItem::Video(t.into()))
100 .collect();
101
102 let mut text_list = TextList::default();
103
104 if let Some((video_list, text_list, _)) = enum_items {
105 *text_list = TextList::default();
106 text_list.items(textlist_from_video_list(&list));
107
108 *video_list = list;
109 } else {
110 text_list.items(textlist_from_video_list(&list));
111 *enum_items = Some((list, text_list, None));
112 };
113 }
114
115 _ => {}
116 },
117 },
118 _ => {}
119 }
120
121 Ok(Box::new(this))
122 }
123}
124
125fn textlist_from_video_list(original: &LinkedList<ListItem>) -> Vec<String> {
126 original
127 .iter()
128 .map(|item| match item {
129 ListItem::Video(video) => video
130 .title
131 .clone()
132 .chars()
133 .map(|c| if c.is_ascii() { c } else { '?' })
134 .collect(),
135 _ => {
136 unreachable!()
137 }
138 })
139 .collect()
140}
141
142#[derive(Debug, Clone, Copy, PartialEq)]
143pub enum MainMenuSelector {
144 Trending,
145 Popular,
146 History,
147}
148
149impl MainMenuSelector {
150 pub fn default() -> Self {
151 Self::Trending
152 }
153}
154
155pub struct MainMenu;
156
157impl MainMenu {
158 pub fn default() -> Vec<Row> {
159 vec![
160 Row {
161 items: vec![RowItem {
162 item: Item::Global(GlobalItem::SearchBar(String::new())),
163 constraint: Constraint::Percentage(100),
164 }],
165 centered: false,
166 height: Constraint::Length(3),
167 },
168 Row {
169 items: vec![
170 RowItem {
171 item: Item::MainMenu(MainMenuItem::SeletorTab(MainMenuSelector::Trending)),
172 constraint: Constraint::Length(15),
173 },
174 RowItem {
175 item: Item::MainMenu(MainMenuItem::SeletorTab(MainMenuSelector::Popular)),
176 constraint: Constraint::Length(15),
177 },
178 RowItem {
179 item: Item::MainMenu(MainMenuItem::SeletorTab(MainMenuSelector::History)),
180 constraint: Constraint::Length(15),
181 },
182 ],
183 centered: true,
184 height: Constraint::Length(3),
185 },
186 Row {
187 items: vec![RowItem {
188 item: Item::MainMenu(MainMenuItem::VideoList(None)),
189 constraint: Constraint::Percentage(100),
190 }],
191 centered: false,
192 height: Constraint::Min(6),
193 },
194 Row {
195 items: vec![RowItem {
196 item: Item::Global(GlobalItem::MessageBar),
197 constraint: Constraint::Percentage(100),
198 }],
199 centered: false,
200 height: Constraint::Length(3),
201 },
202 ]
203 }
204}