2. Share state across Android and iOS
Use shared state and component modules for the product workflow. Give touch targets comfortable space, make the main action reachable without precise gestures, and keep labels visible when a control is selected. Check the same route or screen with long translated text.
The work order is shared application data, so changing its status updates GlobalState. The inline reducer stays beside the action here because this tutorial uses it only once; a transition shared by several screens should be a named reducer.
{"id":"fission-tabs-1","tabs":[{"index":0,"label":"src/app.rs","value":"src-app-rs"},{"index":1,"label":"src/state.rs","value":"src-state-rs"},{"index":2,"label":"src/translations.rs","value":"src-translations-rs"},{"index":3,"label":"src/lib.rs","value":"src-lib-rs"},{"index":4,"label":"preview/lib.rs","value":"preview-lib-rs"}]}
{"id":"fission-tabs-1","index":0,"label":"src/app.rs","value":"src-app-rs"}
use crate::state::*;
use crate::assignment::AssignmentForm;
use fission::prelude::*;
fission_component! {
pub MobileTaskPlanner<MobileTaskState>
view(_planner, state, cx) {
let tokens = &cx.view.env().theme.tokens;
let shift_status = match &state.task {
Some(task) if task.complete => cx.tr("shift.no_due"),
Some(_) => cx.tr("shift.one_due"),
None => cx.tr("shift.no_work"),
};
} =>
Container(SafeArea(
child: Scroll(
direction: FlexDirection::Column,
show_scrollbar: true,
flex_grow: 1.0,
child: Container(
SemanticsRegion(
identifier: Some("mobile-field-work".to_string()),
label: Some(cx.tr("work.screen_label")),
child: Column(gap: tokens.spacing.l, children: [
Row(gap: tokens.spacing.m, align_items: ir_op::AlignItems::Center, children: [
Column(gap: tokens.spacing.xs, children: [
Text(cx.tr("shift.team"))
.weight(tokens.typography.font_weight_bold),
Text(cx.tr("shift.morning"))
.color(tokens.colors.text_secondary),
]),
Spacer(flex_grow: 1.0),
Badge(text: shift_status),
]),
match &state.task {
Some(task) => Container(Column(gap: tokens.spacing.m, children: [
Row(gap: tokens.spacing.s, wrap: ir_op::FlexWrap::Wrap, children: [
Text(task.reference.clone())
.weight(tokens.typography.font_weight_semibold),
Badge(text: if task.complete {
cx.tr("work.complete")
} else {
format!("{} {}", cx.tr("work.due"), task.due)
}),
]),
Text(task.title.clone())
.size(tokens.typography.heading2_size)
.weight(tokens.typography.font_weight_bold),
Text(format!("{} · {}", task.site, task.equipment))
.color(tokens.colors.text_secondary),
Text(task.instructions.clone()),
Button(
child: Text(if task.complete {
cx.tr("work.action.reopen")
} else {
cx.tr("work.action.complete")
}),
key: "toggle-inspection",
on_press: reduce {
if let Some(task) = state.task.as_mut() {
task.complete = !task.complete;
}
},
)
.semantics_identifier("mobile-work-order.complete".to_string()),
]))
.padding_all(tokens.spacing.l)
.bg(tokens.colors.surface)
.border(tokens.colors.border, 1.0)
.border_radius(tokens.radii.large),
None => AssignmentForm(),
},
]),
),
)
.padding_all(tokens.spacing.l),
),
))
.height_length(Length::vh(100.0))
.bg(tokens.colors.background)
}
{"id":"fission-tabs-1","index":1,"label":"src/state.rs","value":"src-state-rs"}
use fission::prelude::*;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct InspectionTask {
pub reference: String,
pub title: String,
pub site: String,
pub equipment: String,
pub due: String,
pub instructions: String,
pub complete: bool,
pub latest_note: Option<String>,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct MobileTaskState {
pub task: Option<InspectionTask>,
}
impl GlobalState for MobileTaskState {}
{"id":"fission-tabs-1","index":2,"label":"src/translations.rs","value":"src-translations-rs"}
use std::collections::HashMap;
use fission::i18n::{Locale, TranslationBundle};
use fission::prelude::*;
pub fn create_env(locale: &str) -> Env {
let mut env = Env::default();
env.locale = Locale::from(locale);
for (bundle_locale, source) in [
("en-US", include_str!("../i18n/en-US.yaml")),
("es-ES", include_str!("../i18n/es-ES.yaml")),
] {
let messages: HashMap<String, String> =
serde_yaml::from_str(source).expect("valid work-order translations");
env.i18n.add_bundle(TranslationBundle {
locale: Locale::from(bundle_locale),
messages,
});
}
env
}
{"id":"fission-tabs-1","index":3,"label":"src/lib.rs","value":"src-lib-rs"}
mod app;
mod assignment;
mod state;
mod translations;
use fission::prelude::*;
#[cfg(not(any(target_arch = "wasm32", target_os = "android", target_os = "ios")))]
pub fn run_desktop() -> anyhow::Result<()> {
DesktopApp::<state::MobileTaskState, _>::new(app::MobileTaskPlanner)
.with_env(translations::create_env("en-US"))
.run()
}
#[cfg(any(target_os = "android", target_os = "ios"))]
fn mobile_app() -> MobileApp<state::MobileTaskState, app::MobileTaskPlanner> {
let app = MobileApp::<state::MobileTaskState, _>::new(app::MobileTaskPlanner)
.with_env(translations::create_env("en-US"))
.with_title("Field work");
#[cfg(target_os = "android")]
let app = app.with_test_control_port(48761);
app
}
#[cfg(any(target_os = "android", target_os = "ios"))]
pub fn run_mobile() -> anyhow::Result<()> { mobile_app().run() }
#[cfg(target_os = "android")]
#[no_mangle]
fn android_main(app_handle: AndroidApp) {
let _ = mobile_app().run_with_android_app(app_handle);
}
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen::prelude::wasm_bindgen(start)]
pub fn run_web() -> Result<(), wasm_bindgen::JsValue> {
console_error_panic_hook::set_once();
WebApp::<state::MobileTaskState, _>::new(app::MobileTaskPlanner)
.with_env(translations::create_env("en-US"))
.with_title("Field work")
.run()
.map_err(|error| wasm_bindgen::JsValue::from_str(&error.to_string()))
}
{"id":"fission-tabs-1","index":4,"label":"preview/lib.rs","value":"preview-lib-rs"}
mod app;
mod assignment;
mod state;
mod translations;
use fission::prelude::*;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(start)]
pub fn run() -> Result<(), JsValue> {
console_error_panic_hook::set_once();
WebApp::<state::MobileTaskState, _>::new(app::MobileTaskPlanner)
.with_env(translations::create_env("en-US"))
.with_state_init(|state| {
state.task = Some(state::InspectionTask {
reference: "WO-1048".to_string(),
title: "Cold-room compressor inspection".to_string(),
site: "Northbank Food Hub".to_string(),
equipment: "CMP-7A-2219".to_string(),
due: "10:30".to_string(),
instructions: "Check the alarm panel and record the pressure reading before the morning delivery arrives.".to_string(),
complete: false,
latest_note: None,
});
})
.with_title("Build Android and iOS apps in Rust")
.mount("#fission-web-mount")
.run()
.map_err(|error| JsValue::from_str(&error.to_string()))
}
{"id":"fission-tabs-1"}
Share state across Android and iOS: Mark the inspection complete and confirm the status and action label change together.
Show the app previewHide the app preview
Show the app preview
Hide the app preview