Rename source download folder on source rename (#7898)

* Rename source download folder on source rename

* Review Changes

* Review Changes 2
This commit is contained in:
AntsyLich 2022-08-30 23:15:34 +06:00 committed by GitHub
parent fc6946ed61
commit 53f5ea7fe9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View File

@ -343,6 +343,30 @@ class DownloadManager(
} }
} }
/**
* Renames source download folder
*
* @param oldSource the old source.
* @param newSource the new source.
*/
fun renameSource(oldSource: Source, newSource: Source) {
val oldFolder = provider.findSourceDir(oldSource) ?: return
val newName = provider.getSourceDirName(newSource)
val capitalizationChanged = oldFolder.name.equals(newName, ignoreCase = true)
if (capitalizationChanged) {
val tempName = newName + "_tmp"
if (oldFolder.renameTo(tempName).not()) {
logcat(LogPriority.ERROR) { "Could not rename source download folder: ${oldFolder.name}." }
return
}
}
if (oldFolder.renameTo(newName).not()) {
logcat(LogPriority.ERROR) { "Could not rename source download folder: ${oldFolder.name}." }
}
}
/** /**
* Renames an already downloaded chapter * Renames an already downloaded chapter
* *

View File

@ -4,6 +4,7 @@ import android.content.Context
import eu.kanade.domain.source.model.SourceData import eu.kanade.domain.source.model.SourceData
import eu.kanade.domain.source.repository.SourceDataRepository import eu.kanade.domain.source.repository.SourceDataRepository
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.download.DownloadManager
import eu.kanade.tachiyomi.extension.ExtensionManager import eu.kanade.tachiyomi.extension.ExtensionManager
import eu.kanade.tachiyomi.source.model.Page import eu.kanade.tachiyomi.source.model.Page
import eu.kanade.tachiyomi.source.model.SChapter import eu.kanade.tachiyomi.source.model.SChapter
@ -19,12 +20,14 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import rx.Observable import rx.Observable
import uy.kohesive.injekt.injectLazy
class SourceManager( class SourceManager(
private val context: Context, private val context: Context,
private val extensionManager: ExtensionManager, private val extensionManager: ExtensionManager,
private val sourceRepository: SourceDataRepository, private val sourceRepository: SourceDataRepository,
) { ) {
private val downloadManager: DownloadManager by injectLazy()
private val scope = CoroutineScope(Job() + Dispatchers.IO) private val scope = CoroutineScope(Job() + Dispatchers.IO)
@ -89,7 +92,12 @@ class SourceManager(
private fun registerStubSource(sourceData: SourceData) { private fun registerStubSource(sourceData: SourceData) {
scope.launch { scope.launch {
val (id, lang, name) = sourceData val (id, lang, name) = sourceData
val dbSourceData = sourceRepository.getSourceData(id)
if (dbSourceData == sourceData) return@launch
sourceRepository.upsertSourceData(id, lang, name) sourceRepository.upsertSourceData(id, lang, name)
if (dbSourceData != null) {
downloadManager.renameSource(StubSource(dbSourceData), StubSource(sourceData))
}
} }
} }