Files
assistant-android/app/src/main/java/com/gh/gamecenter/wrapper/MainWrapperViewModel.kt

105 lines
3.9 KiB
Kotlin

package com.gh.gamecenter.wrapper
import android.app.Application
import androidx.lifecycle.*
import com.gh.common.util.CheckLoginUtils
import com.gh.common.util.RealNameHelper
import com.gh.gamecenter.common.retrofit.Response
import com.gh.gamecenter.entity.BottomTab
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.livedata.Event
import com.gh.gamecenter.login.user.UserManager
import com.gh.gamecenter.retrofit.RetrofitManager
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.transformWhile
import okhttp3.MediaType
import okhttp3.RequestBody
import okhttp3.ResponseBody
import org.json.JSONObject
import retrofit2.HttpException
class MainWrapperViewModel(application: Application, private val mRepository: MainWrapperRepository) :
AndroidViewModel(application) {
private val mApi = RetrofitManager.getInstance().api
val defaultBottomTabIndex
get() = mRepository.defaultBottomTabIndex
val bottomTabListLiveData = mRepository.bottomTabListLiveData
val errorLiveData = mRepository.errorLiveData
val accelerateNotificationPopup = MutableLiveData<List<GameEntity>?>()
private var lastAccelerateNotificationPopupRequestedTime: Long = 0
val tabSelectedLiveData = mRepository.tabSelectEventFlow
.map { it as? MainSelectedEvent.SelectedTab }
.transformWhile { originalEvent ->
emit(Event(originalEvent))
true
}
.asLiveData()
val realNameInfoUpdateLiveData = RealNameHelper.realNameInfoUpdateLiveData
val bottomDoubleTabAction = MutableLiveData<BottomTab>()
/**
* 请求各种弹窗的数据
*/
fun requestAllDialogData() {
requestAccelerateNotificationPopup()
}
fun requestAccelerateNotificationPopup() {
if (CheckLoginUtils.isLogin()) {
if (lastAccelerateNotificationPopupRequestedTime != 0L
&& System.currentTimeMillis() - lastAccelerateNotificationPopupRequestedTime < 15 * 1000L) {
return // 15 秒内不重复请求
}
lastAccelerateNotificationPopupRequestedTime = System.currentTimeMillis()
mApi.getAccelerateNotificationPopup(UserManager.getInstance().userId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : Response<List<GameEntity>>() {
override fun onResponse(response: List<GameEntity>?) {
super.onResponse(response)
accelerateNotificationPopup.postValue(response)
}
override fun onFailure(e: HttpException?) {
super.onFailure(e)
accelerateNotificationPopup.postValue(null)
}
})
} else {
accelerateNotificationPopup.postValue(null)
}
}
fun postMessageRead(messageId: String) {
val jsonObject = JSONObject()
jsonObject.put("type", "system_message")
val body = RequestBody.create(MediaType.parse("application/json"), jsonObject.toString())
RetrofitManager.getInstance().api.postMessageRead(UserManager.getInstance().userId, messageId, body)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : Response<ResponseBody>() {
})
}
class Factory(
private val mApplication: Application,
) : ViewModelProvider.NewInstanceFactory() {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return MainWrapperViewModel(mApplication, MainWrapperRepository.getInstance()) as T
}
}
companion object {
const val SHOULD_SHOW_OPENING_DIALOG = "show_opening_dialog"
}
}