use mobiler_core::*;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone)]
pub enum Msg {
EnableStore,
Products(PluginResponse),
Buy(String),
Started(PluginResponse),
Txn(PluginResponse),
Restore,
Validated(PluginResponse),
Finished(PluginResponse),
}
#[derive(Default, Serialize, Deserialize)]
pub struct Model {
pub products_json: String,
pub owned: Vec<String>,
}
impl MyApp {
fn handle(&self, msg: Msg, model: &mut Model, cx: &mut Cx<Msg>) {
match msg {
Msg::EnableStore => {
cx.subscribe("iap", "iap", "transactions", "", Msg::Txn);
cx.plugin("iap", "products", r#"["pro_month","coins_100"]"#, Msg::Products);
}
Msg::Products(r) => {
if r.ok {
model.products_json = r.output; }
}
Msg::Buy(id) => cx.plugin("iap", "purchase", &id, Msg::Started),
Msg::Started(_) => {} Msg::Txn(r) => {
if r.ok {
cx.post("https://api.example.com/iap/validate", r.output.clone(), Msg::Validated);
}
}
Msg::Validated(r) => {
if r.ok {
let txn_id = r.output.clone();
cx.plugin("iap", "finish", &txn_id, Msg::Finished);
}
}
Msg::Finished(_) => {}
Msg::Restore => cx.plugin("iap", "restore", "", Msg::Started),
}
}
}