use mobiler_core::*;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone)]
pub enum Msg {
ScanPressed,
Scanned(PluginResponse),
}
#[derive(Default, Serialize, Deserialize)]
pub struct Model {
pub last_code: Option<String>,
pub banner: Option<String>,
}
impl MyApp {
fn handle(&self, msg: Msg, model: &mut Model, cx: &mut Cx<Msg>) {
match msg {
Msg::ScanPressed => cx.plugin("scanner", "scan", "", Msg::Scanned),
Msg::Scanned(resp) => {
if resp.ok {
match resp.output.split_once(':') {
Some(("ean13", code)) | Some(("ean8", code)) | Some(("upca", code)) => {
model.last_code = Some(code.to_string());
}
Some(("qr", payload)) => {
model.last_code = Some(payload.to_string());
}
Some((_, value)) => model.last_code = Some(value.to_string()),
None => model.last_code = Some(resp.output.clone()),
}
} else {
model.banner = Some(format!("Scan didn't complete: {}", resp.output));
}
}
}
}
}