Skip to content

Commit

Permalink
updates to the flow in extensionInstaller
Browse files Browse the repository at this point in the history
from state to shared, also using "tryEmit" less for it
  • Loading branch information
Jays2Kings committed Apr 15, 2023
1 parent 2d0bf56 commit 737ae9b
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
62 changes: 13 additions & 49 deletions app/src/main/java/eu/kanade/tachiyomi/extension/ExtensionManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,15 +55,12 @@ class ExtensionManager(

private val iconMap = mutableMapOf<String, Drawable>()

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.
*/
Expand All @@ -73,16 +69,6 @@ class ExtensionManager(

private var subLanguagesEnabledOnFirstRun = preferences.enabledLanguages().isSet()

/**
* List of the currently installed extensions.
*/
// private var installedExtensions = emptyList<Extension.Installed>()
// 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)
}
Expand Down Expand Up @@ -110,36 +96,9 @@ class ExtensionManager(

private var availableSources = hashMapOf<Long, Extension.AvailableSource>()

/**
* List of the currently available extensions.
*/
// var availableExtensions = emptyList<Extension.Available>()
// 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<Extension.Untrusted>())
val untrustedExtensionsFlow = _untrustedExtensionsFlow.asStateFlow()

/**
* List of the currently untrusted extensions.
*/
// var untrustedExtensions = emptyList<Extension.Untrusted>()
// 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)
Expand Down Expand Up @@ -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))
}

/**
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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),
)
}

/**
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand All @@ -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<Pair<String, ExtensionIntallInfo>>()
val downloadSharedFlow = _downloadsSharedFlow.asSharedFlow()

/** Map of download id to installer session id */
val downloadInstallerMap = hashMapOf<String, Int>()
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
Expand All @@ -106,7 +109,7 @@ internal class ExtensionInstaller(private val context: Context) {
*/
suspend fun downloadAndInstall(url: String, extension: ExtensionManager.ExtensionInfo, scope: CoroutineScope): Flow<ExtensionIntallInfo> {
val pkgName = extension.pkgName
downloadsStateFlow.value

val oldDownload = activeDownloads[pkgName]
if (oldDownload != null) {
deleteDownload(pkgName)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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))
}

/**
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,7 +50,7 @@ class ExtensionBottomPresenter : BaseMigrationPresenter<ExtensionBottomSheet>()
listOf(migrationJob, extensionJob).awaitAll()
}
presenterScope.launch {
extensionManager.downloadRelay.asSharedFlow()
extensionManager.downloadSharedFlow
.collect {
if (it.first.startsWith("Finished")) {
firstLoad = true
Expand Down

0 comments on commit 737ae9b

Please sign in to comment.