mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-10 12:47:26 +01:00
parent
fc184f1cfa
commit
cac80daa71
@ -7,11 +7,11 @@ class NetworkToLocalManga(
|
|||||||
private val mangaRepository: MangaRepository,
|
private val mangaRepository: MangaRepository,
|
||||||
) {
|
) {
|
||||||
|
|
||||||
suspend fun await(manga: Manga, sourceId: Long): Manga {
|
suspend fun await(manga: Manga): Manga {
|
||||||
val localManga = getManga(manga.url, sourceId)
|
val localManga = getManga(manga.url, manga.source)
|
||||||
return when {
|
return when {
|
||||||
localManga == null -> {
|
localManga == null -> {
|
||||||
val id = insertManga(manga, sourceId)
|
val id = insertManga(manga)
|
||||||
manga.copy(id = id!!)
|
manga.copy(id = id!!)
|
||||||
}
|
}
|
||||||
!localManga.favorite -> {
|
!localManga.favorite -> {
|
||||||
@ -29,7 +29,7 @@ class NetworkToLocalManga(
|
|||||||
return mangaRepository.getMangaByUrlAndSourceId(url, sourceId)
|
return mangaRepository.getMangaByUrlAndSourceId(url, sourceId)
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun insertManga(manga: Manga, sourceId: Long): Long? {
|
private suspend fun insertManga(manga: Manga): Long? {
|
||||||
return mangaRepository.insert(manga.copy(source = sourceId))
|
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(
|
return Manga.create().copy(
|
||||||
url = url,
|
url = url,
|
||||||
title = title,
|
title = title,
|
||||||
@ -244,6 +244,7 @@ fun SManga.toDomainManga(): Manga {
|
|||||||
thumbnailUrl = thumbnail_url,
|
thumbnailUrl = thumbnail_url,
|
||||||
updateStrategy = update_strategy,
|
updateStrategy = update_strategy,
|
||||||
initialized = initialized,
|
initialized = initialized,
|
||||||
|
source = sourceId,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,13 +32,7 @@ class SourceManager(
|
|||||||
|
|
||||||
private val scope = CoroutineScope(Job() + Dispatchers.IO)
|
private val scope = CoroutineScope(Job() + Dispatchers.IO)
|
||||||
|
|
||||||
private var sourcesMap = ConcurrentHashMap<Long, Source>()
|
private val sourcesMapFlow = MutableStateFlow(ConcurrentHashMap<Long, Source>())
|
||||||
set(value) {
|
|
||||||
field = value
|
|
||||||
sourcesMapFlow.value = field
|
|
||||||
}
|
|
||||||
|
|
||||||
private val sourcesMapFlow = MutableStateFlow(sourcesMap)
|
|
||||||
|
|
||||||
private val stubSourcesMap = ConcurrentHashMap<Long, StubSource>()
|
private val stubSourcesMap = ConcurrentHashMap<Long, StubSource>()
|
||||||
|
|
||||||
@ -56,7 +50,7 @@ class SourceManager(
|
|||||||
registerStubSource(it.toSourceData())
|
registerStubSource(it.toSourceData())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sourcesMap = mutableMap
|
sourcesMapFlow.value = mutableMap
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,18 +66,18 @@ class SourceManager(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun get(sourceKey: Long): Source? {
|
fun get(sourceKey: Long): Source? {
|
||||||
return sourcesMap[sourceKey]
|
return sourcesMapFlow.value[sourceKey]
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getOrStub(sourceKey: Long): Source {
|
fun getOrStub(sourceKey: Long): Source {
|
||||||
return sourcesMap[sourceKey] ?: stubSourcesMap.getOrPut(sourceKey) {
|
return sourcesMapFlow.value[sourceKey] ?: stubSourcesMap.getOrPut(sourceKey) {
|
||||||
runBlocking { createStubSource(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> {
|
fun getStubSources(): List<StubSource> {
|
||||||
val onlineSourceIds = getOnlineSources().map { it.id }
|
val onlineSourceIds = getOnlineSources().map { it.id }
|
||||||
|
@ -131,7 +131,7 @@ open class BrowseSourcePresenter(
|
|||||||
.map {
|
.map {
|
||||||
it.map { sManga ->
|
it.map { sManga ->
|
||||||
withIOContext {
|
withIOContext {
|
||||||
networkToLocalManga.await(sManga.toDomainManga(), sourceId)
|
networkToLocalManga.await(sManga.toDomainManga(sourceId))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -260,6 +260,6 @@ open class GlobalSearchPresenter(
|
|||||||
* @return a manga from the database.
|
* @return a manga from the database.
|
||||||
*/
|
*/
|
||||||
protected open suspend fun networkToLocalManga(sManga: SManga, sourceId: Long): DomainManga {
|
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 New Issue
Block a user