Quick start
A list that loads its first page, loads more at the end, shows a retry when a page fails, and refreshes. This is the sample app, which runs on every platform: its backend is in memory, and its second page fails once, so you see every state.
The page sends events (it opened, the list neared its end, retry was tapped); a pure function turns each event into the work to run; the work updates the state. The page only renders state.
1. The view model
- Kotlin
- Dart
// sample: kotlin-lib/sample/shared/src/commonMain/kotlin/com/srctool/sinew/sample/OrderListViewModel.kt#view-model
class OrderListViewModel(backend: OrderBackend) :
EventActionViewModel<OrderListState, OrderListEvent, LoadOrders, Nothing>(OrderListState()) {
private val pager = Pager.offset<Order>(itemKey = { it.id }) { page, _ -> backend.orders(page) }
override fun mapEventToAction(event: OrderListEvent): LoadOrders? = when (event) {
OrderListEvent.Opened -> LoadOrders(LoadType.Initial) // the pager runs Initial once per query
OrderListEvent.EndReached -> if (currentState.orders.canLoadMore) LoadOrders(LoadType.More) else null
OrderListEvent.RetryTapped -> LoadOrders(currentState.orders.retryType)
OrderListEvent.Refreshed -> LoadOrders(LoadType.Refresh)
}
override suspend fun onAction(action: LoadOrders) {
pager.load(action.type, currentState.orders) { next -> setState { copy(orders = next) } }
}
}
Pager.offsetholds the paging rules: the first page loads once per query,Moreonly starts when there is a next page, and a refresh replaces whatever is running.mapEventToActionis pure: it reads the state and returns the work, ornullfor none right now. Every "should this run" decision is in one testable function.PagingStatestays set after a load, so retry knows what to repeat (retryType).
2. The screen
- Kotlin
- Dart
// sample: kotlin-lib/sample/shared/src/commonMain/kotlin/com/srctool/sinew/sample/OrderListScreen.kt#screen
@Composable
fun OrderListScreen(viewModel: OrderListViewModel) {
val state by viewModel.state.collectAsState()
LaunchedEffect(viewModel) { viewModel.onEvent(OrderListEvent.Opened) }
Column(Modifier.fillMaxSize().background(Camouflage.colors.surface).safeDrawingPadding()) {
CamoButton("Refresh", onClick = { viewModel.onEvent(OrderListEvent.Refreshed) }, modifier = Modifier.padding(8.dp))
CamoPagedList(
state = state.orders.toCamo(),
itemKey = { it.id },
onLoadMore = { viewModel.onEvent(OrderListEvent.EndReached) },
onRetry = { viewModel.onEvent(OrderListEvent.RetryTapped) },
onRefresh = { viewModel.onEvent(OrderListEvent.Refreshed) },
item = { order, _ -> CamoListItem(order.title, supporting = order.status) },
modifier = Modifier.weight(1f).testTag("orders"),
)
}
}
toCamo (toCamoOf on Flutter) turns the paging state into what Camouflage's CamoPagedList shows: the skeleton, the items, load more, retry, refresh and the end of the list. See paging with Camouflage.
Run it
- Kotlin
- Dart
./gradlew :sample:shared:run # desktop
./gradlew :sample:androidApp:installStagingDebug # Android