package com.jld.vod.viewmodel import android.app.Application import android.content.Context import android.util.Log import androidx.lifecycle.AndroidViewModel import androidx.lifecycle.MutableLiveData import androidx.room.Room import com.jld.vod.db.CarInfoDatabase import com.jld.vod.db.LanguageDatabase import com.jld.vod.model.bean.* import com.jld.vod.utils.DateUtils import com.jld.vod.utils.LogUtils import com.jld.vod.utils.ToastUtils import com.jld.vod.utils.http.RetrofitUtils import com.xuexiang.xui.widget.toast.XToast /** * Create by zhaofuxin on 2020/7/14 */ class SplashViewModel(app: Application) : AndroidViewModel(app) { private var app: Application? = null init{ this.app = app } val languagedb = Room.databaseBuilder(app, LanguageDatabase::class.java, "language").build() val carinfodb = Room.databaseBuilder(app, CarInfoDatabase::class.java, "carinfo").build() val findLanguageAllliveData: MutableLiveData>> = MutableLiveData() // 获取车辆信息 val carInfoliveData: MutableLiveData>> = MutableLiveData() val carRunInfoliveData: MutableLiveData> = MutableLiveData() val detectAppUpdatesliveData: MutableLiveData = MutableLiveData() val previewAPKliveData: MutableLiveData = MutableLiveData() //检测是否绑定车辆 val eqCarliveData: MutableLiveData = MutableLiveData() val findCarByUnboundliveData: MutableLiveData>> = MutableLiveData() val bindingliveData: MutableLiveData = MutableLiveData() val findGameByNoInstallliveData: MutableLiveData>> = MutableLiveData() val updateGameInstallStateliveData: MutableLiveData = MutableLiveData() val findHomePageImgliveData: MutableLiveData> = MutableLiveData() val NuInstallGameLiveData: MutableLiveData>> = MutableLiveData() val updateUserLiveData: MutableLiveData = MutableLiveData() val findTimeliveData: MutableLiveData> = MutableLiveData() /** * 获取所有语言信息 */ fun findLanguageAll(): MutableLiveData>> { findLanguageAllliveData.postValue(BaseBean.loading(null)) RetrofitUtils.findLanguageAll( { t -> when (t.code) { 20000 ->{ if (t.data!=null) { Thread(Runnable{ run { if (!languagedb.languageDao().getAll().isEmpty()) { languagedb.languageDao().delete() for ((index,e) in t.data.withIndex()){ languagedb.languageDao().insertAll(t.data[index]) } }else{ for ((index,e) in t.data.withIndex()){ languagedb.languageDao().insertAll(t.data[index]) } } }}).start() } findLanguageAllliveData.postValue(BaseBean.success(t.data)) } else -> findLanguageAllliveData.postValue(BaseBean.error(t.message, t.data)) } }, { ex -> findLanguageAllliveData.postValue(BaseBean.error(ex.message!!, null)) } ) return findLanguageAllliveData } /** * 获取车辆信息 */ fun getCarInfo(): MutableLiveData>> { Log.i("SplashViewModel", "getCarInfo: 获取车辆信息") carInfoliveData.postValue(BaseBean.loading(null)) RetrofitUtils.carInfo( { t -> when (t.code) { 20000 ->{ Log.i("SplashViewModel", "getCarInfo: 获取成功") if (t.data!=null) { //将数据存入机子数据库 Thread(Runnable{ run { Log.i("SplashViewModel", "getCarInfo: 存入本地") if (!carinfodb.carInfoDao().getAll().isEmpty()) { carinfodb.carInfoDao().delete() for ((index,e) in t.data.withIndex()){ carinfodb.carInfoDao().insertAll(t.data[index]) } }else{ for ((index,e) in t.data.withIndex()){ carinfodb.carInfoDao().insertAll(t.data[index]) } } }}).start() } //返回订阅通知,将数据返回 carInfoliveData.postValue(BaseBean.success(t.data)) } else -> carInfoliveData.postValue(BaseBean.error(t.message, t.data)) } }, { ex -> Log.e("SplashViewModel", "ex: 获取失败$ex") carInfoliveData.postValue(BaseBean.error(ex.message!!, null)) }) return carInfoliveData } /** * 获取车辆运行信息 */ fun getCarRunInfo(mContext:Context): MutableLiveData> { carRunInfoliveData.postValue(BaseBean.loading(null)) RetrofitUtils.carRunInfo( { t -> when (t.code) { 20000 -> { // if (t.data != null) // { // DateUtils.setSystemDate(t.data.date,mContext)//设置系统时间 // } carRunInfoliveData.postValue(BaseBean.success(t.data)) } else -> carRunInfoliveData.postValue(BaseBean.error(t.message, t.data)) } }, { ex -> carRunInfoliveData.postValue(BaseBean.error(ex.message!!, null)) } ) return carRunInfoliveData } /** * 检测APP是否有更新(与云服务器交互) */ fun detectAppUpdates(userId:Long,versionNum:Long): MutableLiveData { // detectAppUpdatesliveData.postValue(null) RetrofitUtils.detectAppUpdates(userId,versionNum, { t -> when (t.code) { 20000 -> detectAppUpdatesliveData.postValue(t.data) else -> detectAppUpdatesliveData.postValue(t.message) } }, { ex -> ex.printStackTrace() LogUtils.logD("错误数据:$ex") detectAppUpdatesliveData.postValue("err") } ) return detectAppUpdatesliveData } /** * 下载云apk到本地服务器(与云服务器交互) */ fun previewAPK(apkFile:String): MutableLiveData { previewAPKliveData.postValue(null) RetrofitUtils.previewAPK(apkFile, { t -> when (t.code) { 20000 -> previewAPKliveData.postValue("0") else -> previewAPKliveData.postValue("404") } }, { ex -> ex.printStackTrace() previewAPKliveData.postValue(ex.message!!) } ) return previewAPKliveData } /** * 检测车辆是否绑定云平台 */ fun eqCar(userId: Long): MutableLiveData { LogUtils.logD("eqCartuserId"+userId) eqCarliveData.postValue(null) RetrofitUtils.eqCar( { t -> // LogUtils.logD("eqCart"+t) when (t.code) { 20001 ->eqCarliveData.postValue("20001")// findCarByUnbound(userId)//previewAPKliveData.postValue("0") 20000 -> eqCarliveData.postValue("20000") else -> eqCarliveData.postValue("404") } }, { ex -> LogUtils.logD("eqCar"+ex) // eqCarliveData.postValue("404") //eqCarliveData.postValue(ex.message!!) } ) return eqCarliveData } /** * 获取用户未绑定车辆 */ fun findCarByUnbound(userId:Long): MutableLiveData>> { findCarByUnboundliveData.postValue(null) LogUtils.logD("获取用户未绑定车辆$userId") RetrofitUtils.findCarByUnbound(userId, { t -> when (t.code) { 20000 -> { LogUtils.logD("获取用户未绑定车辆$t") findCarByUnboundliveData.postValue(BaseBean.success(t.data)) } else -> findCarByUnboundliveData.postValue(BaseBean.error(t.message, t.data)) } }, { ex -> findCarByUnboundliveData.postValue(BaseBean.error(ex.message!!, null)) } ) return findCarByUnboundliveData } /** * 车辆绑定云平台 */ fun binding(ciId:String): MutableLiveData { bindingliveData.postValue(null) RetrofitUtils.binding(ciId, { t -> when (t.code) { 20000 -> bindingliveData.postValue("0") else -> bindingliveData.postValue("404") } }, { ex -> bindingliveData.postValue(ex.message!!) } ) return bindingliveData } /** * 获取未安装游戏列表 */ fun findGameByNoInstall(terminal:String): MutableLiveData>> { //findGameByNoInstallliveData.postValue(null) RetrofitUtils.findGameByNoInstall(terminal, { t -> when (t.code) { 20000 ->{ if (t.data != null ) { LogUtils.logD("findGameInstalledPath: 获取未安装游戏,$t----"+t.data+"-----------"+BaseBean.success(t.data)) findGameByNoInstallliveData.postValue(BaseBean.success(t.data)) }else{ LogUtils.logD("findGameInstalledPath: 没有数据,$t----"+t.data+"-----------"+BaseBean.success(t.data)) findGameByNoInstallliveData.postValue(BaseBean.error(t.message, t.data)) } } else -> findGameByNoInstallliveData.postValue(BaseBean.error(t.message, t.data)) } }, { ex -> LogUtils.logD("获取游戏未安装,获取游戏未安装$ex") findGameByNoInstallliveData.postValue(BaseBean.error(ex.message!!, null)) } ) return findGameByNoInstallliveData } /** * 更新游戏安装状态 */ fun updateGameInstallState(resId: String,install: String,terminal: String): MutableLiveData { updateGameInstallStateliveData.postValue(null) RetrofitUtils.updateGameInstallState(resId,install,terminal, { t -> when (t.code) { 20000 -> updateGameInstallStateliveData.postValue("0") else -> updateGameInstallStateliveData.postValue("404") } }, { ex -> updateGameInstallStateliveData.postValue(ex.message!!) } ) return updateGameInstallStateliveData } /** * 获取系统主页静态资源 */ fun findHomePageImg(): MutableLiveData> { findHomePageImgliveData.postValue(null) RetrofitUtils.findHomePageImg( { t -> when (t.code) { 20000 -> findHomePageImgliveData.postValue(BaseBean.success(t.data)) else -> findHomePageImgliveData.postValue(BaseBean.error(t.message, t.data)) } }, { ex -> ex.printStackTrace() findHomePageImgliveData.postValue(BaseBean.error(ex.message!!, null)) } ) return findHomePageImgliveData } /** * 返回需要卸载的游戏 */ fun getNuInstallGame(terminal:String): MutableLiveData>> { NuInstallGameLiveData.postValue(null) RetrofitUtils.getNuInstallGame(terminal, { t -> when (t.code) { 20000 -> NuInstallGameLiveData.postValue(BaseBean.success(t.data)) else -> NuInstallGameLiveData.postValue(BaseBean.error(t.message, t.data)) } }, { ex -> ex.printStackTrace() NuInstallGameLiveData.postValue(BaseBean.error(ex.message!!, null)) } ) return NuInstallGameLiveData } /** * 删除安装目录 */ fun delInstallPath(tiId:String): MutableLiveData>> { NuInstallGameLiveData.postValue(null) RetrofitUtils.delInstallPath(tiId, { t -> when (t.code) { 20000 -> NuInstallGameLiveData.postValue(BaseBean.success(t.data)) else -> NuInstallGameLiveData.postValue(BaseBean.error(t.message, t.data)) } }, { ex -> ex.printStackTrace() NuInstallGameLiveData.postValue(BaseBean.error(ex.message!!, null)) } ) return NuInstallGameLiveData } /** * 绑定用户 */ fun updateUser(userId:String): MutableLiveData { RetrofitUtils.updateUser(userId, { t -> when (t.code) { 20000 -> updateUserLiveData.postValue(true) else -> updateUserLiveData.postValue(false) } }, { ex -> ex.printStackTrace() updateUserLiveData.postValue(false) } ) return updateUserLiveData } /** * 获取车辆运行信息 */ fun findTime(mContext:Context): MutableLiveData> { findTimeliveData.postValue(BaseBean.loading(null)) RetrofitUtils.findTime( { t -> when (t.code) { 20000 -> { if (t.data != null) { DateUtils.setSystemDate(t.data.timestamp,t.data.timeZone,mContext)//设置系统时间 } findTimeliveData.postValue(BaseBean.success(t.data)) } else -> findTimeliveData.postValue(BaseBean.error(t.message, t.data)) } }, { ex -> findTimeliveData.postValue(BaseBean.error(ex.message!!, null)) } ) return findTimeliveData } }