package com.chameth.yaotp.viewmodel import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import com.chameth.yaotp.accounts.Account import com.chameth.yaotp.algos.Otp import com.chameth.yaotp.algos.currentTime import kotlin.math.max class OtpItemViewModel : ViewModel() { var account: Account? = null set(value) { value?.let { label.value = it.label otp.value = it.generateOtp() showTime.value = it.timeBased timeLeft.value = if (it.timeBased) "${calculateTimeLeft(otp.value?.expiresAt ?: 0)}s" else "" } } val label = MutableLiveData() val otp = MutableLiveData() val showTime = MutableLiveData() val timeLeft = MutableLiveData() internal fun calculateTimeLeft(expiry: Long, currentTime: Long = currentTime())= max(0, expiry - currentTime) }