bug修复 - 根据产品修改 宝箱任务 重要逻辑

This commit is contained in:
renhaoting 2025-12-23 15:05:43 +08:00
parent 2c807ca4ee
commit 7b73b833c6
4 changed files with 83 additions and 48 deletions

View File

@ -92,7 +92,7 @@ class BenefitActivity : AppViewsActivity<ViewBinding, UiState, ViewModel>(), OnT
val taskStateHelper = TaskManager.instance().boxTaskStatus() val taskStateHelper = TaskManager.instance().boxTaskStatus()
var subTaskIndex = taskStateHelper.getCurrentBoxIndex() var subTaskIndex = taskStateHelper.getCurrentBoxIndex()
if (subTaskIndex >= 0) { if (subTaskIndex >= 0) {
val currentBoxState = taskStateHelper.getStatusBean().tasks[subTaskIndex] val currentBoxState = taskStateHelper.getStatusBean().boxList[subTaskIndex]
currentBoxState.tasks.forEachIndexed { index, subTask -> currentBoxState.tasks.forEachIndexed { index, subTask ->
val separateLine = View(this@BenefitActivity) val separateLine = View(this@BenefitActivity)
separateLine.setBackgroundResource(R.color.gray_f2) separateLine.setBackgroundResource(R.color.gray_f2)
@ -136,7 +136,7 @@ class BenefitActivity : AppViewsActivity<ViewBinding, UiState, ViewModel>(), OnT
private fun updateSubTasksUI() { private fun updateSubTasksUI() {
val taskStateHelper = TaskManager.instance().boxTaskStatus() val taskStateHelper = TaskManager.instance().boxTaskStatus()
val currentBoxIndex = taskStateHelper.getCurrentBoxIndex() val currentBoxIndex = taskStateHelper.getCurrentBoxIndex()
val subTaskStateList = taskStateHelper.getStatusBean().tasks[currentBoxIndex].tasks val subTaskStateList = taskStateHelper.getStatusBean().boxList[currentBoxIndex].tasks
if (subTaskStateList.size == mSubTaskViewList.size) { if (subTaskStateList.size == mSubTaskViewList.size) {
mSubTaskViewList.forEachIndexed { index, view -> mSubTaskViewList.forEachIndexed { index, view ->
@ -147,7 +147,7 @@ class BenefitActivity : AppViewsActivity<ViewBinding, UiState, ViewModel>(), OnT
private fun updateTopBoxesUI() { private fun updateTopBoxesUI() {
val taskStateHelper = TaskManager.instance().boxTaskStatus() val taskStateHelper = TaskManager.instance().boxTaskStatus()
val boxStateList = taskStateHelper.getStatusBean().tasks val boxStateList = taskStateHelper.getStatusBean().boxList
with (binding) { with (binding) {
boxStateList.forEachIndexed { index, curBoxState -> boxStateList.forEachIndexed { index, curBoxState ->
@ -221,7 +221,7 @@ class BenefitActivity : AppViewsActivity<ViewBinding, UiState, ViewModel>(), OnT
tvHintRewardNum.text = buildString { tvHintRewardNum.text = buildString {
append(ResUtil.getString(R.string.cash)) append(ResUtil.getString(R.string.cash))
append(" ") append(" ")
append(taskStateHelper.getStatusBean().tasks[taskStateHelper.getCurrentBoxIndex()].reward_value) append(taskStateHelper.getStatusBean().boxList[taskStateHelper.getCurrentBoxIndex()].reward_value)
} }
} }
} }

View File

@ -29,8 +29,8 @@ class CounterDownTimerView @JvmOverloads constructor(
} }
private fun startTimer() { private fun startTimer() {
val startMs = TaskManager.instance().boxTaskStatus().getAllBoxStartTimeMs() val startMs = TaskManager.instance().boxTaskStatus().getCurrentBoxStartTimeMs()
val totalMs = TaskManager.instance().boxTaskStatus().getAllBoxTotalDurationMs() val totalMs = TaskManager.instance().boxTaskStatus().getLeftValidBoxTotalDurationMs()
val restMs = startMs + totalMs - System.currentTimeMillis() val restMs = startMs + totalMs - System.currentTimeMillis()

View File

@ -50,11 +50,12 @@ class BoxTaskHelper: BaseTaskHelper<TaskStateBoxRoot, BoxTaskRoot>() {
override fun loadTaskFromSp() { override fun loadTaskFromSp() {
val taskStateBeanInSp = SpUtil.instance().getObject<TaskStateBoxRoot>(mSpKey) val taskStateBeanInSp = SpUtil.instance().getObject<TaskStateBoxRoot>(mSpKey)
if (taskStateBeanInSp == null || isBoxRootTaskExpired(taskStateBeanInSp)) { if (taskStateBeanInSp == null || taskStateBeanInSp.boxList.isNullOrEmpty() || isAllBoxExpiredOrFinished(taskStateBeanInSp)) {
mStateBean = generateStateBeanFromConfig() mStateBean = generateStateBeanFromConfig()
saveState2Sp() saveState2Sp()
} else { } else {
mStateBean = taskStateBeanInSp mStateBean = taskStateBeanInSp
calculateCurrentOngoingBox()
} }
} }
@ -68,27 +69,46 @@ class BoxTaskHelper: BaseTaskHelper<TaskStateBoxRoot, BoxTaskRoot>() {
} }
boxStateList.add(TaskStateBox(task.chest_id, task.chest_name, task.duration_days, boxStateList.add(TaskStateBox(task.chest_id, task.chest_name, task.duration_days,
task.reward_type, task.reward_value, task.is_one_time, task.status, boxSubTaskStateList)) task.reward_type, task.reward_value, task.is_one_time, task.status,
boxSubTaskStateList, DateUtil.getTodayStartTimeMs()))
} }
return TaskStateBoxRoot(DateUtil.getTodayStartTimeMs(), boxStateList) return TaskStateBoxRoot(boxStateList)
} }
@Synchronized @Synchronized
private fun handleEvents(taskType: Int) { private fun handleEvents(taskType: Int) {
val currentBoxIndex = getCurrentBoxIndex() val currentBoxIndex = getCurrentBoxIndex()
if (!isBoxAllFinished(currentBoxIndex)) { if (!isBoxTasksFinished(currentBoxIndex)) {
getCurrentBoxState()?.tasks?.forEachIndexed { index, subTask-> getCurrentBoxState()?.tasks?.forEachIndexed { index, subTask->
if (taskType == subTask.task_type if (taskType == subTask.task_type
&& !isBoxSubTaskFinished(currentBoxIndex, index)) { && !isBoxSubTaskFinished(currentBoxIndex, index)) {
subTask.finishedNum++ subTask.finishedNum++
saveState2Sp() saveState2Sp()
notifyEvent() notifyEvent()
} }
} }
} }
calculateCurrentOngoingBox()
}
private fun calculateCurrentOngoingBox() {
mStateBean.boxList.forEachIndexed { index, box ->
val curBoxStartMs = box.boxStartMs
if (!DateUtil.isPeriodExpired(curBoxStartMs, box.duration_days)) {
val stateEnum = getBoxStateEnum(index)
if (stateEnum == STATE_ONGOING) {
if (mStateBean.currentBoxIndex != index) {
mStateBean.currentBoxIndex = index
mStateBean.boxList[index].boxStartMs = DateUtil.getTodayStartTimeMs()
saveState2Sp()
notifyEvent()
}
return
}
}
}
} }
private fun notifyEvent() { private fun notifyEvent() {
@ -96,26 +116,22 @@ class BoxTaskHelper: BaseTaskHelper<TaskStateBoxRoot, BoxTaskRoot>() {
} }
private fun isBoxRootTaskExpired(taskStateBoxRoot: TaskStateBoxRoot): Boolean { private fun isAllBoxExpiredOrFinished(taskStateBoxRoot: TaskStateBoxRoot): Boolean {
var totalDurationDays = 0 return false
taskStateBoxRoot.tasks.forEach {
totalDurationDays += it.duration_days
}
return DateUtil.isPeriodExpired(taskStateBoxRoot.startMs, totalDurationDays)
} }
private fun isBoxTaskExpired(boxIndex: Int): Boolean { private fun isBoxTaskExpired(boxIndex: Int): Boolean {
if (boxIndex >= 0 && boxIndex < mStateBean.tasks.size) { if (boxIndex >= 0 && boxIndex < mStateBean.boxList.size) {
val curBoxState = mStateBean.tasks[boxIndex] val curBoxState = mStateBean.boxList[boxIndex]
return DateUtil.isPeriodExpired(mStateBean.startMs, curBoxState.duration_days) return DateUtil.isPeriodExpired(curBoxState.boxStartMs, curBoxState.duration_days)
} }
return false return false
} }
private fun isBoxAllTasksFinish(boxIndex: Int) : Boolean { private fun isBoxAllTasksFinish(boxIndex: Int) : Boolean {
if (boxIndex >= 0 && boxIndex < mStateBean.tasks.size) { if (boxIndex >= 0 && boxIndex < mStateBean.boxList.size) {
val curBoxState = mStateBean.tasks[boxIndex] val curBoxState = mStateBean.boxList[boxIndex]
curBoxState.tasks.forEach { curBoxState.tasks.forEach {
if (it.required_count > it.finishedNum) { if (it.required_count > it.finishedNum) {
@ -129,50 +145,68 @@ class BoxTaskHelper: BaseTaskHelper<TaskStateBoxRoot, BoxTaskRoot>() {
} }
fun getCurrentBoxIndex(): Int { fun getCurrentBoxIndex(): Int {
val boxRootStartMs = mStateBean.startMs return mStateBean.currentBoxIndex
mStateBean.tasks.forEachIndexed { index, box ->
if (!DateUtil.isPeriodExpired(boxRootStartMs, box.duration_days)) {
return index
}
}
return 0
} }
fun getCurrentBoxState(): TaskStateBox? { fun getCurrentBoxState(): TaskStateBox? {
val currentBoxIndex = getCurrentBoxIndex() val currentBoxIndex = getCurrentBoxIndex()
if (currentBoxIndex >= 0 && currentBoxIndex < mStateBean.tasks.size) { if (currentBoxIndex >= 0 && currentBoxIndex < mStateBean.boxList.size) {
return mStateBean.tasks[currentBoxIndex] return mStateBean.boxList[currentBoxIndex]
} }
return null return null
} }
fun getAllBoxTotalDurationMs(): Long { fun getBoxState(boxIndex: Int): TaskStateBox? {
var totalDays = 0L if (boxIndex >= 0 && boxIndex < mStateBean.boxList.size) {
mStateBean.tasks.forEach { return mStateBean.boxList[boxIndex]
totalDays += it.duration_days
} }
return totalDays * 24 * 3600000 return null
} }
fun getAllBoxStartTimeMs(): Long { fun getLeftValidBoxTotalDurationMs(): Long {
return mStateBean.startMs var totalLeftMs = 0L
val currentBoxIndex = mStateBean.currentBoxIndex
mStateBean.boxList.forEachIndexed { index, box ->
if (currentBoxIndex == index) {
totalLeftMs += (box.boxStartMs + box.duration_days * 24 * 3600000 - DateUtil.getCurTimeMs())
}
if (index > currentBoxIndex) {
totalLeftMs += (box.duration_days * 24 * 3600000)
}
}
return totalLeftMs
} }
fun isBoxAllFinished(boxIndex: Int) : Boolean { fun getCurrentBoxStartTimeMs(): Long {
return mStateBean.boxList[mStateBean.currentBoxIndex].boxStartMs
}
fun isBoxTasksFinished(boxIndex: Int) : Boolean {
val stateEnum = getBoxStateEnum(boxIndex) val stateEnum = getBoxStateEnum(boxIndex)
return stateEnum == STATE_FINISH || stateEnum == STATE_CLAIMED return stateEnum == STATE_FINISH || stateEnum == STATE_CLAIMED
} }
fun isAllBoxFinished(): Boolean {
var isAllFinished = true
for (index in 0..mStateBean.boxList.size - 1) {
if (isBoxTasksFinished(index)) {
isAllFinished = false
break
}
}
return isAllFinished
}
fun isBoxSubTaskFinished(boxIndex: Int, subTaskIndex: Int) : Boolean { fun isBoxSubTaskFinished(boxIndex: Int, subTaskIndex: Int) : Boolean {
val subTaskState = mStateBean.tasks[boxIndex].tasks[subTaskIndex] val subTaskState = mStateBean.boxList[boxIndex].tasks[subTaskIndex]
return subTaskState.finishedNum >= subTaskState.required_count return subTaskState.finishedNum >= subTaskState.required_count
} }
fun getBoxStateEnum(boxIndex: Int): Int { fun getBoxStateEnum(boxIndex: Int): Int {
if (boxIndex >= 0 && boxIndex < mStateBean.tasks.size) { if (boxIndex >= 0 && boxIndex < mStateBean.boxList.size) {
val curBoxState = mStateBean.tasks[boxIndex] val curBoxState = mStateBean.boxList[boxIndex]
if (curBoxState.hasClaimedReward) { if (curBoxState.hasClaimedReward) {
return STATE_CLAIMED return STATE_CLAIMED
@ -193,7 +227,7 @@ class BoxTaskHelper: BaseTaskHelper<TaskStateBoxRoot, BoxTaskRoot>() {
fun getCouldClaimCashNum(): Float { fun getCouldClaimCashNum(): Float {
var waitClaimCashNum = 0F var waitClaimCashNum = 0F
mStateBean.tasks.forEachIndexed { index, box -> mStateBean.boxList.forEachIndexed { index, box ->
if (getBoxStateEnum(index) == STATE_FINISH) { if (getBoxStateEnum(index) == STATE_FINISH) {
waitClaimCashNum += box.reward_value waitClaimCashNum += box.reward_value
} }
@ -257,7 +291,7 @@ class BoxTaskHelper: BaseTaskHelper<TaskStateBoxRoot, BoxTaskRoot>() {
fun getCurrentBoxTotalProgress(): Int { fun getCurrentBoxTotalProgress(): Int {
val currentBoxIndex = getCurrentBoxIndex() val currentBoxIndex = getCurrentBoxIndex()
val boxList = mStateBean.tasks val boxList = mStateBean.boxList
if (currentBoxIndex >= 0 && currentBoxIndex < boxList.size) { if (currentBoxIndex >= 0 && currentBoxIndex < boxList.size) {
var totalFinished = 0 var totalFinished = 0
var totalRequired = 0 var totalRequired = 0
@ -281,7 +315,7 @@ class BoxTaskHelper: BaseTaskHelper<TaskStateBoxRoot, BoxTaskRoot>() {
val couldClaimCashNum = getCouldClaimCashNum() val couldClaimCashNum = getCouldClaimCashNum()
if (couldClaimCashNum > 0F) { if (couldClaimCashNum > 0F) {
AccountManager.adjustAccountCash(couldClaimCashNum) AccountManager.adjustAccountCash(couldClaimCashNum)
mStateBean.tasks.forEachIndexed { index, box -> mStateBean.boxList.forEachIndexed { index, box ->
if (getBoxStateEnum(index) == STATE_FINISH) { if (getBoxStateEnum(index) == STATE_FINISH) {
box.hasClaimedReward = true box.hasClaimedReward = true
} }

View File

@ -6,8 +6,8 @@ import java.io.Serializable
data class TaskStateBoxRoot( data class TaskStateBoxRoot(
val startMs: Long = 0, val boxList: List<TaskStateBox>,
val tasks: List<TaskStateBox> var currentBoxIndex: Int = 0,
): Serializable ): Serializable
data class TaskStateBox( data class TaskStateBox(
@ -19,6 +19,7 @@ data class TaskStateBox(
val is_one_time: Boolean, val is_one_time: Boolean,
val status: String, val status: String,
val tasks: List<TaskStateBoxSub>, val tasks: List<TaskStateBoxSub>,
var boxStartMs: Long = 0,
// new added // new added
var hasClaimedReward: Boolean = false, var hasClaimedReward: Boolean = false,