Mark
03/18/2025, 2:03 AMState
type from composables in this way? In this particular case, I’m waiting for window focus before reading the clipboard (it won’t work on Android otherwise). I only use this on app launch so I’m not looking to monitor changes in the clipboard, but just wait for the first state value:
@Composable
fun clipTextState(): State<String?> {
val clipTextState: MutableState<String?> = rememberSaveable {
mutableStateOf(null)
}
WindowFocusObserver { isWindowFocused ->
if (isWindowFocused) {
clipTextState.value = /* readClipText */
}
}
return clipTextState
}
Winson Chiu
03/18/2025, 2:07 AMonClipAvailable: (String?) -> Unit
, rememberUpdatedState it, and then invoke it in the observer. The caller can then react to that event however it wants.Mark
03/18/2025, 2:15 AMMark
03/18/2025, 3:18 AM@Composable
fun LaunchedClipboardReaderEffect(
processClipText: suspend CoroutineScope.(String) -> Unit,
) {
var clipText: String? by remember {
mutableStateOf(null)
}
LaunchedEffect(clipText) {
clipText?.let { text ->
processClipText(text)
}
}
WindowFocusObserver { isWindowFocused ->
if (isWindowFocused && clipText == null) { // make sure we don't set more than once
clipText = /* readClipText as non-null String */
}
}
}