From 737ae9b035668b300d971c35831a21c3088b4088 Mon Sep 17 00:00:00 2001 From: Jays2Kings Date: Tue, 28 Mar 2023 14:47:11 -0400 Subject: [PATCH] updates to the flow in extensionInstaller from state to shared, also using "tryEmit" less for it --- .../extension/ExtensionInstallerJob.kt | 2 +- .../tachiyomi/extension/ExtensionManager.kt | 62 ++++--------------- .../extension/util/ExtensionInstaller.kt | 30 +++++---- .../ui/extension/ExtensionBottomPresenter.kt | 3 +- 4 files changed, 32 insertions(+), 65 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionInstallerJob.kt b/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionInstallerJob.kt index de2294db7faa..3a06f3a1abdc 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionInstallerJob.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionInstallerJob.kt @@ -136,7 +136,7 @@ class ExtensionInstallerJob(val context: Context, workerParams: WorkerParameters activeInstalls.forEach { extensionManager.cleanUpInstallation(it) } activeInstalls.clear() - extensionManager.downloadRelay.tryEmit("Finished" to (InstallStep.Installed to null)) + extensionManager.emitToInstaller("Finished", (InstallStep.Installed to null)) if (instance?.get() == this) { instance = null } diff --git a/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt b/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt index 1e4fe97c19e5..f6477ba2bf9e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt @@ -15,7 +15,6 @@ import eu.kanade.tachiyomi.extension.util.ExtensionInstallReceiver import eu.kanade.tachiyomi.extension.util.ExtensionInstaller import eu.kanade.tachiyomi.extension.util.ExtensionLoader import eu.kanade.tachiyomi.source.Source -import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.ui.extension.ExtensionIntallInfo import eu.kanade.tachiyomi.util.system.launchNow import kotlinx.coroutines.CoroutineScope @@ -56,15 +55,12 @@ class ExtensionManager( private val iconMap = mutableMapOf() - val downloadRelay - get() = installer.downloadsStateFlow + val downloadSharedFlow = installer.downloadSharedFlow private fun getExtension(downloadId: Long): String? { return installer.activeDownloads.entries.find { downloadId == it.value }?.key } - fun getActiveInstalls(): Int = installer.activeDownloads.size - /** * Relay used to notify the installed extensions. */ @@ -73,16 +69,6 @@ class ExtensionManager( private var subLanguagesEnabledOnFirstRun = preferences.enabledLanguages().isSet() - /** - * List of the currently installed extensions. - */ -// private var installedExtensions = emptyList() -// set(value) { -// field = value -// installedExtensionsRelay.call(value) -// downloadRelay.tryEmit("Finished/Installed/${value.size}" to (InstallStep.Done to null)) -// } - fun getAppIconForSource(source: Source): Drawable? { return getAppIconForSource(source.id) } @@ -110,36 +96,9 @@ class ExtensionManager( private var availableSources = hashMapOf() - /** - * List of the currently available extensions. - */ -// var availableExtensions = emptyList() -// private set(value) { -// field = value -// availableExtensionsRelay.call(value) -// updatedInstalledExtensionsStatuses(value) -// downloadRelay.tryEmit("Finished/Available/${value.size}" to (InstallStep.Done to null)) -// setupAvailableSourcesMap() -// } - private val _untrustedExtensionsFlow = MutableStateFlow(emptyList()) val untrustedExtensionsFlow = _untrustedExtensionsFlow.asStateFlow() - /** - * List of the currently untrusted extensions. - */ -// var untrustedExtensions = emptyList() -// private set(value) { -// field = value -// untrustedExtensionsRelay.call(value) -// downloadRelay.tryEmit("Finished/Untrusted/${value.size}" to (InstallStep.Done to null)) -// } - - /** - * The source manager where the sources of the extensions are added. - */ - private lateinit var sourceManager: SourceManager - init { initExtensions() ExtensionInstallReceiver(InstallationListener()).register(context) @@ -180,7 +139,7 @@ class ExtensionManager( _availableExtensionsFlow.value = extensions updatedInstalledExtensionsStatuses(extensions) setupAvailableSourcesMap() - downloadRelay.tryEmit("Finished/Available/${extensions.size}" to (InstallStep.Done to null)) + emitToInstaller("Finished/Available/${extensions.size}", (InstallStep.Done to null)) } /** @@ -304,10 +263,6 @@ class ExtensionManager( */ fun setInstalling(downloadId: Long, sessionId: Int) { val pkgName = getExtension(downloadId) ?: return - setInstalling(pkgName, sessionId) - } - - fun setInstalling(pkgName: String, sessionId: Int) { installer.setInstalling(pkgName, sessionId) } @@ -386,7 +341,10 @@ class ExtensionManager( */ private fun registerNewExtension(extension: Extension.Installed) { _installedExtensionsFlow.value += extension - downloadRelay.tryEmit("Finished/${extension.pkgName}" to ExtensionIntallInfo(InstallStep.Installed, null)) + emitToInstaller( + "Finished/${extension.pkgName}", + ExtensionIntallInfo(InstallStep.Installed, null), + ) } /** @@ -403,9 +361,15 @@ class ExtensionManager( } mutInstalledExtensions += extension _installedExtensionsFlow.value = mutInstalledExtensions - downloadRelay.tryEmit("Finished/${extension.pkgName}" to ExtensionIntallInfo(InstallStep.Installed, null)) + emitToInstaller( + "Finished/${extension.pkgName}", + ExtensionIntallInfo(InstallStep.Installed, null), + ) } + fun emitToInstaller(name: String, extensionInfo: ExtensionIntallInfo) = + installer.emitToFlow(name, extensionInfo) + /** * Unregisters the extension in this and the source managers given its package name. Note this * method is called for every uninstalled application in the system. diff --git a/app/src/main/java/eu/kanade/tachiyomi/extension/util/ExtensionInstaller.kt b/app/src/main/java/eu/kanade/tachiyomi/extension/util/ExtensionInstaller.kt index 6a95fc1bcab2..c702e6cec47e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/extension/util/ExtensionInstaller.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/extension/util/ExtensionInstaller.kt @@ -24,9 +24,9 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.SupervisorJob import kotlinx.coroutines.delay import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.MutableSharedFlow +import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.catch -import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.emptyFlow import kotlinx.coroutines.flow.filter @@ -77,7 +77,7 @@ internal class ExtensionInstaller(private val context: Context) { it.onDestroy() ioScope.launch { delay(500) - downloadsStateFlow.emit("Finished" to (InstallStep.Installed to null)) + _downloadsSharedFlow.emit("Finished" to (InstallStep.Installed to null)) } installer = null } @@ -92,10 +92,13 @@ internal class ExtensionInstaller(private val context: Context) { /** * StateFlow used to notify the installation step of every download. */ - val downloadsStateFlow = MutableStateFlow("" to ExtensionIntallInfo(InstallStep.Pending, null)) + private val _downloadsSharedFlow = MutableSharedFlow>() + val downloadSharedFlow = _downloadsSharedFlow.asSharedFlow() /** Map of download id to installer session id */ val downloadInstallerMap = hashMapOf() + fun emitToFlow(name: String, extensionInfo: ExtensionIntallInfo) = + ioScope.launch { _downloadsSharedFlow.emit(name to extensionInfo) } /** * Adds the given extension to the downloads queue and returns a flow containing its @@ -106,7 +109,7 @@ internal class ExtensionInstaller(private val context: Context) { */ suspend fun downloadAndInstall(url: String, extension: ExtensionManager.ExtensionInfo, scope: CoroutineScope): Flow { val pkgName = extension.pkgName - downloadsStateFlow.value + val oldDownload = activeDownloads[pkgName] if (oldDownload != null) { deleteDownload(pkgName) @@ -155,11 +158,11 @@ internal class ExtensionInstaller(private val context: Context) { deleteDownload(pkgName) } .collect { - downloadsStateFlow.emit(extension.pkgName to it) + _downloadsSharedFlow.emit(extension.pkgName to it) } } - return downloadsStateFlow.filter { it.first == extension.pkgName }.map { it.second } + return _downloadsSharedFlow.filter { it.first == extension.pkgName }.map { it.second } .flowOn(Dispatchers.IO) .transformWhile { emit(it) @@ -293,15 +296,16 @@ internal class ExtensionInstaller(private val context: Context) { /** * Sets the result of the installation of an extension. * - * @param downloadId The id of the download. + * @param pkgName the name of the package being installed + * @param sessionId The id of the package manager's session or other installer. */ fun setInstalling(pkgName: String, sessionId: Int) { - downloadsStateFlow.tryEmit(pkgName to ExtensionIntallInfo(InstallStep.Installing, null)) + emitToFlow(pkgName, ExtensionIntallInfo(InstallStep.Installing, null)) downloadInstallerMap[pkgName] = sessionId } suspend fun setPending(pkgName: String) { - downloadsStateFlow.emit(pkgName to ExtensionIntallInfo(InstallStep.Pending, null)) + _downloadsSharedFlow.emit(pkgName to ExtensionIntallInfo(InstallStep.Pending, null)) } fun cancelInstallation(sessionId: Int) { @@ -329,7 +333,7 @@ internal class ExtensionInstaller(private val context: Context) { fun setInstallationResult(pkgName: String, result: Boolean) { val step = if (result) InstallStep.Installed else InstallStep.Error downloadInstallerMap.remove(pkgName) - downloadsStateFlow.tryEmit(pkgName to ExtensionIntallInfo(step, null)) + emitToFlow(pkgName, ExtensionIntallInfo(step, null)) } /** @@ -394,10 +398,10 @@ internal class ExtensionInstaller(private val context: Context) { val pkgName = activeDownloads.entries.find { id == it.value }?.key // Set next installation step if (uri != null && pkgName != null) { - downloadsStateFlow.tryEmit(pkgName to ExtensionIntallInfo(InstallStep.Loading, null)) + emitToFlow(pkgName, ExtensionIntallInfo(InstallStep.Loading, null)) } else if (pkgName != null) { Timber.e("Couldn't locate downloaded APK") - downloadsStateFlow.tryEmit(pkgName to ExtensionIntallInfo(InstallStep.Error, null)) + emitToFlow(pkgName, ExtensionIntallInfo(InstallStep.Error, null)) return } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionBottomPresenter.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionBottomPresenter.kt index b1f33f733f2d..8c2f361b6ecb 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionBottomPresenter.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/extension/ExtensionBottomPresenter.kt @@ -13,7 +13,6 @@ import eu.kanade.tachiyomi.util.system.withUIContext import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll -import kotlinx.coroutines.flow.asSharedFlow import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -51,7 +50,7 @@ class ExtensionBottomPresenter : BaseMigrationPresenter() listOf(migrationJob, extensionJob).awaitAll() } presenterScope.launch { - extensionManager.downloadRelay.asSharedFlow() + extensionManager.downloadSharedFlow .collect { if (it.first.startsWith("Finished")) { firstLoad = true