MainViewModel.kt 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package com.jld.vod.viewmodel
  2. import android.app.Application
  3. import androidx.lifecycle.AndroidViewModel
  4. import androidx.lifecycle.MutableLiveData
  5. import androidx.room.Room
  6. import com.jld.vod.db.CarInfoDatabase
  7. import com.jld.vod.db.LanguageDatabase
  8. import com.jld.vod.db.ModularAllDatabase
  9. import com.jld.vod.model.bean.*
  10. import com.jld.vod.utils.http.RetrofitUtils
  11. /**
  12. * @author ZhaoFuXin
  13. * @Email:18276061387@163.com
  14. * @description:
  15. * @date :2020/7/22 17:15
  16. */
  17. class MainViewModel (app: Application) : AndroidViewModel(app) {
  18. val modularalldb = Room.databaseBuilder(app, ModularAllDatabase::class.java, "modularall").build()
  19. val carinfodb = Room.databaseBuilder(app, CarInfoDatabase::class.java, "carinfo").build()
  20. val languagedb = Room.databaseBuilder(app, LanguageDatabase::class.java, "language").build()
  21. val modelAllLiveData: MutableLiveData<BaseBean<List<ModelAllBean>>> = MutableLiveData()
  22. val findAllModularImgLiveData: MutableLiveData<BaseBean<findAllModularImgBean>> = MutableLiveData()
  23. val findAllModularRotateImgBeanLiveData: MutableLiveData<BaseBean<List<AllModularRotateImgBean>>> = MutableLiveData()
  24. val findPositionAdvertLiveData: MutableLiveData<BaseBean<List<PositionAdvertBean>>> = MutableLiveData()
  25. val carinfoLiveData: MutableLiveData<List<CarInfoBean>> = MutableLiveData()
  26. val ByrtNamesLiveData: MutableLiveData<List<ModelAllBean>> = MutableLiveData()
  27. val LanguageLiveData: MutableLiveData<List<LanguageBean>> = MutableLiveData()
  28. /**
  29. * 获取全部模块信息
  30. */
  31. fun findModularAll(userId:Long): MutableLiveData<BaseBean<List<ModelAllBean>>> {
  32. modelAllLiveData.postValue(BaseBean.loading(null))
  33. RetrofitUtils.findModularAll(userId,
  34. { t ->
  35. // LogUtils.logD("findModularAll"+t)
  36. when (t.code) {
  37. 20000 ->{
  38. if (t.data!=null)
  39. {
  40. Thread(Runnable{ run {
  41. if (!modularalldb.modularAllDao().getAll().isEmpty())
  42. {
  43. modularalldb.modularAllDao().delete()
  44. for ((index,e) in t.data.withIndex()){
  45. modularalldb.modularAllDao().insertAll(t.data[index])
  46. }
  47. }else{
  48. for ((index,e) in t.data.withIndex()){
  49. modularalldb.modularAllDao().insertAll(t.data[index])
  50. }
  51. }
  52. }}).start()
  53. }
  54. modelAllLiveData.postValue(BaseBean.success(t.data))
  55. }
  56. else -> modelAllLiveData.postValue(BaseBean.error(t.message, t.data))
  57. }
  58. },
  59. { ex ->
  60. ex.printStackTrace()
  61. modelAllLiveData.postValue(BaseBean.error(ex.message!!, null))
  62. }
  63. )
  64. return modelAllLiveData
  65. }
  66. /**
  67. * 获取所有静态资源
  68. */
  69. fun findAllModularImg(userId:String): MutableLiveData<BaseBean<findAllModularImgBean>> {
  70. // findAllModularImgLiveData.postValue(BaseBean.loading(null))
  71. RetrofitUtils.findAllModularImg(userId,
  72. { t ->
  73. when (t.code) {
  74. 20000 ->{
  75. findAllModularImgLiveData.postValue(BaseBean.success(t.data))
  76. }
  77. else -> findAllModularImgLiveData.postValue(BaseBean.error(t.message, t.data))
  78. }
  79. },
  80. { ex ->
  81. ex.printStackTrace()
  82. findAllModularImgLiveData.postValue(BaseBean.error(ex.message!!, null))
  83. }
  84. )
  85. return findAllModularImgLiveData
  86. }
  87. /**
  88. * 获取所有静态资源
  89. */
  90. fun findAllModularRotateImg(userId:String,index: Int): MutableLiveData<BaseBean<List<AllModularRotateImgBean>>> {
  91. findAllModularImgLiveData.postValue(BaseBean.loading(null))
  92. RetrofitUtils.findAllModularRotateImg(userId,index,
  93. { t ->
  94. when (t.code) {
  95. 20000 ->{
  96. findAllModularRotateImgBeanLiveData.postValue(BaseBean.success(t.data))
  97. }
  98. else -> findAllModularRotateImgBeanLiveData.postValue(BaseBean.error(t.message, t.data))
  99. }
  100. },
  101. { ex ->
  102. ex.printStackTrace()
  103. findAllModularRotateImgBeanLiveData.postValue(BaseBean.error(ex.message!!, null))
  104. }
  105. )
  106. return findAllModularRotateImgBeanLiveData
  107. }
  108. /**
  109. * 获取固定位置广告
  110. */
  111. fun findPositionAdvert(): MutableLiveData<BaseBean<List<PositionAdvertBean>>> {
  112. findPositionAdvertLiveData.postValue(BaseBean.loading(null))
  113. RetrofitUtils.findPositionAdvert(
  114. { t ->
  115. when (t.code) {
  116. 20000 ->{
  117. findPositionAdvertLiveData.postValue(BaseBean.success(t.data))
  118. }
  119. else -> findPositionAdvertLiveData.postValue(BaseBean.error(t.message, t.data))
  120. }
  121. },
  122. { ex ->
  123. // ex.printStackTrace()
  124. findPositionAdvertLiveData.postValue(BaseBean.error(ex.message!!, null))
  125. }
  126. )
  127. return findPositionAdvertLiveData
  128. }
  129. /**
  130. * 获取车辆信息
  131. */
  132. fun getCarInfo() {
  133. Thread(Runnable {
  134. carinfoLiveData!!.postValue(carinfodb.carInfoDao().getAll())
  135. }).start()
  136. }
  137. /**
  138. * 获取单个模块
  139. */
  140. fun getByrtNamesModular(rtName:String) {
  141. Thread(Runnable {
  142. ByrtNamesLiveData!!.postValue(modularalldb.modularAllDao().loadAllByrtNames(rtName))
  143. }).start()
  144. }
  145. /**
  146. * 获取全部模块
  147. */
  148. fun getAllModular() {
  149. Thread(Runnable {
  150. ByrtNamesLiveData!!.postValue(modularalldb.modularAllDao().getAll())
  151. }).start()
  152. }
  153. /**
  154. * 获取所有语言
  155. */
  156. fun getLanguage() {
  157. Thread(Runnable {
  158. LanguageLiveData!!.postValue(languagedb.languageDao().getAll())
  159. }).start()
  160. }
  161. }