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

import androidx.lifecycle.MutableLiveData
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.OtherDeviceListModel
import com.casic.br.model.OtherDeviceModel
import com.casic.br.model.UserDetailModel
import com.casic.br.utils.retrofit.RetrofitServiceManager
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
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() {

    private val gson by lazy { Gson() }
    val listModel = MutableLiveData<OtherDeviceListModel>()
    val deviceModel = MutableLiveData<OtherDeviceModel>()

    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()
        })

    fun obtainOtherDeviceListByPage() = launch({
        val response = RetrofitServiceManager.obtainOtherDeviceListByPage()
        val responseCode = response.separateResponseCode()
        if (responseCode == 200) {
            listModel.value = gson.fromJson<OtherDeviceListModel>(
                response, object : TypeToken<OtherDeviceListModel>() {}.type
            )
        } else {
            response.toErrorMessage().show(BaseApplication.obtainInstance())
        }
    }, {
        it.printStackTrace()
    })

    fun obtainDeviceDetail(id: String) = launch({
        val response = RetrofitServiceManager.obtainDeviceDetail(id)
        val responseCode = response.separateResponseCode()
        if (responseCode == 200) {
            deviceModel.value = gson.fromJson<OtherDeviceModel>(
                response, object : TypeToken<OtherDeviceModel>() {}.type
            )
        } else {
            response.toErrorMessage().show(BaseApplication.obtainInstance())
        }
    }, {
        it.printStackTrace()
    })
}