use mobiler_core::*;
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Serialize, Deserialize, Clone)]
pub enum Msg {
EnablePush,
PushToken(PluginResponse),
PushEvent(PluginResponse),
Posted(PluginResponse),
}
#[derive(Default, Serialize, Deserialize)]
pub struct Model {
pub token: Option<String>,
pub last_event: Option<String>,
}
impl MyApp {
fn handle(&self, msg: Msg, model: &mut Model, cx: &mut Cx<Msg>) {
match msg {
Msg::EnablePush => {
cx.plugin("push", "register", "", Msg::PushToken);
cx.subscribe("push", "push", "events", "", Msg::PushEvent);
}
Msg::PushToken(r) => {
if r.ok {
model.token = Some(r.output.clone());
cx.post("https://api.example.com/devices", r.output, Msg::Posted);
}
}
Msg::PushEvent(r) => {
if !r.ok {
return;
}
let text = r.as_text().unwrap_or_default().to_string();
model.last_event = Some(text.clone());
let Ok(event) = serde_json::from_str::<Value>(&text) else { return };
match event["mobiler_push"].as_str() {
Some("opened") => { }
Some("received") => { }
_ => {}
}
}
Msg::Posted(_) => {}
}
}
}