diff --git a/app/src/main/java/com/gamedog/vididin/VididinEvents.kt b/app/src/main/java/com/gamedog/vididin/VididinEvents.kt index 1b4e464..97c03ab 100644 --- a/app/src/main/java/com/gamedog/vididin/VididinEvents.kt +++ b/app/src/main/java/com/gamedog/vididin/VididinEvents.kt @@ -42,4 +42,11 @@ object VididinEvents { const val EVENT_JUMP_2_VIDEO = 901 const val EVENT_JUMP_2_SIGN= 902 + + + // Withdraw related + const val EVENT_WITHDRAW_SUB_ITEM_PROGRESS_UPDATED = 500 + const val EVENT_WITHDRAW_SELECTED_SUB_ITEM_CHANGED = 501 + + } \ No newline at end of file diff --git a/app/src/main/java/com/gamedog/vididin/features/withdraw/WithDrawSubActivity.kt b/app/src/main/java/com/gamedog/vididin/features/withdraw/WithDrawSubActivity.kt index bb9de43..0e1fe60 100644 --- a/app/src/main/java/com/gamedog/vididin/features/withdraw/WithDrawSubActivity.kt +++ b/app/src/main/java/com/gamedog/vididin/features/withdraw/WithDrawSubActivity.kt @@ -7,9 +7,10 @@ import androidx.recyclerview.widget.LinearLayoutManager import com.ama.core.architecture.appBase.AppViewsEmptyViewModelActivity import com.ama.core.architecture.util.AndroidUtil import com.ama.core.architecture.util.CommonItemDecoration -import com.ama.core.architecture.util.eventbus.NotifyMan import com.ama.core.architecture.util.setOnClickBatch import com.gamedog.vididin.VidiConst +import com.gamedog.vididin.VididinEvents +import com.gamedog.vididin.beans.WatchAdNotifyBean import com.gamedog.vididin.manager.WithdrawItemBean import com.gamedog.vididin.manager.WithdrawManager import com.gamedog.vididin.router.Router @@ -21,7 +22,6 @@ import com.vididin.real.money.game.databinding.ActivityWithdrawSubBinding as Vie @AndroidEntryPoint class WithDrawSubActivity : AppViewsEmptyViewModelActivity() { - private var mSelectedSubIndex: Int = 0 private lateinit var mCurItem: WithdrawItemBean private var mType: Int = 0 private val mAdapter: WithdrawSubAdapter by lazy { WithdrawSubAdapter() } @@ -51,30 +51,53 @@ class WithDrawSubActivity : AppViewsEmptyViewModelActivity() { } private fun gotoWatchAd() { - val extraData = NotifyMan.NotifyData(mCurItem) - Router.WatchAd.startActivity(this, VidiConst.WATCH_AD_FOR_WITHDRAW_BIG, AndroidUtil.object2Json(extraData)) + Router.WatchAd.startActivity(this, VidiConst.WATCH_AD_FOR_WITHDRAW_BIG, AndroidUtil.object2Json(mCurItem)) } override fun ViewBinding.initListeners() { - mCurItem = WithdrawManager.instance().getItem(mType) - mAdapter.submitList(mCurItem.subItemList) + registerEvents({ data-> + when (data?.mEventType) { + VididinEvents.EVENT_AD_WATCHED_FOR_WITHDRAW_BIG -> { + handleAdWatched(data.mData as WatchAdNotifyBean) + } - mCurItem.subItemList.forEach { - if (it.isSelected) { - mSelectedSubIndex = it.index - return@forEach + VididinEvents.EVENT_WITHDRAW_SUB_ITEM_PROGRESS_UPDATED -> { + if ((data.mData as Int) == mCurItem.selectedSubIndex) { + progressBar.setProgress(mCurItem.subItemList[mCurItem.selectedSubIndex].currentProgress) + } + } + + VididinEvents.EVENT_WITHDRAW_SELECTED_SUB_ITEM_CHANGED -> { + updateUI() + } } - } - updateUI() + }, VididinEvents.EVENT_AD_WATCHED_FOR_WITHDRAW_BIG, + VididinEvents.EVENT_WITHDRAW_SUB_ITEM_PROGRESS_UPDATED, + VididinEvents.EVENT_WITHDRAW_SELECTED_SUB_ITEM_CHANGED) - recyclerView.scrollToPosition(mSelectedSubIndex) + + mCurItem = WithdrawManager.instance().getItem(mType) + updateUI() + } + + private fun handleAdWatched(adNotifyBean: WatchAdNotifyBean) { + val withdrawItemBean = adNotifyBean.extraData + if (WithdrawManager.instance().addAdEarnForSubBean(withdrawItemBean.index, + withdrawItemBean.selectedSubIndex, adNotifyBean.earnMoneyNum)) { + updateUI() + } } private fun updateUI() { with(binding) { + mAdapter.mSelectedSubIndex = mCurItem.selectedSubIndex + mAdapter.submitList(mCurItem.subItemList) + recyclerView.scrollToPosition(mCurItem.selectedSubIndex) + + tvCashTotal.text = mCurItem.totalCashNum.toString() progressBar.enableTouch(false) progressBar.setBarColor(forColor = R.color.green_ce) - progressBar.setProgress(mCurItem.subItemList[mSelectedSubIndex].currentProgress) + progressBar.setProgress(mCurItem.subItemList[mCurItem.selectedSubIndex].currentProgress) } } diff --git a/app/src/main/java/com/gamedog/vididin/features/withdraw/WithdrawSubAdapter.kt b/app/src/main/java/com/gamedog/vididin/features/withdraw/WithdrawSubAdapter.kt index 8f7f8dd..28072ae 100644 --- a/app/src/main/java/com/gamedog/vididin/features/withdraw/WithdrawSubAdapter.kt +++ b/app/src/main/java/com/gamedog/vididin/features/withdraw/WithdrawSubAdapter.kt @@ -14,9 +14,11 @@ import com.vididin.real.money.game.databinding.LayoutItemWithdrawSubBinding as V class WithdrawSubAdapter() : ListAdapter(DiffCallback()) { + var mSelectedSubIndex: Int = 0 + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val binding = ViewBinding.inflate(LayoutInflater.from(parent.context), parent, false) - return ViewHolder(binding) + return ViewHolder(binding,) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { @@ -29,7 +31,7 @@ class WithdrawSubAdapter() : ListAdapter(SpUtil.KEY_WITHDRAW_ITEM_LIST).toMutableList() if (itemList.isEmpty()) { itemList.addAll(generateItemList()) - save2Infos(itemList) + saveInfos2Sp(itemList) } itemList } @@ -46,12 +48,13 @@ class WithdrawManager private constructor() { val subItemCount: Int = (totalCashInItem/EACH_SUB_ITEM_CASH_NUM).toInt() for (i in 0..subItemCount-1) { - subItemList.add(WithdrawSubItem(i, EACH_SUB_ITEM_CASH_NUM, i==0, AndroidUtil.randomInt(50, 70))) + val initProgress = AndroidUtil.randomInt(50, 70) + subItemList.add(WithdrawSubItem(i, EACH_SUB_ITEM_CASH_NUM, initProgress, initProgress, 0F)) } return subItemList } - private fun save2Infos(itemList: MutableList) { + private fun saveInfos2Sp(itemList: MutableList) { SpUtil.instance().putList(SpUtil.KEY_WITHDRAW_ITEM_LIST, itemList) } @@ -63,8 +66,41 @@ class WithdrawManager private constructor() { return mItemList[itemIndex] } - fun updateProgress(itemIndex: Int, updatedItem: WithdrawItemBean) { - // TODO - + + 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 + } else { + val newProgress = subBean.startProgress + (needEarnProgress * (subBean.hasEarnMoneyByAd / subBean.cashTotal)).toInt() + subBean.currentProgress = if (newProgress >= 100) 99 else newProgress + } } } @@ -74,12 +110,14 @@ data class WithdrawItemBean( val index: Int, val totalCashNum: Float, var totalProgress: Int = 0, - val subItemList: List = emptyList() + val subItemList: List = emptyList(), + var selectedSubIndex: Int = 0, ) data class WithdrawSubItem( val index: Int, - val cashNum: Float, - var isSelected: Boolean = false, - val currentProgress: Int = 0, + val cashTotal: Float, + val startProgress: Int = 0, + var currentProgress: Int = 0, + var hasEarnMoneyByAd: Float = 0F, ) \ No newline at end of file