package com.casic.common.detector.gd.vm

import androidx.lifecycle.MutableLiveData
import com.casic.common.detector.gd.model.VersionResultModel
import com.casic.common.detector.gd.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.utils.LoadState

class VersionViewModel : BaseViewModel() {
    private val gson by lazy { Gson() }
    val versionResult = MutableLiveData<VersionResultModel>()

    fun getApplicationVersion() = launch({
        loadState.value = LoadState.Loading
        val response = RetrofitServiceManager.getApplicationVersion()
        versionResult.value = gson.fromJson<VersionResultModel>(
            response, object : TypeToken<VersionResultModel>() {}.type
        )
        loadState.value = LoadState.Success
    }, {
        it.printStackTrace()
        loadState.value = LoadState.Fail
    })
}