mirror of
https://github.com/mihonapp/mihon.git
synced 2025-06-27 19:47:51 +02:00
Significantly improve browsing speed (near instantaneous) (#1946)
This commit is contained in:
@ -97,35 +97,6 @@ class MangaRepositoryImpl(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun insert(manga: Manga): Long? {
|
||||
return handler.awaitOneOrNullExecutable(inTransaction = true) {
|
||||
mangasQueries.insert(
|
||||
source = manga.source,
|
||||
url = manga.url,
|
||||
artist = manga.artist,
|
||||
author = manga.author,
|
||||
description = manga.description,
|
||||
genre = manga.genre,
|
||||
title = manga.title,
|
||||
status = manga.status,
|
||||
thumbnailUrl = manga.thumbnailUrl,
|
||||
favorite = manga.favorite,
|
||||
lastUpdate = manga.lastUpdate,
|
||||
nextUpdate = manga.nextUpdate,
|
||||
calculateInterval = manga.fetchInterval.toLong(),
|
||||
initialized = manga.initialized,
|
||||
viewerFlags = manga.viewerFlags,
|
||||
chapterFlags = manga.chapterFlags,
|
||||
coverLastModified = manga.coverLastModified,
|
||||
dateAdded = manga.dateAdded,
|
||||
updateStrategy = manga.updateStrategy,
|
||||
version = manga.version,
|
||||
notes = manga.notes,
|
||||
)
|
||||
mangasQueries.selectLastInsertedRowId()
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun update(update: MangaUpdate): Boolean {
|
||||
return try {
|
||||
partialUpdate(update)
|
||||
@ -146,6 +117,37 @@ class MangaRepositoryImpl(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun insertNetworkManga(manga: List<Manga>): List<Manga> {
|
||||
return handler.await(inTransaction = true) {
|
||||
manga.map {
|
||||
mangasQueries.insertNetworkManga(
|
||||
source = it.source,
|
||||
url = it.url,
|
||||
artist = it.artist,
|
||||
author = it.author,
|
||||
description = it.description,
|
||||
genre = it.genre,
|
||||
title = it.title,
|
||||
status = it.status,
|
||||
thumbnailUrl = it.thumbnailUrl,
|
||||
favorite = it.favorite,
|
||||
lastUpdate = it.lastUpdate,
|
||||
nextUpdate = it.nextUpdate,
|
||||
calculateInterval = it.fetchInterval.toLong(),
|
||||
initialized = it.initialized,
|
||||
viewerFlags = it.viewerFlags,
|
||||
chapterFlags = it.chapterFlags,
|
||||
coverLastModified = it.coverLastModified,
|
||||
dateAdded = it.dateAdded,
|
||||
updateStrategy = it.updateStrategy,
|
||||
version = it.version,
|
||||
mapper = MangaMapper::mapManga,
|
||||
)
|
||||
.executeAsOne()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun partialUpdate(vararg mangaUpdates: MangaUpdate) {
|
||||
handler.await(inTransaction = true) {
|
||||
mangaUpdates.forEach { value ->
|
||||
|
@ -4,56 +4,67 @@ import androidx.paging.PagingState
|
||||
import eu.kanade.tachiyomi.source.CatalogueSource
|
||||
import eu.kanade.tachiyomi.source.model.FilterList
|
||||
import eu.kanade.tachiyomi.source.model.MangasPage
|
||||
import eu.kanade.tachiyomi.source.model.SManga
|
||||
import mihon.domain.manga.model.toDomainManga
|
||||
import tachiyomi.core.common.util.lang.withIOContext
|
||||
import tachiyomi.domain.source.repository.SourcePagingSourceType
|
||||
import tachiyomi.domain.manga.interactor.NetworkToLocalManga
|
||||
import tachiyomi.domain.manga.model.Manga
|
||||
import tachiyomi.domain.source.repository.SourcePagingSource
|
||||
import uy.kohesive.injekt.Injekt
|
||||
import uy.kohesive.injekt.api.get
|
||||
|
||||
class SourceSearchPagingSource(source: CatalogueSource, val query: String, val filters: FilterList) :
|
||||
SourcePagingSource(source) {
|
||||
class SourceSearchPagingSource(
|
||||
source: CatalogueSource,
|
||||
private val query: String,
|
||||
private val filters: FilterList,
|
||||
) : BaseSourcePagingSource(source) {
|
||||
override suspend fun requestNextPage(currentPage: Int): MangasPage {
|
||||
return source.getSearchManga(currentPage, query, filters)
|
||||
}
|
||||
}
|
||||
|
||||
class SourcePopularPagingSource(source: CatalogueSource) : SourcePagingSource(source) {
|
||||
class SourcePopularPagingSource(source: CatalogueSource) : BaseSourcePagingSource(source) {
|
||||
override suspend fun requestNextPage(currentPage: Int): MangasPage {
|
||||
return source.getPopularManga(currentPage)
|
||||
}
|
||||
}
|
||||
|
||||
class SourceLatestPagingSource(source: CatalogueSource) : SourcePagingSource(source) {
|
||||
class SourceLatestPagingSource(source: CatalogueSource) : BaseSourcePagingSource(source) {
|
||||
override suspend fun requestNextPage(currentPage: Int): MangasPage {
|
||||
return source.getLatestUpdates(currentPage)
|
||||
}
|
||||
}
|
||||
|
||||
abstract class SourcePagingSource(
|
||||
abstract class BaseSourcePagingSource(
|
||||
protected val source: CatalogueSource,
|
||||
) : SourcePagingSourceType() {
|
||||
private val networkToLocalManga: NetworkToLocalManga = Injekt.get(),
|
||||
) : SourcePagingSource() {
|
||||
|
||||
abstract suspend fun requestNextPage(currentPage: Int): MangasPage
|
||||
|
||||
override suspend fun load(params: LoadParams<Long>): LoadResult<Long, SManga> {
|
||||
override suspend fun load(params: LoadParams<Long>): LoadResult<Long, Manga> {
|
||||
val page = params.key ?: 1
|
||||
|
||||
val mangasPage = try {
|
||||
withIOContext {
|
||||
return try {
|
||||
val mangasPage = withIOContext {
|
||||
requestNextPage(page.toInt())
|
||||
.takeIf { it.mangas.isNotEmpty() }
|
||||
?: throw NoResultsException()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
return LoadResult.Error(e)
|
||||
}
|
||||
|
||||
return LoadResult.Page(
|
||||
data = mangasPage.mangas,
|
||||
prevKey = null,
|
||||
nextKey = if (mangasPage.hasNextPage) page + 1 else null,
|
||||
)
|
||||
val manga = mangasPage.mangas.map { it.toDomainManga(source.id) }
|
||||
.let { networkToLocalManga(it) }
|
||||
|
||||
LoadResult.Page(
|
||||
data = manga,
|
||||
prevKey = null,
|
||||
nextKey = if (mangasPage.hasNextPage) page + 1 else null,
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
LoadResult.Error(e)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getRefreshKey(state: PagingState<Long, SManga>): Long? {
|
||||
override fun getRefreshKey(state: PagingState<Long, Manga>): Long? {
|
||||
return state.anchorPosition?.let { anchorPosition ->
|
||||
val anchorPage = state.closestPageToPosition(anchorPosition)
|
||||
anchorPage?.prevKey ?: anchorPage?.nextKey
|
||||
|
@ -10,7 +10,7 @@ import kotlinx.coroutines.flow.map
|
||||
import tachiyomi.data.DatabaseHandler
|
||||
import tachiyomi.domain.source.model.SourceWithCount
|
||||
import tachiyomi.domain.source.model.StubSource
|
||||
import tachiyomi.domain.source.repository.SourcePagingSourceType
|
||||
import tachiyomi.domain.source.repository.SourcePagingSource
|
||||
import tachiyomi.domain.source.repository.SourceRepository
|
||||
import tachiyomi.domain.source.service.SourceManager
|
||||
import tachiyomi.domain.source.model.Source as DomainSource
|
||||
@ -72,17 +72,17 @@ class SourceRepositoryImpl(
|
||||
sourceId: Long,
|
||||
query: String,
|
||||
filterList: FilterList,
|
||||
): SourcePagingSourceType {
|
||||
): SourcePagingSource {
|
||||
val source = sourceManager.get(sourceId) as CatalogueSource
|
||||
return SourceSearchPagingSource(source, query, filterList)
|
||||
}
|
||||
|
||||
override fun getPopular(sourceId: Long): SourcePagingSourceType {
|
||||
override fun getPopular(sourceId: Long): SourcePagingSource {
|
||||
val source = sourceManager.get(sourceId) as CatalogueSource
|
||||
return SourcePopularPagingSource(source)
|
||||
}
|
||||
|
||||
override fun getLatest(sourceId: Long): SourcePagingSourceType {
|
||||
override fun getLatest(sourceId: Long): SourcePagingSource {
|
||||
val source = sourceManager.get(sourceId) as CatalogueSource
|
||||
return SourceLatestPagingSource(source)
|
||||
}
|
||||
|
@ -182,3 +182,31 @@ WHERE _id = :mangaId;
|
||||
|
||||
selectLastInsertedRowId:
|
||||
SELECT last_insert_rowid();
|
||||
|
||||
insertNetworkManga {
|
||||
-- Insert the manga if it doesn't exist already
|
||||
INSERT INTO mangas(
|
||||
source, url, artist, author, description, genre, title, status, thumbnail_url, favorite,
|
||||
last_update, next_update, initialized, viewer, chapter_flags, cover_last_modified, date_added,
|
||||
update_strategy, calculate_interval, last_modified_at, version
|
||||
)
|
||||
SELECT
|
||||
:source, :url, :artist, :author, :description, :genre, :title, :status, :thumbnailUrl, :favorite,
|
||||
:lastUpdate, :nextUpdate, :initialized, :viewerFlags, :chapterFlags, :coverLastModified, :dateAdded,
|
||||
:updateStrategy, :calculateInterval, 0, :version
|
||||
WHERE NOT EXISTS(SELECT 0 FROM mangas WHERE source = :source AND url = :url);
|
||||
|
||||
-- Update the title if it is not favorite
|
||||
UPDATE mangas
|
||||
SET title = :title
|
||||
WHERE source = :source
|
||||
AND url = :url
|
||||
AND favorite = 0;
|
||||
|
||||
-- Finally return the manga
|
||||
SELECT *
|
||||
FROM mangas
|
||||
WHERE source = :source
|
||||
AND url = :url
|
||||
LIMIT 1;
|
||||
}
|
||||
|
Reference in New Issue
Block a user