Updated on 2026-08-14
This commit is contained in:
parent
69754e4159
commit
34cfc843ab
31 changed files with 322 additions and 170 deletions
|
|
@ -14,6 +14,8 @@ dependencies {
|
|||
implementation(deps.androidx.datastore)
|
||||
|
||||
implementation(deps.jodatime)
|
||||
implementation(deps.timber)
|
||||
|
||||
implementation(deps.hilt.android)
|
||||
kapt(deps.hilt.kapt)
|
||||
|
||||
|
|
|
|||
|
|
@ -4,18 +4,19 @@ import com.tangem.data.promo.converters.StoryContentResponseConverter
|
|||
import com.tangem.datasource.api.common.response.getOrThrow
|
||||
import com.tangem.datasource.api.tangemTech.TangemTechApi
|
||||
import com.tangem.datasource.local.preferences.AppPreferencesStore
|
||||
import com.tangem.datasource.local.preferences.PreferencesKeys.SHOULD_SHOW_SWAP_STORIES_KEY
|
||||
import com.tangem.datasource.local.preferences.PreferencesKeys.getShouldShowStoriesKey
|
||||
import com.tangem.datasource.local.preferences.utils.get
|
||||
import com.tangem.datasource.local.preferences.utils.getSyncOrDefault
|
||||
import com.tangem.datasource.local.preferences.utils.store
|
||||
import com.tangem.datasource.local.promo.PromoStoriesStore
|
||||
import com.tangem.domain.promo.PromoRepository
|
||||
import com.tangem.domain.promo.models.StoryContent
|
||||
import com.tangem.utils.SupportedLanguages
|
||||
import com.tangem.utils.coroutines.CoroutineDispatcherProvider
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.flowOf
|
||||
import kotlinx.coroutines.flow.mapLatest
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.coroutines.withTimeoutOrNull
|
||||
|
||||
internal class DefaultPromoRepository(
|
||||
private val tangemApi: TangemTechApi,
|
||||
|
|
@ -42,35 +43,49 @@ internal class DefaultPromoRepository(
|
|||
// Use it on new promo action
|
||||
}
|
||||
|
||||
override suspend fun getStoryById(id: String): StoryContent = withContext(dispatchers.io) {
|
||||
override fun getStoryById(id: String): Flow<StoryContent?> = isReadyToShowStories(id).mapLatest {
|
||||
getStoryByIdSync(id)
|
||||
}
|
||||
|
||||
override suspend fun getStoryByIdSync(id: String): StoryContent? = withContext(dispatchers.io) {
|
||||
if (!isReadyToShowStoriesSync(id)) return@withContext null
|
||||
|
||||
val storedPromo = promoStoriesStore.getSyncOrNull(storyId = id)
|
||||
// Get last stored promo by id if possible or get from network
|
||||
val story = if (storedPromo == null) {
|
||||
val storyContent = tangemApi.getStoryById(
|
||||
storyId = id,
|
||||
language = SupportedLanguages.getCurrentSupportedLanguageCode(),
|
||||
).getOrThrow()
|
||||
promoStoriesStore.store(storyId = id, item = storyContent)
|
||||
val storyContent = runCatching {
|
||||
// Important to return
|
||||
withTimeoutOrNull(STORIES_LOAD_DELAY) {
|
||||
tangemApi.getStoryById(storyId = id).getOrThrow()
|
||||
}
|
||||
}.getOrNull()
|
||||
if (storyContent != null) {
|
||||
promoStoriesStore.store(id, storyContent)
|
||||
}
|
||||
storyContent
|
||||
} else {
|
||||
storedPromo
|
||||
}
|
||||
|
||||
storyContentConverter.convert(story)
|
||||
story?.let { storyContentConverter.convert(it) }
|
||||
}
|
||||
|
||||
override fun isReadyToShowSwapStories(): Flow<Boolean> {
|
||||
return appPreferencesStore.get(SHOULD_SHOW_SWAP_STORIES_KEY, true)
|
||||
override fun isReadyToShowStories(storyId: String): Flow<Boolean> {
|
||||
return appPreferencesStore.get(getShouldShowStoriesKey(storyId), true)
|
||||
}
|
||||
|
||||
override suspend fun isReadyToShowSwapStoriesSync(): Boolean {
|
||||
return appPreferencesStore.getSyncOrDefault(SHOULD_SHOW_SWAP_STORIES_KEY, true)
|
||||
override suspend fun isReadyToShowStoriesSync(storyId: String): Boolean {
|
||||
return appPreferencesStore.getSyncOrDefault(getShouldShowStoriesKey(storyId), true)
|
||||
}
|
||||
|
||||
override suspend fun setNeverToShowSwapStories() {
|
||||
override suspend fun setNeverToShowStories(storyId: String) {
|
||||
appPreferencesStore.store(
|
||||
key = SHOULD_SHOW_SWAP_STORIES_KEY,
|
||||
key = getShouldShowStoriesKey(storyId),
|
||||
value = false,
|
||||
)
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val STORIES_LOAD_DELAY = 1000L
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue