From 4bb0d522179f5d6d9b6033454de905f3cd24d7ff Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sat, 28 Oct 2023 16:30:41 +0300 Subject: [PATCH] Fix downloading --- .../koitharu/kotatsu/core/util/ext/Coil.kt | 2 +- .../download/ui/list/DownloadItemAD.kt | 21 ++++++++++++------- .../download/ui/list/DownloadItemModel.kt | 3 +++ .../download/ui/worker/DownloadWorker.kt | 3 ++- .../settings/backup/PeriodicalBackupWorker.kt | 2 +- 5 files changed, 20 insertions(+), 11 deletions(-) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Coil.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Coil.kt index 67e07d63d..d3448ccfe 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Coil.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Coil.kt @@ -23,7 +23,7 @@ fun ImageView.newImageRequest(lifecycleOwner: LifecycleOwner, data: Any?): Image return null } } - disposeImageRequest() + // disposeImageRequest() return ImageRequest.Builder(context) .data(data) .lifecycle(lifecycleOwner) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/list/DownloadItemAD.kt b/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/list/DownloadItemAD.kt index a1d688210..b9f9a914f 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/list/DownloadItemAD.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/list/DownloadItemAD.kt @@ -6,6 +6,8 @@ import androidx.core.view.isVisible import androidx.lifecycle.LifecycleOwner import androidx.work.WorkInfo import coil.ImageLoader +import coil.request.SuccessResult +import coil.util.CoilUtils import com.hannesdorfmann.adapterdelegates4.dsl.adapterDelegateViewBinding import org.koitharu.kotatsu.R import org.koitharu.kotatsu.core.ui.image.TrimTransformation @@ -55,14 +57,17 @@ fun downloadItemAD( TransitionManager.beginDelayedTransition(binding.constraintLayout) } binding.textViewTitle.text = item.manga.title - binding.imageViewCover.newImageRequest(lifecycleOwner, item.manga.coverUrl)?.apply { - placeholder(R.drawable.ic_placeholder) - fallback(R.drawable.ic_placeholder) - error(R.drawable.ic_error_placeholder) - allowRgb565(true) - transformations(TrimTransformation()) - source(item.manga.source) - enqueueWith(coil) + if ((CoilUtils.result(binding.imageViewCover) as? SuccessResult)?.memoryCacheKey != item.coverCacheKey) { + binding.imageViewCover.newImageRequest(lifecycleOwner, item.manga.coverUrl)?.apply { + placeholder(R.drawable.ic_placeholder) + fallback(R.drawable.ic_placeholder) + error(R.drawable.ic_error_placeholder) + allowRgb565(true) + transformations(TrimTransformation()) + memoryCacheKey(item.coverCacheKey) + source(item.manga.source) + enqueueWith(coil) + } } // binding.textViewTitle.isChecked = item.isExpanded // binding.textViewTitle.drawableEnd = if (item.isExpandable) expandIcon else null diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/list/DownloadItemModel.kt b/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/list/DownloadItemModel.kt index 0775f13dd..fe6bd5d94 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/list/DownloadItemModel.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/list/DownloadItemModel.kt @@ -2,6 +2,7 @@ package org.koitharu.kotatsu.download.ui.list import android.text.format.DateUtils import androidx.work.WorkInfo +import coil.memory.MemoryCache import org.koitharu.kotatsu.list.ui.ListModelDiffCallback import org.koitharu.kotatsu.list.ui.model.ListModel import org.koitharu.kotatsu.parsers.model.Manga @@ -23,6 +24,8 @@ data class DownloadItemModel( val isExpanded: Boolean, ) : ListModel, Comparable { + val coverCacheKey = MemoryCache.Key(manga.coverUrl, mapOf("dl" to "1")) + val percent: Float get() = if (max > 0) progress / max.toFloat() else 0f diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadWorker.kt b/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadWorker.kt index b8816ab48..7137d1b11 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadWorker.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/download/ui/worker/DownloadWorker.kt @@ -105,9 +105,10 @@ class DownloadWorker @AssistedInject constructor( setForeground(getForegroundInfo()) val mangaId = inputData.getLong(MANGA_ID, 0L) val manga = mangaDataRepository.findMangaById(mangaId) ?: return Result.failure() + lastPublishedState = DownloadState(manga, isIndeterminate = true) + publishState(DownloadState(manga, isIndeterminate = true)) val chaptersIds = inputData.getLongArray(CHAPTERS_IDS)?.takeUnless { it.isEmpty() } val downloadedIds = getDoneChapters(manga) - publishState(DownloadState(manga, isIndeterminate = true)) return try { downloadMangaImpl(manga, chaptersIds, downloadedIds) Result.success(currentState.toWorkData()) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/settings/backup/PeriodicalBackupWorker.kt b/app/src/main/kotlin/org/koitharu/kotatsu/settings/backup/PeriodicalBackupWorker.kt index 77f55ab3c..54a34b4fa 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/settings/backup/PeriodicalBackupWorker.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/settings/backup/PeriodicalBackupWorker.kt @@ -77,7 +77,7 @@ class PeriodicalBackupWorker @AssistedInject constructor( } val request = PeriodicWorkRequestBuilder( settings.periodicalBackupFrequency, - TimeUnit.HOURS, + TimeUnit.DAYS, ).setConstraints(constraints.build()) .addTag(TAG) .build()