VidiDin-Android/app/src/main/java/com/gamedog/vididin/manager/WithdrawManager.kt

148 lines
5.2 KiB
Kotlin
Raw Normal View History

2025-12-12 07:27:30 +00:00
package com.gamedog.vididin.manager
import com.ama.core.architecture.util.AndroidUtil
2025-12-15 04:37:51 +00:00
import com.ama.core.architecture.util.DateUtil
2025-12-12 07:27:30 +00:00
import com.ama.core.architecture.util.SpUtil
2025-12-15 02:47:58 +00:00
import com.ama.core.architecture.util.eventbus.NotifyMan
import com.gamedog.vididin.VididinEvents
2025-12-15 03:39:03 +00:00
import com.gamedog.vididin.manager.WithdrawManager.Companion.STATE_NEED_WATCH_AD
2025-12-12 07:27:30 +00:00
class WithdrawManager private constructor() {
private val mItemList: MutableList<WithdrawItemBean> by lazy {
val itemList = SpUtil.instance().getList<WithdrawItemBean>(SpUtil.KEY_WITHDRAW_ITEM_LIST).toMutableList()
if (itemList.isEmpty()) {
itemList.addAll(generateItemList())
2025-12-15 02:47:58 +00:00
saveInfos2Sp(itemList)
2025-12-12 07:27:30 +00:00
}
itemList
}
companion object {
const val EACH_SUB_ITEM_CASH_NUM: Float = 1F
2025-12-15 03:39:03 +00:00
const val STATE_NEED_WATCH_AD: Int = 0
const val STATE_COULD_WITHDRAW: Int = 1
const val STATE_WITHDRAWING: Int = 2
const val STATE_HAS_WITHDRAWED: Int = 3
2025-12-12 07:27:30 +00:00
@Volatile
private var instance: WithdrawManager? = null
fun instance(): WithdrawManager {
return instance ?: synchronized(this) {
instance ?: WithdrawManager().also {
instance = it
}
}
}
}
private fun generateItemList(): MutableList<WithdrawItemBean> {
val itemList = mutableListOf<WithdrawItemBean>()
itemList.add(WithdrawItemBean(0, 10F, AndroidUtil.randomInt(50, 70), generateSubItemList(10F)))
itemList.add(WithdrawItemBean(1, 20F, AndroidUtil.randomInt(50, 70), generateSubItemList(20F)))
itemList.add(WithdrawItemBean(2, 50F, AndroidUtil.randomInt(50, 70), generateSubItemList(50F)))
itemList.add(WithdrawItemBean(3, 100F, AndroidUtil.randomInt(50, 70), generateSubItemList(100F)))
itemList.add(WithdrawItemBean(4, 300F, AndroidUtil.randomInt(50, 70), generateSubItemList(300F)))
return itemList
}
private fun generateSubItemList(totalCashInItem: Float): List<WithdrawSubItem> {
val subItemList = mutableListOf<WithdrawSubItem>()
val subItemCount: Int = (totalCashInItem/EACH_SUB_ITEM_CASH_NUM).toInt()
for (i in 0..subItemCount-1) {
2025-12-15 02:47:58 +00:00
val initProgress = AndroidUtil.randomInt(50, 70)
subItemList.add(WithdrawSubItem(i, EACH_SUB_ITEM_CASH_NUM, initProgress, initProgress, 0F))
2025-12-12 07:27:30 +00:00
}
return subItemList
}
2025-12-15 02:47:58 +00:00
private fun saveInfos2Sp(itemList: MutableList<WithdrawItemBean>) {
2025-12-12 07:27:30 +00:00
SpUtil.instance().putList(SpUtil.KEY_WITHDRAW_ITEM_LIST, itemList)
}
fun getItemList(): List<WithdrawItemBean> {
return mItemList
}
2025-12-12 09:23:21 +00:00
fun getItem(itemIndex: Int): WithdrawItemBean {
return mItemList[itemIndex]
2025-12-12 07:27:30 +00:00
}
2025-12-15 02:47:58 +00:00
fun addAdEarnForSubBean(itemIndex: Int, selectedSubIndex: Int, earnMoneyNum: Float) : Boolean {
if (itemIndex >= 0 && itemIndex < mItemList.size) {
try {
val subBean = mItemList[itemIndex].subItemList[selectedSubIndex]
subBean.hasEarnMoneyByAd += earnMoneyNum * 5 // dollar 2 bariz
calculateSubBeanProgress(subBean) //传入 itembean 更新 selectedIndex为下一个
saveInfos2Sp(mItemList)
notifyProgressUpdated(subBean)
return true
} catch (e: Exception) {
e.printStackTrace()
}
}
return false
}
private fun notifyProgressUpdated(subBean: WithdrawSubItem) {
NotifyMan.instance().sendEvent(VididinEvents.EVENT_WITHDRAW_SUB_ITEM_PROGRESS_UPDATED,
NotifyMan.NotifyData(subBean.index))
}
private fun notifySelectedSubBeanChanged(subBean: WithdrawSubItem) {
NotifyMan.instance().sendEvent(VididinEvents.EVENT_WITHDRAW_SELECTED_SUB_ITEM_CHANGED, NotifyMan.NotifyData(subBean.index))
}
private fun calculateSubBeanProgress(subBean: WithdrawSubItem) {
val needEarnProgress = 100 - subBean.startProgress
if (subBean.hasEarnMoneyByAd >= subBean.cashTotal) {
subBean.currentProgress = 100
2025-12-15 03:39:03 +00:00
// update state
if (subBean.withdrawState == STATE_NEED_WATCH_AD) {
subBean.withdrawState = STATE_COULD_WITHDRAW
}
2025-12-15 02:47:58 +00:00
} else {
val newProgress = subBean.startProgress + (needEarnProgress * (subBean.hasEarnMoneyByAd / subBean.cashTotal)).toInt()
subBean.currentProgress = if (newProgress >= 100) 99 else newProgress
}
2025-12-12 07:27:30 +00:00
}
2025-12-15 03:39:03 +00:00
fun updateSubBeanState(subBean: WithdrawSubItem, newState: Int) {
subBean.withdrawState = newState
saveInfos2Sp(mItemList)
}
2025-12-15 04:37:51 +00:00
fun startItem(curItem: WithdrawItemBean) {
if (curItem.startMs <= 0L) {
curItem.startMs = DateUtil.getCurTimeMs()
saveInfos2Sp(mItemList)
}
}
2025-12-12 07:27:30 +00:00
}
data class WithdrawItemBean(
val index: Int,
val totalCashNum: Float,
var totalProgress: Int = 0,
2025-12-15 02:47:58 +00:00
val subItemList: List<WithdrawSubItem> = emptyList(),
2025-12-15 04:37:51 +00:00
var startMs: Long = 0L,
2025-12-12 07:27:30 +00:00
)
data class WithdrawSubItem(
val index: Int,
2025-12-15 02:47:58 +00:00
val cashTotal: Float,
val startProgress: Int = 0,
var currentProgress: Int = 0,
var hasEarnMoneyByAd: Float = 0F,
2025-12-15 03:39:03 +00:00
var withdrawState: Int = STATE_NEED_WATCH_AD,
2025-12-12 07:27:30 +00:00
)