SplashViewModel.kt 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. package com.jld.vod.viewmodel
  2. import android.app.Application
  3. import android.content.Context
  4. import android.util.Log
  5. import androidx.lifecycle.AndroidViewModel
  6. import androidx.lifecycle.MutableLiveData
  7. import androidx.room.Room
  8. import com.jld.vod.db.CarInfoDatabase
  9. import com.jld.vod.db.LanguageDatabase
  10. import com.jld.vod.model.bean.*
  11. import com.jld.vod.utils.DateUtils
  12. import com.jld.vod.utils.LogUtils
  13. import com.jld.vod.utils.ToastUtils
  14. import com.jld.vod.utils.http.RetrofitUtils
  15. import com.xuexiang.xui.widget.toast.XToast
  16. /**
  17. * Create by zhaofuxin on 2020/7/14
  18. */
  19. class SplashViewModel(app: Application) : AndroidViewModel(app) {
  20. private var app: Application? = null
  21. init{ this.app = app }
  22. val languagedb = Room.databaseBuilder(app, LanguageDatabase::class.java, "language").build()
  23. val carinfodb = Room.databaseBuilder(app, CarInfoDatabase::class.java, "carinfo").build()
  24. val findLanguageAllliveData: MutableLiveData<BaseBean<List<LanguageBean>>> = MutableLiveData()
  25. // 获取车辆信息
  26. val carInfoliveData: MutableLiveData<BaseBean<List<CarInfoBean>>> = MutableLiveData()
  27. val carRunInfoliveData: MutableLiveData<BaseBean<CarRunInfoBean>> = MutableLiveData()
  28. val detectAppUpdatesliveData: MutableLiveData<String> = MutableLiveData()
  29. val previewAPKliveData: MutableLiveData<String> = MutableLiveData()
  30. //检测是否绑定车辆
  31. val eqCarliveData: MutableLiveData<String> = MutableLiveData()
  32. val findCarByUnboundliveData: MutableLiveData<BaseBean<List<FindCarByUnboundBean>>> = MutableLiveData()
  33. val bindingliveData: MutableLiveData<String> = MutableLiveData()
  34. val findGameByNoInstallliveData: MutableLiveData<BaseBean<List<FindGameByNoInstallBean>>> = MutableLiveData()
  35. val updateGameInstallStateliveData: MutableLiveData<String> = MutableLiveData()
  36. val findHomePageImgliveData: MutableLiveData<BaseBean<HomePageImgBean>> = MutableLiveData()
  37. val NuInstallGameLiveData: MutableLiveData<BaseBean<List<NuInstallGameBean>>> = MutableLiveData()
  38. val updateUserLiveData: MutableLiveData<Boolean> = MutableLiveData()
  39. val findTimeliveData: MutableLiveData<BaseBean<SysFindTimeBean>> = MutableLiveData()
  40. /**
  41. * 获取所有语言信息
  42. */
  43. fun findLanguageAll(): MutableLiveData<BaseBean<List<LanguageBean>>> {
  44. findLanguageAllliveData.postValue(BaseBean.loading(null))
  45. RetrofitUtils.findLanguageAll(
  46. { t ->
  47. when (t.code) {
  48. 20000 ->{
  49. if (t.data!=null)
  50. {
  51. Thread(Runnable{ run {
  52. if (!languagedb.languageDao().getAll().isEmpty())
  53. {
  54. languagedb.languageDao().delete()
  55. for ((index,e) in t.data.withIndex()){
  56. languagedb.languageDao().insertAll(t.data[index])
  57. }
  58. }else{
  59. for ((index,e) in t.data.withIndex()){
  60. languagedb.languageDao().insertAll(t.data[index])
  61. }
  62. }
  63. }}).start()
  64. }
  65. findLanguageAllliveData.postValue(BaseBean.success(t.data))
  66. }
  67. else -> findLanguageAllliveData.postValue(BaseBean.error(t.message, t.data))
  68. }
  69. },
  70. { ex ->
  71. findLanguageAllliveData.postValue(BaseBean.error(ex.message!!, null))
  72. }
  73. )
  74. return findLanguageAllliveData
  75. }
  76. /**
  77. * 获取车辆信息
  78. */
  79. fun getCarInfo(): MutableLiveData<BaseBean<List<CarInfoBean>>> {
  80. Log.i("SplashViewModel", "getCarInfo: 获取车辆信息")
  81. carInfoliveData.postValue(BaseBean.loading(null))
  82. RetrofitUtils.carInfo(
  83. { t ->
  84. when (t.code) {
  85. 20000 ->{
  86. Log.i("SplashViewModel", "getCarInfo: 获取成功")
  87. if (t.data!=null)
  88. {
  89. //将数据存入机子数据库
  90. Thread(Runnable{ run {
  91. Log.i("SplashViewModel", "getCarInfo: 存入本地")
  92. if (!carinfodb.carInfoDao().getAll().isEmpty())
  93. {
  94. carinfodb.carInfoDao().delete()
  95. for ((index,e) in t.data.withIndex()){
  96. carinfodb.carInfoDao().insertAll(t.data[index])
  97. }
  98. }else{
  99. for ((index,e) in t.data.withIndex()){
  100. carinfodb.carInfoDao().insertAll(t.data[index])
  101. }
  102. }
  103. }}).start()
  104. }
  105. //返回订阅通知,将数据返回
  106. carInfoliveData.postValue(BaseBean.success(t.data))
  107. }
  108. else -> carInfoliveData.postValue(BaseBean.error(t.message, t.data))
  109. }
  110. },
  111. { ex ->
  112. Log.e("SplashViewModel", "ex: 获取失败$ex")
  113. carInfoliveData.postValue(BaseBean.error(ex.message!!, null))
  114. })
  115. return carInfoliveData
  116. }
  117. /**
  118. * 获取车辆运行信息
  119. */
  120. fun getCarRunInfo(mContext:Context): MutableLiveData<BaseBean<CarRunInfoBean>> {
  121. carRunInfoliveData.postValue(BaseBean.loading(null))
  122. RetrofitUtils.carRunInfo(
  123. { t ->
  124. when (t.code) {
  125. 20000 -> {
  126. // if (t.data != null)
  127. // {
  128. // DateUtils.setSystemDate(t.data.date,mContext)//设置系统时间
  129. // }
  130. carRunInfoliveData.postValue(BaseBean.success(t.data))
  131. }
  132. else -> carRunInfoliveData.postValue(BaseBean.error(t.message, t.data))
  133. }
  134. },
  135. { ex ->
  136. carRunInfoliveData.postValue(BaseBean.error(ex.message!!, null))
  137. }
  138. )
  139. return carRunInfoliveData
  140. }
  141. /**
  142. * 检测APP是否有更新(与云服务器交互)
  143. */
  144. fun detectAppUpdates(userId:Long,versionNum:Long): MutableLiveData<String> {
  145. // detectAppUpdatesliveData.postValue(null)
  146. RetrofitUtils.detectAppUpdates(userId,versionNum, { t ->
  147. when (t.code) {
  148. 20000 -> detectAppUpdatesliveData.postValue(t.data)
  149. else -> detectAppUpdatesliveData.postValue(t.message)
  150. }
  151. }, { ex ->
  152. ex.printStackTrace()
  153. LogUtils.logD("错误数据:$ex")
  154. detectAppUpdatesliveData.postValue("err")
  155. }
  156. )
  157. return detectAppUpdatesliveData
  158. }
  159. /**
  160. * 下载云apk到本地服务器(与云服务器交互)
  161. */
  162. fun previewAPK(apkFile:String): MutableLiveData<String> {
  163. previewAPKliveData.postValue(null)
  164. RetrofitUtils.previewAPK(apkFile,
  165. { t ->
  166. when (t.code) {
  167. 20000 -> previewAPKliveData.postValue("0")
  168. else -> previewAPKliveData.postValue("404")
  169. }
  170. },
  171. { ex ->
  172. ex.printStackTrace()
  173. previewAPKliveData.postValue(ex.message!!)
  174. }
  175. )
  176. return previewAPKliveData
  177. }
  178. /**
  179. * 检测车辆是否绑定云平台
  180. */
  181. fun eqCar(userId: Long): MutableLiveData<String> {
  182. LogUtils.logD("eqCartuserId"+userId)
  183. eqCarliveData.postValue(null)
  184. RetrofitUtils.eqCar(
  185. { t ->
  186. // LogUtils.logD("eqCart"+t)
  187. when (t.code) {
  188. 20001 ->eqCarliveData.postValue("20001")// findCarByUnbound(userId)//previewAPKliveData.postValue("0")
  189. 20000 -> eqCarliveData.postValue("20000")
  190. else -> eqCarliveData.postValue("404")
  191. }
  192. },
  193. { ex ->
  194. LogUtils.logD("eqCar"+ex)
  195. // eqCarliveData.postValue("404")
  196. //eqCarliveData.postValue(ex.message!!)
  197. }
  198. )
  199. return eqCarliveData
  200. }
  201. /**
  202. * 获取用户未绑定车辆
  203. */
  204. fun findCarByUnbound(userId:Long): MutableLiveData<BaseBean<List<FindCarByUnboundBean>>> {
  205. findCarByUnboundliveData.postValue(null)
  206. LogUtils.logD("获取用户未绑定车辆$userId")
  207. RetrofitUtils.findCarByUnbound(userId,
  208. { t ->
  209. when (t.code) {
  210. 20000 -> {
  211. LogUtils.logD("获取用户未绑定车辆$t")
  212. findCarByUnboundliveData.postValue(BaseBean.success(t.data))
  213. }
  214. else -> findCarByUnboundliveData.postValue(BaseBean.error(t.message, t.data))
  215. }
  216. },
  217. { ex ->
  218. findCarByUnboundliveData.postValue(BaseBean.error(ex.message!!, null))
  219. }
  220. )
  221. return findCarByUnboundliveData
  222. }
  223. /**
  224. * 车辆绑定云平台
  225. */
  226. fun binding(ciId:String): MutableLiveData<String> {
  227. bindingliveData.postValue(null)
  228. RetrofitUtils.binding(ciId,
  229. { t ->
  230. when (t.code) {
  231. 20000 -> bindingliveData.postValue("0")
  232. else -> bindingliveData.postValue("404")
  233. }
  234. },
  235. { ex ->
  236. bindingliveData.postValue(ex.message!!)
  237. }
  238. )
  239. return bindingliveData
  240. }
  241. /**
  242. * 获取未安装游戏列表
  243. */
  244. fun findGameByNoInstall(terminal:String): MutableLiveData<BaseBean<List<FindGameByNoInstallBean>>> {
  245. //findGameByNoInstallliveData.postValue(null)
  246. RetrofitUtils.findGameByNoInstall(terminal, { t ->
  247. when (t.code) {
  248. 20000 ->{
  249. if (t.data != null ) {
  250. LogUtils.logD("findGameInstalledPath: 获取未安装游戏,$t----"+t.data+"-----------"+BaseBean.success(t.data))
  251. findGameByNoInstallliveData.postValue(BaseBean.success(t.data))
  252. }else{
  253. LogUtils.logD("findGameInstalledPath: 没有数据,$t----"+t.data+"-----------"+BaseBean.success(t.data))
  254. findGameByNoInstallliveData.postValue(BaseBean.error(t.message, t.data))
  255. }
  256. }
  257. else -> findGameByNoInstallliveData.postValue(BaseBean.error(t.message, t.data))
  258. }
  259. },
  260. { ex ->
  261. LogUtils.logD("获取游戏未安装,获取游戏未安装$ex")
  262. findGameByNoInstallliveData.postValue(BaseBean.error(ex.message!!, null))
  263. }
  264. )
  265. return findGameByNoInstallliveData
  266. }
  267. /**
  268. * 更新游戏安装状态
  269. */
  270. fun updateGameInstallState(resId: String,install: String,terminal: String): MutableLiveData<String> {
  271. updateGameInstallStateliveData.postValue(null)
  272. RetrofitUtils.updateGameInstallState(resId,install,terminal,
  273. { t ->
  274. when (t.code) {
  275. 20000 -> updateGameInstallStateliveData.postValue("0")
  276. else -> updateGameInstallStateliveData.postValue("404")
  277. }
  278. },
  279. { ex ->
  280. updateGameInstallStateliveData.postValue(ex.message!!)
  281. }
  282. )
  283. return updateGameInstallStateliveData
  284. }
  285. /**
  286. * 获取系统主页静态资源
  287. */
  288. fun findHomePageImg(): MutableLiveData<BaseBean<HomePageImgBean>> {
  289. findHomePageImgliveData.postValue(null)
  290. RetrofitUtils.findHomePageImg(
  291. { t ->
  292. when (t.code) {
  293. 20000 -> findHomePageImgliveData.postValue(BaseBean.success(t.data))
  294. else -> findHomePageImgliveData.postValue(BaseBean.error(t.message, t.data))
  295. }
  296. },
  297. { ex ->
  298. ex.printStackTrace()
  299. findHomePageImgliveData.postValue(BaseBean.error(ex.message!!, null))
  300. }
  301. )
  302. return findHomePageImgliveData
  303. }
  304. /**
  305. * 返回需要卸载的游戏
  306. */
  307. fun getNuInstallGame(terminal:String): MutableLiveData<BaseBean<List<NuInstallGameBean>>> {
  308. NuInstallGameLiveData.postValue(null)
  309. RetrofitUtils.getNuInstallGame(terminal,
  310. { t ->
  311. when (t.code) {
  312. 20000 -> NuInstallGameLiveData.postValue(BaseBean.success(t.data))
  313. else -> NuInstallGameLiveData.postValue(BaseBean.error(t.message, t.data))
  314. }
  315. },
  316. { ex ->
  317. ex.printStackTrace()
  318. NuInstallGameLiveData.postValue(BaseBean.error(ex.message!!, null))
  319. }
  320. )
  321. return NuInstallGameLiveData
  322. }
  323. /**
  324. * 删除安装目录
  325. */
  326. fun delInstallPath(tiId:String): MutableLiveData<BaseBean<List<NuInstallGameBean>>> {
  327. NuInstallGameLiveData.postValue(null)
  328. RetrofitUtils.delInstallPath(tiId,
  329. { t ->
  330. when (t.code) {
  331. 20000 -> NuInstallGameLiveData.postValue(BaseBean.success(t.data))
  332. else -> NuInstallGameLiveData.postValue(BaseBean.error(t.message, t.data))
  333. }
  334. },
  335. { ex ->
  336. ex.printStackTrace()
  337. NuInstallGameLiveData.postValue(BaseBean.error(ex.message!!, null))
  338. }
  339. )
  340. return NuInstallGameLiveData
  341. }
  342. /**
  343. * 绑定用户
  344. */
  345. fun updateUser(userId:String): MutableLiveData<Boolean> {
  346. RetrofitUtils.updateUser(userId,
  347. { t ->
  348. when (t.code) {
  349. 20000 -> updateUserLiveData.postValue(true)
  350. else -> updateUserLiveData.postValue(false)
  351. }
  352. },
  353. { ex ->
  354. ex.printStackTrace()
  355. updateUserLiveData.postValue(false)
  356. }
  357. )
  358. return updateUserLiveData
  359. }
  360. /**
  361. * 获取车辆运行信息
  362. */
  363. fun findTime(mContext:Context): MutableLiveData<BaseBean<SysFindTimeBean>> {
  364. findTimeliveData.postValue(BaseBean.loading(null))
  365. RetrofitUtils.findTime(
  366. { t ->
  367. when (t.code) {
  368. 20000 -> {
  369. if (t.data != null)
  370. {
  371. DateUtils.setSystemDate(t.data.timestamp,t.data.timeZone,mContext)//设置系统时间
  372. }
  373. findTimeliveData.postValue(BaseBean.success(t.data))
  374. }
  375. else -> findTimeliveData.postValue(BaseBean.error(t.message, t.data))
  376. }
  377. },
  378. { ex ->
  379. findTimeliveData.postValue(BaseBean.error(ex.message!!, null))
  380. }
  381. )
  382. return findTimeliveData
  383. }
  384. }