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

import android.content.Context
import androidx.lifecycle.MutableLiveData
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 groupData = MutableLiveData<ProjectGroupModel>()
    val groupDeviceData = MutableLiveData<GroupDeviceModel>()
    val deviceListData = MutableLiveData<DeviceListModel>()
    val groupListData = MutableLiveData<GroupListModel>()
    val changeGroupResultData = MutableLiveData<CommonResultModel>()

    fun getProGroupList(context: Context) = launch({
        val response = RetrofitServiceManager.getProGroupList()
        val responseCode = response.separateResponseCode()
        if (responseCode == 200) {
            groupData.value = gson.fromJson<ProjectGroupModel>(
                response, object : TypeToken<ProjectGroupModel>() {}.type
            )
        } else {
            response.toErrorMessage().show(context)
        }
    }, {
        it.printStackTrace()
    })

    fun getDeviceListByGroup(context: Context, wellGroupId: String) = launch({
        loadState.value = LoadState.Loading
        val response = RetrofitServiceManager.getDeviceListByGroup(wellGroupId)
        val responseCode = response.separateResponseCode()
        if (responseCode == 200) {
            loadState.value = LoadState.Success
            groupDeviceData.value = gson.fromJson<GroupDeviceModel>(
                response, object : TypeToken<GroupDeviceModel>() {}.type
            )
        } else {
            loadState.value = LoadState.Fail
            response.toErrorMessage().show(context)
        }
    }, {
        loadState.value = LoadState.Fail
        it.printStackTrace()
    })

    fun getDeviceListByPage(context: Context, groupId: String, order: Int, page: Int) = launch({
        loadState.value = LoadState.Loading
        val response = RetrofitServiceManager.getDeviceListByPage(groupId, order, page)
        val responseCode = response.separateResponseCode()
        if (responseCode == 200) {
            loadState.value = LoadState.Success
            deviceListData.value = gson.fromJson<DeviceListModel>(
                response, object : TypeToken<DeviceListModel>() {}.type
            )
        } else {
            loadState.value = LoadState.Fail
            response.toErrorMessage().show(context)
        }
    }, {
        loadState.value = LoadState.Fail
        it.printStackTrace()
    })

    fun getGroupList(context: Context) = launch({
        val response = RetrofitServiceManager.getGroupList()
        val responseCode = response.separateResponseCode()
        if (responseCode == 200) {
            groupListData.value = gson.fromJson<GroupListModel>(
                response, object : TypeToken<GroupListModel>() {}.type
            )
        } else {
            response.toErrorMessage().show(context)
        }
    }, {
        it.printStackTrace()
    })

    fun changeGroupId(context: Context, oldGroupId: String, newGroupId: String) = launch({
        val response = RetrofitServiceManager.changeGroupId(oldGroupId, newGroupId)
        val responseCode = response.separateResponseCode()
        if (responseCode == 200) {
            changeGroupResultData.value = gson.fromJson<CommonResultModel>(
                response, object : TypeToken<CommonResultModel>() {}.type
            )
        } else {
            response.toErrorMessage().show(context)
        }
    }, {
        it.printStackTrace()
    })
}