0
votes

HandleAPIError in Utils class. create custom method for Snackbar for the API Error.

Unresolved reference. None of the following candidates is applicable because of receiver type mismatch: public fun View.snackbar(message: String, action: (() → Unit)? = ...): Unit defined in file Utils.kt

Receiver type in Fragment in requireview(). which receiver type used for activity.

Utils.kt

fun View.snackbar(message: String, action: (() -> Unit)? = null){
    val snackbar = Snackbar.make(this, message, Snackbar.LENGTH_LONG)
    action?.let {
        snackbar.setAction("Retry"){
            it()
        }
        snackbar.show()
    }
}

fun Fragment.handleApiError(
    failure : Resource.Failure,
    retry : (() -> Unit)? = null
    ){
    when{
        failure.isNetworkError -> requireView().snackbar("Please check internet Connection", retry )
    }
}

fun Activity.handleApiError(
    failure : Resource.Failure,
    retry : (() -> Unit)? = null
){
    when{
        //which receiver type used for snackbar?
        failure.isNetworkError -> snackbar(
            "Please check internet Connection",
            retry
        )
}
}
1
Do you solution it? i have a sample problem, can you help me?hungcuiga1

1 Answers

0
votes

Try using this.window.decorView.rootView as:

fun Activity.handleApiError(
        failure : Resource.Failure,
        retry : (() -> Unit)? = null
){
    when{
        //which receiver type used for snackbar?
        failure.isNetworkError -> this.window.decorView.rootView.snackbar(
                "Please check internet Connection",
                retry
        )
    }
}