From 50ad34bfef6f73ca20f5f9724b5f6d1daeb497dd Mon Sep 17 00:00:00 2001 From: Dwiki Riyadi Date: Thu, 19 Nov 2026 09:08:25 +0700 Subject: [PATCH 1/2] docs: app services and the SV layer on the usage site, 1.1.0 (#86) The Presentation page gets an App services section (the service, the host, and the Riverpod wiring) with a Kotlin and a Dart sample each, copied from the libraries' region-marked samples; the Exception page names the SV layer; the changelog page lists the 1.1.0 release; the home page and the install snippet say 1.1.0. Co-Authored-By: Claude Sonnet 5.5 --- .../docs/getting-started/installation.mdx | 2 +- docs/usage/docs/index.mdx | 4 +- docs/usage/docs/modules/exception.mdx | 2 + docs/usage/docs/modules/presentation.mdx | 165 ++++++++++++++++++ docs/usage/docs/resources/changelog.mdx | 17 ++ 5 files changed, 187 insertions(+), 3 deletions(-) diff --git a/docs/usage/docs/getting-started/installation.mdx b/docs/usage/docs/getting-started/installation.mdx index fdd773e..c54c337 100644 --- a/docs/usage/docs/getting-started/installation.mdx +++ b/docs/usage/docs/getting-started/installation.mdx @@ -24,7 +24,7 @@ Add the BOM once, then the modules you need, with no versions: ```kotlin dependencies { - implementation(platform("com.srctool.sinew:sinew-bom:1.0.0")) + implementation(platform("com.srctool.sinew:sinew-bom:1.1.0")) implementation("com.srctool.sinew:sinew-models") implementation("com.srctool.sinew:sinew-exception") implementation("com.srctool.sinew:sinew-network") diff --git a/docs/usage/docs/index.mdx b/docs/usage/docs/index.mdx index 9fab752..b7d3e8c 100644 --- a/docs/usage/docs/index.mdx +++ b/docs/usage/docs/index.mdx @@ -10,9 +10,9 @@ Sinew is a plug-and-play app architecture for **Kotlin Multiplatform** and **Flu These docs are for developers **using** Sinew in an app: adding the packages, wiring them, and building a screen on them. Every sample has a Kotlin and a Dart version; the language switch in the sidebar changes them all. -:::info[Version 1.0] +:::info[Version 1.1] -Sinew 1.0.0 is out: the Kotlin modules are on [Maven Central](https://central.sonatype.com/namespace/com.srctool.sinew) (`com.srctool.sinew`) and the Dart packages are on [pub.dev](https://pub.dev/publishers/srctool.com/packages) (publisher `srctool.com`). Every module is released at one version, and the [changelog](/docs/resources/changelog) lists what is in it. +Sinew 1.1.0 is out (1.0.0 was the first release): the Kotlin modules are on [Maven Central](https://central.sonatype.com/namespace/com.srctool.sinew) (`com.srctool.sinew`) and the Dart packages are on [pub.dev](https://pub.dev/publishers/srctool.com/packages) (publisher `srctool.com`). Every module is released at one version, and the [changelog](/docs/resources/changelog) lists what is in it. ::: diff --git a/docs/usage/docs/modules/exception.mdx b/docs/usage/docs/modules/exception.mdx index c6f9ba2..31faa3c 100644 --- a/docs/usage/docs/modules/exception.mdx +++ b/docs/usage/docs/modules/exception.mdx @@ -13,6 +13,7 @@ sidebar_position: 2 - Built-in types for connectivity, HTTP statuses, parsing, storage and polling: `NoInternetConnectionException`, `ValidationException`, `UnauthorizedException`, `NotFoundException`, `ServerException`, `ParseException`, `StorageFullException` and more. Each has a short **type code**. - `processCall` and `processUseCase`: guards that turn whatever a block throws into a `Result`. - `ExceptionHandler`: one rule in a guard's list, "is this a timeout?" and "make this exception for this call". +- `ExceptionLayer`: where a failure came from, a short code in its `code`: `R` repository, `UC` use case, `VM` view model or notifier, `UT` utility, and `SV` for an [app service](/docs/modules/presentation#app-services). - `CrashReporter`: where bugs (unexpected and parse failures) are reported. - `Localizer` and `displayMessage`: the text a page shows. @@ -111,3 +112,4 @@ CrashReporter.install(MyReporter()); // once, at the composition root - The codes and the `sinew.` key prefix are reserved. Your own exceptions use other codes and keys. - Validation field messages are shown beside their fields, without a code. +- Pick the layer of the guard you write: a repository uses `Repository`, a use case `UseCase`, and an app service guards its steps with `ExceptionLayer.Service` (Dart: `ExceptionLayer.service`), so its bugs read `MODULE-SV-FUNCTION-TYPE`. diff --git a/docs/usage/docs/modules/presentation.mdx b/docs/usage/docs/modules/presentation.mdx index 0bd5957..0b80ac4 100644 --- a/docs/usage/docs/modules/presentation.mdx +++ b/docs/usage/docs/modules/presentation.mdx @@ -14,6 +14,7 @@ A page has **state** (what it renders) and **effects** (one-time reactions), and - `StateEffectHandler`: a page with one intent. `setState`, `emitEffect`, and a stream of each. - `EventActionHandler`: a page with several intents. The page sends events, a pure `mapEventToAction` decides the work, and `onAction` does it. +- `AppService` and `AppServiceHost`: work that lives with the app or the signed-in session and has no screen (a purchase from the store, a pub/sub message, a push). See [App services](#app-services). - On Kotlin, `StateEffectViewModel` and `EventActionViewModel` are the same on an AndroidX `ViewModel`, which survives rotation. On Flutter, `StateEffectNotifier` and `EventActionNotifier` are mixins on a Riverpod `Notifier`. ## A page with one intent @@ -69,9 +70,173 @@ final class Counter extends StateEffectHandler { For a page with several intents, see the event and action version in the [quick start](/docs/getting-started/quick-start). +## App services {#app-services} + +Some work isn't tied to a page: a purchase can arrive from the store at any time, with or without a screen open. A page handler can't own that, because it lives only as long as its page. An **app service** is the second kind of driver. Like a page handler it calls use cases and folds their `Result`, but it lives for the app or for the signed-in session, and it has no UI. + +A service has three steps. `init` catches up on what happened while the app wasn't running, once per start and before `listen`. `listen` subscribes to the sources. `destroy` stops and releases. Guard each step with the **service layer**, so a bug in one is reported with an `SV` code (here `BIL-SV-INIT-GEN`). + + + + +```kotlin +// sample: kotlin-lib/sample/docs/src/main/kotlin/com/srctool/sinew/docs/Presentation.kt#app-service +/** Work with the app's lifetime and no UI: purchases arrive from the store at any time, with or without a screen. */ +class BillingService( + private val store: PurchaseStore, + private val acknowledge: AcknowledgePurchase, +) : AppService(ServiceLifetime.App) { + + /** Catches up first, before [listen] subscribes. A bug here is reported as `BIL-SV-INIT-GEN`. */ + override suspend fun init() { + processResult("BIL", "INIT", ExceptionLayer.Service) { + store.unacknowledged().forEach { acknowledge(it) } + } + } + + override fun listen(scope: CoroutineScope) { + scope.launch { + store.arrivals.collect { purchase -> + processResult("BIL", "LISTEN", ExceptionLayer.Service) { acknowledge(purchase) } + } + } + } +} +``` + + + + +```dart +// sample: dart-lib/packages/sinew_presentation/example/main.dart#app-service +/// Work with the app's lifetime and no UI: purchases arrive from the store at any time, with or without a screen. +final class BillingService extends AppService { + BillingService(this.store, this.acknowledge) : super(ServiceLifetime.app); + + final PurchaseStore store; + final AcknowledgePurchase acknowledge; + StreamSubscription? _arrivals; + + /// Catches up first, before [listen] subscribes. A bug here is reported as `BIL-SV-INIT-GEN`. + @override + Future init() async { + await processCall( + 'BIL', + 'INIT', + ExceptionLayer.service, + const [], + () async { + for (final purchase in await store.unacknowledged()) { + await acknowledge(purchase); + } + }, + ); + } + + @override + void listen() { + _arrivals = store.arrivals.listen((purchase) async { + await processCall( + 'BIL', + 'LISTEN', + ExceptionLayer.service, + const [], + () => acknowledge(purchase), + ); + }); + } + + @override + Future destroy() async => _arrivals?.cancel(); +} +``` + + + + +An `AppServiceHost` starts and stops the services. Build it once at app start; `startApp` runs `init` then `listen` for the app-scope services in list order, and the session owner calls `startSession` after sign-in and `stopSession` after sign-out. + + + + +```kotlin +// sample: kotlin-lib/sample/docs/src/main/kotlin/com/srctool/sinew/docs/Presentation.kt#app-service-host +/** Built once at app start, with the scope that outlives every screen. */ +fun appServices(store: PurchaseStore, acknowledge: AcknowledgePurchase) = AppServiceHost( + appScope = CoroutineScope(SupervisorJob() + Dispatchers.Default), + services = listOf(BillingService(store, acknowledge)), +) + +/** App services start at launch, session services at sign-in and stop at sign-out. */ +class AppLifecycle(private val services: AppServiceHost) { + fun launched() = services.startApp() + fun signedIn() = services.startSession() + suspend fun signedOut() = services.stopSession() +} +``` + + + + +```dart +// sample: dart-lib/packages/sinew_presentation/example/main.dart#app-service-host +/// Built once in `main()`, before `runApp`, and kept for the life of the app. +AppServiceHost appServices( + PurchaseStore store, + AcknowledgePurchase acknowledge, +) => AppServiceHost([BillingService(store, acknowledge)]); + +/// App services start at launch, session services at sign-in and stop at sign-out. +final class AppLifecycle { + AppLifecycle(this.services); + + final AppServiceHost services; + + Future launched() => services.startApp(); + + Future signedIn() => services.startSession(); + + Future signedOut() => services.stopSession(); +} +``` + + + + +On Flutter with Riverpod, `sinew_riverpod_providers` holds the host: override `appServicesProvider` with your services, and read `appServiceHostProvider` once at startup, which starts the app services. Disposing the container destroys them. + + + + +Kotlin has no provider layer. Build the host once where the app starts (`Application.onCreate`, the iOS app's init) and keep it in your dependency container, as the sample above does. + + + + +```dart +// sample: dart-lib/packages/sinew_riverpod_providers/example/main.dart#app-services-provider +// The app lists its services once; reading the host starts the app services. +final container = ProviderContainer( + overrides: [ + appServicesProvider.overrideWithValue([WelcomeService()]), + ], +); +final host = container.read(appServiceHostProvider); + +// The session owner starts the session services after sign-in and stops them at sign-out. +await host.startSession(); +await host.stopSession(); +``` + + + + ## Good to know - `mapEventToAction` is pure: it reads state and returns the work, or `null` for none right now. Work that must not overlap is guarded there, by reading state. - `onAction` isn't awaited by `onEvent`, so actions may run concurrently. A throw in it is a bug and reaches the platform's error handler. - Clearing a view model, or disposing a provider, closes the effects. Late work then changes nothing. - On Flutter, `useOnOpened` sends "the page opened" once, after the first build, and `useEffects` listens to effects while the page is mounted. +- A service guards its own steps. Whatever a step still throws never stops the other services, or that service from listening: it is reported as an unhandled error, like any bug. +- A service's use cases are idempotent, because stores and push providers deliver the same event again. Pages never call or wait on a service: they observe the repository it writes to. +- `stopSession` cancels a session service's `init` that is still running on Kotlin. Dart can't cancel an `await`, so there that service just never starts listening. A second `startSession` while a session runs does nothing. diff --git a/docs/usage/docs/resources/changelog.mdx b/docs/usage/docs/resources/changelog.mdx index 36b1945..01cb86b 100644 --- a/docs/usage/docs/resources/changelog.mdx +++ b/docs/usage/docs/resources/changelog.mdx @@ -11,6 +11,23 @@ Every module is released at one version, so one changelog covers a release. The - [Sinew for Kotlin](https://github.com/srctool/sinew-kotlin/blob/main/CHANGELOG.md) - [Sinew for Dart and Flutter](https://github.com/srctool/sinew-dart/blob/main/CHANGELOG.md) +## Releases + +### 1.1.0 + +Adds **app services**: work with the app's or the signed-in session's lifetime and no UI (a purchase, pub/sub, a push). New public API, nothing removed. + +- `ExceptionLayer.Service` (Dart: `ExceptionLayer.service`), the `SV` layer, in `sinew-models` / `sinew_models`. +- `AppService`, `ServiceLifetime` and `AppServiceHost` in `sinew-presentation` / `sinew_presentation`. +- `appServicesProvider` and `appServiceHostProvider` in `sinew_riverpod_providers`, which now needs `sinew_presentation` 1.1.0. +- Flutter 3.50 is supported: `sinew_devtools_ui` imports Material from `material_ui`. + +See [App services](/docs/modules/presentation#app-services). + +### 1.0.0 + +The first release: every module on Maven Central and every package on pub.dev, at one version. + :::info[Versioning] Sinew follows semantic versioning. A mistake in a released version is never deleted: it gets a patch release. From 9d850f5e369ba36400973d36b371afd9dcdb5ff0 Mon Sep 17 00:00:00 2001 From: Dwiki Riyadi Date: Thu, 19 Nov 2026 12:04:07 +0700 Subject: [PATCH 2/2] chore(submodules): bump to the merged app-service samples Co-Authored-By: Claude Sonnet 5.5 --- dart-lib | 2 +- kotlin-lib | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dart-lib b/dart-lib index cff46a9..d5c0612 160000 --- a/dart-lib +++ b/dart-lib @@ -1 +1 @@ -Subproject commit cff46a989930f5025bd23990dfd11ed9de99f4f1 +Subproject commit d5c061216d6ffff6b959ab7d082eacae2ad8228b diff --git a/kotlin-lib b/kotlin-lib index 1f11331..f3ac531 160000 --- a/kotlin-lib +++ b/kotlin-lib @@ -1 +1 @@ -Subproject commit 1f11331546e51f8e3366fb282606911c3a11f91a +Subproject commit f3ac531d507dc3de05353f2f954cef271c0a086c