create IDownloadMonitor.kt interface

This commit is contained in:
AmirHossein Abdolmotallebi 2024-08-18 05:07:12 +03:30
parent 90ab43449e
commit b77c2f4cce
8 changed files with 38 additions and 22 deletions

View File

@ -34,6 +34,7 @@ import org.koin.dsl.bind
import org.koin.dsl.module
import com.abdownloadmanager.updatechecker.DummyUpdateChecker
import com.abdownloadmanager.updatechecker.UpdateChecker
import ir.amirab.downloader.monitor.IDownloadMonitor
val downloaderModule = module {
single<IDownloadQueueDatabase> {
@ -93,7 +94,7 @@ val downloaderModule = module {
single {
DownloadManager(get(), get(), get(), get(), get())
}.bind(DownloadManagerMinimalControl::class)
single {
single<IDownloadMonitor> {
DownloadMonitor(get())
}
single {

View File

@ -8,8 +8,8 @@ import ir.amirab.util.flow.mapStateFlow
import com.abdownloadmanager.desktop.utils.newScopeBasedOn
import androidx.compose.runtime.toMutableStateList
import com.arkivanov.decompose.ComponentContext
import ir.amirab.downloader.monitor.DownloadMonitor
import ir.amirab.downloader.monitor.IDownloadItemState
import ir.amirab.downloader.monitor.IDownloadMonitor
import ir.amirab.downloader.queue.DownloadQueue
import ir.amirab.downloader.queue.QueueManager
import kotlinx.coroutines.CoroutineScope
@ -22,7 +22,7 @@ class QueueInfoComponent(
id: Long,
) : BaseComponent(ctx),
KoinComponent {
private val downloadMonitor: DownloadMonitor by inject()
private val downloadMonitor: IDownloadMonitor by inject()
private val queueManager: QueueManager by inject()
val downloadQueue = queueManager.queues.value.find {
it.id == id

View File

@ -9,7 +9,6 @@ import com.arkivanov.decompose.ComponentContext
import com.arkivanov.decompose.router.slot.SlotNavigation
import com.arkivanov.decompose.router.slot.childSlot
import com.arkivanov.decompose.router.slot.navigate
import ir.amirab.downloader.monitor.DownloadMonitor
import ir.amirab.downloader.queue.QueueManager
import kotlinx.coroutines.flow.*
import kotlinx.coroutines.launch
@ -25,7 +24,6 @@ class QueuesComponent(
ContainsEffects<QueuesComponentEffects> by supportEffects(),
KoinComponent {
val queueManager: QueueManager by inject()
val downloadMonitor: DownloadMonitor by inject()
private val queues = queueManager.queues

View File

@ -41,7 +41,7 @@ class SingleDownloadComponent(
ContainsEffects<SingleDownloadEffects> by supportEffects(),
KoinComponent {
private val downloadSystem: DownloadSystem by inject()
private val downloadMonitor: DownloadMonitor = downloadSystem.downloadMonitor
private val downloadMonitor: IDownloadMonitor = downloadSystem.downloadMonitor
private val downloadManager: DownloadManager = downloadSystem.downloadManager
val itemStateFlow = downloadMonitor.downloadListFlow.map {
it.firstOrNull { it.id == downloadId }

View File

@ -4,10 +4,10 @@ import com.abdownloadmanager.desktop.storage.AppSettingsStorage
import com.abdownloadmanager.desktop.utils.AutoStartManager
import com.abdownloadmanager.desktop.utils.DownloadSystem
import ir.amirab.downloader.DownloadSettings
import ir.amirab.downloader.monitor.DownloadMonitor
import com.abdownloadmanager.integration.Integration
import com.abdownloadmanager.integration.IntegrationResult
import ir.amirab.downloader.DownloadManager
import ir.amirab.downloader.monitor.IDownloadMonitor
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.launchIn
@ -25,7 +25,7 @@ class AppRepository : KoinComponent {
private val downloadSystem : DownloadSystem by inject()
private val downloadSettings: DownloadSettings by inject()
private val downloadManager: DownloadManager = downloadSystem.downloadManager
private val downloadMonitor: DownloadMonitor = downloadSystem.downloadMonitor
private val downloadMonitor: IDownloadMonitor = downloadSystem.downloadMonitor
private val integration: Integration by inject()
val speedLimiter = appSettings.speedLimit

View File

@ -10,7 +10,7 @@ import ir.amirab.downloader.downloaditem.contexts.RemovedBy
import ir.amirab.downloader.downloaditem.contexts.ResumedBy
import ir.amirab.downloader.downloaditem.contexts.StoppedBy
import ir.amirab.downloader.downloaditem.contexts.User
import ir.amirab.downloader.monitor.DownloadMonitor
import ir.amirab.downloader.monitor.IDownloadMonitor
import ir.amirab.downloader.queue.QueueManager
import ir.amirab.downloader.utils.OnDuplicateStrategy
import kotlinx.coroutines.CoroutineScope
@ -26,7 +26,7 @@ import java.io.File
class DownloadSystem(
val downloadManager: DownloadManager,
val queueManager: QueueManager,
val downloadMonitor: DownloadMonitor,
val downloadMonitor: IDownloadMonitor,
private val scope: CoroutineScope,
private val downloadListDB: IDownloadListDb,
private val foldersRegistry: DownloadFoldersRegistry,

View File

@ -16,11 +16,11 @@ import kotlinx.coroutines.launch
class DownloadMonitor(
private val downloadManager: DownloadManager,
) {
) : IDownloadMonitor {
private val scope = CoroutineScope(SupervisorJob())
private var avSpeedCollectorJob: Job? = null
var useAverageSpeed = false
override var useAverageSpeed = false
set(value) {
if (value == field) return
field = value
@ -35,9 +35,9 @@ class DownloadMonitor(
null
}
}
val activeDownloadListFlow = MutableStateFlow<List<ProcessingDownloadItemState>>(emptyList())
val completedDownloadListFlow = MutableStateFlow<List<CompletedDownloadItemState>>(emptyList())
val downloadListFlow: Flow<List<IDownloadItemState>> =
override val activeDownloadListFlow = MutableStateFlow<List<ProcessingDownloadItemState>>(emptyList())
override val completedDownloadListFlow = MutableStateFlow<List<CompletedDownloadItemState>>(emptyList())
override val downloadListFlow: Flow<List<IDownloadItemState>> =
combine(activeDownloadListFlow, completedDownloadListFlow) { a, b -> a + b }
init {
@ -72,9 +72,9 @@ class DownloadMonitor(
}
val downloadSpeedFlow = MutableStateFlow<Map<Long, Long>>(emptyMap())
private val downloadSpeedFlow = MutableStateFlow<Map<Long, Long>>(emptyMap())
val averageDownloadSpeedFlow = downloadSpeedFlow
private val averageDownloadSpeedFlow = downloadSpeedFlow
.saved(5)
.map { lastStats ->
val last = lastStats.lastOrNull() ?: return@map emptyMap()
@ -85,7 +85,7 @@ class DownloadMonitor(
}
}.stateIn(scope, SharingStarted.WhileSubscribed(), emptyMap())
var speedMeterJob: Job? = null
private var speedMeterJob: Job? = null
private fun startSpeedMeter() {
speedMeterJob?.cancel()
speedMeterJob = scope.launch {
@ -119,7 +119,7 @@ class DownloadMonitor(
}
fun getPreferedSpeedFlow(): StateFlow<Map<Long, Long>> {
private fun getPreferedSpeedFlow(): StateFlow<Map<Long, Long>> {
return when {
useAverageSpeed -> averageDownloadSpeedFlow
else -> downloadSpeedFlow
@ -127,7 +127,7 @@ class DownloadMonitor(
}
fun getSpeedOf(id: Long): Long {
private fun getSpeedOf(id: Long): Long {
val speed = getPreferedSpeedFlow().value.getOrElse(id) { -1 }
// println("speed of $id is $speed")
return speed
@ -250,7 +250,7 @@ class DownloadMonitor(
}
val activeDownloadCount = downloadManager.listOfJobsEvents.map {
override val activeDownloadCount = downloadManager.listOfJobsEvents.map {
downloadManager.getActiveCount()
}.stateIn(
scope,
@ -258,7 +258,7 @@ class DownloadMonitor(
downloadManager.getActiveCount()
)
suspend fun waitForDownloadToFinishOrCancel(
override suspend fun waitForDownloadToFinishOrCancel(
id: Long
): Boolean {
val event = downloadManager

View File

@ -0,0 +1,17 @@
package ir.amirab.downloader.monitor
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
interface IDownloadMonitor {
var useAverageSpeed: Boolean
val activeDownloadListFlow: MutableStateFlow<List<ProcessingDownloadItemState>>
val completedDownloadListFlow: MutableStateFlow<List<CompletedDownloadItemState>>
val downloadListFlow: Flow<List<IDownloadItemState>>
val activeDownloadCount: StateFlow<Int>
suspend fun waitForDownloadToFinishOrCancel(
id: Long
): Boolean
}