1234567891011121314151617181920212223242526272829 |
- 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<String>()
- val otp = MutableLiveData<Otp>()
- val showTime = MutableLiveData<Boolean>()
- val timeLeft = MutableLiveData<String>()
-
- internal fun calculateTimeLeft(expiry: Long, currentTime: Long = currentTime())= max(0, expiry - currentTime)
-
- }
|