1use ratatui_image::protocol::StatefulProtocol;
4use sonos_sdk::{GroupId, PlaybackState, SonosSystem, SpeakerId};
5
6#[derive(Clone, Debug, PartialEq)]
8pub enum ListEntry {
9 GroupHeader(GroupId),
10 SpeakerRow(SpeakerId),
11 AddToGroupRow(GroupId),
13 CreateNewGroupRow,
15}
16
17#[derive(Clone, Debug)]
19pub struct PickUpState {
20 pub speaker_id: SpeakerId,
21 pub original_group_id: Option<GroupId>,
22}
23
24pub enum SpeakerListAction {
26 Handled,
27 FocusTabBar,
28}
29
30pub fn build_list_entries(system: &SonosSystem, pick_up: Option<&PickUpState>) -> Vec<ListEntry> {
39 let groups = system.groups();
40 let mut entries = Vec::new();
41
42 for group in &groups {
44 if group.is_standalone() {
45 continue;
46 }
47 entries.push(ListEntry::GroupHeader(group.id.clone()));
48 for member in group.members() {
49 entries.push(ListEntry::SpeakerRow(member.id.clone()));
50 }
51 if pick_up.is_some() {
52 entries.push(ListEntry::AddToGroupRow(group.id.clone()));
53 }
54 }
55
56 for group in &groups {
58 if !group.is_standalone() {
59 continue;
60 }
61 entries.push(ListEntry::GroupHeader(group.id.clone()));
62 if let Some(coord) = group.coordinator() {
63 entries.push(ListEntry::SpeakerRow(coord.id.clone()));
64 }
65 if pick_up.is_some() {
66 entries.push(ListEntry::AddToGroupRow(group.id.clone()));
67 }
68 }
69
70 if pick_up.is_some() {
71 entries.push(ListEntry::CreateNewGroupRow);
72 }
73
74 entries
75}
76
77pub fn group_for_entry(entries: &[ListEntry], index: usize) -> Option<GroupId> {
79 if index >= entries.len() {
80 return None;
81 }
82 if let ListEntry::AddToGroupRow(gid) = &entries[index] {
83 return Some(gid.clone());
84 }
85 if matches!(&entries[index], ListEntry::CreateNewGroupRow) {
86 return None;
87 }
88 for i in (0..=index).rev() {
89 if let ListEntry::GroupHeader(gid) = &entries[i] {
90 return Some(gid.clone());
91 }
92 }
93 None
94}
95
96pub struct BottomBarData {
98 pub group_name: String,
99 pub track_title: Option<String>,
100 pub track_artist: Option<String>,
101 pub track_album: Option<String>,
102 pub album_art_protocol: Option<StatefulProtocol>,
103 pub playback_state: Option<PlaybackState>,
104 pub progress: f64,
105 pub position_ms: u64,
106 pub duration_ms: u64,
107 pub volume: u16,
108 pub is_wide: bool,
109 pub group_volume_active: bool,
112}
113
114pub struct SpeakerListData {
116 pub entries: Vec<ListEntry>,
117 pub entry_data: Vec<EntryRenderData>,
118 pub selected_index: usize,
119 pub picked_up_speaker_id: Option<SpeakerId>,
121}
122
123pub struct EntryRenderData {
125 pub name: String,
126 pub model_name: Option<String>,
127 pub speaker_volume: Option<u16>,
128 pub group_volume: Option<u16>,
129 pub playback_state: Option<PlaybackState>,
130 pub track_info: Option<String>,
131 pub is_last_in_group: bool,
133 pub is_home_group: bool,
135}
136
137pub enum SettingsAction {
139 Handled,
140 FocusTabBar,
141}
142
143pub struct SettingsItem {
145 pub label: &'static str,
146 pub value: String,
147 pub options: Vec<String>,
148}
149
150pub struct SettingsData {
152 pub items: Vec<SettingsItem>,
153 pub selected_row: usize,
154 pub dropdown_open: bool,
155 pub dropdown_index: usize,
156}