| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- package com.jld.vod.viewmodel
- import android.app.Application
- 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.db.ModularAllDatabase
- import com.jld.vod.model.bean.*
- import com.jld.vod.utils.http.RetrofitUtils
- /**
- * @author ZhaoFuXin
- * @Email:18276061387@163.com
- * @description:
- * @date :2020/7/22 17:15
- */
- class MainViewModel (app: Application) : AndroidViewModel(app) {
- val modularalldb = Room.databaseBuilder(app, ModularAllDatabase::class.java, "modularall").build()
- val carinfodb = Room.databaseBuilder(app, CarInfoDatabase::class.java, "carinfo").build()
- val languagedb = Room.databaseBuilder(app, LanguageDatabase::class.java, "language").build()
- val modelAllLiveData: MutableLiveData<BaseBean<List<ModelAllBean>>> = MutableLiveData()
- val findAllModularImgLiveData: MutableLiveData<BaseBean<findAllModularImgBean>> = MutableLiveData()
- val findAllModularRotateImgBeanLiveData: MutableLiveData<BaseBean<List<AllModularRotateImgBean>>> = MutableLiveData()
- val findPositionAdvertLiveData: MutableLiveData<BaseBean<List<PositionAdvertBean>>> = MutableLiveData()
- val carinfoLiveData: MutableLiveData<List<CarInfoBean>> = MutableLiveData()
- val ByrtNamesLiveData: MutableLiveData<List<ModelAllBean>> = MutableLiveData()
- val LanguageLiveData: MutableLiveData<List<LanguageBean>> = MutableLiveData()
- /**
- * 获取全部模块信息
- */
- fun findModularAll(userId:Long): MutableLiveData<BaseBean<List<ModelAllBean>>> {
- modelAllLiveData.postValue(BaseBean.loading(null))
- RetrofitUtils.findModularAll(userId,
- { t ->
- // LogUtils.logD("findModularAll"+t)
- when (t.code) {
- 20000 ->{
- if (t.data!=null)
- {
- Thread(Runnable{ run {
- if (!modularalldb.modularAllDao().getAll().isEmpty())
- {
- modularalldb.modularAllDao().delete()
- for ((index,e) in t.data.withIndex()){
- modularalldb.modularAllDao().insertAll(t.data[index])
- }
- }else{
- for ((index,e) in t.data.withIndex()){
- modularalldb.modularAllDao().insertAll(t.data[index])
- }
- }
- }}).start()
- }
- modelAllLiveData.postValue(BaseBean.success(t.data))
- }
- else -> modelAllLiveData.postValue(BaseBean.error(t.message, t.data))
- }
- },
- { ex ->
- ex.printStackTrace()
- modelAllLiveData.postValue(BaseBean.error(ex.message!!, null))
- }
- )
- return modelAllLiveData
- }
- /**
- * 获取所有静态资源
- */
- fun findAllModularImg(userId:String): MutableLiveData<BaseBean<findAllModularImgBean>> {
- // findAllModularImgLiveData.postValue(BaseBean.loading(null))
- RetrofitUtils.findAllModularImg(userId,
- { t ->
- when (t.code) {
- 20000 ->{
- findAllModularImgLiveData.postValue(BaseBean.success(t.data))
- }
- else -> findAllModularImgLiveData.postValue(BaseBean.error(t.message, t.data))
- }
- },
- { ex ->
- ex.printStackTrace()
- findAllModularImgLiveData.postValue(BaseBean.error(ex.message!!, null))
- }
- )
- return findAllModularImgLiveData
- }
- /**
- * 获取所有静态资源
- */
- fun findAllModularRotateImg(userId:String,index: Int): MutableLiveData<BaseBean<List<AllModularRotateImgBean>>> {
- findAllModularImgLiveData.postValue(BaseBean.loading(null))
- RetrofitUtils.findAllModularRotateImg(userId,index,
- { t ->
- when (t.code) {
- 20000 ->{
- findAllModularRotateImgBeanLiveData.postValue(BaseBean.success(t.data))
- }
- else -> findAllModularRotateImgBeanLiveData.postValue(BaseBean.error(t.message, t.data))
- }
- },
- { ex ->
- ex.printStackTrace()
- findAllModularRotateImgBeanLiveData.postValue(BaseBean.error(ex.message!!, null))
- }
- )
- return findAllModularRotateImgBeanLiveData
- }
- /**
- * 获取固定位置广告
- */
- fun findPositionAdvert(): MutableLiveData<BaseBean<List<PositionAdvertBean>>> {
- findPositionAdvertLiveData.postValue(BaseBean.loading(null))
- RetrofitUtils.findPositionAdvert(
- { t ->
- when (t.code) {
- 20000 ->{
- findPositionAdvertLiveData.postValue(BaseBean.success(t.data))
- }
- else -> findPositionAdvertLiveData.postValue(BaseBean.error(t.message, t.data))
- }
- },
- { ex ->
- // ex.printStackTrace()
- findPositionAdvertLiveData.postValue(BaseBean.error(ex.message!!, null))
- }
- )
- return findPositionAdvertLiveData
- }
- /**
- * 获取车辆信息
- */
- fun getCarInfo() {
- Thread(Runnable {
- carinfoLiveData!!.postValue(carinfodb.carInfoDao().getAll())
- }).start()
- }
- /**
- * 获取单个模块
- */
- fun getByrtNamesModular(rtName:String) {
- Thread(Runnable {
- ByrtNamesLiveData!!.postValue(modularalldb.modularAllDao().loadAllByrtNames(rtName))
- }).start()
- }
- /**
- * 获取全部模块
- */
- fun getAllModular() {
- Thread(Runnable {
- ByrtNamesLiveData!!.postValue(modularalldb.modularAllDao().getAll())
- }).start()
- }
- /**
- * 获取所有语言
- */
- fun getLanguage() {
- Thread(Runnable {
- LanguageLiveData!!.postValue(languagedb.languageDao().getAll())
- }).start()
- }
- }
|