-
Notifications
You must be signed in to change notification settings - Fork 119
Demonstrate KMP ViewModel on multiple screens #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| /* | ||
| * Copyright 2024 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.example.fruitties.android.ui | ||
|
|
||
| import androidx.compose.foundation.layout.Arrangement | ||
| import androidx.compose.foundation.layout.Column | ||
| import androidx.compose.foundation.layout.Spacer | ||
| import androidx.compose.foundation.layout.WindowInsets | ||
| import androidx.compose.foundation.layout.WindowInsetsSides | ||
| import androidx.compose.foundation.layout.fillMaxWidth | ||
| import androidx.compose.foundation.layout.only | ||
| import androidx.compose.foundation.layout.padding | ||
| import androidx.compose.foundation.layout.safeDrawing | ||
| import androidx.compose.foundation.layout.systemBars | ||
| import androidx.compose.foundation.layout.windowInsetsBottomHeight | ||
| import androidx.compose.foundation.lazy.LazyColumn | ||
| import androidx.compose.foundation.lazy.items | ||
| import androidx.compose.material.icons.Icons | ||
| import androidx.compose.material.icons.automirrored.filled.ArrowBack | ||
| import androidx.compose.material3.CenterAlignedTopAppBar | ||
| import androidx.compose.material3.ExperimentalMaterial3Api | ||
| import androidx.compose.material3.Icon | ||
| import androidx.compose.material3.IconButton | ||
| import androidx.compose.material3.MaterialTheme | ||
| import androidx.compose.material3.Scaffold | ||
| import androidx.compose.material3.Text | ||
| import androidx.compose.material3.TopAppBarColors | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.collectAsState | ||
| import androidx.compose.runtime.getValue | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.platform.LocalContext | ||
| import androidx.compose.ui.res.stringResource | ||
| import androidx.compose.ui.unit.dp | ||
| import androidx.lifecycle.viewmodel.compose.viewModel | ||
| import com.example.fruitties.android.R | ||
| import com.example.fruitties.android.di.App | ||
| import com.example.fruitties.viewmodel.CartViewModel | ||
|
|
||
| @OptIn(ExperimentalMaterial3Api::class) | ||
| @Composable | ||
| fun CartScreen(onNavBack: () -> Unit) { | ||
| // Instantiate a ViewModel with a dependency on the AppContainer. | ||
| // To make ViewModel compatible with KMP, the ViewModel factory must | ||
| // create an instance without referencing the Android Application. | ||
| // Here we put the KMP-compatible AppContainer into the extras | ||
| // so it can be passed to the ViewModel factory. | ||
| val app = LocalContext.current.applicationContext as App | ||
| val extras = remember(app) { | ||
| val container = app.container | ||
| CartViewModel.newCreationExtras(container) | ||
| } | ||
| val viewModel: CartViewModel = viewModel( | ||
| factory = CartViewModel.Factory, | ||
| extras = extras, | ||
| ) | ||
|
|
||
| val cartState by viewModel.cartUiState.collectAsState() | ||
|
|
||
| Scaffold( | ||
| topBar = { | ||
| CenterAlignedTopAppBar( | ||
| navigationIcon = { | ||
| IconButton(onClick = onNavBack) { | ||
| Icon( | ||
| imageVector = Icons.AutoMirrored.Filled.ArrowBack, | ||
| contentDescription = "Navigate back", | ||
| ) | ||
| } | ||
| }, | ||
| title = { | ||
| Text(text = stringResource(R.string.frutties)) | ||
| }, | ||
| colors = TopAppBarColors( | ||
| containerColor = MaterialTheme.colorScheme.primary, | ||
| scrolledContainerColor = MaterialTheme.colorScheme.primary, | ||
| navigationIconContentColor = MaterialTheme.colorScheme.onPrimary, | ||
| titleContentColor = MaterialTheme.colorScheme.onPrimary, | ||
| actionIconContentColor = MaterialTheme.colorScheme.onPrimary, | ||
| ), | ||
|
Comment on lines
+90
to
+96
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of manually constructing colors = TopAppBarDefaults.centerAlignedTopAppBarColors(
containerColor = MaterialTheme.colorScheme.primary,
navigationIconContentColor = MaterialTheme.colorScheme.onPrimary,
titleContentColor = MaterialTheme.colorScheme.onPrimary,
actionIconContentColor = MaterialTheme.colorScheme.onPrimary,
), |
||
| ) | ||
| }, | ||
| contentWindowInsets = WindowInsets.safeDrawing.only( | ||
| // Do not include Bottom so scrolled content is drawn below system bars. | ||
| // Include Horizontal because some devices have camera cutouts on the side. | ||
| WindowInsetsSides.Top + WindowInsetsSides.Horizontal, | ||
| ), | ||
| ) { paddingValues -> | ||
| Column( | ||
| modifier = Modifier | ||
| .padding(16.dp) | ||
| // Support edge-to-edge (required on Android 15) | ||
| // https://developer.android.com/develop/ui/compose/layouts/insets#inset-size | ||
| .padding(paddingValues), | ||
| ) { | ||
| val cartItemCount = cartState.cartDetails.sumOf { it.count } | ||
| Text( | ||
| text = "Cart has $cartItemCount items", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| modifier = Modifier.padding(8.dp), | ||
| ) | ||
| LazyColumn( | ||
| modifier = Modifier.fillMaxWidth(), | ||
| horizontalAlignment = Alignment.CenterHorizontally, | ||
| ) { | ||
| items(cartState.cartDetails) { cartItem -> | ||
| Text(text = "${cartItem.fruittie.name}: ${cartItem.count}") | ||
| } | ||
| item { | ||
| Spacer( | ||
| Modifier.windowInsetsBottomHeight( | ||
| WindowInsets.systemBars, | ||
| ), | ||
| ) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The navigation logic for the back button in the
CartScreen's top app bar is inconsistent with the system back behavior. Currently, it clears the entire back stack and navigates to a newListScreen. This is more of a "navigate up to home" action. The system back gesture, handled byonBackinNavDisplay, will simply pop theCartScreenoff the stack. For a consistent user experience, consider using the same pop behavior for the in-app back button.onNavBack = { backStack.removeLastOrNull() },