kotlin 倒计时功能

1、倒计时工具类:

fun countDownCoroutines(
    total: Int,
    scope: CoroutineScope,
    onTick: (Int) -> Unit,
    onStart: (() -> Unit)? = null,
    onFinish: (() -> Unit)? = null,
): Job {
    return flow {
        for (i in total downTo 0) {
            emit(i)
            delay(1000)
        }
    }
        .flowOn(Dispatchers.Main)
        .onStart { onStart?.invoke() }
        .onCompletion { onFinish?.invoke() }//like java finally
        .onEach { onTick.invoke(it) }
        .launchIn(scope)
}

fun countDownDelayCoroutines(
    total: Int,
    delayTimeMillis: Long,
    scope: CoroutineScope,
    onTick: (Int) -> Unit,
    onStart: (() -> Unit)? = null,
    onFinish: (() -> Unit)? = null,
): Job {
    return flow {
        for (i in total downTo 0) {
            emit(i)
            delay(delayTimeMillis)
        }
    }
        .flowOn(Dispatchers.Main)
        .onStart { onStart?.invoke() }
        .onCompletion { onFinish?.invoke() }//like java finally
        .onEach { onTick.invoke(it) }
        .launchIn(scope)
}

/**
 * 倒计时的实现
 */
fun FragmentActivity.countDown(
    time: Int = 5,
    start: (scop: CoroutineScope) -> Unit={},
    end: () -> Unit={},
    next: (time: Int) -> Unit={},
    delayTimeMillis: Long?=1000
) {

    lifecycleScope.launch {
        // 在这个范围内启动的协程会在Lifecycle被销毁的时候自动取消

        flow {
            (time downTo 0).forEach {
                delayTimeMillis?.let { it1 -> delay(it1) }
                emit(it)
            }
        }.onStart {
            // 倒计时开始 ,在这里可以让Button 禁止点击状态
            start(this@launch)

        }.onCompletion {
            // 倒计时结束 ,在这里可以让Button 恢复点击状态
            end()
        }.catch {
            //错误
            LogUtil.e(it.message ?: "Unkown Error")
        }.collect {
            // 在这里 更新值来显示到UI
            next(it)
        }

    }
}

 2、调用,60秒倒计时

 countDownCoroutines(60, lifecycleScope, onTick = {
                getCodeTx.isEnabled = false
                mBinding.getCodeTx.apply {
                    text = getString(R.string.login_code_count_time, it.plus(0).toString())
                    text.apply {
                        text = toClickSpan(
                            it.plus(0).toString().length + 1..text.toString().length,
                            color = getColorRes(R.color.login_send_code_normal)
                        ) {}
                        movementMethod = LinkMovementMethod.getInstance()
                        highlightColor = Color.TRANSPARENT
                    }
                }
            }) {
                getCodeTx.text = getString(R.string.login_text_reset_code)
                getCodeTx.isEnabled = true
            }

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值