Skip to content

Commit

Permalink
states and effects
Browse files Browse the repository at this point in the history
  • Loading branch information
mozesi committed Dec 5, 2023
1 parent 95c5bed commit c84e19f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 5 deletions.
2 changes: 2 additions & 0 deletions AdvancedStateAndSideEffectsCodelab/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ dependencies {
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
implementation "com.google.dagger:hilt-android:2.48.1"
kapt "com.google.dagger:hilt-compiler:2.48.1"
implementation "androidx.lifecycle:lifecycle-runtime-compose:$lifecycle_version"


implementation "io.coil-kt:coil-compose:2.5.0"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import androidx.compose.samples.crane.base.ExploreSection
import androidx.compose.samples.crane.data.ExploreModel
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel

typealias OnExploreItemClicked = (ExploreModel) -> Unit
Expand Down Expand Up @@ -77,7 +78,7 @@ fun CraneHomeContent(
viewModel: MainViewModel = viewModel(),
) {
// TODO Codelab: collectAsStateWithLifecycle step - consume stream of data from the ViewModel
val suggestedDestinations: List<ExploreModel> = remember { emptyList() }
val suggestedDestinations by viewModel.suggestedDestinations.collectAsStateWithLifecycle()

val onPeopleChanged: (Int) -> Unit = { viewModel.updatePeople(it) }
var tabSelected by remember { mutableStateOf(CraneScreen.Fly) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.samples.crane.R
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import kotlinx.coroutines.delay

private const val SplashWaitTime: Long = 2000

Expand All @@ -32,6 +36,12 @@ fun LandingScreen(onTimeout: () -> Unit, modifier: Modifier = Modifier) {
Box(modifier = modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
// TODO Codelab: LaunchedEffect and rememberUpdatedState step
// TODO: Make LandingScreen disappear after loading data
val currentOntimeout by rememberUpdatedState(onTimeout)

LaunchedEffect(onTimeout){
delay(SplashWaitTime)
onTimeout()
}
Image(painterResource(id = R.drawable.ic_crane_drawer), contentDescription = null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ import androidx.activity.compose.setContent
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.samples.crane.details.launchDetailsActivity
import androidx.compose.samples.crane.ui.CraneTheme
import androidx.core.view.WindowCompat
Expand Down Expand Up @@ -51,6 +55,12 @@ class MainActivity : ComponentActivity() {
@Composable
private fun MainScreen(onExploreItemClicked: OnExploreItemClicked) {
Surface(color = MaterialTheme.colors.primary) {
CraneHome(onExploreItemClicked = onExploreItemClicked)
var showLandingScreen by remember { mutableStateOf(true) }
if (showLandingScreen) {
LandingScreen(onTimeout = { showLandingScreen = false })
} else {
CraneHome(onExploreItemClicked = onExploreItemClicked)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject
Expand All @@ -38,19 +41,24 @@ class MainViewModel @Inject constructor(

val hotels: List<ExploreModel> = destinationsRepository.hotels
val restaurants: List<ExploreModel> = destinationsRepository.restaurants
private val _suggestedDestinations = MutableStateFlow<List<ExploreModel>>(emptyList())
val suggestedDestinations : StateFlow<List<ExploreModel>> = _suggestedDestinations.asStateFlow()

init {
_suggestedDestinations.value = destinationsRepository.destinations
}
fun updatePeople(people: Int) {
viewModelScope.launch {
if (people > MAX_PEOPLE) {
// TODO Codelab: Uncomment
// _suggestedDestinations.value = emptyList()
_suggestedDestinations.value = emptyList()
} else {
val newDestinations = withContext(defaultDispatcher) {
destinationsRepository.destinations
.shuffled(Random(people * (1..100).shuffled().first()))
}
// TODO Codelab: Uncomment
// _suggestedDestinations.value = newDestinations
_suggestedDestinations.value = newDestinations
}
}
}
Expand All @@ -62,7 +70,7 @@ class MainViewModel @Inject constructor(
.filter { it.city.nameToDisplay.contains(newDestination) }
}
// TODO Codelab: Uncomment
// _suggestedDestinations.value = newDestinations
_suggestedDestinations.value = newDestinations
}
}
}

0 comments on commit c84e19f

Please sign in to comment.