use serde_json::json;
use crate::activity::Activity;
use crate::view::View;
use crate::error::Result;
pub struct Checkbox {
view: View,
aid: i64,
}
impl Checkbox {
pub fn new(activity: &mut Activity, text: &str, parent: Option<i64>) -> Result<Self> {
Self::new_with_checked(activity, text, parent, false)
}
pub fn new_with_checked(activity: &mut Activity, text: &str, parent: Option<i64>, checked: bool) -> Result<Self> {
let mut params = json!({
"aid": activity.id(),
"text": text,
"checked": checked
});
if let Some(parent_id) = parent {
params["parent"] = json!(parent_id);
}
let response = activity.send_read(&json!({
"method": "createCheckbox",
"params": params
}))?;
let id = response
.as_i64()
.ok_or_else(|| crate::error::GuiError::InvalidResponse("Invalid id".to_string()))?;
Ok(Checkbox {
view: View::new(id),
aid: activity.id(),
})
}
pub fn id(&self) -> i64 {
self.view.id()
}
pub fn view(&self) -> &View {
&self.view
}
pub fn set_text(&self, activity: &mut Activity, text: &str) -> Result<()> {
activity.send(&json!({
"method": "setText",
"params": {
"aid": self.aid,
"id": self.view.id(),
"text": text
}
}))?;
Ok(())
}
pub fn set_checked(&self, activity: &mut Activity, checked: bool) -> Result<()> {
activity.send(&json!({
"method": "setChecked",
"params": {
"aid": self.aid,
"id": self.view.id(),
"checked": checked
}
}))?;
Ok(())
}
}