mirror of
https://github.com/amir1376/ab-download-manager.git
synced 2025-02-20 11:43:24 +08:00
auto update category folders after changing default download folder (#320)
This commit is contained in:
parent
4d5a38938c
commit
54c2d603b4
@ -7,9 +7,11 @@ import ir.amirab.downloader.DownloadSettings
|
|||||||
import com.abdownloadmanager.integration.Integration
|
import com.abdownloadmanager.integration.Integration
|
||||||
import com.abdownloadmanager.integration.IntegrationResult
|
import com.abdownloadmanager.integration.IntegrationResult
|
||||||
import com.abdownloadmanager.utils.autoremove.RemovedDownloadsFromDiskTracker
|
import com.abdownloadmanager.utils.autoremove.RemovedDownloadsFromDiskTracker
|
||||||
|
import com.abdownloadmanager.utils.category.CategoryManager
|
||||||
import com.abdownloadmanager.utils.proxy.ProxyManager
|
import com.abdownloadmanager.utils.proxy.ProxyManager
|
||||||
import ir.amirab.downloader.DownloadManager
|
import ir.amirab.downloader.DownloadManager
|
||||||
import ir.amirab.downloader.monitor.IDownloadMonitor
|
import ir.amirab.downloader.monitor.IDownloadMonitor
|
||||||
|
import ir.amirab.util.flow.withPrevious
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.flow.debounce
|
import kotlinx.coroutines.flow.debounce
|
||||||
import kotlinx.coroutines.flow.launchIn
|
import kotlinx.coroutines.flow.launchIn
|
||||||
@ -31,6 +33,7 @@ class AppRepository : KoinComponent {
|
|||||||
private val downloadMonitor: IDownloadMonitor = downloadSystem.downloadMonitor
|
private val downloadMonitor: IDownloadMonitor = downloadSystem.downloadMonitor
|
||||||
private val integration: Integration by inject()
|
private val integration: Integration by inject()
|
||||||
private val removedDownloadsFromDiskTracker: RemovedDownloadsFromDiskTracker by inject()
|
private val removedDownloadsFromDiskTracker: RemovedDownloadsFromDiskTracker by inject()
|
||||||
|
val categoryManager: CategoryManager by inject()
|
||||||
|
|
||||||
val speedLimiter = appSettings.speedLimit
|
val speedLimiter = appSettings.speedLimit
|
||||||
val threadCount = appSettings.threadCount
|
val threadCount = appSettings.threadCount
|
||||||
@ -44,6 +47,18 @@ class AppRepository : KoinComponent {
|
|||||||
val trackDeletedFilesOnDisk = appSettings.trackDeletedFilesOnDisk
|
val trackDeletedFilesOnDisk = appSettings.trackDeletedFilesOnDisk
|
||||||
|
|
||||||
init {
|
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
|
//maybe its better to move this to another place
|
||||||
appSettings.autoStartOnBoot
|
appSettings.autoStartOnBoot
|
||||||
.debounce(500)
|
.debounce(500)
|
||||||
|
@ -208,6 +208,26 @@ class CategoryManager(
|
|||||||
.mapNotNull { it.getDownloadPath() }.contains(folder)
|
.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 {
|
companion object {
|
||||||
/**
|
/**
|
||||||
* Reserved ids for default categories
|
* Reserved ids for default categories
|
||||||
|
@ -54,7 +54,7 @@ fun <T> Flow<T>.rest(time: Long, emitLastEmissionWithoutRest: Boolean = false):
|
|||||||
|
|
||||||
fun <T, R> Flow<T>.concurrentMap(
|
fun <T, R> Flow<T>.concurrentMap(
|
||||||
capacity: Int = Channel.BUFFERED,
|
capacity: Int = Channel.BUFFERED,
|
||||||
transformBlock: suspend (T) -> R
|
transformBlock: suspend (T) -> R,
|
||||||
): Flow<R> {
|
): Flow<R> {
|
||||||
return flow {
|
return flow {
|
||||||
coroutineScope {
|
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)
|
block(it)
|
||||||
emit(it)
|
emit(it)
|
||||||
}
|
}
|
||||||
@ -237,4 +237,6 @@ fun <T, R> Flow<T>.withPrevious(
|
|||||||
val current = it[1] as T
|
val current = it[1] as T
|
||||||
transform(previous, current)
|
transform(previous, current)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun <T> Flow<T>.withPrevious(): Flow<Pair<T?, T>> = withPrevious { previous, current -> previous to current }
|
Loading…
x
Reference in New Issue
Block a user