Newer
Older
SmartKitchenTablet / app / src / main / java / com / casic / br / vm / DeviceViewModel.kt
package com.casic.br.vm

import com.amap.api.location.AMapLocation
import com.casic.br.base.BaseApplication
import com.casic.br.extensions.separateResponseCode
import com.casic.br.extensions.toErrorMessage
import com.casic.br.model.UserDetailModel
import com.casic.br.utils.retrofit.RetrofitServiceManager
import com.pengxh.kt.lite.extensions.launch
import com.pengxh.kt.lite.extensions.show
import com.pengxh.kt.lite.vm.BaseViewModel
import com.pengxh.kt.lite.vm.LoadState
import com.tuya.smart.sdk.bean.DeviceBean
import org.json.JSONObject

class DeviceViewModel : BaseViewModel() {
    fun addDevice(
        devResp: DeviceBean,
        userModel: UserDetailModel.DataModel,
        iotDevice: String,
        aMapLocation: AMapLocation
    ) = launch({
        val response =
            RetrofitServiceManager.addDevice(
                devResp,
                userModel,
                iotDevice,
                aMapLocation.province + aMapLocation.district
            )
        val responseCode = response.separateResponseCode()
        if (responseCode == 200) {
            loadState.value = LoadState.Success
        } else {
            loadState.value = LoadState.Fail
            response.toErrorMessage().show(BaseApplication.obtainInstance())
        }
    }, {
        loadState.value = LoadState.Fail
        it.printStackTrace()
    })

    fun addDeviceData(dataModel: JSONObject) =
        launch({ RetrofitServiceManager.addDeviceData(dataModel) }, {
            it.printStackTrace()
        })
}