pub struct Cx<E> { /* private fields */ }Expand description
Effects an app requests during update, generic over the app event type so
continuations stay fully typed.
Implementations§
Source§impl<E> Cx<E>
impl<E> Cx<E>
Sourcepub fn with_app_info(info: AppInfo) -> Self
pub fn with_app_info(info: AppInfo) -> Self
A Cx carrying info — for app unit tests that exercise code reading app_info.
Sourcepub fn app_info(&self) -> &AppInfo
pub fn app_info(&self) -> &AppInfo
The app’s own version/build/platform/bundle id, as the shell reported it at startup
(before restore/init), so init can put the build number on the first request.
All fields are empty when the shell predates it. view has no Cx: copy what a screen
shows into the Model in init.
Sourcepub fn notify(
&mut self,
plugin: impl Into<String>,
op: impl Into<String>,
input: impl Into<String>,
)
pub fn notify( &mut self, plugin: impl Into<String>, op: impl Into<String>, input: impl Into<String>, )
Fire-and-forget call to a native plugin.
Sourcepub fn plugin(
&mut self,
plugin: impl Into<String>,
op: impl Into<String>,
input: impl Into<String>,
then: impl FnOnce(PluginResponse) -> E + Send + 'static,
)
pub fn plugin( &mut self, plugin: impl Into<String>, op: impl Into<String>, input: impl Into<String>, then: impl FnOnce(PluginResponse) -> E + Send + 'static, )
Request/response call: when the plugin replies, then(response) produces
the typed event delivered back to your update.
Sourcepub fn subscribe(
&mut self,
key: impl Into<String>,
plugin: impl Into<String>,
op: impl Into<String>,
input: impl Into<String>,
on_event: impl Fn(PluginResponse) -> E + Send + 'static,
)
pub fn subscribe( &mut self, key: impl Into<String>, plugin: impl Into<String>, op: impl Into<String>, input: impl Into<String>, on_event: impl Fn(PluginResponse) -> E + Send + 'static, )
Subscribe to a streaming plugin: the shell starts a native source and delivers
every event it produces to on_event (which fires repeatedly, once per
event), each producing a typed event into your update. key is a
caller-chosen id for this subscription — pass the same key to
unsubscribe to stop it. Call subscribe once per
key (e.g. in init or on a connect event); calling it
again with a live key starts a second source.
Sourcepub fn unsubscribe(&mut self, key: impl Into<String>)
pub fn unsubscribe(&mut self, key: impl Into<String>)
Stop the streaming subscription started under key by subscribe.
The shell tears down the native source registered under key, so it stops
producing events. No-op if key isn’t subscribed.
Sourcepub fn upload(
&mut self,
url: impl Into<String>,
source: impl Into<String>,
) -> TransferBuilder<'_, E>
pub fn upload( &mut self, url: impl Into<String>, source: impl Into<String>, ) -> TransferBuilder<'_, E>
Upload the file at source (a path / content:// / file:// / blob: handle)
as the raw request body. Finish with [TransferBuilder::start]. The terminal
TransferEvent::Done’s Response body is the server’s reply (e.g. {"url": …}), kept
up to 64 KB on every shell.
Sourcepub fn download(
&mut self,
url: impl Into<String>,
dest: impl Into<String>,
) -> TransferBuilder<'_, E>
pub fn download( &mut self, url: impl Into<String>, dest: impl Into<String>, ) -> TransferBuilder<'_, E>
Download url to dest (a sandbox path on iOS/Android; a filename hint on web,
which returns a blob: handle). Finish with [TransferBuilder::start].
Sourcepub fn save(&mut self, data: impl Into<String>)
pub fn save(&mut self, data: impl Into<String>)
Persist data (handed back to MobilerApp::restore on next startup).
The shell stores it as plaintext (UserDefaults, SharedPreferences, localStorage), and on
Android it is included in Auto Backup. Keep secrets (tokens, keys) in the securestore
plugin instead (ADR-0031, ADR-0037).
Sourcepub fn copy(&mut self, text: impl Into<String>)
pub fn copy(&mut self, text: impl Into<String>)
Copy text to the system clipboard (built-in clipboard capability).
Open the system share sheet with text (built-in share capability).
Sourcepub fn open_url(&mut self, url: impl Into<String>)
pub fn open_url(&mut self, url: impl Into<String>)
Open url in the platform browser / default handler (built-in browser
capability). Fire-and-forget: the app leaves the foreground.
Sourcepub fn open_url_then(
&mut self,
url: impl Into<String>,
then: impl FnOnce(PluginResponse) -> E + Send + 'static,
)
pub fn open_url_then( &mut self, url: impl Into<String>, then: impl FnOnce(PluginResponse) -> E + Send + 'static, )
Like open_url, but then learns whether it opened: ok: true when the
system handed the link to an app; ok: false with a reason ("no app can open this link",
"invalid url", "blocked" — a web pop-up blocker —, "cancelled" — the user declined the
system’s confirmation) otherwise — e.g. tel: on a device without a dialer:
cx.open_url_then("tel:+381601234567", |r| Msg::Dialed(r.ok)). A browser can’t tell whether
the desktop can place a call, so on web ok only means the link was handed off.
Sourcepub fn toast(&mut self, text: impl Into<String>)
pub fn toast(&mut self, text: impl Into<String>)
Show a transient toast / snackbar with text (built-in toast capability).
Sourcepub fn haptic(&mut self, style: impl Into<String>)
pub fn haptic(&mut self, style: impl Into<String>)
Fire a haptic tap (built-in haptics capability). style is "light",
"medium", or "heavy"; unknown styles fall back to medium.
Sourcepub fn request(
&mut self,
method: impl Into<String>,
url: impl Into<String>,
) -> RequestBuilder<'_, E>
pub fn request( &mut self, method: impl Into<String>, url: impl Into<String>, ) -> RequestBuilder<'_, E>
Start an HTTP request with full control — headers, and later timeouts and
query params — finished with [RequestBuilder::send].
cx.request("PUT", url)
.bearer(&token)
.body(json)
.send(|outcome| match outcome.status() {
Some(409) => Event::NeedsRebase,
Some(s) if outcome.is_success() => Event::Saved,
Some(s) => Event::ServerError(s),
None => Event::Offline,
});Sourcepub fn get(
&mut self,
url: impl Into<String>,
then: impl FnOnce(HttpOutcome) -> E + Send + 'static,
)
pub fn get( &mut self, url: impl Into<String>, then: impl FnOnce(HttpOutcome) -> E + Send + 'static, )
GET url, delivering the outcome to then.
Sourcepub fn post(
&mut self,
url: impl Into<String>,
body: impl Into<String>,
then: impl FnOnce(HttpOutcome) -> E + Send + 'static,
)
pub fn post( &mut self, url: impl Into<String>, body: impl Into<String>, then: impl FnOnce(HttpOutcome) -> E + Send + 'static, )
POST url with body, delivering the outcome to then.
Sourcepub fn put(
&mut self,
url: impl Into<String>,
body: impl Into<String>,
then: impl FnOnce(HttpOutcome) -> E + Send + 'static,
)
pub fn put( &mut self, url: impl Into<String>, body: impl Into<String>, then: impl FnOnce(HttpOutcome) -> E + Send + 'static, )
PUT url with body, delivering the outcome to then.
Sourcepub fn patch(
&mut self,
url: impl Into<String>,
body: impl Into<String>,
then: impl FnOnce(HttpOutcome) -> E + Send + 'static,
)
pub fn patch( &mut self, url: impl Into<String>, body: impl Into<String>, then: impl FnOnce(HttpOutcome) -> E + Send + 'static, )
PATCH url with body, delivering the outcome to then.
Sourcepub fn delete(
&mut self,
url: impl Into<String>,
then: impl FnOnce(HttpOutcome) -> E + Send + 'static,
)
pub fn delete( &mut self, url: impl Into<String>, then: impl FnOnce(HttpOutcome) -> E + Send + 'static, )
DELETE url, delivering the outcome to then.
Sourcepub fn device_model(
&mut self,
then: impl FnOnce(PluginResponse) -> E + Send + 'static,
)
pub fn device_model( &mut self, then: impl FnOnce(PluginResponse) -> E + Send + 'static, )
Query the device model/name via the built-in device capability; the result
(response.output, e.g. “Google Pixel 7” / “Apple iPhone (iOS 18.0)”) is
delivered to then.
Sourcepub fn device_info(
&mut self,
then: impl FnOnce(Option<DeviceInfo>) -> E + Send + 'static,
)
pub fn device_info( &mut self, then: impl FnOnce(Option<DeviceInfo>) -> E + Send + 'static, )
The phone’s OS version and model (DeviceInfo) via the built-in device capability, for
a support message or bug report. None when the shell can’t answer: the web shell, or a
native shell that predates this call. Unlike device_model’s display string, every field is separate.
Sourcepub fn device_locale(
&mut self,
then: impl FnOnce(PluginResponse) -> E + Send + 'static,
)
pub fn device_locale( &mut self, then: impl FnOnce(PluginResponse) -> E + Send + 'static, )
Query the device’s preferred locale as a BCP-47 language tag (e.g. "de-CH", "en-US")
via the built-in device capability; then receives it in response.output. Pair with
Locale::from_tag to choose the app’s language /
formatting locale at startup. Works on iOS, Android, and web.
Sourcepub fn system_appearance(
&mut self,
then: impl FnOnce(PluginResponse) -> E + Send + 'static,
)
pub fn system_appearance( &mut self, then: impl FnOnce(PluginResponse) -> E + Send + 'static, )
The OS light/dark setting ("light" / "dark" in response.output) via the built-in
device capability — the system’s value even while the app forces one with with_appearance.
Sourcepub fn subscribe_appearance(
&mut self,
key: impl Into<String>,
on_event: impl Fn(PluginResponse) -> E + Send + 'static,
)
pub fn subscribe_appearance( &mut self, key: impl Into<String>, on_event: impl Fn(PluginResponse) -> E + Send + 'static, )
Subscribe to the OS light/dark setting: on_event gets "light" / "dark" once right away,
then on every change. Stop it with unsubscribe(key).
Sourcepub fn pick_photo(
&mut self,
then: impl FnOnce(PluginResponse) -> E + Send + 'static,
)
pub fn pick_photo( &mut self, then: impl FnOnce(PluginResponse) -> E + Send + 'static, )
Let the user pick an image (built-in photo capability — the system photo
picker, no permission required). then receives the result: on success
response.ok is true and response.output is a local image URI you can
hand straight to the image(...) widget; on cancel, ok is false.
Sourcepub fn capture_photo(
&mut self,
then: impl FnOnce(PluginResponse) -> E + Send + 'static,
)
pub fn capture_photo( &mut self, then: impl FnOnce(PluginResponse) -> E + Send + 'static, )
Capture a photo with the device camera (built-in camera capability — launches
the system camera). then receives the result: on success response.ok is
true and response.output is a local image URI you can hand straight to the
image(...) widget; on cancel, ok is false. iOS requires an
NSCameraUsageDescription (the template ships one, opt-in); Android captures via
the system camera app, so no extra runtime permission is needed.
Sourcepub fn pick_photo_with(
&mut self,
options: PhotoOptions,
then: impl FnOnce(Result<Photo, PhotoError>) -> E + Send + 'static,
)
pub fn pick_photo_with( &mut self, options: PhotoOptions, then: impl FnOnce(Result<Photo, PhotoError>) -> E + Send + 'static, )
pick_photo with per-call options: the shell re-encodes the picked image to
the format and limits asked for (applying its orientation and, by default, dropping its EXIF and
GPS data) and delivers a Photo (a new file handle plus MIME, size and dimensions), or a
PhotoError. The rules are ADR-0045’s; pick_photo itself is unchanged. The handle is
short-lived (ADR-0049, see Photo): upload it soon, don’t keep it in saved state.
Sourcepub fn capture_photo_with(
&mut self,
options: PhotoOptions,
then: impl FnOnce(Result<Photo, PhotoError>) -> E + Send + 'static,
)
pub fn capture_photo_with( &mut self, options: PhotoOptions, then: impl FnOnce(Result<Photo, PhotoError>) -> E + Send + 'static, )
capture_photo with per-call options, like
pick_photo_with.
Sourcepub fn confirm(
&mut self,
title: impl Into<String>,
message: impl Into<String>,
then: impl FnOnce(PluginResponse) -> E + Send + 'static,
)
pub fn confirm( &mut self, title: impl Into<String>, message: impl Into<String>, then: impl FnOnce(PluginResponse) -> E + Send + 'static, )
Ask the user to confirm via a native dialog (built-in dialog capability).
then receives the choice: response.ok is true if confirmed, false if
cancelled/dismissed. Resolves asynchronously (the user replies whenever).
Sourcepub fn confirm_with(
&mut self,
dialog: Confirm,
then: impl FnOnce(PluginResponse) -> E + Send + 'static,
)
pub fn confirm_with( &mut self, dialog: Confirm, then: impl FnOnce(PluginResponse) -> E + Send + 'static, )
As confirm, with the app’s own button labels and an optional destructive
style — e.g. Confirm::new("Cancel booking?", msg).confirm_label("Cancel booking") .cancel_label("Keep it").destructive(). Same response: ok is true only if confirmed.
Sourcepub fn snackbar(
&mut self,
snackbar: Snackbar,
then: impl FnOnce(PluginResponse) -> E + Send + 'static,
)
pub fn snackbar( &mut self, snackbar: Snackbar, then: impl FnOnce(PluginResponse) -> E + Send + 'static, )
Show a snackbar (built-in snackbar capability) above the bottom navigation and the FAB,
e.g. cx.snackbar(Snackbar::new("Booking cancelled").action("Undo"), |r| Msg::Undo(r.ok)).
then gets ok: true (output "action") when its action is tapped; otherwise ok: false
with output "timeout", "dismissed" (swiped away) or "replaced" (a newer snackbar). With
no scaffold on screen there’s nothing to show it on, and it resolves "timeout" at once.
Sourcepub fn pick_date(
&mut self,
then: impl FnOnce(PluginResponse) -> E + Send + 'static,
)
pub fn pick_date( &mut self, then: impl FnOnce(PluginResponse) -> E + Send + 'static, )
Let the user pick a date via the native date picker (built-in datetime
capability). On success response.ok is true and response.output is the
chosen date as an ISO YYYY-MM-DD string; on cancel/dismiss, ok is false.
Resolves asynchronously (the user replies whenever).
Sourcepub fn pick_time(
&mut self,
then: impl FnOnce(PluginResponse) -> E + Send + 'static,
)
pub fn pick_time( &mut self, then: impl FnOnce(PluginResponse) -> E + Send + 'static, )
Let the user pick a time via the native time picker (built-in datetime
capability). On success response.ok is true and response.output is the
chosen time as a 24-hour HH:MM string; on cancel/dismiss, ok is false.
Resolves asynchronously (the user replies whenever).
Sourcepub fn pick_date_with(
&mut self,
picker: Picker,
then: impl FnOnce(PluginResponse) -> E + Send + 'static,
)
pub fn pick_date_with( &mut self, picker: Picker, then: impl FnOnce(PluginResponse) -> E + Send + 'static, )
As pick_date, with the app’s own title and button labels.
Sourcepub fn pick_time_with(
&mut self,
picker: Picker,
then: impl FnOnce(PluginResponse) -> E + Send + 'static,
)
pub fn pick_time_with( &mut self, picker: Picker, then: impl FnOnce(PluginResponse) -> E + Send + 'static, )
As pick_time, with the app’s own title and button labels.
Sourcepub fn now(&mut self, then: impl FnOnce(PluginResponse) -> E + Send + 'static)
pub fn now(&mut self, then: impl FnOnce(PluginResponse) -> E + Send + 'static)
Read the current local date-time (built-in datetime capability). The core is a
pure state machine and can’t read the clock itself, so stamping an event with “now”
— a ledger entry, a log line — goes through the shell. response.output is the local
date-time as YYYY-MM-DD HH:MM:SS (sortable lexicographically; the date is the first
10 chars). No UI — resolves immediately. Pair with pick_date when
the user should choose a different date instead.