From bfb0f800ad48ff03b9cf0ddb054f1b7722668259 Mon Sep 17 00:00:00 2001 From: shabinder Date: Mon, 23 Aug 2021 22:51:27 +0530 Subject: [PATCH] Refactoring --- .../shabinder/spotiflyer/service/ForegroundService.kt | 9 +++++++-- .../file_manager/DesktopFileManager.kt | 10 +++++++++- .../file_manager/WebFileManager.kt | 9 +++++++-- .../com/shabinder/common/providers/DesktopActual.kt | 10 ++-------- .../kotlin/com.shabinder.common.providers/WebActual.kt | 9 ++------- 5 files changed, 27 insertions(+), 20 deletions(-) diff --git a/android/src/main/java/com/shabinder/spotiflyer/service/ForegroundService.kt b/android/src/main/java/com/shabinder/spotiflyer/service/ForegroundService.kt index d4c478d8..1a314084 100644 --- a/android/src/main/java/com/shabinder/spotiflyer/service/ForegroundService.kt +++ b/android/src/main/java/com/shabinder/spotiflyer/service/ForegroundService.kt @@ -81,6 +81,7 @@ class ForegroundService : LifecycleService() { inner class DownloadServiceBinder : Binder() { val service get() = this@ForegroundService } + private val myBinder: IBinder = DownloadServiceBinder() override fun onBind(intent: Intent): IBinder { @@ -304,12 +305,16 @@ class ForegroundService : LifecycleService() { override fun onDestroy() { super.onDestroy() - if (isFinished) { killService() } + if (isFinished) { + killService() + } } override fun onTaskRemoved(rootIntent: Intent?) { super.onTaskRemoved(rootIntent) - if (isFinished) { killService() } + if (isFinished) { + killService() + } } companion object { diff --git a/common/core-components/src/desktopMain/kotlin/com.shabinder.common.core_components/file_manager/DesktopFileManager.kt b/common/core-components/src/desktopMain/kotlin/com.shabinder.common.core_components/file_manager/DesktopFileManager.kt index fe80625a..922efcae 100644 --- a/common/core-components/src/desktopMain/kotlin/com.shabinder.common.core_components/file_manager/DesktopFileManager.kt +++ b/common/core-components/src/desktopMain/kotlin/com.shabinder.common.core_components/file_manager/DesktopFileManager.kt @@ -22,12 +22,14 @@ import co.touchlab.kermit.Kermit import com.mpatric.mp3agic.InvalidDataException import com.mpatric.mp3agic.Mp3File import com.shabinder.common.core_components.media_converter.MediaConverter +import com.shabinder.common.core_components.parallel_executor.ParallelExecutor import com.shabinder.common.core_components.picture.Picture import com.shabinder.common.core_components.preference_manager.PreferenceManager import com.shabinder.common.core_components.removeAllTags import com.shabinder.common.core_components.setId3v1Tags import com.shabinder.common.core_components.setId3v2TagsAndSaveFile import com.shabinder.common.database.SpotiFlyerDatabase +import com.shabinder.common.models.DownloadStatus import com.shabinder.common.models.TrackDetails import com.shabinder.common.models.dispatcherIO import com.shabinder.common.models.event.coroutines.SuspendableEvent @@ -36,6 +38,7 @@ import com.shabinder.common.models.event.coroutines.map import com.shabinder.database.Database import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.GlobalScope +import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import org.jetbrains.skija.Image @@ -50,10 +53,15 @@ import java.net.HttpURLConnection import java.net.URL import javax.imageio.ImageIO -actual internal fun fileManagerModule() = module { +internal actual fun fileManagerModule() = module { single { DesktopFileManager(get(), get(), get(), get()) } bind FileManager::class } +val DownloadProgressFlow: MutableSharedFlow> = MutableSharedFlow(1) + +// Scope Allowing 4 Parallel Downloads +val DownloadScope = ParallelExecutor(Dispatchers.IO) + class DesktopFileManager( override val logger: Kermit, override val preferenceManager: PreferenceManager, diff --git a/common/core-components/src/jsMain/kotlin/com.shabinder.common.core_components/file_manager/WebFileManager.kt b/common/core-components/src/jsMain/kotlin/com.shabinder.common.core_components/file_manager/WebFileManager.kt index 76e61865..e99569bd 100644 --- a/common/core-components/src/jsMain/kotlin/com.shabinder.common.core_components/file_manager/WebFileManager.kt +++ b/common/core-components/src/jsMain/kotlin/com.shabinder.common.core_components/file_manager/WebFileManager.kt @@ -17,9 +17,7 @@ package com.shabinder.common.core_components.file_manager import co.touchlab.kermit.Kermit -import com.shabinder.common.core_components.DownloadProgressFlow import com.shabinder.common.core_components.ID3Writer -import com.shabinder.common.core_components.allTracksStatus import com.shabinder.common.core_components.media_converter.MediaConverter import com.shabinder.common.core_components.picture.Picture import com.shabinder.common.core_components.preference_manager.PreferenceManager @@ -34,6 +32,7 @@ import com.shabinder.common.utils.removeIllegalChars import com.shabinder.database.Database import kotlinext.js.Object import kotlinext.js.js +import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.collect import org.khronos.webgl.ArrayBuffer import org.khronos.webgl.Int8Array @@ -142,3 +141,9 @@ class WebFileManager( fun ByteArray.toArrayBuffer(): ArrayBuffer { return this.unsafeCast().buffer } + +val DownloadProgressFlow: MutableSharedFlow> = MutableSharedFlow(1) + +// Error:https://github.com/Kotlin/kotlinx.atomicfu/issues/182 +// val DownloadScope = ParallelExecutor(Dispatchers.Default) //Download Pool of 4 parallel +val allTracksStatus: HashMap = hashMapOf() diff --git a/common/providers/src/desktopMain/kotlin/com/shabinder/common/providers/DesktopActual.kt b/common/providers/src/desktopMain/kotlin/com/shabinder/common/providers/DesktopActual.kt index 6a32e638..388e5305 100644 --- a/common/providers/src/desktopMain/kotlin/com/shabinder/common/providers/DesktopActual.kt +++ b/common/providers/src/desktopMain/kotlin/com/shabinder/common/providers/DesktopActual.kt @@ -16,22 +16,16 @@ package com.shabinder.common.providers +import com.shabinder.common.core_components.file_manager.DownloadProgressFlow +import com.shabinder.common.core_components.file_manager.DownloadScope import com.shabinder.common.core_components.file_manager.FileManager import com.shabinder.common.core_components.file_manager.downloadFile -import com.shabinder.common.core_components.parallel_executor.ParallelExecutor import com.shabinder.common.models.DownloadResult import com.shabinder.common.models.DownloadStatus import com.shabinder.common.models.SpotiFlyerException import com.shabinder.common.models.TrackDetails -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.collect -val DownloadProgressFlow: MutableSharedFlow> = MutableSharedFlow(1) - -// Scope Allowing 4 Parallel Downloads -val DownloadScope = ParallelExecutor(Dispatchers.IO) - actual suspend fun downloadTracks( list: List, fetcher: FetchPlatformQueryResult, diff --git a/common/providers/src/jsMain/kotlin/com.shabinder.common.providers/WebActual.kt b/common/providers/src/jsMain/kotlin/com.shabinder.common.providers/WebActual.kt index d4a49fe6..803e8043 100644 --- a/common/providers/src/jsMain/kotlin/com.shabinder.common.providers/WebActual.kt +++ b/common/providers/src/jsMain/kotlin/com.shabinder.common.providers/WebActual.kt @@ -16,19 +16,14 @@ package com.shabinder.common.providers +import com.shabinder.common.core_components.file_manager.DownloadProgressFlow import com.shabinder.common.core_components.file_manager.FileManager +import com.shabinder.common.core_components.file_manager.allTracksStatus import com.shabinder.common.core_components.file_manager.downloadFile import com.shabinder.common.models.* -import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.collect import kotlinx.coroutines.withContext -val DownloadProgressFlow: MutableSharedFlow> = MutableSharedFlow(1) - -// Error:https://github.com/Kotlin/kotlinx.atomicfu/issues/182 -// val DownloadScope = ParallelExecutor(Dispatchers.Default) //Download Pool of 4 parallel -val allTracksStatus: HashMap = hashMapOf() - actual suspend fun downloadTracks( list: List, fetcher: FetchPlatformQueryResult,