Sample App
samples/shop is a small coffee shop for Android, desktop and iOS (Compose Multiplatform) where every Transmission
feature does a real job. Each screen opens with a note naming the feature it shows, and a live inspector lists the
signals, effects and data flowing between routers.
Run it
| Platform | Command |
|---|---|
| Desktop | ./gradlew :samples:shop:desktopApp:run |
| Android | ./gradlew :samples:shop:androidApp:installDebug |
| iOS | Open samples/shop/iosApp/ShopIOSApp.xcodeproj in Xcode and run |
| Tests | ./gradlew :samples:shop:shared:desktopTest |
The sample follows the Kotlin Multiplatform layout for AGP 9: shared code in one module and one entry point per platform.
| Module | Contents |
|---|---|
samples/shop/shared |
All screens, ViewModels, transformers and tests; builds the Shop iOS framework |
samples/shop/androidApp |
MainActivity, ShopApplication, manifest and resources |
samples/shop/desktopApp |
main() for the desktop window |
samples/shop/iosApp |
Xcode project that embeds the Shop framework |
What each screen shows
| Screen | Router | Features |
|---|---|---|
| Catalog | catalog-router, global |
Published state; signals handled in order (search always matches the last query); a broadcast effect received by another router; a cached query other routers call |
| Cart | cart-router, global |
Effects from another router; a cross-router query for prices; targeted effects from a promotions transformer; a query and a command used by checkout |
| Checkout | checkout-router, global |
A multi-step flow in one handler with awaitSignal; retry after a declined card; cancel; a cross-router query and command |
| Lab | lab-router, isolated |
10,000 signals arrive in order; no lost updates on a plain var; a throwing handler goes to onError and processing continues; an isolated router can't reach the cart |
Things to try:
- Type a search quickly: the list always matches the final query.
- Add items, then apply
SAVE10orHALFOFFin the cart. - Check out with a card ending in
0000to see the declined-card retry, then use4242 4242 4242 4242. - In the Lab, publish
AddItemfrom the isolated router: the cart badge doesn't change.
How it's built
Each feature declares its state, queries and commands in a Contracts object and builds its transformer with
transformer { }:
object Cart : Contracts() {
val state by state(CartState())
val summary by query<CartState>()
val clear by command()
}
fun cartTransformer() = transformer(Cart) {
val cart = state(Cart.state)
on<CartEffect.AddItem> { effect ->
val product = Catalog.productById(effect.productId) ?: return@on // a query to the catalog router
cart.update { it.add(product) }
}
on<PromoEffect.Applied> { effect -> cart.update { it.copy(promo = effect.promo) } }
answer(Cart.summary) { cart.value }
handle(Cart.clear) { cart.value = CartState() }
}
Each screen's ViewModel is a plain ViewModel that owns a router tied to viewModelScope:
class CartViewModel(cart: Transformer, promotions: Transformer) : ViewModel() {
private val router = viewModelScope.router("cart-router", cart, promotions) { global() }
val state = router.state(Cart.state)
fun applyPromo(code: String) = router.process(CartSignal.ApplyPromo(code))
}
The checkout waits for each step with awaitSignal:
on<CheckoutSignal.Start> {
val cart = Cart.summary()
val address = awaitSignal<CheckoutSignal.SubmitAddress>(cancelOn = CheckoutSignal.Cancel::class)
?.address ?: return@on cancelled()
// payment in a retry loop, then Cart.clear() and publish(OrderPlaced(...))
}
See Testing for how the sample is tested.