|
@@ -0,0 +1,262 @@
|
|
|
+package com.swago.room.reward
|
|
|
+
|
|
|
+import android.animation.Animator
|
|
|
+import android.animation.ObjectAnimator
|
|
|
+import android.content.Context
|
|
|
+import android.util.AttributeSet
|
|
|
+import android.view.LayoutInflater
|
|
|
+import android.view.View
|
|
|
+import android.widget.FrameLayout
|
|
|
+import android.widget.Toast
|
|
|
+import com.swago.baseswago.baseroom.IRoomActiveListener
|
|
|
+import com.swago.baseswago.baseroom.IRoomInfo
|
|
|
+import com.swago.baseswago.baseroom.RoomTimer
|
|
|
+import com.swago.baseswago.baseroom.SwagoRoomManager
|
|
|
+import com.swago.baseswago.model.RedEnvelope
|
|
|
+import com.swago.baseswago.model.live.OnlineRewardsModel
|
|
|
+import com.swago.baseswago.util.AppContext
|
|
|
+import com.swago.baseswago.util.LogUtil
|
|
|
+import com.swago.baseswago.util.NoDoubleClickListener
|
|
|
+import com.swago.baseswago.util.TimeUtil
|
|
|
+import com.swago.room.R
|
|
|
+import com.swago.room.databinding.ViewRewardsBinding
|
|
|
+
|
|
|
+
|
|
|
+class RewardView : FrameLayout, IRoomActiveListener, RoomTimer.TimeTickListener {
|
|
|
+ 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 showChangeRoom = false
|
|
|
+ private var countdownOver = false //倒计时是否结束
|
|
|
+ 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
|
|
|
+ var countdownOverFun: (() -> Unit)?= null
|
|
|
+
|
|
|
+ private
|
|
|
+ constructor(context: Context) : this(context, null)
|
|
|
+ constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
|
|
|
+ constructor(context: Context, attrs: AttributeSet?, defStyle: Int) : super(
|
|
|
+ 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() {
|
|
|
+ override fun onClick() {
|
|
|
+ if (rewardsModel == null) {
|
|
|
+ Toast.makeText(
|
|
|
+ AppContext.getContext(),
|
|
|
+ AppContext.getContext().getString(R.string.user_above_level_10),
|
|
|
+ Toast.LENGTH_SHORT
|
|
|
+ ).show()
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if (scaleXAnimator?.isRunning == true && countdownOver){
|
|
|
+ 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
|
|
|
+
|
|
|
+ // 创建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) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun leaveRoom(iRoomInfo: IRoomInfo) {
|
|
|
+ visibility = View.GONE
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun joinedRoom(iRoomInfo: IRoomInfo) {
|
|
|
+ if (showChangeRoom){
|
|
|
+ visibility = View.VISIBLE
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun endRoom(iRoomInfo: IRoomInfo?) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun closeRoomed() {
|
|
|
+ scaleXAnimator?.cancel()
|
|
|
+ scaleYAnimator?.cancel()
|
|
|
+ scaleXAnimator = null
|
|
|
+ scaleYAnimator = null
|
|
|
+ RoomTimer.timeTickList.remove(this)
|
|
|
+ SwagoRoomManager.removeListener(this)
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onTimeCount(time: Int) {
|
|
|
+ if (timeL == 0L){
|
|
|
+ if (scaleXAnimator?.isRunning == false && rewardsModel != null){
|
|
|
+ startGetAnimation()
|
|
|
+ countdownOverFun?.invoke()
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ totalTime ++
|
|
|
+ timeL--
|
|
|
+ binding?.tvTime?.text = TimeUtil.convertMinSecond(timeL)
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ fun checkOnlineTime(model: OnlineRewardsModel?) {
|
|
|
+ showChangeRoom = true
|
|
|
+ if (model == null) {
|
|
|
+ rewardsModel = null
|
|
|
+ binding?.tvTime?.setBackgroundResource(R.mipmap.bg_get_reward)
|
|
|
+ binding?.tvTime?.text = "Get!"
|
|
|
+ timeL = 0L
|
|
|
+ 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){
|
|
|
+ countdownOver = false
|
|
|
+ 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(900)
|
|
|
+ rewardsModel?.ten = true
|
|
|
+ nextReward()
|
|
|
+ }
|
|
|
+ "fifteen" -> {
|
|
|
+ totalTime = totalTime.coerceAtLeast(1800)
|
|
|
+ rewardsModel?.fifteen = true
|
|
|
+ nextReward()
|
|
|
+ }
|
|
|
+ "thirty" -> {
|
|
|
+ totalTime = totalTime.coerceAtLeast(3600)
|
|
|
+ rewardsModel?.thirty = true
|
|
|
+ visibility = View.GONE
|
|
|
+ showChangeRoom = false
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ 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}"
|
|
|
+ //领取奖励倒计时
|
|
|
+ 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}"
|
|
|
+
|
|
|
+ //领取奖励倒计时
|
|
|
+ if (totalTime >= 900){
|
|
|
+ startGetAnimation()
|
|
|
+ } else {
|
|
|
+ timeL = 900 - 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}"
|
|
|
+ //领取奖励倒计时
|
|
|
+ if (totalTime >= 1800){
|
|
|
+ startGetAnimation()
|
|
|
+ } else {
|
|
|
+ timeL = 1800 - 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}"
|
|
|
+ //领取奖励倒计时
|
|
|
+ if (totalTime >= 3600){
|
|
|
+ startGetAnimation()
|
|
|
+ } else {
|
|
|
+ timeL = 3600 - totalTime
|
|
|
+ binding?.tvTime?.text = TimeUtil.convertMinSecond(timeL)
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ visibility =View.GONE
|
|
|
+ showChangeRoom = false
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 倒计时结束成功
|
|
|
+ * */
|
|
|
+ fun countdownOverSuccess(){
|
|
|
+ countdownOver = true
|
|
|
+ }
|
|
|
+}
|