mistletoe5215 / CoroutineExtension

some custom api and fix bug usage for coroutine 1.3.2
7 stars 2 forks source link

未开始调用FlowCountDownTimer.start()之前切换Fragment导致调用start()时直接走already completed的判断 #2

Open PostLiu opened 3 years ago

PostLiu commented 3 years ago

出现的情况: 采用ViewPager2加载2个不同的Fragment,其中一个Fragment中的点击调用倒计时工具,在未开始调用start前来回切换Fragment,调用倒计时导致直接走'already completed'的判断,其中倒计时的pause和resume方法分别放在Fragment的onPause和onResume方法中

    private fun sendCode(phone: String) {
        viewModel.sendCode(phone).catch {
            countDownTimer.cancel()
            binding.sendCode.text = "发送失败,重新发送"
            binding.sendCode.isEnabled = true
            toast(it.message.toString())
        }.asLiveDataUI(this, {
            toast("发送成功,请注意查看短信验证码")
            countDownTimer.start()
            countDownTimer.countDown {
                binding.sendCode.text = String.format(getString(R.string.next_send_code), it)
                binding.sendCode.isEnabled = false
            }.complete {
                binding.sendCode.text = "重新发送"
                binding.sendCode.isEnabled = true
                countDownTimer.cancel()
            }
        })
    }

    override fun onPause() {
        super.onPause()
        countDownTimer.pause()
    }

    override fun onResume() {
        super.onResume()
        countDownTimer.resume()
    }

    override fun onDestroy() {
        super.onDestroy()
        countDownTimer.cancel()
    }
PostLiu commented 3 years ago

我真边给出的解决方法就是将 FlowCountDownTimer 中的判断

if (mJobState == JobState.COMPLETE) {
            Log.e(TAG, "already completed")
            return this
        }

给注释掉