auto update category folders after changing default download folder (#320)

This commit is contained in:
AmirHossein Abdolmotallebi 2024-12-26 09:31:15 +03:30 committed by GitHub
parent 4d5a38938c
commit 54c2d603b4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 40 additions and 3 deletions

View File

@ -7,9 +7,11 @@ import ir.amirab.downloader.DownloadSettings
import com.abdownloadmanager.integration.Integration
import com.abdownloadmanager.integration.IntegrationResult
import com.abdownloadmanager.utils.autoremove.RemovedDownloadsFromDiskTracker
import com.abdownloadmanager.utils.category.CategoryManager
import com.abdownloadmanager.utils.proxy.ProxyManager
import ir.amirab.downloader.DownloadManager
import ir.amirab.downloader.monitor.IDownloadMonitor
import ir.amirab.util.flow.withPrevious
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.launchIn
@ -31,6 +33,7 @@ class AppRepository : KoinComponent {
private val downloadMonitor: IDownloadMonitor = downloadSystem.downloadMonitor
private val integration: Integration by inject()
private val removedDownloadsFromDiskTracker: RemovedDownloadsFromDiskTracker by inject()
val categoryManager: CategoryManager by inject()
val speedLimiter = appSettings.speedLimit
val threadCount = appSettings.threadCount
@ -44,6 +47,18 @@ class AppRepository : KoinComponent {
val trackDeletedFilesOnDisk = appSettings.trackDeletedFilesOnDisk
init {
saveLocation
.debounce(500)
.withPrevious()
.onEach { (oldDownloadFolder, newDownloadFolder) ->
if (oldDownloadFolder == null) {
return@onEach
}
categoryManager.updateCategoryFoldersBasedOnDefaultDownloadFolder(
previousDownloadFolder = oldDownloadFolder,
currentDownloadFolder = newDownloadFolder,
)
}.launchIn(scope)
//maybe its better to move this to another place
appSettings.autoStartOnBoot
.debounce(500)

View File

@ -208,6 +208,26 @@ class CategoryManager(
.mapNotNull { it.getDownloadPath() }.contains(folder)
}
@Suppress("NAME_SHADOWING")
fun updateCategoryFoldersBasedOnDefaultDownloadFolder(
previousDownloadFolder: String,
currentDownloadFolder: String,
) {
val previousDownloadFolder = File(previousDownloadFolder).absoluteFile
val currentDownloadFolder = File(currentDownloadFolder).absoluteFile
for (category in getCategories()) {
val categoryPath = File(category.path).absoluteFile
if (categoryPath.startsWith(previousDownloadFolder)) {
val relativePath = categoryPath.relativeTo(previousDownloadFolder)
updateCategory(category.id) {
it.copy(
path = currentDownloadFolder.resolve(relativePath).absolutePath
)
}
}
}
}
companion object {
/**
* Reserved ids for default categories

View File

@ -54,7 +54,7 @@ fun <T> Flow<T>.rest(time: Long, emitLastEmissionWithoutRest: Boolean = false):
fun <T, R> Flow<T>.concurrentMap(
capacity: Int = Channel.BUFFERED,
transformBlock: suspend (T) -> R
transformBlock: suspend (T) -> R,
): Flow<R> {
return flow {
coroutineScope {
@ -222,7 +222,7 @@ fun <T> Flow<T>.chunked(count: Int): Flow<List<T>> = flow {
}
}
fun <T> Flow<T>.onEachLatest(block:suspend (T)->Unit) = transformLatest {
fun <T> Flow<T>.onEachLatest(block: suspend (T) -> Unit) = transformLatest {
block(it)
emit(it)
}
@ -238,3 +238,5 @@ fun <T, R> Flow<T>.withPrevious(
transform(previous, current)
}
}
fun <T> Flow<T>.withPrevious(): Flow<Pair<T?, T>> = withPrevious { previous, current -> previous to current }