There is CustomWebViewClient
with override function onPageFinished
. What is the shortest way to notify MainViewModel
about the function triggered? I mean some event.
I suppose that can use StateFlow
, something like this:
class MainViewModel : ViewModel() {
init {
val client = CustomWebViewClient()
viewModelScope.launch {
client.onPageFinished.collect {
// ...
}
}
}
}
class CustomWebViewClient() : WebViewClient() {
private val _onPageFinished = MutableStateFlow("")
val onPageFinished = _onPageFinished.asStateFlow()
override fun onPageFinished(view: WebView, url: String) {
_onPageFinished.update { "" }
}
}
But in this case need to transfer unnecessary empty string and will be occurs first call before onPageFinished
called because MutableStateFlow
has value. So appear required add some enum or class in order to do filter with when
keyword.
Maybe is there more shortest way to do that?