Skip to content

Commit

Permalink
Merge pull request #3156 from CruGlobal/downloadAllToolsForPinnedLang…
Browse files Browse the repository at this point in the history
…uages

GT-2161 Adjust download logic for pinned languages
  • Loading branch information
frett authored Oct 12, 2023
2 parents 044c825 + 057a6ed commit 1f13db5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.combineTransform
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.consumeAsFlow
import kotlinx.coroutines.flow.distinctUntilChanged
Expand Down Expand Up @@ -58,6 +57,7 @@ import org.cru.godtools.db.repository.TranslationsRepository
import org.cru.godtools.downloadmanager.work.scheduleDownloadTranslationWork
import org.cru.godtools.model.DownloadedFile
import org.cru.godtools.model.DownloadedTranslationFile
import org.cru.godtools.model.Tool
import org.cru.godtools.model.Translation
import org.cru.godtools.model.TranslationKey
import org.cru.godtools.shared.tool.parser.ManifestParser
Expand Down Expand Up @@ -465,50 +465,62 @@ class GodToolsDownloadManager @VisibleForTesting internal constructor(
)

init {
// Download Translations for the app language
// Download Translations for Favorite Tools in the app language
settings.appLanguageFlow
.map { setOf(it) }
.distinctUntilChanged()
.downloadFavoriteTranslations()

// Download Translations for pinned languages
// Download Translations for All Tools in the pinned languages
languagesRepository.getPinnedLanguagesFlow()
.map { it.mapTo(mutableSetOf()) { it.code } }
.distinctUntilChanged()
.downloadFavoriteTranslations()
.downloadAllToolTranslations()

// Stale Downloaded Attachments
attachmentsRepository.getAttachmentsFlow()
.combineTransform(downloadedFilesRepository.getDownloadedFilesFlow()) { attachments, files ->
.combine(downloadedFilesRepository.getDownloadedFilesFlow()) { attachments, files ->
val filenames = files.mapTo(mutableSetOf()) { it.filename }
emit(attachments.filter { it.isDownloaded && it.localFilename !in filenames }.map { it.id }.toSet())
attachments
.filter { it.isDownloaded && it.localFilename !in filenames }
.mapTo(mutableSetOf()) { it.id }
}
.distinctUntilChanged()
.conflate()
.onEach { coroutineScope { it.forEach { launch { downloadManager.downloadAttachment(it) } } } }
.launchIn(coroutineScope)
.downloadAttachments()

// Tool Banner Attachments
attachmentsRepository.getAttachmentsFlow()
.combine(toolsRepository.getResourcesFlow()) { attachments, tools ->
val banners =
tools.flatMap { listOfNotNull(it.bannerId, it.detailsBannerId, it.detailsBannerAnimationId) }
attachments.filter { !it.isDownloaded && it.id in banners }.map { it.id }.toSet()
val banners = tools.flatMapTo(mutableSetOf()) {
setOfNotNull(it.bannerId, it.detailsBannerId, it.detailsBannerAnimationId)
}

attachments
.filter { it.id in banners && !it.isDownloaded }
.mapTo(mutableSetOf()) { it.id }
}
.distinctUntilChanged()
.conflate()
.onEach { it.forEach { coroutineScope.launch { downloadManager.downloadAttachment(it) } } }
.launchIn(coroutineScope)
.downloadAttachments()
}

@VisibleForTesting
internal val downloadTranslationsForDefaultLanguageJob =
flowOf(setOf(Settings.defaultLanguage)).downloadFavoriteTranslations()

private fun Flow<Collection<Locale>>.downloadFavoriteTranslations() = toolsRepository.getFavoriteToolsFlow()
.downloadTranslations(this)

private fun Flow<Collection<Locale>>.downloadAllToolTranslations() = toolsRepository.getToolsFlow()
.downloadTranslations(this)

private fun Flow<Set<Long>>.downloadAttachments() = this
.distinctUntilChanged()
.conflate()
.onEach { it.forEach { coroutineScope.launch { downloadManager.downloadAttachment(it) } } }
.launchIn(coroutineScope)

private fun Flow<Collection<Tool>>.downloadTranslations(languages: Flow<Collection<Locale>>) = this
.map { it.mapNotNullTo(mutableSetOf()) { it.code } }
.distinctUntilChanged()
.combineTransformLatest(this) { t, l ->
.combineTransformLatest(languages) { t, l ->
emitAll(translationsRepository.getTranslationsForToolsAndLocalesFlow(t, l))
}
.map {
Expand All @@ -519,7 +531,7 @@ class GodToolsDownloadManager @VisibleForTesting internal constructor(
.distinctUntilChanged()
.conflate()
.onEach {
coroutineScope { it.forEach { launch { downloadManager.downloadLatestPublishedTranslation(it) } } }
it.forEach { coroutineScope.launch { downloadManager.downloadLatestPublishedTranslation(it) } }
}
.launchIn(coroutineScope)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class GodToolsDownloadManagerDispatcherTest {
private val downloadedFilesFlow = MutableSharedFlow<List<DownloadedFile>>(replay = 1)
private val favoriteToolsFlow = MutableSharedFlow<List<Tool>>(replay = 1)
private val resourcesFlow = MutableSharedFlow<List<Tool>>(replay = 1)
private val toolsFlow = MutableSharedFlow<List<Tool>>(replay = 1)

private val attachmentsRepository: AttachmentsRepository = mockk {
every { getAttachmentsFlow() } returns attachmentsFlow
Expand All @@ -62,6 +63,7 @@ class GodToolsDownloadManagerDispatcherTest {
mockk {
every { getResourcesFlow() } returns resourcesFlow
every { getFavoriteToolsFlow() } returns favoriteToolsFlow
every { getToolsFlow() } returns toolsFlow
}
}
private val translationsRepository: TranslationsRepository by lazy {
Expand Down Expand Up @@ -151,7 +153,7 @@ class GodToolsDownloadManagerDispatcherTest {
}

@Test
fun `Favorite Tools downloadLatestPublishedTranslation() - pinned languages`() = testScope.runTest {
fun `downloadLatestPublishedTranslation() - pinned languages - All Tools`() = testScope.runTest {
dispatcher.downloadTranslationsForDefaultLanguageJob.cancel()

val translationsFlow = MutableSharedFlow<List<Translation>>(replay = 1)
Expand All @@ -163,7 +165,7 @@ class GodToolsDownloadManagerDispatcherTest {
} returns translationsFlow
verify { downloadManager wasNot Called }

favoriteToolsFlow.emit(listOf(Tool("tool1"), Tool("tool2")))
toolsFlow.emit(listOf(Tool("tool1"), Tool("tool2")))
pinnedLanguagesFlow.emit(listOf(Language(Locale.FRENCH), Language(Locale.GERMAN)))
runCurrent()
verifyAll {
Expand Down

0 comments on commit 1f13db5

Please sign in to comment.