Fast-Android-Networking icon indicating copy to clipboard operation
Fast-Android-Networking copied to clipboard

getObjectSingle() doesn't accept object of Generic type

Open usmanrana07 opened this issue 4 years ago • 1 comments

When I'm trying to pass a generic object then it is not allowing me to do so. Like in current syntax it can accept 'ApiResponse.class' but not ApiResponse of Books type

Rx2AndroidNetworking.get(url).build().getObjectSingle(ApiResponse.class)

usmanrana07 avatar Feb 27 '20 08:02 usmanrana07

maybe can help you case

 inline fun <reified T> refreshListUnconfirmed(sort: String): NetState<T> {
        val netState: NetState<T> = NetState()
        Rx2AndroidNetworking.get(DEPARTMENT_BUDGET_UNCONFIRMED)
            .addQueryParameter("sort", sort)
            .build()
            .getObjectSingle(T::class.java)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .retry(RETRY_TIMES)
            .subscribe(netState)
        return netState
    }

NetState.kt


class NetState<T> : ResourceSingleObserver<T>() {
    private val successLiveData = MutableLiveData<T>()
    private val errorLiveData = MutableLiveData<Throwable>()
    private val loadingLiveData = MutableLiveData<Boolean>()

    override fun onSuccess(t: T) {
        successLiveData.value = t
        dispose()
    }

    override fun onError(e: Throwable) {
        errorLiveData.value = e
        dispose()
    }

    fun whenSuccess(): LiveData<T> = successLiveData
    fun whenError(): LiveData<Throwable> = errorLiveData

ViewModel

 fun getListBelumKonfirmasi(): NetState<ResponseDepartmentBudgetConfirmationApprovalInfo> {
        return mRepo.refreshListUnconfirmed("budgetYear-ASC")
    }

View (Activity)

    private fun updateUI() {
        val getListUnconfirmed = viewModel.getListBelumKonfirmasi()

        getListUnconfirmed.whenSuccess().observe(this) {
            val datas = it.data.departmentBudgetConfirmationApprovalInfos
            when {
                datas.isNotEmpty()   -> initRV(datas.asArrayList())
                else                 -> root.show()
            }
        }

        getListUnconfirmed.whenError().observe(this) {
            // do someshit here ..
        }
    }

abehbatre avatar Mar 05 '20 13:03 abehbatre