Newer
Older
CasicSmartTube / app / src / main / java / com / casic / smarttube / vm / DeviceViewModel.kt
package com.casic.smarttube.vm

import androidx.lifecycle.MutableLiveData
import com.casic.smarttube.base.BaseApplication
import com.casic.smarttube.extensions.separateResponseCode
import com.casic.smarttube.extensions.toErrorMessage
import com.casic.smarttube.model.DeviceHistoryDataModel
import com.casic.smarttube.model.DeviceListModel
import com.casic.smarttube.model.MapDeviceModel
import com.casic.smarttube.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

class DeviceViewModel : BaseViewModel() {

    private val gson = Gson()
    val mapDeviceModel = MutableLiveData<MapDeviceModel>()
    val historyDataModel = MutableLiveData<DeviceHistoryDataModel>()

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

    fun obtainDeviceListByType(
        deptid: String, keywords: String, isOnline: String, page: Int
    ) = launch({
        val response =
            RetrofitServiceManager.obtainDeviceListByType(deptid, keywords, isOnline, page)
        val responseCode = response.separateResponseCode()
        if (responseCode == 200) {
//            deviceListModel.value = gson.fromJson<DeviceListModel>(
//                response, object : TypeToken<DeviceListModel>() {}.type
//            )
        } else {
            response.toErrorMessage().show(BaseApplication.obtainInstance())
        }
    }, {
        it.printStackTrace()
    })

    fun obtainDeviceHistoryData(
        deptid: String, keywords: String, beginTime: String?, endTime: String?
    ) = launch({
        val response = RetrofitServiceManager.obtainDeviceHistoryData(
            deptid, keywords, beginTime, endTime
        )
        val responseCode = response.separateResponseCode()
        if (responseCode == 200) {
            historyDataModel.value = gson.fromJson<DeviceHistoryDataModel>(
                response, object : TypeToken<DeviceListModel>() {}.type
            )
        } else {
            response.toErrorMessage().show(BaseApplication.obtainInstance())
        }
    }, {
        it.printStackTrace()
    })
}