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.CommonResultModel import com.casic.smarttube.model.DeviceListModel import com.casic.smarttube.model.GroupDeviceModel import com.casic.smarttube.model.GroupListModel import com.casic.smarttube.model.ProjectGroupModel import com.casic.smarttube.retrofit.RetrofitServiceManager import com.google.gson.Gson import com.google.gson.reflect.TypeToken import com.pengxh.kt.lite.base.BaseViewModel import com.pengxh.kt.lite.extensions.launch import com.pengxh.kt.lite.extensions.show import com.pengxh.kt.lite.utils.LoadState class ProjectGroupViewModel : BaseViewModel() { private val gson = Gson() val groupModel = MutableLiveData<ProjectGroupModel>() val groupDeviceModel = MutableLiveData<GroupDeviceModel>() val deviceListModel = MutableLiveData<DeviceListModel>() val groupListModel = MutableLiveData<GroupListModel>() val changeGroupResult = MutableLiveData<CommonResultModel>() 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() }) fun obtainGroupList() = launch({ val response = RetrofitServiceManager.obtainGroupList() val responseCode = response.separateResponseCode() if (responseCode == 200) { groupListModel.value = gson.fromJson<GroupListModel>( response, object : TypeToken<GroupListModel>() {}.type ) } else { response.toErrorMessage().show(BaseApplication.obtainInstance()) } }, { it.printStackTrace() }) fun changeGroupId(oldGroupId: String, newGroupId: String) = launch({ val response = RetrofitServiceManager.changeGroupId(oldGroupId, newGroupId) val responseCode = response.separateResponseCode() if (responseCode == 200) { changeGroupResult.value = gson.fromJson<CommonResultModel>( response, object : TypeToken<CommonResultModel>() {}.type ) } else { response.toErrorMessage().show(BaseApplication.obtainInstance()) } }, { it.printStackTrace() }) }