Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Commit

Permalink
Cancel and restart the timer task in the correct lifecycle methods
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Wong committed Oct 13, 2021
1 parent d7877cd commit 62fee38
Showing 1 changed file with 10 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import androidx.annotation.ColorRes
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
Expand Down Expand Up @@ -91,7 +90,6 @@ class CertificateLightDetailFragment : Fragment(R.layout.fragment_certificate_li
binding.toolbar.setNavigationOnClickListener { parentFragmentManager.popBackStack() }

displayQrCode()
displayValidity()
displayCertificateDetails()
setupStatusInfo()

Expand All @@ -101,18 +99,13 @@ class CertificateLightDetailFragment : Fragment(R.layout.fragment_certificate_li
override fun onResume() {
super.onResume()
requireActivity().window.overrideScreenBrightness(true)
startValidityTimer()
}

override fun onPause() {
super.onPause()
requireActivity().window.overrideScreenBrightness(false)
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
validityTimer?.cancel()
validityTimer = null
cancelValidityTimer()
}

private fun displayQrCode() {
Expand All @@ -123,10 +116,10 @@ class CertificateLightDetailFragment : Fragment(R.layout.fragment_certificate_li
}

@SuppressLint("SetTextI18n")
private fun displayValidity() {
private fun startValidityTimer() {
val expirationTime = certificateHolder.expirationTime ?: return

validityTimer?.cancel()
cancelValidityTimer()
validityTimer = fixedRateTimer(period = TimeUnit.SECONDS.toMillis(1L)) {
val now = Instant.now()
val remainingTimeInSeconds = expirationTime.epochSecond - now.epochSecond
Expand All @@ -141,14 +134,17 @@ class CertificateLightDetailFragment : Fragment(R.layout.fragment_certificate_li
}

if (remainingTimeInSeconds <= 0) {
validityTimer?.cancel()
validityTimer = null

cancelValidityTimer()
deleteCertificateLightAndShowOriginal()
}
}
}

private fun cancelValidityTimer() {
validityTimer?.cancel()
validityTimer = null
}

private fun displayCertificateDetails() {
val chLightCert = certificateHolder.certificate as? ChLightCert ?: return

Expand Down

0 comments on commit 62fee38

Please sign in to comment.