src/app/pages/main_menu.rs

262311623d24bff1c8fa9dbee99aa8e32cbc9cee
youtube-tui/src/app/pages/main_menu.rs history · blame · raw

180 lines · 5409 bytes

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