package com.xuqm.sdhbwfu.core.extensions

import com.xuqm.sdhbwfu.core.model.HttpResult
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers


fun <D, T : HttpResult<D>> Observable<T>.io_main() =
    subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())


fun <D, T : HttpResult<D>> Observable<T>.subscribeBy(
    onResponse: (D?) -> Unit,
    onFailure: (String) -> Unit
) =
    subscribe({
        if ("success" == it.status) {
            onResponse(it.data)
        } else {
            onFailure(it.data.toString())
        }
    }, {
        onFailure(it.message!!)
    })