Yet Another OTP generator
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

OtpItemViewModel.kt 926B

1234567891011121314151617181920212223242526272829
  1. package com.chameth.yaotp.viewmodel
  2. import androidx.lifecycle.MutableLiveData
  3. import androidx.lifecycle.ViewModel
  4. import com.chameth.yaotp.accounts.Account
  5. import com.chameth.yaotp.algos.Otp
  6. import com.chameth.yaotp.algos.currentTime
  7. import kotlin.math.max
  8. class OtpItemViewModel : ViewModel() {
  9. var account: Account? = null
  10. set(value) {
  11. value?.let {
  12. label.value = it.label
  13. otp.value = it.generateOtp()
  14. showTime.value = it.timeBased
  15. timeLeft.value = if (it.timeBased) "${calculateTimeLeft(otp.value?.expiresAt ?: 0)}s" else ""
  16. }
  17. }
  18. val label = MutableLiveData<String>()
  19. val otp = MutableLiveData<Otp>()
  20. val showTime = MutableLiveData<Boolean>()
  21. val timeLeft = MutableLiveData<String>()
  22. internal fun calculateTimeLeft(expiry: Long, currentTime: Long = currentTime())= max(0, expiry - currentTime)
  23. }