Search code examples
androidkotlinretrofitrunnable

Hot to pass values throw a onSuccess response runnable?


So my goal like the the title says is to pass API response results throw an onSuccess.run() method to a fragment. Just to give some context, I start to do the method like this in a Manager class for example:

    override fun callUser(onSuccess:Runnable, onFailure:Runnable){

 NetworkManager.instance.performCall(NetworkManager.REGISTRATION.verifyUser(id),
                object : NetworkManager.OnRequestCallback<UserInfoResponse> {
                    override fun onSuccess(body: UserInfoResponse?) {
                        body?.data?.let {
                            onSuccess.run()
                        }
                    }

                    override fun onError(errorBody: String?) {
                        onFailure.run()
                    }
                })
    }

Then I go to a fragment and call the method above like this:

objectManager.callVerifyAdvisor(
                    Runnable {[On Success stuff },
                    Runnable {[On Error]}
        }

The problem is that, although I can decide in the fragment the actions I want to do in the onSuccess() and onFailure() methods, I cant get the API Results to that fragment by doing this way.

So my ideia is to do something like [I used comments to specify the sections that matter]:

NetworkManager.instance.performCall(NetworkManager.REGISTRATION.verifyUser(id),
                    object : NetworkManager.OnRequestCallback<UserInfoResponse> {
                        override fun onSuccess(body: UserInfoResponse?) {
                            body?.data?.let {
                                it.userName // I get this from api response
                                onSuccess.run()
                            }
                        }

                        override fun onError(errorBody: String?) {
                            onFailure.run()
                        }
                    })
        }

Then on my fragment I want something like this:

   objectManager.callVerifyAdvisor(
              Runnable {[On Success stuff }, //receive here the it.userName
              Runnable {[On Error]}
}

Can someone give any ideia how to do this? Side note -> I put kotlin on the tag because this has some kind of functional stuff.


Solution

  • You just need to replace you onSuccess from Runnable to your custom functional type callback:

    override fun callUser(onSuccess: (String) -> Unit, onFailure: Runnable) {...
    

    Then pass userName to callback:

    NetworkManager.instance.performCall(NetworkManager.REGISTRATION.verifyUser(id),
                    object : NetworkManager.OnRequestCallback<UserInfoResponse> {
                        override fun onSuccess(body: UserInfoResponse?) {
                            body?.data?.let {
                                onSuccess.invoke(it.userName) // Pass userName
                            }
                        }
    
                        override fun onError(errorBody: String?) {
                            onFailure.run()
                        }
                    })
        }
    

    And then you can get userName from this callback:

    objectManager.callVerifyAdvisor(
              {userName -> } // Here you get your result
              Runnable {[On Error]}
    }