Skip to content

Commit ea8995f

Browse files
committed
update dark mode mode detector docs
1 parent 46cbe22 commit ea8995f

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

docs/runtime/darkmode-detector.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,40 @@ dependencies {
1414

1515
## Usage
1616

17+
### Manual usage
18+
19+
This is the core functionality - it’s totally up to you how you want to use it. Just don’t forget to dispose its callback (`removeListener`) when you don’t need it anymore.
20+
21+
You can use Compose states, RxJava/Kotlin, or anything else you prefer.
22+
23+
Here we’re using a coroutines Flow wrapper just for demonstration.
24+
25+
```kt
26+
val isSystemDarkFlow: Flow<Boolean> = callbackFlow<Boolean> {
27+
val listener: Consumer<Boolean> = { isDark: Boolean ->
28+
trySend(isDark)
29+
}
30+
31+
val darkModeDetector = getPlatformDarkModeDetector()
32+
33+
// emit initial value
34+
trySend(darkModeDetector.isDark())
35+
36+
// listen to dark mode change events
37+
darkModeDetector.registerListener(listener)
38+
awaitClose {
39+
darkModeDetector.removeListener(listener)
40+
}
41+
}
42+
isSystemDarkFlow.collect {
43+
println("dark mode changes, isDark: $it")
44+
}
45+
```
46+
47+
### In compose
48+
49+
There is already a composable function available out of the box, which you can use it in your compose applications.
50+
1751
```kotlin
1852
@Composable
1953
fun App() {

0 commit comments

Comments
 (0)