1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use super::*;
use crate::favorites::resolve_parent_genre;
impl App {
/// The currently visible list. In Normal mode: library. In Search mode: search results.
pub fn visible_stations(&self) -> Vec<&Station> {
if self.input_mode == InputMode::Search {
return self.search_results.iter().collect();
}
if let Some(genre) = self.library.available_genres.get(self.selected_genre_idx) {
if genre == "All" {
self.library.stations.iter().collect()
} else {
self.library
.stations
.iter()
.filter(|s| resolve_parent_genre(&s.genre).eq_ignore_ascii_case(genre))
.collect()
}
} else {
self.library.stations.iter().collect()
}
}
/// Get the currently playing station, if any.
pub fn now_playing(&self) -> Option<&Station> {
self.playing_url.as_ref().and_then(|url| {
self.library
.stations
.iter()
.find(|s| s.url == *url)
.or_else(|| self.search_results.iter().find(|s| s.url == *url))
.or_else(|| {
self.undo_history.iter().rev().find_map(|(station, _, _)| {
if station.url == *url {
Some(station)
} else {
None
}
})
})
})
}
/// Count visible stations without allocating a Vec.
pub fn visible_count(&self) -> usize {
if self.input_mode == InputMode::Search {
return self.search_results.len();
}
if let Some(genre) = self.library.available_genres.get(self.selected_genre_idx) {
if genre == "All" {
self.library.stations.len()
} else {
self.library
.stations
.iter()
.filter(|s| resolve_parent_genre(&s.genre).eq_ignore_ascii_case(genre))
.count()
}
} else {
self.library.stations.len()
}
}
/// Try to select the currently playing station in the visible list.
pub(super) fn select_playing(&mut self) {
if let Some(ref url) = self.playing_url {
if let Some(pos) = self.visible_stations().iter().position(|s| s.url == *url) {
self.selected = pos;
}
}
}
}