Fix timestamp update on conversation re-entry from background.

This commit is contained in:
Alex Hart
2023-09-25 11:08:10 -03:00
committed by Cody Henthorne
parent b3399b5242
commit f44b44a354

View File

@@ -5,7 +5,10 @@ import android.os.Looper
import androidx.annotation.VisibleForTesting
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import org.signal.core.util.logging.Log
import org.thoughtcrime.securesms.util.Util
import java.util.concurrent.TimeUnit
import kotlin.math.max
/**
* Lifecycle-aware class which will call onTick every 1 minute.
@@ -17,6 +20,7 @@ class ConversationUpdateTick(
private val handler = Handler(Looper.getMainLooper())
private var isResumed = false
private var lastTick = -1L
constructor(onTickListener: () -> Unit) : this(object : OnTickListener {
override fun onTick() {
@@ -28,7 +32,30 @@ class ConversationUpdateTick(
isResumed = true
handler.removeCallbacksAndMessages(null)
handler.postDelayed(this::onTick, TIMEOUT)
if (lastTick > 0) {
val timeSinceLastTick = System.currentTimeMillis() - lastTick
if (timeSinceLastTick <= 0) {
Log.w(TAG, "Time since last tick is invalid. Reinitializing and posting update in $TIMEOUT ms")
lastTick = System.currentTimeMillis()
handler.postDelayed(this::onTick, TIMEOUT)
}
val timeUntilNextTick = Util.clamp(TIMEOUT - timeSinceLastTick, 0, TIMEOUT)
if (timeSinceLastTick == 0L) {
Log.i(TAG, "Last tick outside timeout period. Posting update immediately")
handler.post(this::onTick)
} else {
Log.i(TAG, "Last tick within timeout period. Posting update in $timeUntilNextTick ms")
handler.postDelayed(this::onTick, timeUntilNextTick)
}
} else {
Log.i(TAG, "No time since last tick. Initialising and posting update in $TIMEOUT ms")
lastTick = System.currentTimeMillis()
handler.postDelayed(this::onTick, TIMEOUT)
}
}
override fun onPause(owner: LifecycleOwner) {
@@ -40,6 +67,7 @@ class ConversationUpdateTick(
private fun onTick() {
if (isResumed) {
onTickListener.onTick()
lastTick = System.currentTimeMillis()
handler.removeCallbacksAndMessages(null)
handler.postDelayed(this::onTick, TIMEOUT)
@@ -53,5 +81,7 @@ class ConversationUpdateTick(
companion object {
@VisibleForTesting
val TIMEOUT = TimeUnit.MINUTES.toMillis(1)
private val TAG = Log.tag(ConversationUpdateTick::class.java)
}
}