diff --git a/app/src/main/java/com/gamedog/vididin/features/benefit/BenefitActivity.kt b/app/src/main/java/com/gamedog/vididin/features/benefit/BenefitActivity.kt index 585f84a..ddc5c81 100644 --- a/app/src/main/java/com/gamedog/vididin/features/benefit/BenefitActivity.kt +++ b/app/src/main/java/com/gamedog/vididin/features/benefit/BenefitActivity.kt @@ -92,7 +92,7 @@ class BenefitActivity : AppViewsActivity(), OnT val taskStateHelper = TaskManager.instance().boxTaskStatus() var subTaskIndex = taskStateHelper.getCurrentBoxIndex() if (subTaskIndex >= 0) { - val currentBoxState = taskStateHelper.getStatusBean().tasks[subTaskIndex] + val currentBoxState = taskStateHelper.getStatusBean().boxList[subTaskIndex] currentBoxState.tasks.forEachIndexed { index, subTask -> val separateLine = View(this@BenefitActivity) separateLine.setBackgroundResource(R.color.gray_f2) @@ -136,7 +136,7 @@ class BenefitActivity : AppViewsActivity(), OnT private fun updateSubTasksUI() { val taskStateHelper = TaskManager.instance().boxTaskStatus() val currentBoxIndex = taskStateHelper.getCurrentBoxIndex() - val subTaskStateList = taskStateHelper.getStatusBean().tasks[currentBoxIndex].tasks + val subTaskStateList = taskStateHelper.getStatusBean().boxList[currentBoxIndex].tasks if (subTaskStateList.size == mSubTaskViewList.size) { mSubTaskViewList.forEachIndexed { index, view -> @@ -147,7 +147,7 @@ class BenefitActivity : AppViewsActivity(), OnT private fun updateTopBoxesUI() { val taskStateHelper = TaskManager.instance().boxTaskStatus() - val boxStateList = taskStateHelper.getStatusBean().tasks + val boxStateList = taskStateHelper.getStatusBean().boxList with (binding) { boxStateList.forEachIndexed { index, curBoxState -> @@ -221,7 +221,7 @@ class BenefitActivity : AppViewsActivity(), OnT tvHintRewardNum.text = buildString { append(ResUtil.getString(R.string.cash)) append(" ") - append(taskStateHelper.getStatusBean().tasks[taskStateHelper.getCurrentBoxIndex()].reward_value) + append(taskStateHelper.getStatusBean().boxList[taskStateHelper.getCurrentBoxIndex()].reward_value) } } } diff --git a/app/src/main/java/com/gamedog/vididin/features/benefit/widget/CounterDownTimerView.kt b/app/src/main/java/com/gamedog/vididin/features/benefit/widget/CounterDownTimerView.kt index 9b29c63..cb36196 100644 --- a/app/src/main/java/com/gamedog/vididin/features/benefit/widget/CounterDownTimerView.kt +++ b/app/src/main/java/com/gamedog/vididin/features/benefit/widget/CounterDownTimerView.kt @@ -29,8 +29,8 @@ class CounterDownTimerView @JvmOverloads constructor( } private fun startTimer() { - val startMs = TaskManager.instance().boxTaskStatus().getAllBoxStartTimeMs() - val totalMs = TaskManager.instance().boxTaskStatus().getAllBoxTotalDurationMs() + val startMs = TaskManager.instance().boxTaskStatus().getCurrentBoxStartTimeMs() + val totalMs = TaskManager.instance().boxTaskStatus().getLeftValidBoxTotalDurationMs() val restMs = startMs + totalMs - System.currentTimeMillis() diff --git a/app/src/main/java/com/gamedog/vididin/manager/helpers/BoxTaskHelper.kt b/app/src/main/java/com/gamedog/vididin/manager/helpers/BoxTaskHelper.kt index 3e263e2..5747da5 100644 --- a/app/src/main/java/com/gamedog/vididin/manager/helpers/BoxTaskHelper.kt +++ b/app/src/main/java/com/gamedog/vididin/manager/helpers/BoxTaskHelper.kt @@ -50,11 +50,12 @@ class BoxTaskHelper: BaseTaskHelper() { override fun loadTaskFromSp() { val taskStateBeanInSp = SpUtil.instance().getObject(mSpKey) - if (taskStateBeanInSp == null || isBoxRootTaskExpired(taskStateBeanInSp)) { + if (taskStateBeanInSp == null || taskStateBeanInSp.boxList.isNullOrEmpty() || isAllBoxExpiredOrFinished(taskStateBeanInSp)) { mStateBean = generateStateBeanFromConfig() saveState2Sp() } else { mStateBean = taskStateBeanInSp + calculateCurrentOngoingBox() } } @@ -68,27 +69,46 @@ class BoxTaskHelper: BaseTaskHelper() { } 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 private fun handleEvents(taskType: Int) { val currentBoxIndex = getCurrentBoxIndex() - if (!isBoxAllFinished(currentBoxIndex)) { + if (!isBoxTasksFinished(currentBoxIndex)) { getCurrentBoxState()?.tasks?.forEachIndexed { index, subTask-> if (taskType == subTask.task_type && !isBoxSubTaskFinished(currentBoxIndex, index)) { subTask.finishedNum++ saveState2Sp() - 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() { @@ -96,26 +116,22 @@ class BoxTaskHelper: BaseTaskHelper() { } - private fun isBoxRootTaskExpired(taskStateBoxRoot: TaskStateBoxRoot): Boolean { - var totalDurationDays = 0 - taskStateBoxRoot.tasks.forEach { - totalDurationDays += it.duration_days - } - return DateUtil.isPeriodExpired(taskStateBoxRoot.startMs, totalDurationDays) + private fun isAllBoxExpiredOrFinished(taskStateBoxRoot: TaskStateBoxRoot): Boolean { + return false } private fun isBoxTaskExpired(boxIndex: Int): Boolean { - if (boxIndex >= 0 && boxIndex < mStateBean.tasks.size) { - val curBoxState = mStateBean.tasks[boxIndex] - return DateUtil.isPeriodExpired(mStateBean.startMs, curBoxState.duration_days) + if (boxIndex >= 0 && boxIndex < mStateBean.boxList.size) { + val curBoxState = mStateBean.boxList[boxIndex] + return DateUtil.isPeriodExpired(curBoxState.boxStartMs, curBoxState.duration_days) } return false } private fun isBoxAllTasksFinish(boxIndex: Int) : Boolean { - if (boxIndex >= 0 && boxIndex < mStateBean.tasks.size) { - val curBoxState = mStateBean.tasks[boxIndex] + if (boxIndex >= 0 && boxIndex < mStateBean.boxList.size) { + val curBoxState = mStateBean.boxList[boxIndex] curBoxState.tasks.forEach { if (it.required_count > it.finishedNum) { @@ -129,50 +145,68 @@ class BoxTaskHelper: BaseTaskHelper() { } fun getCurrentBoxIndex(): Int { - val boxRootStartMs = mStateBean.startMs - - mStateBean.tasks.forEachIndexed { index, box -> - if (!DateUtil.isPeriodExpired(boxRootStartMs, box.duration_days)) { - return index - } - } - return 0 + return mStateBean.currentBoxIndex } fun getCurrentBoxState(): TaskStateBox? { val currentBoxIndex = getCurrentBoxIndex() - if (currentBoxIndex >= 0 && currentBoxIndex < mStateBean.tasks.size) { - return mStateBean.tasks[currentBoxIndex] + if (currentBoxIndex >= 0 && currentBoxIndex < mStateBean.boxList.size) { + return mStateBean.boxList[currentBoxIndex] } return null } - fun getAllBoxTotalDurationMs(): Long { - var totalDays = 0L - mStateBean.tasks.forEach { - totalDays += it.duration_days + fun getBoxState(boxIndex: Int): TaskStateBox? { + if (boxIndex >= 0 && boxIndex < mStateBean.boxList.size) { + return mStateBean.boxList[boxIndex] } - return totalDays * 24 * 3600000 + return null } - fun getAllBoxStartTimeMs(): Long { - return mStateBean.startMs + fun getLeftValidBoxTotalDurationMs(): Long { + 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) 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 { - val subTaskState = mStateBean.tasks[boxIndex].tasks[subTaskIndex] + val subTaskState = mStateBean.boxList[boxIndex].tasks[subTaskIndex] return subTaskState.finishedNum >= subTaskState.required_count } fun getBoxStateEnum(boxIndex: Int): Int { - if (boxIndex >= 0 && boxIndex < mStateBean.tasks.size) { - val curBoxState = mStateBean.tasks[boxIndex] + if (boxIndex >= 0 && boxIndex < mStateBean.boxList.size) { + val curBoxState = mStateBean.boxList[boxIndex] if (curBoxState.hasClaimedReward) { return STATE_CLAIMED @@ -193,7 +227,7 @@ class BoxTaskHelper: BaseTaskHelper() { fun getCouldClaimCashNum(): Float { var waitClaimCashNum = 0F - mStateBean.tasks.forEachIndexed { index, box -> + mStateBean.boxList.forEachIndexed { index, box -> if (getBoxStateEnum(index) == STATE_FINISH) { waitClaimCashNum += box.reward_value } @@ -257,7 +291,7 @@ class BoxTaskHelper: BaseTaskHelper() { fun getCurrentBoxTotalProgress(): Int { val currentBoxIndex = getCurrentBoxIndex() - val boxList = mStateBean.tasks + val boxList = mStateBean.boxList if (currentBoxIndex >= 0 && currentBoxIndex < boxList.size) { var totalFinished = 0 var totalRequired = 0 @@ -281,7 +315,7 @@ class BoxTaskHelper: BaseTaskHelper() { val couldClaimCashNum = getCouldClaimCashNum() if (couldClaimCashNum > 0F) { AccountManager.adjustAccountCash(couldClaimCashNum) - mStateBean.tasks.forEachIndexed { index, box -> + mStateBean.boxList.forEachIndexed { index, box -> if (getBoxStateEnum(index) == STATE_FINISH) { box.hasClaimedReward = true } diff --git a/app/src/main/java/com/gamedog/vididin/manager/taskbeans/TaskStateBoxRoot.kt b/app/src/main/java/com/gamedog/vididin/manager/taskbeans/TaskStateBoxRoot.kt index bc8f5d4..6b438f2 100644 --- a/app/src/main/java/com/gamedog/vididin/manager/taskbeans/TaskStateBoxRoot.kt +++ b/app/src/main/java/com/gamedog/vididin/manager/taskbeans/TaskStateBoxRoot.kt @@ -6,8 +6,8 @@ import java.io.Serializable data class TaskStateBoxRoot( - val startMs: Long = 0, - val tasks: List + val boxList: List, + var currentBoxIndex: Int = 0, ): Serializable data class TaskStateBox( @@ -19,6 +19,7 @@ data class TaskStateBox( val is_one_time: Boolean, val status: String, val tasks: List, + var boxStartMs: Long = 0, // new added var hasClaimedReward: Boolean = false,