Skip to content

Instantly share code, notes, and snippets.

@SeongUgJung
Created February 15, 2022 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SeongUgJung/116b0991a5a75fded42a7a56fd673e16 to your computer and use it in GitHub Desktop.
Save SeongUgJung/116b0991a5a75fded42a7a56fd673e16 to your computer and use it in GitHub Desktop.
@Composable
fun ObservableBoolean.observeAsState(): State<Boolean> {
val value = get()
val state = remember { mutableStateOf(value) }
DisposableEffect(state) {
val callback: Observable.OnPropertyChangedCallback = object : Observable.OnPropertyChangedCallback() {
override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
if (sender == null && sender !is ObservableBoolean) return
state.value = (sender as ObservableBoolean).get()
}
}
addOnPropertyChangedCallback(callback)
onDispose {
removeOnPropertyChangedCallback(callback)
}
}
return state
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment