From 1e8a8d4da03218cbfb5e586e07543b888fd9dd40 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 26 Jul 2022 14:23:11 +0200 Subject: [PATCH] Display the nightly popup only once a day --- .../im/vector/app/nightly/NightlyProxy.kt | 29 ++++++++++++++++++- .../vector/app/features/home/HomeActivity.kt | 4 +-- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/vector/src/gplay/java/im/vector/app/nightly/NightlyProxy.kt b/vector/src/gplay/java/im/vector/app/nightly/NightlyProxy.kt index 73262e21a9..dd543318ab 100644 --- a/vector/src/gplay/java/im/vector/app/nightly/NightlyProxy.kt +++ b/vector/src/gplay/java/im/vector/app/nightly/NightlyProxy.kt @@ -16,13 +16,21 @@ package im.vector.app.nightly +import android.content.Context +import androidx.core.content.edit import com.google.firebase.appdistribution.FirebaseAppDistribution import com.google.firebase.appdistribution.FirebaseAppDistributionException +import im.vector.app.core.di.DefaultSharedPreferences +import im.vector.app.core.time.Clock import timber.log.Timber import javax.inject.Inject -class NightlyProxy @Inject constructor() { +class NightlyProxy @Inject constructor( + private val clock: Clock, + private val context: Context, +) { fun updateIfNewReleaseAvailable() { + if (!canDisplayPopup()) return val firebaseAppDistribution = FirebaseAppDistribution.getInstance() firebaseAppDistribution.updateIfNewReleaseAvailable() .addOnProgressListener { up -> @@ -44,4 +52,23 @@ class NightlyProxy @Inject constructor() { } } } + + private fun canDisplayPopup(): Boolean { + val sharedPreferences = DefaultSharedPreferences.getInstance(context) + val today = clock.epochMillis() / A_DAY_IN_MILLIS + val lastDisplayPopupDay = sharedPreferences.getLong(SHARED_PREF_KEY, 0) + return (today > lastDisplayPopupDay) + .also { canDisplayPopup -> + if (canDisplayPopup) { + sharedPreferences.edit { + putLong(SHARED_PREF_KEY, today) + } + } + } + } + + companion object { + private const val A_DAY_IN_MILLIS = 8_600_000L + private const val SHARED_PREF_KEY = "LAST_NIGHTLY_POPUP_DAY" + } } diff --git a/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt b/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt index 925e594ce7..f1848e653f 100644 --- a/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt +++ b/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt @@ -537,9 +537,7 @@ class HomeActivity : serverBackupStatusViewModel.refreshRemoteStateIfNeeded() // Check nightly - if (isFirstCreation()) { - nightlyProxy.updateIfNewReleaseAvailable() - } + nightlyProxy.updateIfNewReleaseAvailable() } override fun getMenuRes() = R.menu.home