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.DeviceListModel import com.casic.smarttube.model.GroupDeviceModel import com.casic.smarttube.model.ProjectGroupModel 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 import com.pengxh.kt.lite.vm.LoadState class ProjectGroupViewModel : BaseViewModel() { private val gson = Gson() val groupModel = MutableLiveData<ProjectGroupModel>() val groupDeviceModel = MutableLiveData<GroupDeviceModel>() val deviceListModel = MutableLiveData<DeviceListModel>() fun obtainProGroupList() = launch({ val response = RetrofitServiceManager.obtainProGroupList() val responseCode = response.separateResponseCode() if (responseCode == 200) { groupModel.value = gson.fromJson<ProjectGroupModel>( response, object : TypeToken<ProjectGroupModel>() {}.type ) } else { response.toErrorMessage().show(BaseApplication.obtainInstance()) } }, { it.printStackTrace() }) fun obtainDeviceListByGroup(wellGroupId: String) = launch({ loadState.value = LoadState.Loading val response = RetrofitServiceManager.obtainDeviceListByGroup(wellGroupId) val responseCode = response.separateResponseCode() if (responseCode == 200) { loadState.value = LoadState.Success groupDeviceModel.value = gson.fromJson<GroupDeviceModel>( response, object : TypeToken<GroupDeviceModel>() {}.type ) } else { loadState.value = LoadState.Fail response.toErrorMessage().show(BaseApplication.obtainInstance()) } }, { it.printStackTrace() }) fun obtainDeviceListByPage(groupId: String, order: Int, page: Int) = launch({ loadState.value = LoadState.Loading val response = RetrofitServiceManager.obtainDeviceListByPage(groupId, order, page) val responseCode = response.separateResponseCode() if (responseCode == 200) { loadState.value = LoadState.Success deviceListModel.value = gson.fromJson<DeviceListModel>( response, object : TypeToken<DeviceListModel>() {}.type ) } else { loadState.value = LoadState.Fail response.toErrorMessage().show(BaseApplication.obtainInstance()) } }, { it.printStackTrace() }) }