SplashViewModel.kt 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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 ->eqCarliveData.postValue("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("404")
  192. //eqCarliveData.postValue(ex.message!!)
  193. }
  194. )
  195. return eqCarliveData
  196. }
  197. /**
  198. * 获取用户未绑定车辆
  199. */
  200. fun findCarByUnbound(userId:Long): MutableLiveData<BaseBean<List<FindCarByUnboundBean>>> {
  201. findCarByUnboundliveData.postValue(null)
  202. LogUtils.logD("获取用户未绑定车辆$userId")
  203. RetrofitUtils.findCarByUnbound(userId,
  204. { t ->
  205. when (t.code) {
  206. 20000 -> {
  207. LogUtils.logD("获取用户未绑定车辆$t")
  208. findCarByUnboundliveData.postValue(BaseBean.success(t.data))
  209. }
  210. else -> findCarByUnboundliveData.postValue(BaseBean.error(t.message, t.data))
  211. }
  212. },
  213. { ex ->
  214. findCarByUnboundliveData.postValue(BaseBean.error(ex.message!!, null))
  215. }
  216. )
  217. return findCarByUnboundliveData
  218. }
  219. /**
  220. * 车辆绑定云平台
  221. */
  222. fun binding(ciId:String): MutableLiveData<String> {
  223. bindingliveData.postValue(null)
  224. RetrofitUtils.binding(ciId,
  225. { t ->
  226. when (t.code) {
  227. 20000 -> bindingliveData.postValue("0")
  228. else -> bindingliveData.postValue("404")
  229. }
  230. },
  231. { ex ->
  232. bindingliveData.postValue(ex.message!!)
  233. }
  234. )
  235. return bindingliveData
  236. }
  237. /**
  238. * 获取未安装游戏列表
  239. */
  240. fun findGameByNoInstall(terminal:String): MutableLiveData<BaseBean<List<FindGameByNoInstallBean>>> {
  241. LogUtils.logD("findGameInstalledPath: 正在未安装游戏")
  242. findGameByNoInstallliveData.postValue(null)
  243. RetrofitUtils.findGameByNoInstall(terminal,
  244. { t ->
  245. when (t.code) {
  246. 20000 ->{
  247. if (t.data != null)
  248. {
  249. LogUtils.logD("findGameInstalledPath: 获取未安装游戏,$t----"+t.data)
  250. findGameByNoInstallliveData.postValue(BaseBean.success(t.data))
  251. }else{
  252. findGameByNoInstallliveData.postValue(BaseBean.error(t.message, t.data))
  253. }
  254. }
  255. else -> findGameByNoInstallliveData.postValue(BaseBean.error(t.message, t.data))
  256. }
  257. },
  258. { ex ->
  259. LogUtils.logD("获取游戏未安装,获取游戏未安装$ex")
  260. findGameByNoInstallliveData.postValue(BaseBean.error(ex.message!!, null))
  261. }
  262. )
  263. return findGameByNoInstallliveData
  264. }
  265. /**
  266. * 更新游戏安装状态
  267. */
  268. fun updateGameInstallState(resId: String,install: String,terminal: String): MutableLiveData<String> {
  269. updateGameInstallStateliveData.postValue(null)
  270. RetrofitUtils.updateGameInstallState(resId,install,terminal,
  271. { t ->
  272. when (t.code) {
  273. 20000 -> updateGameInstallStateliveData.postValue("0")
  274. else -> updateGameInstallStateliveData.postValue("404")
  275. }
  276. },
  277. { ex ->
  278. updateGameInstallStateliveData.postValue(ex.message!!)
  279. }
  280. )
  281. return updateGameInstallStateliveData
  282. }
  283. /**
  284. * 获取系统主页静态资源
  285. */
  286. fun findHomePageImg(): MutableLiveData<BaseBean<HomePageImgBean>> {
  287. findHomePageImgliveData.postValue(null)
  288. RetrofitUtils.findHomePageImg(
  289. { t ->
  290. when (t.code) {
  291. 20000 -> findHomePageImgliveData.postValue(BaseBean.success(t.data))
  292. else -> findHomePageImgliveData.postValue(BaseBean.error(t.message, t.data))
  293. }
  294. },
  295. { ex ->
  296. ex.printStackTrace()
  297. findHomePageImgliveData.postValue(BaseBean.error(ex.message!!, null))
  298. }
  299. )
  300. return findHomePageImgliveData
  301. }
  302. /**
  303. * 返回需要卸载的游戏
  304. */
  305. fun getNuInstallGame(terminal:String): MutableLiveData<BaseBean<List<NuInstallGameBean>>> {
  306. NuInstallGameLiveData.postValue(null)
  307. RetrofitUtils.getNuInstallGame(terminal,
  308. { t ->
  309. when (t.code) {
  310. 20000 -> NuInstallGameLiveData.postValue(BaseBean.success(t.data))
  311. else -> NuInstallGameLiveData.postValue(BaseBean.error(t.message, t.data))
  312. }
  313. },
  314. { ex ->
  315. ex.printStackTrace()
  316. NuInstallGameLiveData.postValue(BaseBean.error(ex.message!!, null))
  317. }
  318. )
  319. return NuInstallGameLiveData
  320. }
  321. /**
  322. * 删除安装目录
  323. */
  324. fun delInstallPath(tiId:String): MutableLiveData<BaseBean<List<NuInstallGameBean>>> {
  325. NuInstallGameLiveData.postValue(null)
  326. RetrofitUtils.delInstallPath(tiId,
  327. { t ->
  328. when (t.code) {
  329. 20000 -> NuInstallGameLiveData.postValue(BaseBean.success(t.data))
  330. else -> NuInstallGameLiveData.postValue(BaseBean.error(t.message, t.data))
  331. }
  332. },
  333. { ex ->
  334. ex.printStackTrace()
  335. NuInstallGameLiveData.postValue(BaseBean.error(ex.message!!, null))
  336. }
  337. )
  338. return NuInstallGameLiveData
  339. }
  340. /**
  341. * 绑定用户
  342. */
  343. fun updateUser(userId:String): MutableLiveData<Boolean> {
  344. RetrofitUtils.updateUser(userId,
  345. { t ->
  346. when (t.code) {
  347. 20000 -> updateUserLiveData.postValue(true)
  348. else -> updateUserLiveData.postValue(false)
  349. }
  350. },
  351. { ex ->
  352. ex.printStackTrace()
  353. updateUserLiveData.postValue(false)
  354. }
  355. )
  356. return updateUserLiveData
  357. }
  358. }