mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-21 20:47:03 -05:00
parent
fc184f1cfa
commit
cac80daa71
5 changed files with 15 additions and 20 deletions
|
@ -7,11 +7,11 @@ class NetworkToLocalManga(
|
|||
private val mangaRepository: MangaRepository,
|
||||
) {
|
||||
|
||||
suspend fun await(manga: Manga, sourceId: Long): Manga {
|
||||
val localManga = getManga(manga.url, sourceId)
|
||||
suspend fun await(manga: Manga): Manga {
|
||||
val localManga = getManga(manga.url, manga.source)
|
||||
return when {
|
||||
localManga == null -> {
|
||||
val id = insertManga(manga, sourceId)
|
||||
val id = insertManga(manga)
|
||||
manga.copy(id = id!!)
|
||||
}
|
||||
!localManga.favorite -> {
|
||||
|
@ -29,7 +29,7 @@ class NetworkToLocalManga(
|
|||
return mangaRepository.getMangaByUrlAndSourceId(url, sourceId)
|
||||
}
|
||||
|
||||
private suspend fun insertManga(manga: Manga, sourceId: Long): Long? {
|
||||
return mangaRepository.insert(manga.copy(source = sourceId))
|
||||
private suspend fun insertManga(manga: Manga): Long? {
|
||||
return mangaRepository.insert(manga)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -232,7 +232,7 @@ fun Manga.toMangaUpdate(): MangaUpdate {
|
|||
)
|
||||
}
|
||||
|
||||
fun SManga.toDomainManga(): Manga {
|
||||
fun SManga.toDomainManga(sourceId: Long): Manga {
|
||||
return Manga.create().copy(
|
||||
url = url,
|
||||
title = title,
|
||||
|
@ -244,6 +244,7 @@ fun SManga.toDomainManga(): Manga {
|
|||
thumbnailUrl = thumbnail_url,
|
||||
updateStrategy = update_strategy,
|
||||
initialized = initialized,
|
||||
source = sourceId,
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -32,13 +32,7 @@ class SourceManager(
|
|||
|
||||
private val scope = CoroutineScope(Job() + Dispatchers.IO)
|
||||
|
||||
private var sourcesMap = ConcurrentHashMap<Long, Source>()
|
||||
set(value) {
|
||||
field = value
|
||||
sourcesMapFlow.value = field
|
||||
}
|
||||
|
||||
private val sourcesMapFlow = MutableStateFlow(sourcesMap)
|
||||
private val sourcesMapFlow = MutableStateFlow(ConcurrentHashMap<Long, Source>())
|
||||
|
||||
private val stubSourcesMap = ConcurrentHashMap<Long, StubSource>()
|
||||
|
||||
|
@ -56,7 +50,7 @@ class SourceManager(
|
|||
registerStubSource(it.toSourceData())
|
||||
}
|
||||
}
|
||||
sourcesMap = mutableMap
|
||||
sourcesMapFlow.value = mutableMap
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -72,18 +66,18 @@ class SourceManager(
|
|||
}
|
||||
|
||||
fun get(sourceKey: Long): Source? {
|
||||
return sourcesMap[sourceKey]
|
||||
return sourcesMapFlow.value[sourceKey]
|
||||
}
|
||||
|
||||
fun getOrStub(sourceKey: Long): Source {
|
||||
return sourcesMap[sourceKey] ?: stubSourcesMap.getOrPut(sourceKey) {
|
||||
return sourcesMapFlow.value[sourceKey] ?: stubSourcesMap.getOrPut(sourceKey) {
|
||||
runBlocking { createStubSource(sourceKey) }
|
||||
}
|
||||
}
|
||||
|
||||
fun getOnlineSources() = sourcesMap.values.filterIsInstance<HttpSource>()
|
||||
fun getOnlineSources() = sourcesMapFlow.value.values.filterIsInstance<HttpSource>()
|
||||
|
||||
fun getCatalogueSources() = sourcesMap.values.filterIsInstance<CatalogueSource>()
|
||||
fun getCatalogueSources() = sourcesMapFlow.value.values.filterIsInstance<CatalogueSource>()
|
||||
|
||||
fun getStubSources(): List<StubSource> {
|
||||
val onlineSourceIds = getOnlineSources().map { it.id }
|
||||
|
|
|
@ -131,7 +131,7 @@ open class BrowseSourcePresenter(
|
|||
.map {
|
||||
it.map { sManga ->
|
||||
withIOContext {
|
||||
networkToLocalManga.await(sManga.toDomainManga(), sourceId)
|
||||
networkToLocalManga.await(sManga.toDomainManga(sourceId))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -260,6 +260,6 @@ open class GlobalSearchPresenter(
|
|||
* @return a manga from the database.
|
||||
*/
|
||||
protected open suspend fun networkToLocalManga(sManga: SManga, sourceId: Long): DomainManga {
|
||||
return networkToLocalManga.await(sManga.toDomainManga(), sourceId)
|
||||
return networkToLocalManga.await(sManga.toDomainManga(sourceId))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue