Skip to content

Commit

Permalink
Use collectAsStateWithLifecycle
Browse files Browse the repository at this point in the history
  • Loading branch information
zsmb13 committed Nov 13, 2024
1 parent 49eea32 commit e623ea8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
Expand All @@ -36,6 +35,7 @@ import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import coil3.compose.AsyncImage
import com.jetbrains.kmpapp.R
import com.jetbrains.kmpapp.data.MuseumObject
Expand All @@ -44,7 +44,7 @@ import org.koin.androidx.compose.koinViewModel
@Composable
fun DetailScreen(objectId: Int, navigateBack: () -> Unit) {
val viewModel: DetailViewModel = koinViewModel()
val obj by viewModel.museumObject.collectAsState()
val obj by viewModel.museumObject.collectAsStateWithLifecycle()

LaunchedEffect(objectId) {
viewModel.setId(objectId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,20 @@ import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import coil3.compose.AsyncImage
import com.jetbrains.kmpapp.data.MuseumObject
import org.koin.androidx.compose.koinViewModel

@Composable
fun ListScreen(navigateToDetails: (objectId: Int) -> Unit) {
val viewModel: ListViewModel = koinViewModel()
val objects by viewModel.objects.collectAsState()
val objects by viewModel.objects.collectAsStateWithLifecycle()

AnimatedContent(objects.isNotEmpty()) { objectsAvailable ->
if (objectsAvailable) {
Expand Down

0 comments on commit e623ea8

Please sign in to comment.