mirror of
https://github.com/mihonapp/mihon.git
synced 2025-11-13 20:48:56 +01:00
Use Flow in ExtensionManager and SourceManager (#7547)
- Replace ExtensionManager relay and observable with Flow
- Inverse SourceManager dependency
- SourceManager observers ExtensionManager flow
- Separate SourceData from SourceRepository as it created a circular dependency
This commit is contained in:
@@ -87,10 +87,10 @@ class AppModule(val app: Application) : InjektModule {
|
||||
|
||||
addSingletonFactory { NetworkHelper(app) }
|
||||
|
||||
addSingletonFactory { SourceManager(app).also { get<ExtensionManager>().init(it) } }
|
||||
|
||||
addSingletonFactory { ExtensionManager(app) }
|
||||
|
||||
addSingletonFactory { SourceManager(app, get(), get()) }
|
||||
|
||||
addSingletonFactory { DownloadManager(app) }
|
||||
|
||||
addSingletonFactory { TrackManager(app) }
|
||||
|
||||
@@ -14,7 +14,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.util.lang.launchNow
|
||||
import eu.kanade.tachiyomi.util.preference.plusAssign
|
||||
import eu.kanade.tachiyomi.util.system.logcat
|
||||
@@ -88,22 +87,23 @@ class ExtensionManager(
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Relay used to notify the available extensions.
|
||||
*/
|
||||
private val availableExtensionsRelay = BehaviorRelay.create<List<Extension.Available>>()
|
||||
|
||||
/**
|
||||
* List of the currently available extensions.
|
||||
*/
|
||||
var availableExtensions = emptyList<Extension.Available>()
|
||||
private set(value) {
|
||||
field = value
|
||||
availableExtensionsRelay.call(value)
|
||||
availableExtensionsFlow.value = field
|
||||
updatedInstalledExtensionsStatuses(value)
|
||||
setupAvailableExtensionsSourcesDataMap(value)
|
||||
}
|
||||
|
||||
private val availableExtensionsFlow = MutableStateFlow(availableExtensions)
|
||||
|
||||
fun getAvailableExtensionsFlow(): StateFlow<List<Extension.Available>> {
|
||||
return availableExtensionsFlow.asStateFlow()
|
||||
}
|
||||
|
||||
private var availableExtensionsSourcesData: Map<Long, SourceData> = mapOf()
|
||||
|
||||
private fun setupAvailableExtensionsSourcesDataMap(extensions: List<Extension.Available>) {
|
||||
@@ -115,30 +115,22 @@ class ExtensionManager(
|
||||
|
||||
fun getSourceData(id: Long) = availableExtensionsSourcesData[id]
|
||||
|
||||
/**
|
||||
* Relay used to notify the untrusted extensions.
|
||||
*/
|
||||
private val untrustedExtensionsRelay = BehaviorRelay.create<List<Extension.Untrusted>>()
|
||||
|
||||
/**
|
||||
* List of the currently untrusted extensions.
|
||||
*/
|
||||
var untrustedExtensions = emptyList<Extension.Untrusted>()
|
||||
private set(value) {
|
||||
field = value
|
||||
untrustedExtensionsRelay.call(value)
|
||||
untrustedExtensionsFlow.value = field
|
||||
}
|
||||
|
||||
/**
|
||||
* The source manager where the sources of the extensions are added.
|
||||
*/
|
||||
private lateinit var sourceManager: SourceManager
|
||||
private val untrustedExtensionsFlow = MutableStateFlow(untrustedExtensions)
|
||||
|
||||
/**
|
||||
* Initializes this manager with the given source manager.
|
||||
*/
|
||||
fun init(sourceManager: SourceManager) {
|
||||
this.sourceManager = sourceManager
|
||||
fun getUntrustedExtensionsFlow(): StateFlow<List<Extension.Untrusted>> {
|
||||
return untrustedExtensionsFlow.asStateFlow()
|
||||
}
|
||||
|
||||
init {
|
||||
initExtensions()
|
||||
ExtensionInstallReceiver(InstallationListener()).register(context)
|
||||
}
|
||||
@@ -152,36 +144,12 @@ class ExtensionManager(
|
||||
installedExtensions = extensions
|
||||
.filterIsInstance<LoadResult.Success>()
|
||||
.map { it.extension }
|
||||
installedExtensions
|
||||
.flatMap { it.sources }
|
||||
.forEach { sourceManager.registerSource(it) }
|
||||
|
||||
untrustedExtensions = extensions
|
||||
.filterIsInstance<LoadResult.Untrusted>()
|
||||
.map { it.extension }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the relay of the installed extensions as an observable.
|
||||
*/
|
||||
fun getInstalledExtensionsObservable(): Observable<List<Extension.Installed>> {
|
||||
return installedExtensionsRelay.asObservable()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the relay of the available extensions as an observable.
|
||||
*/
|
||||
fun getAvailableExtensionsObservable(): Observable<List<Extension.Available>> {
|
||||
return availableExtensionsRelay.asObservable()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the relay of the untrusted extensions as an observable.
|
||||
*/
|
||||
fun getUntrustedExtensionsObservable(): Observable<List<Extension.Untrusted>> {
|
||||
return untrustedExtensionsRelay.asObservable()
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds the available extensions in the [api] and updates [availableExtensions].
|
||||
*/
|
||||
@@ -324,7 +292,6 @@ class ExtensionManager(
|
||||
*/
|
||||
private fun registerNewExtension(extension: Extension.Installed) {
|
||||
installedExtensions += extension
|
||||
extension.sources.forEach { sourceManager.registerSource(it) }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -338,11 +305,9 @@ class ExtensionManager(
|
||||
val oldExtension = mutInstalledExtensions.find { it.pkgName == extension.pkgName }
|
||||
if (oldExtension != null) {
|
||||
mutInstalledExtensions -= oldExtension
|
||||
extension.sources.forEach { sourceManager.unregisterSource(it) }
|
||||
}
|
||||
mutInstalledExtensions += extension
|
||||
installedExtensions = mutInstalledExtensions
|
||||
extension.sources.forEach { sourceManager.registerSource(it) }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -355,7 +320,6 @@ class ExtensionManager(
|
||||
val installedExtension = installedExtensions.find { it.pkgName == pkgName }
|
||||
if (installedExtension != null) {
|
||||
installedExtensions -= installedExtension
|
||||
installedExtension.sources.forEach { sourceManager.unregisterSource(it) }
|
||||
}
|
||||
val untrustedExtension = untrustedExtensions.find { it.pkgName == pkgName }
|
||||
if (untrustedExtension != null) {
|
||||
|
||||
@@ -1,42 +1,72 @@
|
||||
package eu.kanade.tachiyomi.source
|
||||
|
||||
import android.content.Context
|
||||
import eu.kanade.domain.source.interactor.GetSourceData
|
||||
import eu.kanade.domain.source.interactor.UpsertSourceData
|
||||
import eu.kanade.domain.source.model.SourceData
|
||||
import eu.kanade.domain.source.repository.SourceDataRepository
|
||||
import eu.kanade.tachiyomi.R
|
||||
import eu.kanade.tachiyomi.extension.ExtensionManager
|
||||
import eu.kanade.tachiyomi.source.model.Page
|
||||
import eu.kanade.tachiyomi.source.model.SChapter
|
||||
import eu.kanade.tachiyomi.source.model.SManga
|
||||
import eu.kanade.tachiyomi.source.online.HttpSource
|
||||
import eu.kanade.tachiyomi.util.lang.launchIO
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import rx.Observable
|
||||
import tachiyomi.source.model.ChapterInfo
|
||||
import tachiyomi.source.model.MangaInfo
|
||||
import uy.kohesive.injekt.injectLazy
|
||||
|
||||
class SourceManager(private val context: Context) {
|
||||
class SourceManager(
|
||||
private val context: Context,
|
||||
private val extensionManager: ExtensionManager,
|
||||
private val sourceRepository: SourceDataRepository,
|
||||
) {
|
||||
|
||||
private val extensionManager: ExtensionManager by injectLazy()
|
||||
private val getSourceData: GetSourceData by injectLazy()
|
||||
private val upsertSourceData: UpsertSourceData by injectLazy()
|
||||
private val scope = CoroutineScope(Job() + Dispatchers.IO)
|
||||
|
||||
private var sourcesMap = emptyMap<Long, Source>()
|
||||
set(value) {
|
||||
field = value
|
||||
sourcesMapFlow.value = field
|
||||
}
|
||||
|
||||
private val sourcesMapFlow = MutableStateFlow(sourcesMap)
|
||||
|
||||
private val sourcesMap = mutableMapOf<Long, Source>()
|
||||
private val stubSourcesMap = mutableMapOf<Long, StubSource>()
|
||||
|
||||
private val _catalogueSources: MutableStateFlow<List<CatalogueSource>> = MutableStateFlow(listOf())
|
||||
val catalogueSources: Flow<List<CatalogueSource>> = _catalogueSources
|
||||
val onlineSources: Flow<List<HttpSource>> =
|
||||
_catalogueSources.map { sources -> sources.filterIsInstance<HttpSource>() }
|
||||
val catalogueSources: Flow<List<CatalogueSource>> = sourcesMapFlow.map { it.values.filterIsInstance<CatalogueSource>() }
|
||||
val onlineSources: Flow<List<HttpSource>> = catalogueSources.map { sources -> sources.filterIsInstance<HttpSource>() }
|
||||
|
||||
init {
|
||||
createInternalSources().forEach { registerSource(it) }
|
||||
scope.launch {
|
||||
extensionManager.getInstalledExtensionsFlow()
|
||||
.collectLatest { extensions ->
|
||||
val mutableMap = mutableMapOf<Long, Source>(LocalSource.ID to LocalSource(context))
|
||||
extensions.forEach { extension ->
|
||||
extension.sources.forEach {
|
||||
mutableMap[it.id] = it
|
||||
registerStubSource(it.toSourceData())
|
||||
}
|
||||
}
|
||||
sourcesMap = mutableMap
|
||||
}
|
||||
}
|
||||
|
||||
scope.launch {
|
||||
sourceRepository.subscribeAll()
|
||||
.collectLatest { sources ->
|
||||
val mutableMap = stubSourcesMap.toMutableMap()
|
||||
sources.forEach {
|
||||
mutableMap[it.id] = StubSource(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun get(sourceKey: Long): Source? {
|
||||
@@ -58,44 +88,15 @@ class SourceManager(private val context: Context) {
|
||||
return stubSourcesMap.values.filterNot { it.id in onlineSourceIds }
|
||||
}
|
||||
|
||||
internal fun registerSource(source: Source) {
|
||||
if (!sourcesMap.containsKey(source.id)) {
|
||||
sourcesMap[source.id] = source
|
||||
}
|
||||
registerStubSource(source.toSourceData())
|
||||
triggerCatalogueSources()
|
||||
}
|
||||
|
||||
private fun registerStubSource(sourceData: SourceData) {
|
||||
launchIO {
|
||||
val dbSourceData = getSourceData.await(sourceData.id)
|
||||
|
||||
if (dbSourceData != sourceData) {
|
||||
upsertSourceData.await(sourceData)
|
||||
}
|
||||
if (stubSourcesMap[sourceData.id]?.toSourceData() != sourceData) {
|
||||
stubSourcesMap[sourceData.id] = StubSource(sourceData)
|
||||
}
|
||||
scope.launch {
|
||||
val (id, lang, name) = sourceData
|
||||
sourceRepository.upsertSourceData(id, lang, name)
|
||||
}
|
||||
}
|
||||
|
||||
internal fun unregisterSource(source: Source) {
|
||||
sourcesMap.remove(source.id)
|
||||
triggerCatalogueSources()
|
||||
}
|
||||
|
||||
private fun triggerCatalogueSources() {
|
||||
_catalogueSources.update {
|
||||
sourcesMap.values.filterIsInstance<CatalogueSource>()
|
||||
}
|
||||
}
|
||||
|
||||
private fun createInternalSources(): List<Source> = listOf(
|
||||
LocalSource(context),
|
||||
)
|
||||
|
||||
private suspend fun createStubSource(id: Long): StubSource {
|
||||
getSourceData.await(id)?.let {
|
||||
sourceRepository.getSourceData(id)?.let {
|
||||
return StubSource(it)
|
||||
}
|
||||
extensionManager.getSourceData(id)?.let {
|
||||
|
||||
Reference in New Issue
Block a user