diff --git a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletMiddleware.kt b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletMiddleware.kt index 31da876cf0..a451256f60 100644 --- a/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletMiddleware.kt +++ b/app/src/main/java/com/tangem/tap/features/wallet/redux/WalletMiddleware.kt @@ -16,6 +16,7 @@ import com.tangem.common.extensions.toHexString import com.tangem.tap.common.analytics.AnalyticsEvent import com.tangem.tap.common.analytics.FirebaseAnalyticsHandler import com.tangem.tap.common.extensions.copyToClipboard +import com.tangem.tap.common.extensions.isGreaterThan import com.tangem.tap.common.redux.AppState import com.tangem.tap.common.redux.navigation.AppScreen import com.tangem.tap.common.redux.navigation.NavigationAction @@ -43,6 +44,7 @@ import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.rekotlin.Action import org.rekotlin.Middleware +import java.math.BigDecimal class WalletMiddleware { private val topUpMiddleware = TopUpMiddleware() @@ -99,6 +101,7 @@ class WalletMiddleware { is WalletAction.LoadWallet.Success -> { store.dispatch(WalletAction.CheckHashesCountOnline) if (!store.state.walletState.updatingWallet) setupWalletUpdate(action.wallet) + tryToShowAppRatingWarning(action.wallet) } is WalletAction.CreatePayId.CompleteCreatingPayId -> { scope.launch { @@ -199,11 +202,6 @@ class WalletMiddleware { } updateWarningMessages() } - val readyToShow = preferencesStorage.appRatingLaunchObserver.isReadyToShow() - if (readyToShow) { - FirebaseAnalyticsHandler.triggerEvent(AnalyticsEvent.APP_RATING_DISPLAYED) - addWarningMessage(WarningMessagesManager.appRatingWarning(), true) - } } is WalletAction.CheckHashesCountOnline -> checkHashesCountOnline() is WalletAction.SaveCardId -> { @@ -229,6 +227,19 @@ class WalletMiddleware { } } + private fun tryToShowAppRatingWarning(wallet: Wallet) { + val nonZeroWalletsCount = wallet.amounts.filter { + it.value.value?.isGreaterThan(BigDecimal.ZERO) ?: false + }.size + if (nonZeroWalletsCount > 0) { + preferencesStorage.appRatingLaunchObserver.foundWalletWithFunds() + } + if (preferencesStorage.appRatingLaunchObserver.isReadyToShow()) { + FirebaseAnalyticsHandler.triggerEvent(AnalyticsEvent.APP_RATING_DISPLAYED) + addWarningMessage(WarningMessagesManager.appRatingWarning(), true) + } + } + private fun setupWalletUpdate(wallet: Wallet) { if (!wallet.recentTransactions.toPendingTransactions(wallet.address).isNullOrEmpty()) { store.dispatch(WalletAction.UpdateWallet.ScheduleUpdatingWallet) diff --git a/app/src/main/java/com/tangem/tap/persistence/PreferencesStorage.kt b/app/src/main/java/com/tangem/tap/persistence/PreferencesStorage.kt index 0c0179481e..4ab011a469 100644 --- a/app/src/main/java/com/tangem/tap/persistence/PreferencesStorage.kt +++ b/app/src/main/java/com/tangem/tap/persistence/PreferencesStorage.kt @@ -11,6 +11,7 @@ import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory import com.tangem.tap.common.entities.TapCurrency.Companion.DEFAULT_FIAT_CURRENCY import com.tangem.tap.common.redux.global.FiatCurrencyName import com.tangem.tap.network.coinmarketcap.FiatCurrency +import java.util.* class PreferencesStorage(applicationContext: Application) { @@ -104,16 +105,34 @@ class AppRatingLaunchObserver( private val preferences: SharedPreferences, private val launchCounts: Int, ) { - private val K_SHOW_RATING_DIALOG = "show_rating_dialog_after_app_launch_count" + private val K_SHOW_RATING_AT_LAUNCH_COUNT = "showRatingDialogAtLaunchCount" + private val K_FUNDS_FOUND_DATE = "aFundsFoundDate" private val deferShowing = 20 private val firstShowing = 3 + private var fundsFoundDate: Calendar? = null + + init { + val dateTimeMs = preferences.getLong(K_FUNDS_FOUND_DATE, -1) + if (dateTimeMs > 0) fundsFoundDate = Calendar.getInstance().apply { timeInMillis = dateTimeMs } + } + + fun foundWalletWithFunds() { + if (fundsFoundDate != null) return + + fundsFoundDate = Calendar.getInstance() + preferences.edit().putLong(K_FUNDS_FOUND_DATE, fundsFoundDate!!.timeInMillis).apply() + updateNextShowing(launchCounts + firstShowing) + } fun isReadyToShow(): Boolean { - if (launchCounts == firstShowing) return true + val fundsDate = fundsFoundDate ?: return false - val nextShowingAt = getCounterOfNextShowing() - return launchCounts >= nextShowingAt + val diff = Calendar.getInstance().timeInMillis - fundsDate.timeInMillis + val diffInDays = diff / (100 * 60 * 60 * 24) + if (diffInDays >= firstShowing) return true + + return launchCounts >= getCounterOfNextShowing() } fun applyDelayedShowing() { @@ -125,10 +144,10 @@ class AppRatingLaunchObserver( } private fun updateNextShowing(at: Int) { - preferences.edit().putInt(K_SHOW_RATING_DIALOG, at).apply() + preferences.edit().putInt(K_SHOW_RATING_AT_LAUNCH_COUNT, at).apply() } private fun getCounterOfNextShowing(): Int { - return preferences.getInt(K_SHOW_RATING_DIALOG, firstShowing) + return preferences.getInt(K_SHOW_RATING_AT_LAUNCH_COUNT, firstShowing) } } \ No newline at end of file