The draft is retained local state. A valid submission creates a shared record; an invalid submission changes only validation feedback and leaves any previous record intact. The small pure prepare_record function is used by the actual reducer and narrow unit tests.
FormControl owns the label, required state and validation relationship. A nearby red string alone would not give the native accessibility tree the same validation information. The semantic identifiers name a control's meaning, not its current English label.
{"id":"fission-tabs-1","tabs":[{"index":0,"label":"src/app.rs","value":"src-app-rs"},{"index":1,"label":"src/app/state.rs","value":"src-app-state-rs"},{"index":2,"label":"src/app/screen.rs","value":"src-app-screen-rs"},{"index":3,"label":"src/i18n.rs","value":"src-i18n-rs"},{"index":4,"label":"src/lib.rs","value":"src-lib-rs"},{"index":5,"label":"preview/lib.rs","value":"preview-lib-rs"},{"index":6,"label":"i18n/en-US.yaml","value":"i18n-en-us-yaml"},{"index":7,"label":"i18n/es-ES.yaml","value":"i18n-es-es-yaml"}]}
{"id":"fission-tabs-1","index":0,"label":"src/app.rs","value":"src-app-rs"}
mod screen;
pub mod state;
pub use screen::InspectionScreen;
pub use state::InspectionState;
{"id":"fission-tabs-1","index":1,"label":"src/app/state.rs","value":"src-app-state-rs"}
use fission::prelude::*;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct InspectionRecord {
pub asset: String,
pub note: String,
}
pub fn prepare_record(asset: &str, note: &str) -> Option<InspectionRecord> {
let (asset, note) = (asset.trim(), note.trim());
if asset.is_empty() || note.is_empty() {
return None;
}
Some(InspectionRecord {
asset: asset.to_owned(),
note: note.to_owned(),
})
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct InspectionState {
pub recorded: Option<InspectionRecord>,
pub locale: Locale,
}
impl Default for InspectionState {
fn default() -> Self {
Self {
recorded: None,
locale: Locale::from("en-US"),
}
}
}
impl GlobalState for InspectionState {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn incomplete_input_never_becomes_a_record() {
assert_eq!(prepare_record("Pump 4", " "), None);
assert_eq!(prepare_record(" ", "Seal is intact"), None);
}
#[test]
fn accepted_record_contains_trimmed_user_input() {
assert_eq!(
prepare_record(" Pump 4 ", " Seal is intact "),
Some(InspectionRecord {
asset: "Pump 4".into(),
note: "Seal is intact".into(),
})
);
}
}
{"id":"fission-tabs-1","index":2,"label":"src/app/screen.rs","value":"src-app-screen-rs"}
use super::state::{prepare_record, InspectionState};
use fission::prelude::*;
fission_component! {
pub InspectionScreen<InspectionState> {
state {
asset: String = String::new(),
note: String = String::new(),
attempted: bool = false,
}
}
view(form, state, cx) {
let tokens = &cx.view.env().theme.tokens;
let asset_error = (form.attempted && form.asset.trim().is_empty())
.then(|| cx.tr("inspection.asset_required"));
let note_error = (form.attempted && form.note.trim().is_empty())
.then(|| cx.tr("inspection.note_required"));
} => SafeArea(child: Scroll(child: Container(
Column(gap: tokens.spacing.m, children: [
Text(cx.tr("inspection.title")).size(tokens.typography.heading2_size),
Text(cx.tr("inspection.explanation")),
Row(gap: tokens.spacing.s, children: [
Button(child: Text(cx.tr("language.english")), key: "english",
on_press: reduce { state.locale = Locale::from("en-US"); }),
Button(child: Text(cx.tr("language.spanish")), key: "spanish",
on_press: reduce { state.locale = Locale::from("es-ES"); }),
]),
FormControl(
label: Some(cx.tr("inspection.asset")), required: true, error: asset_error,
child: TextInput(
value: form.asset.clone(), bind(on_input): to(form.asset),
semantics_identifier: Some("inspection.asset".into()),
),
),
FormControl(
label: Some(cx.tr("inspection.note")), required: true, error: note_error,
child: TextInput(
value: form.note.clone(), bind(on_input): to(form.note),
multiline: true, keyboard_type: TextInputType::Multiline,
semantics_identifier: Some("inspection.note".into()),
),
),
Button(
child: Text(cx.tr("inspection.record")), key: "record-inspection",
on_press: reduce {
form.attempted = true;
if let Some(record) = prepare_record(&form.asset, &form.note) {
state.recorded = Some(record);
form.asset.clear();
form.note.clear();
form.attempted = false;
}
},
).semantics_identifier("inspection.record"),
if let Some(record) = &state.recorded {
SemanticsRegion(
identifier: Some("inspection.result".into()), role: Role::Status,
child: Column(gap: tokens.spacing.s, children: [
Text(cx.tr("inspection.recorded")),
Text(record.asset.clone()).weight(tokens.typography.font_weight_semibold),
Text(record.note.clone()),
]),
)
} else {
Text(cx.tr("inspection.empty"))
},
Text(cx.tr("inspection.persistence")).color(tokens.colors.text_secondary),
])
).padding_all(tokens.spacing.l)))
}
{"id":"fission-tabs-1","index":3,"label":"src/i18n.rs","value":"src-i18n-rs"}
use fission::i18n::{Locale, TranslationBundle};
use fission::prelude::*;
use std::collections::HashMap;
pub fn create_env() -> Env {
let mut env = Env::default();
env.locale = Locale::from("en-US");
for (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 task translations");
env.i18n.add_bundle(TranslationBundle {
locale: Locale::from(locale),
messages,
});
}
env
}
pub fn sync_env(state: &crate::app::InspectionState, env: &mut Env) {
env.locale = state.locale.clone();
}
{"id":"fission-tabs-1","index":4,"label":"src/lib.rs","value":"src-lib-rs"}
pub mod app;
mod i18n;
use app::{InspectionScreen, InspectionState};
use fission::prelude::*;
#[cfg(not(any(target_arch = "wasm32", target_os = "android", target_os = "ios")))]
pub fn run_desktop() -> anyhow::Result<()> {
DesktopApp::<InspectionState, _>::new(InspectionScreen)
.with_env(i18n::create_env())
.with_sync_env(i18n::sync_env)
.run()
}
#[cfg(any(target_os = "android", target_os = "ios"))]
pub fn run_mobile() -> anyhow::Result<()> {
MobileApp::<InspectionState, _>::new(InspectionScreen)
.with_env(i18n::create_env())
.with_sync_env(i18n::sync_env)
.run()
}
#[cfg(target_os = "android")]
#[no_mangle]
fn android_main(app_handle: AndroidApp) {
let app = MobileApp::<InspectionState, _>::new(InspectionScreen)
.with_env(i18n::create_env())
.with_sync_env(i18n::sync_env);
#[cfg(debug_assertions)]
let app = app.with_test_control_port(48761);
let _ = 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::<InspectionState, _>::new(InspectionScreen)
.with_env(i18n::create_env())
.with_sync_env(i18n::sync_env)
.run()
.map_err(|error| wasm_bindgen::JsValue::from_str(&error.to_string()))
}
{"id":"fission-tabs-1","index":5,"label":"preview/lib.rs","value":"preview-lib-rs"}
mod app;
mod i18n;
use fission::prelude::*;
use wasm_bindgen::prelude::*;
#[wasm_bindgen(start)]
pub fn run() -> Result<(), JsValue> {
console_error_panic_hook::set_once();
WebApp::<app::InspectionState, _>::new(app::InspectionScreen)
.with_env(i18n::create_env())
.with_sync_env(i18n::sync_env)
.mount("#fission-web-mount")
.run()
.map_err(|error| JsValue::from_str(&error.to_string()))
}
{"id":"fission-tabs-1","index":6,"label":"i18n/en-US.yaml","value":"i18n-en-us-yaml"}
inspection.title: "Record an inspection"
inspection.explanation: "Name the asset you checked and describe what you found. No preloaded work order is assumed."
inspection.asset: "Asset or work reference"
inspection.note: "Inspection note"
inspection.asset_required: "Enter the asset or work reference."
inspection.note_required: "Add an inspection note."
inspection.record: "Record for this session"
inspection.recorded: "Inspection recorded for this session."
inspection.empty: "No inspection has been recorded."
inspection.persistence: "This record is kept in memory. It has not been written to a database or sent to a service."
language.english: "English"
language.spanish: "Español"
{"id":"fission-tabs-1","index":7,"label":"i18n/es-ES.yaml","value":"i18n-es-es-yaml"}
inspection.title: "Registrar una inspección"
inspection.explanation: "Indica el equipo revisado y describe lo que observaste. No se presupone ninguna orden precargada."
inspection.asset: "Equipo o referencia de trabajo"
inspection.note: "Nota de inspección"
inspection.asset_required: "Introduce el equipo o la referencia de trabajo."
inspection.note_required: "Añade una nota de inspección."
inspection.record: "Registrar para esta sesión"
inspection.recorded: "Inspección registrada para esta sesión."
inspection.empty: "No se ha registrado ninguna inspección."
inspection.persistence: "El registro se conserva en memoria. No se ha escrito en una base de datos ni enviado a un servicio."
language.english: "Inglés"
language.spanish: "Español"
{"id":"fission-tabs-1"}