Title updates now rename downloads

Auto d/l now starts after metadata refresh
This commit is contained in:
Jay 2020-02-03 00:45:25 -08:00
parent f3b96c430e
commit 86992a02d8
6 changed files with 49 additions and 9 deletions

View File

@ -1,6 +1,9 @@
package eu.kanade.tachiyomi.data.database.models
import eu.kanade.tachiyomi.data.download.DownloadProvider
import eu.kanade.tachiyomi.source.model.SManga
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
open class MangaImpl : Manga {
@ -37,13 +40,15 @@ open class MangaImpl : Manga {
override var hide_title: Boolean = false
override fun copyFrom(other: SManga) {
if (other is MangaImpl && (other as MangaImpl)::title.isInitialized && !other.title
.isBlank()) {
if (other is MangaImpl && (other as MangaImpl)::title.isInitialized &&
!other.title.isBlank() && other.title != originalTitle()) {
val oldTitle = originalTitle()
title = if (currentTitle() != originalTitle()) {
val customTitle = currentTitle()
val trueTitle = other.title
"${customTitle}${SManga.splitter}${trueTitle}"
} else other.title
Injekt.get<DownloadProvider>().renameMangaFolder(oldTitle, title, source)
}
super.copyFrom(other)
}

View File

@ -9,6 +9,7 @@ import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.source.Source
import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.util.DiskUtil
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.util.concurrent.TimeUnit
@ -101,7 +102,9 @@ class DownloadCache(
if (sourceDir != null) {
val mangaDir = sourceDir.files[provider.getMangaDirName(manga)]
if (mangaDir != null) {
return mangaDir.files.size
return mangaDir.files
.filter { !it.endsWith(Downloader.TMP_DIR_SUFFIX) }
.size
}
}
return 0
@ -227,6 +230,20 @@ class DownloadCache(
}
}
fun renameFolder(from: String, to: String, source: Long) {
val sourceDir = rootDir.files[source] ?: return
val list = sourceDir.files.toMutableMap()
val mangaFiles = sourceDir.files[DiskUtil.buildValidFilename(from)] ?: return
val newDir = MangaDirectory(
UniFile.fromUri(
context, Uri.parse(sourceDir.dir.filePath + "/" + DiskUtil.buildValidFilename(to))
)
)
newDir.files = mangaFiles.files
list.remove(DiskUtil.buildValidFilename(from))
list[to] = newDir
}
/**
* Removes a manga that has been deleted from this cache.

View File

@ -132,6 +132,16 @@ class DownloadProvider(private val context: Context) {
cache.renew()
}
fun renameMangaFolder(from: String, to: String, sourceId: Long) {
val sourceManager by injectLazy<SourceManager>()
val source = sourceManager.get(sourceId) ?: return
val sourceDir = findSourceDir(source)
val mangaDir = sourceDir?.findFile(DiskUtil.buildValidFilename(from))
mangaDir?.renameTo(to)
val cache = DownloadCache(context, this, sourceManager)
cache.renameFolder(from, to, sourceId)
}
/**
* Returns a list of all files in manga directory
*

View File

@ -493,4 +493,8 @@ class Downloader(
return queue.none { it.status <= Download.DOWNLOADING }
}
companion object {
const val TMP_DIR_SUFFIX = "_tmp"
}
}

View File

@ -329,13 +329,20 @@ class LibraryUpdateService(
.doOnCompleted {
if (newUpdates.isNotEmpty()) {
showResultNotification(newUpdates)
if (downloadNew && hasDownloads) {
DownloadService.start(this)
}
if (preferences.refreshCoversToo().getOrDefault()) {
updateDetails(newUpdates.map { it.first }).observeOn(Schedulers.io())
.doOnCompleted {
cancelProgressNotification()
if (downloadNew && hasDownloads) {
DownloadService.start(this)
}
}
.subscribeOn(Schedulers.io()).subscribe {}
}
else if (downloadNew && hasDownloads) {
DownloadService.start(this)
}
}
if (failedUpdates.isNotEmpty()) {
@ -391,7 +398,6 @@ class LibraryUpdateService(
fun updateDetails(mangaToUpdate: List<LibraryManga>): Observable<LibraryManga> {
// Initialize the variables holding the progress of the updates.
val count = AtomicInteger(0)
val coverCache by injectLazy<CoverCache>()
// Emit each manga and update it sequentially.
return Observable.from(mangaToUpdate)
@ -406,7 +412,6 @@ class LibraryUpdateService(
.map { networkManga ->
manga.copyFrom(networkManga)
db.insertManga(manga).executeAsBlocking()
coverCache.deleteFromCache(manga.thumbnail_url)
MangaImpl.setLastCoverFetch(manga.id!!, Date().time)
manga
}

View File

@ -95,7 +95,6 @@ class MangaInfoPresenter(
manga.copyFrom(networkManga)
manga.initialized = true
db.insertManga(manga).executeAsBlocking()
coverCache.deleteFromCache(manga.thumbnail_url)
MangaImpl.setLastCoverFetch(manga.id!!, Date().time)
manga
}