1use serde_json::{json, Value};
4use std::os::unix::net::UnixStream;
5
6use crate::connection::Connection;
7use crate::error::Result;
8use crate::components::*;
9
10pub struct Activity {
12 conn: Connection,
13 aid: i64,
14}
15
16impl Activity {
17 pub fn new(dialog: bool) -> Result<Self> {
22 eprintln!("[DEBUG] Activity::new() - creating connection...");
23 let mut conn = Connection::new()?;
24
25 eprintln!("[DEBUG] Activity::new() - sending newActivity...");
26 let response = conn.send_read(&json!({
27 "method": "newActivity",
28 "params": {
29 "dialog": dialog,
30 "canceloutside": !dialog
31 }
32 }))?;
33
34 eprintln!("[DEBUG] Activity::new() - got response: {:?}", response);
35 let aid = response[0]
37 .as_i64()
38 .ok_or_else(|| crate::error::GuiError::InvalidResponse("Missing aid".to_string()))?;
39
40 eprintln!("[DEBUG] Activity::new() - aid = {}", aid);
41 Ok(Activity { conn, aid })
42 }
43
44 pub fn id(&self) -> i64 {
46 self.aid
47 }
48
49 pub fn send_read(&mut self, msg: &Value) -> Result<Value> {
51 self.conn.send_read(msg)
52 }
53
54 pub fn send(&mut self, msg: &Value) -> Result<()> {
56 self.conn.send(msg)
57 }
58
59 pub fn event_stream(&mut self) -> &mut UnixStream {
61 self.conn.event_stream()
62 }
63
64 pub fn create_linear_layout(&mut self, parent: Option<i64>) -> Result<LinearLayout> {
66 LinearLayout::new(self, parent)
67 }
68
69 pub fn create_linear_layout_horizontal(&mut self, parent: Option<i64>) -> Result<LinearLayout> {
71 LinearLayout::new_with_orientation(self, parent, false)
72 }
73
74 pub fn create_nested_scroll_view(&mut self, parent: Option<i64>) -> Result<NestedScrollView> {
76 NestedScrollView::new(self, parent)
77 }
78
79 pub fn create_text_view(&mut self, text: &str, parent: Option<i64>) -> Result<TextView> {
81 TextView::new(self, text, parent)
82 }
83
84 pub fn create_button(&mut self, text: &str, parent: Option<i64>) -> Result<Button> {
86 Button::new(self, text, parent)
87 }
88
89 pub fn create_edit_text(&mut self, text: &str, parent: Option<i64>) -> Result<EditText> {
91 EditText::new(self, text, parent)
92 }
93
94 pub fn create_edit_text_multiline(&mut self, text: &str, parent: Option<i64>) -> Result<EditText> {
96 EditText::new_multiline(self, text, parent)
97 }
98
99 pub fn create_checkbox(&mut self, text: &str, parent: Option<i64>) -> Result<Checkbox> {
101 Checkbox::new(self, text, parent)
102 }
103
104 pub fn create_checkbox_checked(&mut self, text: &str, parent: Option<i64>, checked: bool) -> Result<Checkbox> {
106 Checkbox::new_with_checked(self, text, parent, checked)
107 }
108
109 pub fn create_switch(&mut self, text: &str, parent: Option<i64>) -> Result<Switch> {
111 Switch::new(self, text, parent)
112 }
113
114 pub fn create_switch_checked(&mut self, text: &str, parent: Option<i64>, checked: bool) -> Result<Switch> {
116 Switch::new_with_checked(self, text, parent, checked)
117 }
118
119 pub fn create_radio_button(&mut self, text: &str, parent: Option<i64>) -> Result<RadioButton> {
121 RadioButton::new(self, text, parent)
122 }
123
124 pub fn create_radio_button_checked(&mut self, text: &str, parent: Option<i64>, checked: bool) -> Result<RadioButton> {
126 RadioButton::new_with_checked(self, text, parent, checked)
127 }
128
129 pub fn create_radio_group(&mut self, parent: Option<i64>) -> Result<RadioGroup> {
131 RadioGroup::new(self, parent)
132 }
133
134 pub fn create_spinner(&mut self, parent: Option<i64>) -> Result<Spinner> {
136 Spinner::new(self, parent)
137 }
138
139 pub fn create_image_view(&mut self, parent: Option<i64>) -> Result<ImageView> {
141 ImageView::new(self, parent)
142 }
143
144 pub fn create_progress_bar(&mut self, parent: Option<i64>) -> Result<ProgressBar> {
146 ProgressBar::new(self, parent)
147 }
148
149 pub fn create_toggle_button(&mut self, text: &str, parent: Option<i64>) -> Result<ToggleButton> {
151 ToggleButton::new(self, text, parent)
152 }
153
154 pub fn create_toggle_button_checked(&mut self, text: &str, parent: Option<i64>, checked: bool) -> Result<ToggleButton> {
156 ToggleButton::new_with_checked(self, text, parent, checked)
157 }
158
159 pub fn create_space(&mut self, parent: Option<i64>) -> Result<Space> {
161 Space::new(self, parent)
162 }
163
164 pub fn create_frame_layout(&mut self, parent: Option<i64>) -> Result<FrameLayout> {
166 FrameLayout::new(self, parent)
167 }
168
169 pub fn create_grid_layout(&mut self, rows: i32, cols: i32, parent: Option<i64>) -> Result<GridLayout> {
171 GridLayout::new(self, rows, cols, parent)
172 }
173
174 pub fn create_horizontal_scroll_view(&mut self, parent: Option<i64>) -> Result<HorizontalScrollView> {
176 HorizontalScrollView::new(self, parent)
177 }
178
179 pub fn create_horizontal_scroll_view_with_params(&mut self, parent: Option<i64>,
181 fillviewport: bool, snapping: bool, nobar: bool) -> Result<HorizontalScrollView> {
182 HorizontalScrollView::new_with_params(self, parent, fillviewport, snapping, nobar)
183 }
184
185 pub fn create_swipe_refresh_layout(&mut self, parent: Option<i64>) -> Result<SwipeRefreshLayout> {
187 SwipeRefreshLayout::new(self, parent)
188 }
189
190 pub fn create_tab_layout(&mut self, parent: Option<i64>) -> Result<TabLayout> {
192 TabLayout::new(self, parent)
193 }
194
195 pub fn create_web_view(&mut self, parent: Option<i64>) -> Result<WebView> {
197 WebView::new(self, parent)
198 }
199
200 pub fn set_title(&mut self, title: &str) -> Result<()> {
202 self.send(&json!({
203 "method": "setTheme",
204 "params": {
205 "aid": self.aid,
206 "statusBarColor": 0,
207 "colorPrimary": 0,
208 "windowBackground": 0,
209 "textColor": 0,
210 "colorAccent": 0
211 }
212 }))?;
213
214 self.send_read(&json!({
215 "method": "setTaskDescription",
216 "params": {
217 "aid": self.aid,
218 "label": title
219 }
220 }))?;
221
222 Ok(())
223 }
224
225 pub fn finish(&mut self) -> Result<()> {
227 self.send_read(&json!({
228 "method": "finishActivity",
229 "params": {
230 "aid": self.aid,
231 "finishing": true
232 }
233 }))?;
234 Ok(())
235 }
236}