Skip to content

Instantly share code, notes, and snippets.

@halilozercan
Created July 13, 2020 22:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save halilozercan/abf75e2f5e97335a850d731af7cbc5ee to your computer and use it in GitHub Desktop.
Save halilozercan/abf75e2f5e97335a850d731af7cbc5ee to your computer and use it in GitHub Desktop.
// Simply put the Demo composable in a Compose tree and observe the logs
// Focus on how dependency changes affect the recomposition.
// How does Ambient work?
// When basic callbacks fire: onCommit, launchInComposition, onActive, remember
@Composable
fun Demo() {
var textCounter by mutableStateOf(0)
Column {
TestComposable("Counter: $textCounter")
Button(onClick = { textCounter++ }) {
Text("Increase")
}
}
}
val ProvidableCounterAmbient = ambientOf { "default" }
@Composable
fun TestComposable(text: String) {
// only first commit
onActive {
Log.d("ComposeCallbacks", "onActive $text")
}
// When this composer commit each time
onCommit {
Log.d("ComposeCallbacks", "onCommit $text")
}
launchInComposition {
Log.d("ComposeCallbacks", "launched in composition")
delay(2000)
Log.d("ComposeCallbacks", "launched in composition after 2 seconds")
}
val textRemembered = remember { text }
Log.d("ComposeCallbacks", "onCompose remembered $textRemembered")
Providers(ProvidableCounterAmbient provides text) {
Row {
ShowTextAmbient()
Spacer(modifier = Modifier.width(16.dp))
ShowTextRegular(text)
}
}
}
@Composable
fun ShowTextAmbient() {
onCommit {
Log.d("ComposeCallbacks", "ShowTextAmbient commits")
}
ShowTextAmbient2()
}
@Composable
fun ShowTextAmbient2() {
onCommit {
Log.d("ComposeCallbacks", "ShowTextAmbient2 commits")
}
ShowTextAmbient3()
}
@Composable
fun ShowTextAmbient3() {
onCommit {
Log.d("ComposeCallbacks", "ShowTextAmbient3 commits")
}
Text(ProvidableCounterAmbient.current)
}
@Composable
fun ShowTextRegular(text: String) {
onCommit {
Log.d("ComposeCallbacks", "ShowTextRegular commits")
}
ShowTextRegular2(text)
}
@Composable
fun ShowTextRegular2(text: String) {
onCommit {
Log.d("ComposeCallbacks", "ShowTextRegular2 commits")
}
ShowTextRegular3(text)
}
@Composable
fun ShowTextRegular3(text: String) {
onCommit {
Log.d("ComposeCallbacks", "ShowTextRegular3 commits")
}
Text(text)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment