Created
February 15, 2022 10:01
-
-
Save SeongUgJung/116b0991a5a75fded42a7a56fd673e16 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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