Issue
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?
Solution
You can add lambda parameter into CustomWebViewClient constructor that will get called once page is finished.
class MainViewModel : ViewModel() {
init {
val client = CustomWebViewClient({handle the event})
}
}
class CustomWebViewClient(onPageFinished: () -> Unit) : WebViewClient() {
override fun onPageFinished(view: WebView, url: String) {
onPageFinished()
}
}
Please note that referencing anything from android.* package in a ViewModel is most often a big no-go.
Answered By - Mieszko Koźma
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.