src/structs/search_settings.rs
361 lines · 10842 bytes
1use crossterm::event::KeyCode;
2
3use crate::widgets::text_list::TextList;
4
5#[derive(Debug, Clone)]
6pub struct SearchSettings {
7 pub sort_by: SearchSettingsSortBy,
8 pub date: Option<SearchSettingsDate>,
9 pub duartion: Option<SearchSettingsDuration>,
10 pub r#type: Option<SearchSettingsType>,
11
12 pub row: bool, // left: true, right: false
13
14 pub text_list: TextList,
15 pub select_text_list: TextList,
16}
17
18impl Default for SearchSettings {
19 fn default() -> Self {
20 let mut text_list = TextList::default();
21 text_list.items = vec![
22 String::from("Sort by"),
23 String::from("Date"),
24 String::from("Duration"),
25 String::from("Type"),
26 String::from("Reset All"),
27 ];
28
29 let mut out = SearchSettings {
30 sort_by: SearchSettingsSortBy::Relevance,
31 date: None,
32 duartion: None,
33 r#type: None,
34
35 row: true,
36
37 text_list,
38 select_text_list: TextList::default(),
39 };
40
41 out.update_left();
42
43 out
44 }
45}
46
47impl SearchSettings {
48 pub fn to_vec(self) -> Vec<String> {
49 let mut vec = vec![self.sort_by.as_url_string()];
50
51 if let Some(date) = self.date {
52 vec.push(date.as_url_string());
53 }
54
55 if let Some(duration) = self.duartion {
56 vec.push(duration.as_url_string());
57 }
58
59 if let Some(r#type) = self.r#type {
60 vec.push(r#type.as_url_string());
61 }
62
63 vec
64 }
65
66 pub fn key_input(&mut self, keycode: KeyCode) {
67 match keycode {
68 KeyCode::Up => {
69 if self.row {
70 self.text_list.up();
71 self.update_left()
72 } else {
73 self.select_text_list.up();
74
75 match self.text_list.selected {
76 0 => self.sort_by = SearchSettingsSortBy::index_to_value(self.select_text_list.selected),
77 1 => self.date = SearchSettingsDate::index_to_value(self.select_text_list.selected),
78 2 => self.duartion = SearchSettingsDuration::index_to_value(self.select_text_list.selected),
79 3 => self.r#type = SearchSettingsType::index_to_value(self.select_text_list.selected),
80 _ => {}
81 }
82 }
83 }
84 KeyCode::Down => {
85 if self.row {
86 self.text_list.down();
87 self.update_left()
88 } else {
89 self.select_text_list.down();
90
91 match self.text_list.selected {
92 0 => self.sort_by = SearchSettingsSortBy::index_to_value(self.select_text_list.selected),
93 1 => self.date = SearchSettingsDate::index_to_value(self.select_text_list.selected),
94 2 => self.duartion = SearchSettingsDuration::index_to_value(self.select_text_list.selected),
95 3 => self.r#type = SearchSettingsType::index_to_value(self.select_text_list.selected),
96 _ => {}
97 }
98 }
99 }
100 KeyCode::Left => {
101 self.row = true;
102 }
103 KeyCode::Right => {
104 self.row = false;
105 }
106 KeyCode::Enter => {
107 if self.text_list.selected == 4 && !self.row {
108 *self = Self::default();
109 self.text_list.selected = 4;
110 self.update_left();
111 } else {
112 self.row = false;
113 }
114 }
115 _ => {}
116 }
117 }
118
119 pub fn update_left(&mut self) {
120 match self.text_list.selected {
121 0 => {
122 self.select_text_list.items = SearchSettingsSortBy::as_vec();
123 self.select_text_list.selected = SearchSettingsSortBy::value_to_index(self.sort_by);
124 }
125 1 => {
126 self.select_text_list.items = SearchSettingsDate::as_vec();
127 self.select_text_list.selected = SearchSettingsDate::value_to_index(self.date);
128 }
129 2 => {
130 self.select_text_list.items = SearchSettingsDuration::as_vec();
131 self.select_text_list.selected =
132 SearchSettingsDuration::value_to_index(self.duartion);
133 }
134 3 => {
135 self.select_text_list.items = SearchSettingsType::as_vec();
136 self.select_text_list.selected = SearchSettingsType::value_to_index(self.r#type);
137 }
138 4 => {
139 self.select_text_list.items = vec![String::from("Reset All")];
140 self.select_text_list.selected = 0;
141 }
142 _ => {}
143 }
144 }
145}
146
147pub trait AsUrlString {
148 fn as_url_string(&self) -> String;
149}
150
151#[derive(Debug, Clone, Copy)]
152pub enum SearchSettingsSortBy {
153 Relevance,
154 Rating,
155 Date,
156 ViewCount,
157}
158
159impl AsUrlString for SearchSettingsSortBy {
160 fn as_url_string(&self) -> String {
161 format!(
162 "sort_by={}",
163 match self {
164 SearchSettingsSortBy::Relevance => String::from("relevance"),
165 SearchSettingsSortBy::Rating => String::from("rating"),
166 SearchSettingsSortBy::Date => String::from("upload_date"),
167 SearchSettingsSortBy::ViewCount => String::from("view_count"),
168 }
169 )
170 }
171}
172
173impl SearchSettingsSortBy {
174 pub fn as_vec() -> Vec<String> {
175 vec![
176 String::from("Relevance"),
177 String::from("Rating"),
178 String::from("Date"),
179 String::from("View Count"),
180 ]
181 }
182
183 pub fn index_to_value(index: usize) -> SearchSettingsSortBy {
184 match index {
185 0 => SearchSettingsSortBy::Relevance,
186 1 => SearchSettingsSortBy::Rating,
187 2 => SearchSettingsSortBy::Date,
188 3 => SearchSettingsSortBy::ViewCount,
189 _ => unreachable!(),
190 }
191 }
192
193 pub fn value_to_index(value: SearchSettingsSortBy) -> usize {
194 match value {
195 SearchSettingsSortBy::Relevance => 0,
196 SearchSettingsSortBy::Rating => 1,
197 SearchSettingsSortBy::Date => 2,
198 SearchSettingsSortBy::ViewCount => 3,
199 }
200 }
201}
202
203#[derive(Debug, Clone, Copy)]
204pub enum SearchSettingsDate {
205 Hour,
206 Day,
207 Week,
208 Month,
209 Year,
210}
211
212impl AsUrlString for SearchSettingsDate {
213 fn as_url_string(&self) -> String {
214 format!(
215 "date={}",
216 match self {
217 SearchSettingsDate::Hour => String::from("hour"),
218 SearchSettingsDate::Day => String::from("today"),
219 SearchSettingsDate::Week => String::from("week"),
220 SearchSettingsDate::Month => String::from("month"),
221 SearchSettingsDate::Year => String::from("year"),
222 }
223 )
224 }
225}
226
227impl SearchSettingsDate {
228 pub fn as_vec() -> Vec<String> {
229 vec![
230 String::from("No Filter"),
231 String::from("Hour"),
232 String::from("Day"),
233 String::from("Week"),
234 String::from("Month"),
235 String::from("Year"),
236 ]
237 }
238
239 pub fn index_to_value(index: usize) -> Option<SearchSettingsDate> {
240 match index {
241 0 => None,
242 1 => Some(SearchSettingsDate::Hour),
243 2 => Some(SearchSettingsDate::Day),
244 3 => Some(SearchSettingsDate::Week),
245 4 => Some(SearchSettingsDate::Month),
246 5 => Some(SearchSettingsDate::Year),
247 _ => unreachable!(),
248 }
249 }
250
251 pub fn value_to_index(value: Option<SearchSettingsDate>) -> usize {
252 match value {
253 None => 0,
254 Some(SearchSettingsDate::Hour) => 1,
255 Some(SearchSettingsDate::Day) => 2,
256 Some(SearchSettingsDate::Week) => 3,
257 Some(SearchSettingsDate::Month) => 4,
258 Some(SearchSettingsDate::Year) => 5,
259 }
260 }
261}
262
263#[derive(Debug, Clone, Copy)]
264pub enum SearchSettingsDuration {
265 Short,
266 Long,
267}
268
269impl AsUrlString for SearchSettingsDuration {
270 fn as_url_string(&self) -> String {
271 format!(
272 "duration={}",
273 match self {
274 SearchSettingsDuration::Short => String::from("short"),
275 SearchSettingsDuration::Long => String::from("long"),
276 }
277 )
278 }
279}
280
281impl SearchSettingsDuration {
282 pub fn as_vec() -> Vec<String> {
283 vec![
284 String::from("No Filter"),
285 String::from("Short"),
286 String::from("Long"),
287 ]
288 }
289
290 pub fn index_to_value(index: usize) -> Option<SearchSettingsDuration> {
291 match index {
292 0 => None,
293 1 => Some(SearchSettingsDuration::Short),
294 2 => Some(SearchSettingsDuration::Long),
295 _ => unreachable!(),
296 }
297 }
298
299 pub fn value_to_index(value: Option<SearchSettingsDuration>) -> usize {
300 match value {
301 None => 0,
302 Some(SearchSettingsDuration::Short) => 1,
303 Some(SearchSettingsDuration::Long) => 2,
304 }
305 }
306}
307
308#[derive(Debug, Clone, Copy)]
309pub enum SearchSettingsType {
310 Video,
311 Channel,
312 Playlist,
313 All,
314}
315
316impl AsUrlString for SearchSettingsType {
317 fn as_url_string(&self) -> String {
318 format!(
319 "type={}",
320 match self {
321 SearchSettingsType::Video => String::from("video"),
322 SearchSettingsType::Channel => String::from("channel"),
323 SearchSettingsType::Playlist => String::from("playlist"),
324 SearchSettingsType::All => String::from("all"),
325 }
326 )
327 }
328}
329
330impl SearchSettingsType {
331 pub fn as_vec() -> Vec<String> {
332 vec![
333 String::from("No Filter"),
334 String::from("Video"),
335 String::from("Channel"),
336 String::from("Playlist"),
337 String::from("All"),
338 ]
339 }
340
341 pub fn index_to_value(index: usize) -> Option<SearchSettingsType> {
342 match index {
343 0 => None,
344 1 => Some(SearchSettingsType::Video),
345 2 => Some(SearchSettingsType::Channel),
346 3 => Some(SearchSettingsType::Playlist),
347 4 => Some(SearchSettingsType::All),
348 _ => unreachable!(),
349 }
350 }
351
352 pub fn value_to_index(value: Option<SearchSettingsType>) -> usize {
353 match value {
354 None => 0,
355 Some(SearchSettingsType::Video) => 1,
356 Some(SearchSettingsType::Channel) => 2,
357 Some(SearchSettingsType::Playlist) => 3,
358 Some(SearchSettingsType::All) => 4,
359 }
360 }
361}