Updated on 2026-08-14

This commit is contained in:
Tangem 2023-09-25 09:55:08 +08:00
parent 9a02d6a58a
commit 6c850776d5
37 changed files with 611 additions and 104 deletions

View file

@ -0,0 +1,102 @@
package com.tangem.data.settings
import com.tangem.datasource.local.settings.AppLaunchCountStore
import com.tangem.datasource.local.settings.AppRatingShowingCountStore
import com.tangem.datasource.local.settings.FundsFoundDateInMillisStore
import com.tangem.datasource.local.settings.UserInteractingStatusStore
import com.tangem.domain.settings.repositories.AppRatingRepository
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.withContext
import java.util.Calendar
internal class DefaultAppRatingRepository(
private val fundsFoundDateInMillisStore: FundsFoundDateInMillisStore,
private val appLaunchCountStore: AppLaunchCountStore,
private val appRatingShowingCountStore: AppRatingShowingCountStore,
private val userInteractingStatusStore: UserInteractingStatusStore,
private val dispatchers: CoroutineDispatcherProvider,
) : AppRatingRepository {
override suspend fun initialize() {
fundsFoundDateInMillisStore.getSyncOrNull()
?: fundsFoundDateInMillisStore.store(item = FUNDS_FOUND_DATE_UNDEFINED)
appLaunchCountStore.getSyncOrNull()
?: appLaunchCountStore.store(item = DEFAULT_APP_LAUNCH_COUNT)
appRatingShowingCountStore.getSyncOrNull()
?: appRatingShowingCountStore.store(item = FIRST_SHOWING_COUNT)
userInteractingStatusStore.getSyncOrNull()
?: userInteractingStatusStore.store(item = false)
}
override suspend fun setWalletWithFundsFound() {
withContext(dispatchers.io) {
val foundDate = fundsFoundDateInMillisStore.getSyncOrNull()
if (foundDate != null && foundDate != FUNDS_FOUND_DATE_UNDEFINED) return@withContext
fundsFoundDateInMillisStore.store(item = Calendar.getInstance().timeInMillis)
val appLaunchCount = appLaunchCountStore.getSyncOrNull().let { count ->
if (count == null) {
appLaunchCountStore.store(item = DEFAULT_APP_LAUNCH_COUNT)
DEFAULT_APP_LAUNCH_COUNT
} else {
count
}
}
appRatingShowingCountStore.store(item = appLaunchCount + FIRST_SHOWING_COUNT)
userInteractingStatusStore.getSyncOrNull()
?: userInteractingStatusStore.store(item = false)
}
}
override fun isReadyToShow(): Flow<Boolean> {
// TODO: [REDACTED_JIRA]
return combine(
userInteractingStatusStore.get(),
appRatingShowingCountStore.get(),
appLaunchCountStore.get(),
fundsFoundDateInMillisStore.get(),
) { isInteracting, ratingShowingCount, appLaunchCount, fundsFoundDate ->
if (!isInteracting) {
val diff = Calendar.getInstance().timeInMillis - fundsFoundDate
val diffInDays = diff / DAY_IN_MILLIS
appLaunchCount >= ratingShowingCount && diffInDays >= FIRST_SHOWING_COUNT
} else {
appLaunchCount >= ratingShowingCount
}
}
}
override suspend fun remindLater() {
appLaunchCountStore.getSyncOrNull()?.let {
updateNextShowing(at = it + DEFER_SHOWING_COUNT)
}
}
override suspend fun setNeverToShow() {
updateNextShowing(at = Int.MAX_VALUE)
}
private suspend fun updateNextShowing(at: Int) {
withContext(dispatchers.io) {
appRatingShowingCountStore.store(item = at)
userInteractingStatusStore.store(item = true)
}
}
private companion object {
const val FUNDS_FOUND_DATE_UNDEFINED = -1L
const val DEFAULT_APP_LAUNCH_COUNT = 1
const val FIRST_SHOWING_COUNT = 3
const val DEFER_SHOWING_COUNT = 20
const val DAY_IN_MILLIS = 1000 * 60 * 60 * 24
}
}

View file

@ -10,12 +10,6 @@ internal class DefaultSettingsRepository(
private val dispatchers: CoroutineDispatcherProvider,
) : SettingsRepository {
override suspend fun isUserAlreadyRateApp(): Boolean {
return withContext(dispatchers.io) {
preferencesDataSource.appRatingLaunchObserver.isReadyToShow()
}
}
override suspend fun shouldShowSaveUserWalletScreen(): Boolean {
return withContext(dispatchers.io) { preferencesDataSource.shouldShowSaveUserWalletScreen }
}

View file

@ -1,7 +1,13 @@
package com.tangem.data.settings.di
import com.tangem.data.settings.DefaultAppRatingRepository
import com.tangem.data.settings.DefaultSettingsRepository
import com.tangem.data.source.preferences.PreferencesDataSource
import com.tangem.datasource.local.settings.AppLaunchCountStore
import com.tangem.datasource.local.settings.AppRatingShowingCountStore
import com.tangem.datasource.local.settings.FundsFoundDateInMillisStore
import com.tangem.datasource.local.settings.UserInteractingStatusStore
import com.tangem.domain.settings.repositories.AppRatingRepository
import com.tangem.domain.settings.repositories.SettingsRepository
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
import dagger.Module
@ -25,4 +31,22 @@ internal object SettingsDataModule {
dispatchers = dispatchers,
)
}
@Provides
@Singleton
fun provideAppRatingRepository(
fundsFoundDateInMillisStore: FundsFoundDateInMillisStore,
appLaunchCountStore: AppLaunchCountStore,
appRatingShowingCountStore: AppRatingShowingCountStore,
userInteractingStatusStore: UserInteractingStatusStore,
dispatchers: CoroutineDispatcherProvider,
): AppRatingRepository {
return DefaultAppRatingRepository(
fundsFoundDateInMillisStore = fundsFoundDateInMillisStore,
appLaunchCountStore = appLaunchCountStore,
appRatingShowingCountStore = appRatingShowingCountStore,
userInteractingStatusStore = userInteractingStatusStore,
dispatchers = dispatchers,
)
}
}