Skip to content

Commit

Permalink
remove some duplciated logic for dispatching attachment downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
frett committed Oct 12, 2023
1 parent 8b1948e commit 057a6ed
Showing 1 changed file with 19 additions and 14 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 @@ -480,26 +479,26 @@ class GodToolsDownloadManager @VisibleForTesting internal constructor(

// 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
Expand All @@ -512,6 +511,12 @@ class GodToolsDownloadManager @VisibleForTesting internal constructor(
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()
Expand Down

0 comments on commit 057a6ed

Please sign in to comment.