Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import io.github.landwarderer.futon.core.ui.util.ReversibleHandle
import io.github.landwarderer.futon.core.util.ext.flattenLatest
import io.github.landwarderer.futon.mihon.MihonExtensionManager
import io.github.landwarderer.futon.mihon.model.MihonMangaSource
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.channels.trySendBlocking
import kotlinx.coroutines.flow.Flow
Expand All @@ -32,7 +35,9 @@ import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
import org.koitharu.kotatsu.parsers.model.ContentType
import org.koitharu.kotatsu.parsers.model.MangaParserSource
Expand All @@ -54,10 +59,20 @@ class MangaSourcesRepository @Inject constructor(
private val mihonExtensionManager: MihonExtensionManager,
) {

private val repositoryScope = CoroutineScope(SupervisorJob() + Dispatchers.IO)
private val isNewSourcesAssimilated = AtomicBoolean(false)
private val dao: MangaSourcesDao
get() = db.getSourcesDao()

init {
mihonExtensionManager.installedExtensions
.onEach {
isNewSourcesAssimilated.set(false)
assimilateNewSources()
}
.launchIn(repositoryScope)
}

val allMangaSources: Set<MangaParserSource> = Collections.unmodifiableSet(
EnumSet.noneOf<MangaParserSource>(MangaParserSource::class.java).also {
MangaParserSource.entries.filterNotTo(it, MangaParserSource::isBroken)
Expand All @@ -69,7 +84,8 @@ class MangaSourcesRepository @Inject constructor(
val order = settings.sourcesSortOrder
return dao.findAll(!settings.isAllSourcesEnabled, order).toSources(settings.isNsfwContentDisabled, order)
.let { enabled ->
val external = getExternalSources()
val enabledNames = enabled.mapToSet { it.name }
val external = getExternalSources().filterNot { it.name in enabledNames }
val list = ArrayList<MangaSourceInfo>(enabled.size + external.size)
external.mapTo(list) { MangaSourceInfo(it, isEnabled = true, isPinned = true) }
list.addAll(enabled)
Expand Down Expand Up @@ -225,8 +241,10 @@ class MangaSourcesRepository @Inject constructor(
}.flattenLatest()
.onStart { assimilateNewSources() }
.combine(observeExternalSources()) { enabled, external ->
val list = ArrayList<MangaSourceInfo>(enabled.size + external.size)
external.mapTo(list) { MangaSourceInfo(it, isEnabled = true, isPinned = true) }
val enabledNames = enabled.mapToSet { it.name }
val newExternal = external.filterNot { it.name in enabledNames }
val list = ArrayList<MangaSourceInfo>(enabled.size + newExternal.size)
newExternal.mapTo(list) { MangaSourceInfo(it, isEnabled = true, isPinned = true) }
list.addAll(enabled)
list
}
Expand Down Expand Up @@ -313,11 +331,11 @@ class MangaSourcesRepository @Inject constructor(
val entities = new.map { x ->
MangaSourceEntity(
source = x.name,
isEnabled = isAllEnabled,
isEnabled = if (x is MihonMangaSource) true else isAllEnabled,
sortKey = ++maxSortKey,
addedIn = BuildConfig.VERSION_CODE,
lastUsedAt = 0,
isPinned = false,
isPinned = x is MihonMangaSource,
cfState = CloudFlareHelper.PROTECTION_NOT_DETECTED,
title = x.getTitle(context),
)
Expand Down Expand Up @@ -422,16 +440,18 @@ class MangaSourcesRepository @Inject constructor(
}

fun getExternalSources(): List<MangaSource> {
val external = context.packageManager.queryIntentContentProviders(
return context.packageManager.queryIntentContentProviders(
Intent("app.futon.parser.PROVIDE_MANGA"), 0,
).map { resolveInfo ->
ExternalMangaSource(
packageName = resolveInfo.providerInfo.packageName,
authority = resolveInfo.providerInfo.authority,
)
}
val mihon = mihonExtensionManager.getMihonMangaSources()
return external + mihon
}

fun getMihonSources(): List<MangaSource> {
return mihonExtensionManager.getMihonMangaSources()
}

private fun List<MangaSourceEntity>.toSources(
Expand All @@ -448,7 +468,7 @@ class MangaSourcesRepository @Inject constructor(
if (source.isBroken) {
continue
}
if (source is MangaParserSource || source.name.startsWith("mihon:") || source.name.startsWith("MIHON_")) {
if (source is MangaParserSource || source is MihonMangaSource) {
result.add(
MangaSourceInfo(
mangaSource = source,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ class SuggestionsWorker @AssistedInject constructor(
if (appSettings.isSuggestionsIncludeDisabledSources) {
val result = sourcesRepository.allMangaSources.toMutableList<MangaSource>()
result.addAll(sourcesRepository.getExternalSources())
result.addAll(sourcesRepository.getMihonSources())
result.shuffle()
result.sortWith(compareBy(nullsLast(LocaleComparator())) { it.getLocale() })
return result
Expand Down
Loading