Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onSuccess doesn't executed until I use runOnUiThread method #1

Open
ahmed-shehataa opened this issue Dec 6, 2020 · 0 comments
Open

Comments

@ahmed-shehataa
Copy link

ahmed-shehataa commented Dec 6, 2020

I tried to use your custom call but process doesn't execute till I use runOnUiThread ..
so I created my custom call but the response doesn't execute till I use runOnUiThread .

Config.getClient()
            .create(Api_Service::class.java)
            .getTerms("api/Lookups/GetTermsAndConditions/1")
            .onSuccess {
               runOnUiThread {
                   it?.let { terms ->
                       Log.e("dataa", terms.data)
                       Toast.makeText(this, terms.totalCount.toString(),
                           Toast.LENGTH_SHORT).show()

                       textView?.text = terms.totalCount.toString()
                   }
               }

            }.onFailed { s1, s2, _ ->
                Toast.makeText(this, s1,
                    Toast.LENGTH_SHORT).show()
                textView?.text = s2

            }.onInternetError {
                runOnUiThread {
                    Toast.makeText(this, "No Internet",
                        Toast.LENGTH_SHORT).show()
                    textView?.text = "No Internet"
                }

            }.run()

My simple class

class Simple<R>(private val call: Call<R>) {

    //private lateinit var response: Response<R>
    private var onResponse: ((R?) -> Unit)? = null

    private var onInternet: (() -> Unit)? = null

    private lateinit var onError: (
        message: String,
        errorBodyAsString: String?,
        throwable: Throwable?
    ) -> Unit

    private lateinit var onNullResponse: () -> Unit

    /*
   ** For making the request async
    */
    fun run(): Simple<R> {

        // After successfully response
        call.enqueue(object : Callback<R> {
            override fun onResponse(call: Call<R>, response: Response<R>?) {
                response?.let {
                    if (response.isSuccessful) {
                        onResponse?.let { it1 -> it1(response.body()) }

                    } else {
                        // Pass current error body to onError()
                        onError(
                            response.message(),
                            response.errorBody()?.string(),
                            null
                        )
                    }
                } ?: onNullResponse()

            }

            override fun onFailure(call: Call<R>, t: Throwable) {
                findErrorType(t)

            }
        })

        return this
    }

    private fun findErrorType(throwable: Throwable?) {
        when (throwable) {
            is IOException ->
                // there is error on internet connection
                onInternet?.let { it() }
            is HttpException ->
                onError("", "", throwable)
        }
    }

    fun onSuccess(onResponse: (R?) -> Unit): Simple<R> =
        with(this) {
            this.onResponse = onResponse
            this
        }


    fun onFailed(onError: (String, String?, Throwable?) -> Unit): Simple<R> = with(this) {
        this.onError = onError
        this
    }



    fun onInternetError(onInternetError: () -> Unit): Simple<R> {
        this.onInternet = onInternetError
        return this
    }

    fun drop() {
        call.cancel()
    }

    fun isDropped() = call.isCanceled


}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant