You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/runtime/darkmode-detector.md
+34Lines changed: 34 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,6 +14,40 @@ dependencies {
14
14
15
15
## Usage
16
16
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.
0 commit comments