Hi guys I want to show a timer in items of a `Lazy...
# compose
m
Hi guys I want to show a timer in items of a
LazyRow
:
Copy code
val timerState = produceState(
    initialValue = TimerCounter(0L, 0L, 0L),
    key1 = product.id,
    producer = {
        withContext(Dispatchers.Default) {
            val endTimeInMillis =
                product.productDeal?.dealEndTime?.toTimeInMillis() ?: return@withContext
            val endTimeInSeconds = endTimeInMillis / 1000
            while (isActive) {
                val currentTimeInSecond = System.currentTimeMillis() / 1000
                if (endTimeInSeconds < currentTimeInSecond) {
                    value = TimerCounter(0L, 0L, 0L)
                    return@withContext
                }
                var reminders: Long
                val durationInSeconds = endTimeInSeconds - currentTimeInSecond
                val remainDays = durationInSeconds / DAY_IN_SECONDS
                reminders = durationInSeconds % DAY_IN_SECONDS
                val remainHours = ((reminders / HOURS_IN_SECONDS)) + (remainDays * 24)
                reminders %= HOURS_IN_SECONDS
                val remainMinutes = reminders / MINUTE_IN_SECOND
                val remainSecond = reminders % MINUTE_IN_SECOND
                value = TimerCounter(remainHours, remainMinutes, remainSecond)
                delay(1000)
            }
        }
    }
)
the above code is in composable which shows list item. is this a right approach? in case not, which
Effect
should?
c
I don't think you really need an effect. I would just have a loop in a VM that updates times accordingly and have the UI react to those updates.