Bladeren bron

feat: 领取奖励

tongmengxiao 2 maanden geleden
bovenliggende
commit
cd7b5de997

+ 5 - 5
baseswago/src/main/java/com/swago/baseswago/model/live/OnlineRewardsModel.kt

@@ -1,11 +1,11 @@
 package com.swago.baseswago.model.live
 
 data class OnlineRewardsModel (
-    val five:Boolean,
-    val ten:Boolean,
-    val fifteen:Boolean,
-    val thirty:Boolean,
-    val onlineDuration:Int,
+    var five:Boolean,
+    var ten:Boolean,
+    var fifteen:Boolean,
+    var thirty:Boolean,
+    var onlineDuration:Int,
     val coin:CoinOnlineBean,
     )
 

+ 7 - 5
room/src/main/java/com/swago/room/base/BaseComFragment.kt

@@ -578,11 +578,12 @@ abstract class BaseComFragment<T : FragmentBaseComBinding> : BaseXFragment<T>(),
 
         //查询直播间在线时长与奖励领取情况
         roomVm.onlineRewardsData.observe(this){
-            if (it.onlineDuration > 30){
-                binding.ivRewards.visibility = View.GONE
-            } else {
-                binding.ivRewards.setOnlineCoin(it)
-            }
+            binding.ivRewards.checkOnlineTime(it)
+        }
+
+        //积分领取成功
+        roomVm.getRewardsSuccess.observe(this){
+            binding.ivRewards.setRewardsSuccess(it)
         }
 
         //IM下发新的红包
@@ -893,6 +894,7 @@ abstract class BaseComFragment<T : FragmentBaseComBinding> : BaseXFragment<T>(),
                     //查询直播间在线时长与奖励领取情况
                     roomVm.getOnlineRewards()
                 } else {
+                    binding.ivRewards.checkOnlineTime(null)
                     binding.ivRewards.visibility = View.VISIBLE
                 }
             }

+ 156 - 111
room/src/main/java/com/swago/room/reward/RewardView.kt

@@ -23,16 +23,19 @@ import com.swago.room.databinding.ViewRewardsBinding
 
 
 class RewardView : FrameLayout, IRoomActiveListener, RoomTimer.TimeTickListener {
-    private var  binding: ViewRewardsBinding? = null
-    private var timeL = 0L
-    private var duration:String = ""
-    private var nextCoins:Int = 0
-    private var coins:Int = 0
-    private var nextTime:Int = 0
-    private var rewardsModel: OnlineRewardsModel ?= null
-    private var scaleXAnimator:ObjectAnimator ?= null
-    private var scaleYAnimator:ObjectAnimator ?= null
-    var openGetCoins:((duration:String,coins:Int,nextCoins:Int,nextTime:Int) ->Unit)? = null
+    private var binding: ViewRewardsBinding? = null
+    private var timeL = 300L //倒计时
+    private var totalTime = 0L //在直播间里时间
+    private var duration: String = ""
+    private var coins: Int = 0
+    private var nextCoins: Int = 0
+    private var nextTime: Int = 0
+    private var rewardsModel: OnlineRewardsModel? = null
+    private var scaleXAnimator: ObjectAnimator? = null
+    private var scaleYAnimator: ObjectAnimator? = null
+    var openGetCoins: ((duration: String, coins: Int, nextCoins: Int, nextTime: Int) -> Unit)? =
+        null
+
     private
     constructor(context: Context) : this(context, null)
     constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
@@ -40,44 +43,48 @@ class RewardView : FrameLayout, IRoomActiveListener, RoomTimer.TimeTickListener
         context,
         attrs,
         defStyle
-    ){
+    ) {
         initView()
     }
+
     private fun initView() {
         SwagoRoomManager.addListener(this)
         RoomTimer.timeTickList.add(this)
         binding = ViewRewardsBinding.inflate(LayoutInflater.from(context), this, true)
 
-        binding?.ivRewards?.setOnClickListener(object: NoDoubleClickListener(){
+        binding?.ivRewards?.setOnClickListener(object : NoDoubleClickListener() {
             override fun onClick() {
-                if (rewardsModel == null){
-                    Toast.makeText(AppContext.getContext(), "10级以上用户可以领取", Toast.LENGTH_SHORT).show()
+                if (rewardsModel == null) {
+                    Toast.makeText(
+                        AppContext.getContext(),
+                        "10级以上用户可以领取",
+                        Toast.LENGTH_SHORT
+                    ).show()
                     return
                 }
-                timeL ++
-                scaleXAnimator?.cancel()
-                scaleYAnimator?.cancel()
-                binding?.tvTime?.setBackgroundResource(R.mipmap.bg_time_reward)
-                openGetCoins?.invoke(duration,coins,nextCoins,nextTime)
+                if (scaleXAnimator?.isRunning == true){
+                    openGetCoins?.invoke(duration, coins, nextCoins, nextTime)
+                }
             }
         })
         startAnimation()
     }
 
-     private fun startAnimation(){
-         // 创建X轴放大的动画
-          scaleXAnimator = ObjectAnimator.ofFloat(binding?.clReward, "scaleX", 1.0f, 1.05f)
-         scaleXAnimator?.setDuration(500)
-         scaleXAnimator?.repeatCount = ObjectAnimator.INFINITE
-         scaleXAnimator?.repeatMode = ObjectAnimator.REVERSE
+    private fun startAnimation() {
+        // 创建X轴放大的动画
+        scaleXAnimator = ObjectAnimator.ofFloat(binding?.clReward, "scaleX", 1.0f, 1.05f)
+        scaleXAnimator?.setDuration(500)
+        scaleXAnimator?.repeatCount = ObjectAnimator.INFINITE
+        scaleXAnimator?.repeatMode = ObjectAnimator.REVERSE
 
-         // 创建Y轴放大的动画
-         scaleYAnimator = ObjectAnimator.ofFloat(binding?.clReward, "scaleY", 1.0f, 1.05f)
-         scaleYAnimator?.setDuration(500)
-         scaleYAnimator?.repeatCount = ObjectAnimator.INFINITE
-         scaleYAnimator?.repeatMode = ObjectAnimator.REVERSE
+        // 创建Y轴放大的动画
+        scaleYAnimator = ObjectAnimator.ofFloat(binding?.clReward, "scaleY", 1.0f, 1.05f)
+        scaleYAnimator?.setDuration(500)
+        scaleYAnimator?.repeatCount = ObjectAnimator.INFINITE
+        scaleYAnimator?.repeatMode = ObjectAnimator.REVERSE
+
+    }
 
-     }
     override fun changeRoom(iRoomInfo: IRoomInfo) {
 
     }
@@ -99,94 +106,132 @@ class RewardView : FrameLayout, IRoomActiveListener, RoomTimer.TimeTickListener
     }
 
     override fun onTimeCount(time: Int) {
-        when{
-            timeL== 300L -> {
-                // 启动动画
-                duration ="five"
-                scaleXAnimator?.start()
-                scaleYAnimator?.start()
-                binding?.tvTime?.setBackgroundResource(R.mipmap.bg_get_reward)
-                binding?.tvTime?.text = "Get!"
-            }
-
-            timeL== 600L -> {
-                // 启动动画
-                duration ="ten"
-                scaleXAnimator?.start()
-                scaleYAnimator?.start()
-                binding?.tvTime?.setBackgroundResource(R.mipmap.bg_get_reward)
-                binding?.tvTime?.text = "Get!"
-            }
-
-           timeL== 900L -> {
-                // 启动动画
-                duration ="fifteen"
-                scaleXAnimator?.start()
-                scaleYAnimator?.start()
-                binding?.tvTime?.setBackgroundResource(R.mipmap.bg_get_reward)
-                binding?.tvTime?.text = "Get!"
-            }
-            timeL== 1800L -> {
-                // 启动动画
-                duration ="thirty"
-                scaleXAnimator?.start()
-                scaleYAnimator?.start()
-                binding?.tvTime?.setBackgroundResource(R.mipmap.bg_get_reward)
-                binding?.tvTime?.text = "Get!"
-            }
-
-            timeL> 1800L -> {
-                scaleXAnimator?.cancel()
-                scaleYAnimator?.cancel()
-                visibility = View.GONE
-            }
-            else -> {
-                timeL ++
-                binding?.tvTime?.text = TimeUtil.convertMinSecond(timeL)
+        if (timeL == 0L){
+            if (scaleXAnimator?.isRunning == false){
+                startGetAnimation()
             }
+        } else {
+            totalTime ++
+            timeL--
+            binding?.tvTime?.text = TimeUtil.convertMinSecond(timeL)
         }
 
     }
 
-    fun setOnlineCoin(model: OnlineRewardsModel){
-        timeL = model.onlineDuration * 60L
-        timeL ++
-        rewardsModel = model
-        if (!model.five){
-            coins = model.coin.five
-            nextCoins = model.coin.ten
-            nextTime = 10
-            binding?.tvCoins?.text = "+${model.coin.five}"
-            visibility = View.VISIBLE
-            binding?.tvTime?.text = TimeUtil.convertMinSecond(timeL)
+    fun checkOnlineTime(model: OnlineRewardsModel?) {
+        if (model == null) {
+            rewardsModel = null
             return
         }
-        if (!model.ten){
-            coins = model.coin.ten
-            nextCoins = model.coin.fifteen
-            nextTime = 15
-            binding?.tvCoins?.text = "+${model.coin.ten}"
-            visibility = View.VISIBLE
-            binding?.tvTime?.text = TimeUtil.convertMinSecond(timeL)
-            return
-        }
-        if (!model.fifteen){
-            coins = model.coin.fifteen
-            nextCoins = model.coin.thirty
-            nextTime = 30
-            binding?.tvCoins?.text = "+${model.coin.fifteen}"
-            visibility = View.VISIBLE
-            binding?.tvTime?.text = TimeUtil.convertMinSecond(timeL)
-            return
+        rewardsModel = model
+        visibility = View.VISIBLE
+        totalTime = model.onlineDuration * 60L
+        nextReward()
+    }
+
+    private fun startGetAnimation() {
+        // 启动动画
+        timeL = 0L
+        scaleXAnimator?.start()
+        scaleYAnimator?.start()
+        binding?.tvTime?.setBackgroundResource(R.mipmap.bg_get_reward)
+        binding?.tvTime?.text = "Get!"
+    }
+
+    fun setRewardsSuccess(success:Boolean){
+        if (success){
+            scaleXAnimator?.cancel()
+            scaleYAnimator?.cancel()
+            binding?.tvTime?.setBackgroundResource(R.mipmap.bg_time_reward)
+            when(duration){
+                "five" -> {
+                    totalTime = totalTime.coerceAtLeast(300)
+                    rewardsModel?.five = true
+                    nextReward()
+                }
+                "ten" -> {
+                    totalTime = totalTime.coerceAtLeast(600)
+                    rewardsModel?.ten = true
+                    nextReward()
+                }
+                "fifteen" -> {
+                    totalTime = totalTime.coerceAtLeast(900)
+                    rewardsModel?.fifteen = true
+                    nextReward()
+                }
+                "thirty" -> {
+                    totalTime = totalTime.coerceAtLeast(1800)
+                    rewardsModel?.thirty = true
+                    visibility = View.GONE
+                }
+            }
         }
-        if (!model.thirty){
-            coins = model.coin.thirty
-            nextCoins = 0
-            nextTime = 0
-            binding?.tvCoins?.text = "+${model.coin.thirty}"
-            visibility = View.VISIBLE
-            binding?.tvTime?.text = TimeUtil.convertMinSecond(timeL)
-            return
+    }
+    private fun nextReward(){
+        rewardsModel?.let { model ->
+            //待领取的奖励
+            if (!model.five){
+                visibility = View.VISIBLE
+                coins = model.coin.five
+                duration = "five"
+                nextCoins = model.coin.ten
+                nextTime = 10
+                binding?.tvCoins?.text = "+${model.coin.five}fiv"
+                //领取奖励倒计时
+                if (totalTime >= 300){
+                    startGetAnimation()
+                } else {
+                    timeL = 300 - totalTime
+                    binding?.tvTime?.text = TimeUtil.convertMinSecond(timeL)
+                }
+            } else if (!model.ten){
+                visibility = View.VISIBLE
+                coins = model.coin.ten
+                duration = "ten"
+                nextCoins = model.coin.fifteen
+                nextTime = 15
+                binding?.tvCoins?.text = "+${model.coin.ten}ten"
+
+                //领取奖励倒计时
+                if (totalTime >= 600){
+                    startGetAnimation()
+                } else {
+                    timeL = 600 - totalTime
+                    binding?.tvTime?.text = TimeUtil.convertMinSecond(timeL)
+                }
+            } else if (!model.fifteen){
+                visibility = View.VISIBLE
+                coins = model.coin.fifteen
+                duration = "fifteen"
+                nextCoins = model.coin.thirty
+                nextTime = 30
+                binding?.tvCoins?.text = "+${model.coin.fifteen}fif"
+                //领取奖励倒计时
+                if (totalTime >= 900){
+                    startGetAnimation()
+                } else {
+                    timeL = 900 - totalTime
+                    binding?.tvTime?.text = TimeUtil.convertMinSecond(timeL)
+                }
+            } else if (!model.thirty){
+                visibility = View.VISIBLE
+                coins = model.coin.thirty
+                duration = "thirty"
+                nextCoins = 0
+                nextTime = 0
+                binding?.tvCoins?.text = "+${model.coin.thirty}thi"
+                //领取奖励倒计时
+                if (totalTime >= 1800){
+                    startGetAnimation()
+                } else {
+                    timeL = 1800 - totalTime
+                    binding?.tvTime?.text = TimeUtil.convertMinSecond(timeL)
+                }
+            } else {
+                visibility =View.GONE
+            }
+
         }
+
     }
 }

+ 11 - 1
room/src/main/java/com/swago/room/vm/RoomVm.kt

@@ -534,17 +534,27 @@ class RoomVm(application: Application) : AbsRoomVm(application) {
             }
         }
     }
-
+    /**
+     *奖励领取成功
+     * */
+    val getRewardsSuccess by lazy {
+        MutableLiveData<Boolean>()
+    }
     fun receiveOnlineRewards(duration:String){
         requestData2(false) {
             requestData {
                  ApiManager.roomApi.receiveOnlineRewards(duration)
+                 getRewardsSuccess.value = true
                 Toast.makeText(
                     AppContext.getContext(),
                     "success",
                     Toast.LENGTH_SHORT
                 ).show()
             }
+
+            requestError{
+                Toast.makeText(AppContext.getContext(), it.message, Toast.LENGTH_SHORT).show()
+            }
         }
     }
 

+ 1 - 1
room/src/main/java/com/swago/room/widget/ComHeaderView.kt

@@ -88,7 +88,7 @@ class ComHeaderView  : ConstraintLayout, IRoomActiveListener, IHeader, RoomTimer
             }
         })
 
-        binding.tvIncome.setOnClickListener(object:NoDoubleClickListener(){
+        binding.clIncome.setOnClickListener(object:NoDoubleClickListener(){
             override fun onClick() {
                 ARouter.getInstance()
                     .build(ARouteConstant.Room.rankList)

+ 5 - 4
room/src/main/res/layout/dialog_get_rewards.xml

@@ -43,8 +43,9 @@
             android:id="@+id/clCoins"
             android:layout_width="match_parent"
             android:layout_height="64dp"
-            android:layout_marginStart="24dp"
-            android:layout_marginEnd="24dp"
+            android:layout_marginStart="30dp"
+            android:layout_marginEnd="30dp"
+            android:layout_marginTop="12dp"
             android:background="@drawable/shape_b131f6_12"
             app:layout_constraintTop_toBottomOf="@+id/tvGet">
             <TextView
@@ -69,8 +70,8 @@
             app:layout_constraintEnd_toEndOf="parent"
             android:layout_marginTop="10dp"
             android:textSize="12sp"
-            android:layout_marginStart="24dp"
-            android:layout_marginEnd="24dp"
+            android:layout_marginStart="30dp"
+            android:layout_marginEnd="30dp"
             android:gravity="center"
             android:textColor="@color/_ffffff"
             android:text="Continue in the room for 25 mins to earn another 100 coins."