SplashViewModel.kt 14 KB

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