kotlin 如何在Jetpack Compose中重构每一分钟?

5n0oy7gb  于 2023-04-07  发布在  Kotlin
关注(0)|答案(5)|浏览(174)

我想显示一个计时器倒计时在我的composable,但我不知道如何实现这一点。
我想设置一分钟的延迟/超时,然后以这种方式触发重组,但我不确定这是否是正确的思考方式。

@Composable
fun Countdown(completedAt: Date) {
    val minutesLeft = ceil((completedAt.time - Date().time) / 60_000.0).toInt()

    Handler(Looper.getMainLooper()).postDelayed({
        // TODO: Recompose
    }, 60_000)

    Text(text = "$minutesLeft minutes until completed")
}

我的目标是让文本每分钟更新一次新的时间。我如何才能做到这一点?

7kjnsjlb

7kjnsjlb1#

将分钟数存储为状态。
另外,请确保清理DisposableEffect中的postDelayed回调,以防止冲突延迟和内存泄漏。
我已经将这个逻辑移到了一个minutesLeft可组合函数中,这样它就可以被重用。

@Composable
fun minutesLeft(until: Date): Int {
    var value by remember { mutableStateOf(getMinutesLeft(until)) }

    DisposableEffect(Unit) {
        val handler = Handler(Looper.getMainLooper())

        val runnable = {
            value = getMinutesLeft(until)
        }

        handler.postDelayed(runnable, 60_000)

        onDispose {
            handler.removeCallbacks(runnable)
        }
    }

    return value
}

private fun getMinutesLeft(until: Date): Int {
    return ceil((until.time - Date().time) / 60_000.0).toInt()
}

用法

@Composable
fun Countdown(completedAt: Date) {
    val minutes = minutesLeft(until = completedAt)

    Text(text = "$minutes minutes until completed")
}
ttcibm8c

ttcibm8c2#

您可以将ViewModelCountDownTimer类一起使用。
类似于:

val countTimeViewModel : CountTimeViewModel = viewModel()
 val minutes  = countTimeViewModel.minutes.observeAsState(60)

  Button( onClick={ 
       countTimeViewModel.onStartClicked(60000*60) } 
  ){
        Text("Start")
  }

  Text(""+minutes.value)

其中:

class CountTimeViewModel : ViewModel() {
    private var timer: CountDownTimer? = null

    private val _minutes = MutableLiveData(totalTime)
    val minutes: LiveData<Int> get() =  _minutes
   
    private var totalTime : Long = 0L

    fun startCountDown() {

        timer = object : CountDownTimer(totalTime, 60000) {
            override fun onTick(millisecs: Long) {

                // Minutes
                val minutes = (millisecs / MSECS_IN_SEC / SECS_IN_MINUTES % SECS_IN_MINUTES).toInt()
                _minutes.postValue(minutes)
            }

            override fun onFinish() {
               //...countdown completed

            }
        }

    }

    fun onStartClicked(totalTime : Long) {
        this.totalTime = totalTime
        startCountDown()
        timer?.start()
    }

    override fun onCleared() {
        super.onCleared()
        timer?.cancel()
    }

    companion object {
        const val SECS_IN_MINUTES = 60
        const val MSECS_IN_SEC = 1000
    }
}
kgsdhlau

kgsdhlau3#

@Composable
fun minutesLeft(until: Date): Int {
    var timeout by remember { mutableStateOf(getMinutesLeft(until)) }

    Text(text = "$timeout minutes until completed")

    LaunchedEffect(timeout) {
        if (timeout > 0) {
            delay(1000 * 60)
            timeout -= 1
        }
    }
}

private fun getMinutesLeft(until: Date): Int {
    return ceil((until.time - Date().time) / 60_000.0).toInt()
}
i2byvkas

i2byvkas4#

这些代码可能会给予你一个想法,以实现这一目标:

val time = (timerDate.time).minus(Calendar.getInstance().timeInMillis)
var timer by remember { mutableStateOf(time) }
  LaunchedEffect(key1 = timer) {
      if (timer > 0) {
          delay(1000L)
          timer -= 1000L
       }
     }
  val secMilSec: Long = 1000
  val minMilSec = 60 * secMilSec
  val hourMilSec = 60 * minMilSec
  val dayMilSec = 24 * hourMilSec
  val hours = (time % dayMilSec / hourMilSec).toInt()
  val minutes = (time % dayMilSec % hourMilSec / minMilSec).toInt()
  val seconds = (time % dayMilSec % hourMilSec % minMilSec / secMilSec).toInt()

Text(text = String.format(" %02d:%02d:%02d", hours, minutes, seconds))
xkftehaa

xkftehaa5#

如果不是每一毫秒都比我的小TickTack重要,那么一个组合物就可以完成这项工作:

var tickTack: Boolean by rememberSaveable { mutableStateOf(false) }
if (tickTack) {
    LaunchedEffect(key1 = 0) {
        delay(RefreshTime)
        tickTack = !tickTack
    }
} else {
    LaunchedEffect(key1 = 0) {
        delay(RefreshTime)
        tickTack = !tickTack
    }
}
Log.d("TAG, tickTack", tickTack.toString())

相关问题