diff --git a/apps/docs/pages/guides/getting-started/quickstarts/kotlin.mdx b/apps/docs/pages/guides/getting-started/quickstarts/kotlin.mdx index 38642fa6321..df0815e0ede 100644 --- a/apps/docs/pages/guides/getting-started/quickstarts/kotlin.mdx +++ b/apps/docs/pages/guides/getting-started/quickstarts/kotlin.mdx @@ -161,6 +161,30 @@ export const meta = { + + + + This kind of object will be consumed by the view + + + + ```kotlin + data class Product( + val productId: String, + val name: String, + val description: String, + val price: Double, + val image: String, + val category: String, + val nutrition: String, + val _id: Int, + ) + ``` + + + + + @@ -169,10 +193,11 @@ export const meta = { ```kotlin - suspend fun getProducts() { + suspend fun getProducts(): ProductDto { val result = client.postgrest["products"] .select().decodeList() // Handle result data for next step + return result } ``` @@ -180,6 +205,86 @@ export const meta = { + + + + + + ```kotlin + class ProductListViewModel @Inject constructor( + private val productRepository: ProductRepository + ) : ViewModel() { + + private val _productList = MutableStateFlow?>(listOf()) + val productList: Flow?> = _productList + + init { + getProducts() + } + + fun getProducts() { + viewModelScope.launch { + val products = productRepository.getProducts() + _productList.emit(products?.map { it -> it.asDomainModel() }) + } + } + + private fun ProductDto.asDomainModel(): Product { + return Product( + productId = this.productId, + name = this.name, + price = this.price, + image = this.image, + description = this.description, + category = this.category, + nutrition = this.nutrition, + _id = this._id + ) + } + ``` + + + + + + + + + ```kotlin + @Composable + fun ProductListScreen( + modifier: Modifier = Modifier, + navController: NavController, + viewModel: ProductListViewModel = hiltViewModel(), + ) { + val productList = viewModel.productList.collectAsState(initial = listOf()).value + if (!productList.isNullOrEmpty()) { + LazyColumn( + modifier = modifier.padding(24.dp), + contentPadding = PaddingValues(5.dp) + ) { + items(productList) { item -> + ProductListItem( + product = item, + modifier = modifier, + onClick = { + navController.navigate( + ProductDetailsDestination.createRouteWithParam( + item.id + ) + ) + }, + ) + } + } + } + } + ``` + + + + +