Implement FirstThrottler, to gain 200 ms for first refresh

This commit is contained in:
Benoit Marty 2021-03-24 12:09:06 +01:00
parent 0693ce13e4
commit af023669ba
4 changed files with 27 additions and 8 deletions

View File

@ -39,7 +39,7 @@ inline fun <T> LiveData<LiveEvent<T>>.observeEventFirstThrottle(owner: Lifecycle
val firstThrottler = FirstThrottler(minimumInterval) val firstThrottler = FirstThrottler(minimumInterval)
this.observe(owner, EventObserver { this.observe(owner, EventObserver {
if (firstThrottler.canHandle()) { if (firstThrottler.canHandle() is FirstThrottler.CanHandlerResult.Yes) {
it.run(observer) it.run(observer)
} }
}) })

View File

@ -24,14 +24,27 @@ import android.os.SystemClock
class FirstThrottler(private val minimumInterval: Long = 800) { class FirstThrottler(private val minimumInterval: Long = 800) {
private var lastDate = 0L private var lastDate = 0L
fun canHandle(): Boolean { sealed class CanHandlerResult {
object Yes : CanHandlerResult()
data class No(val shouldWaitMillis: Long) : CanHandlerResult()
fun waitMillis(): Long {
return when (this) {
Yes -> 0
is No -> shouldWaitMillis
}
}
}
fun canHandle(): CanHandlerResult {
val now = SystemClock.elapsedRealtime() val now = SystemClock.elapsedRealtime()
if (now > lastDate + minimumInterval) { val delaySinceLast = now - lastDate
if (delaySinceLast > minimumInterval) {
lastDate = now lastDate = now
return true return CanHandlerResult.Yes
} }
// Too soon // Too soon
return false return CanHandlerResult.No(minimumInterval - delaySinceLast)
} }
} }

View File

@ -26,6 +26,7 @@ import im.vector.app.ActiveSessionDataSource
import im.vector.app.BuildConfig import im.vector.app.BuildConfig
import im.vector.app.R import im.vector.app.R
import im.vector.app.core.resources.StringProvider import im.vector.app.core.resources.StringProvider
import im.vector.app.core.utils.FirstThrottler
import im.vector.app.features.settings.VectorPreferences import im.vector.app.features.settings.VectorPreferences
import me.gujun.android.span.span import me.gujun.android.span.span
import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.Session
@ -194,10 +195,14 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
notificationUtils.cancelNotificationMessage(roomId, ROOM_INVITATION_NOTIFICATION_ID) notificationUtils.cancelNotificationMessage(roomId, ROOM_INVITATION_NOTIFICATION_ID)
} }
private var firstThrottler = FirstThrottler(200)
fun refreshNotificationDrawer() { fun refreshNotificationDrawer() {
// Implement last throttler // Implement last throttler
Timber.v("refreshNotificationDrawer()") val canHandle = firstThrottler.canHandle()
Timber.v("refreshNotificationDrawer(), delay: ${canHandle.waitMillis()} ms")
backgroundHandler.removeCallbacksAndMessages(null) backgroundHandler.removeCallbacksAndMessages(null)
backgroundHandler.postDelayed( backgroundHandler.postDelayed(
{ {
try { try {
@ -206,7 +211,8 @@ class NotificationDrawerManager @Inject constructor(private val context: Context
// It can happen if for instance session has been destroyed. It's a bit ugly to try catch like this, but it's safer // It can happen if for instance session has been destroyed. It's a bit ugly to try catch like this, but it's safer
Timber.w(throwable, "refreshNotificationDrawerBg failure") Timber.w(throwable, "refreshNotificationDrawerBg failure")
} }
}, 200) },
canHandle.waitMillis())
} }
@WorkerThread @WorkerThread

View File

@ -101,7 +101,7 @@ class VectorSettingsHelpAboutFragment @Inject constructor(
// third party notice // third party notice
findPreference<VectorPreference>(VectorPreferences.SETTINGS_THIRD_PARTY_NOTICES_PREFERENCE_KEY)!! findPreference<VectorPreference>(VectorPreferences.SETTINGS_THIRD_PARTY_NOTICES_PREFERENCE_KEY)!!
.onPreferenceClickListener = Preference.OnPreferenceClickListener { .onPreferenceClickListener = Preference.OnPreferenceClickListener {
if (firstThrottler.canHandle()) { if (firstThrottler.canHandle() is FirstThrottler.CanHandlerResult.Yes) {
activity?.displayInWebView(VectorSettingsUrls.THIRD_PARTY_LICENSES) activity?.displayInWebView(VectorSettingsUrls.THIRD_PARTY_LICENSES)
} }
false false