修复游戏详情加载变慢问题

This commit is contained in:
kehaoyuan
2020-01-19 12:21:03 +08:00
parent cc75cb497d
commit 3dc678bc27
3 changed files with 31 additions and 25 deletions

View File

@ -1,6 +1,5 @@
package com.gh.gamecenter.gamedetail
import android.annotation.SuppressLint
import android.app.Application
import android.os.Build
import androidx.lifecycle.AndroidViewModel
@ -13,9 +12,7 @@ import com.gh.common.util.ConcernUtils
import com.gh.common.util.DataUtils
import com.gh.gamecenter.entity.GameDetailEntity
import com.gh.gamecenter.entity.GameEntity
import com.gh.gamecenter.entity.GameVideoInfo
import com.gh.gamecenter.mvvm.Resource
import com.gh.gamecenter.retrofit.BiResponse
import com.gh.gamecenter.retrofit.Response
import com.gh.gamecenter.retrofit.RetrofitManager
import io.reactivex.android.schedulers.AndroidSchedulers
@ -96,8 +93,7 @@ class GameDetailViewModel(application: Application,
displayTopVideo = response.topVideo != null
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
getGameVideoInfo(response)
// gameDetailLiveData.postValue(Resource.success(response))
gameDetailLiveData.postValue(Resource.success(response))
}
override fun onFailure(e: HttpException?) {
@ -106,25 +102,6 @@ class GameDetailViewModel(application: Application,
})
}
@SuppressLint("CheckResult")
private fun getGameVideoInfo(response: GameDetailEntity) {
mApi.getGameVideoInfo(game?.id)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : BiResponse<GameVideoInfo>() {
override fun onSuccess(data: GameVideoInfo) {
response.introVideo?.videoCount = data.videoCount
gameDetailLiveData.postValue(Resource.success(response))
}
override fun onFailure(exception: Exception) {
gameDetailLiveData.postValue(Resource.success(response))
}
})
}
fun concernCommand(isConcern: Boolean) {
val listener = object : ConcernUtils.onConcernListener {
override fun onSuccess() {

View File

@ -284,7 +284,8 @@ class DescAdapter(context: Context,
private fun initIntroViewHolder(viewHolder: GameDetailIntroViewHolder, descItemData: DescItemData) {
viewHolder.introGallery.isNestedScrollingEnabled = false
if (viewHolder.introGallery.adapter == null) {
val introGalleryAdapter = viewHolder.introGallery.adapter
if (introGalleryAdapter == null) {
viewHolder.introGallery.setHasFixedSize(true)
viewHolder.introGallery.layoutManager = LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false)
val gameGalleryAdapter = GameGalleryAdapter(
@ -295,6 +296,9 @@ class DescAdapter(context: Context,
StringUtils.buildString(mEntrance, "+(游戏详情[", gameName, "]:游戏介绍)"))
viewHolder.introGallery.adapter = gameGalleryAdapter
viewHolder.introGallery.addItemDecoration(HorizontalItemDecoration(mContext, 5, gameGalleryAdapter.itemCount))
} else {
// 视频数量会延迟刷新
if (introGalleryAdapter.itemCount > 0) introGalleryAdapter.notifyItemChanged(0)
}
if (TextUtils.isEmpty(descItemData.intro?.description)) {
viewHolder.introContent.text = ""

View File

@ -13,6 +13,7 @@ import com.gh.common.util.ErrorHelper
import com.gh.common.util.RandomUtils
import com.gh.common.util.toObject
import com.gh.gamecenter.entity.*
import com.gh.gamecenter.retrofit.BiResponse
import com.gh.gamecenter.retrofit.Response
import com.gh.gamecenter.retrofit.RetrofitManager
import io.reactivex.Observable
@ -35,6 +36,9 @@ class DescViewModel(application: Application,
// 构建列表
gameDetail?.let {
// 加载游戏介绍中的视频数量
getGameVideoInfo(it)
// 这里的高、低优先级是指相对于游戏介绍区域的位置,高在上面低在下面
val highPriorityCustomColumnList = arrayListOf<GameDetailEntity.CustomColumn>()
val lowPriorityCustomColumnList = arrayListOf<GameDetailEntity.CustomColumn>()
@ -185,6 +189,7 @@ class DescViewModel(application: Application,
}
})
}
fun unvoteComment(commentId: String, callback: () -> Unit) {
RetrofitManager.getInstance(getApplication())
.api
@ -265,6 +270,26 @@ class DescViewModel(application: Application,
})
}
@SuppressLint("CheckResult")
private fun getGameVideoInfo(detail: GameDetailEntity) {
RetrofitManager.getInstance(getApplication())
.api.getGameVideoInfo(game?.id)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(object : BiResponse<GameVideoInfo>() {
override fun onSuccess(data: GameVideoInfo) {
if (detail.introVideo?.videoCount != data.videoCount) {
detail.introVideo?.videoCount = data.videoCount
list.postValue(mDataList)
}
}
override fun onFailure(exception: Exception) {
}
})
}
class Factory(private val mApplication: Application,
private val game: GameEntity?,
private val gameDetail: GameDetailEntity?) : ViewModelProvider.NewInstanceFactory() {