Files
peko-android/app/src/main/java/com/chwl/app/home/MeViewModel.kt

67 lines
2.1 KiB
Kotlin

package com.chwl.app.home
import androidx.lifecycle.MutableLiveData
import com.chwl.app.R
import com.chwl.app.base.BaseViewModel
import com.chwl.core.home.model.HomeModel
import com.chwl.core.im.custom.bean.RouterType
import com.chwl.core.room.bean.MeCenterInfo
import com.chwl.library.utils.ResUtil
class MeViewModel : BaseViewModel() {
private var menuList: List<MeCenterInfo>? = null
val menuListLiveData = MutableLiveData<List<MeCenterInfo>?>()
private var donationMenuVisible = false
fun updateDonationMenuVisible(isVisible: Boolean) {
donationMenuVisible = isVisible
if (!menuList.isNullOrEmpty()) {
menuListLiveData.value = transformMenuList(menuList)
}
}
fun requestMenuList() {
safeLaunch(onError = {
if (menuListLiveData.value.isNullOrEmpty()) {
menuListLiveData.postValue(transformMenuList(null))
}
}) {
val value = HomeModel.requestMeCenterInfoList()
menuList = value
menuListLiveData.postValue(transformMenuList(value))
}
}
private fun transformMenuList(list: List<MeCenterInfo>?): List<MeCenterInfo> {
val finalList = ArrayList<MeCenterInfo>()
if (list.isNullOrEmpty()) {
finalList.addAll(getDefaultMenuList())
} else {
finalList.addAll(list)
}
if (!donationMenuVisible) {
val donationMenuPosition = finalList.indexOfFirst {
it.skipType == RouterType.MY_DONATION
}
if (donationMenuPosition >= 0) {
finalList.removeAt(donationMenuPosition)
}
}
return finalList
}
private fun getDefaultMenuList(): MutableList<MeCenterInfo> {
return ArrayList<MeCenterInfo>().apply {
add(
MeCenterInfo(
icon = R.drawable.me_ic_menu_setting,
centerName = ResUtil.getString(R.string.me_setting),
skipType = RouterType.MY_SET
)
)
}
}
}