Improve loading both local and remote manga

pull/488/head
Koitharu 3 years ago
parent c88a9dff36
commit 6b93e49f56
Signed by: Koitharu
GPG Key ID: 676DEE768C17A9D7

@ -17,6 +17,8 @@ fun Collection<Manga>.distinctById() = distinctBy { it.id }
@JvmName("chaptersIds") @JvmName("chaptersIds")
fun Collection<MangaChapter>.ids() = mapToSet { it.id } fun Collection<MangaChapter>.ids() = mapToSet { it.id }
fun Collection<MangaChapter>.findById(id: Long) = find { x -> x.id == id }
fun Collection<ChapterListItem>.countChaptersByBranch(): Int { fun Collection<ChapterListItem>.countChaptersByBranch(): Int {
if (size <= 1) { if (size <= 1) {
return size return size
@ -30,7 +32,7 @@ fun Collection<ChapterListItem>.countChaptersByBranch(): Int {
} }
fun Manga.findChapter(id: Long): MangaChapter? { fun Manga.findChapter(id: Long): MangaChapter? {
return chapters?.find { it.id == id } return chapters?.findById(id)
} }
fun Manga.getPreferredBranch(history: MangaHistory?): String? { fun Manga.getPreferredBranch(history: MangaHistory?): String? {
@ -39,7 +41,7 @@ fun Manga.getPreferredBranch(history: MangaHistory?): String? {
return null return null
} }
if (history != null) { if (history != null) {
val currentChapter = ch.find { it.id == history.chapterId } val currentChapter = ch.findById(history.chapterId)
if (currentChapter != null) { if (currentChapter != null) {
return currentChapter.branch return currentChapter.branch
} }

@ -5,6 +5,8 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onCompletion import kotlinx.coroutines.flow.onCompletion
@ -72,7 +74,7 @@ fun <T1, T2, T3, T4, T5, T6, R> combine(
flow4: Flow<T4>, flow4: Flow<T4>,
flow5: Flow<T5>, flow5: Flow<T5>,
flow6: Flow<T6>, flow6: Flow<T6>,
transform: suspend (T1, T2, T3, T4, T5, T6) -> R transform: suspend (T1, T2, T3, T4, T5, T6) -> R,
): Flow<R> = combine(flow, flow2, flow3, flow4, flow5, flow6) { args: Array<*> -> ): Flow<R> = combine(flow, flow2, flow3, flow4, flow5, flow6) { args: Array<*> ->
transform( transform(
args[0] as T1, args[0] as T1,
@ -83,3 +85,7 @@ fun <T1, T2, T3, T4, T5, T6, R> combine(
args[5] as T6, args[5] as T6,
) )
} }
suspend fun <T : Any> Flow<T?>.firstNotNull(): T = checkNotNull(first { x -> x != null })
suspend fun <T : Any> Flow<T?>.firstNotNullOrNull(): T? = firstOrNull { x -> x != null }

@ -8,10 +8,11 @@ import android.view.ViewGroup
import android.view.ViewParent import android.view.ViewParent
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import android.widget.Checkable import android.widget.Checkable
import android.widget.CompoundButton
import androidx.core.view.children import androidx.core.view.children
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2 import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.progressindicator.BaseProgressIndicator
import com.google.android.material.slider.Slider import com.google.android.material.slider.Slider
import com.google.android.material.tabs.TabLayout import com.google.android.material.tabs.TabLayout
import kotlin.math.roundToInt import kotlin.math.roundToInt
@ -155,3 +156,11 @@ fun TabLayout.setTabsEnabled(enabled: Boolean) {
getTabAt(i)?.view?.isEnabled = enabled getTabAt(i)?.view?.isEnabled = enabled
} }
} }
fun BaseProgressIndicator<*>.showOrHide(value: Boolean) {
if (value) {
if (!isVisible) show()
} else {
if (isVisible) hide()
}
}

@ -1,8 +1,11 @@
package org.koitharu.kotatsu.details.domain package org.koitharu.kotatsu.details.domain
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flatMapLatest
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import org.koitharu.kotatsu.core.model.isLocal import org.koitharu.kotatsu.core.model.isLocal
import org.koitharu.kotatsu.core.parser.MangaDataRepository import org.koitharu.kotatsu.core.parser.MangaDataRepository
import org.koitharu.kotatsu.core.parser.MangaIntent import org.koitharu.kotatsu.core.parser.MangaIntent
@ -23,24 +26,28 @@ class DoubleMangaLoadUseCase @Inject constructor(
private val recoverUseCase: RecoverMangaUseCase, private val recoverUseCase: RecoverMangaUseCase,
) { ) {
suspend operator fun invoke(manga: Manga): DoubleManga = coroutineScope { operator fun invoke(manga: Manga): Flow<DoubleManga> = flow<DoubleManga> {
val remoteDeferred = async(Dispatchers.Default) { loadRemote(manga) } var lastValue: DoubleManga? = null
val localDeferred = async(Dispatchers.Default) { loadLocal(manga) } var emitted = false
DoubleManga( invokeImpl(manga).collect {
remoteManga = remoteDeferred.await(), lastValue = it
localManga = localDeferred.await(), if (it.any != null) {
) emitted = true
} emit(it)
}
}
if (!emitted) {
lastValue?.requireAny()
}
}.flowOn(Dispatchers.Default)
suspend operator fun invoke(mangaId: Long): DoubleManga { operator fun invoke(mangaId: Long): Flow<DoubleManga> = flow {
val manga = mangaDataRepository.findMangaById(mangaId) ?: throwNFE() emit(mangaDataRepository.findMangaById(mangaId) ?: throwNFE())
return invoke(manga) }.flatMapLatest { invoke(it) }
}
suspend operator fun invoke(intent: MangaIntent): DoubleManga { operator fun invoke(intent: MangaIntent): Flow<DoubleManga> = flow {
val manga = mangaDataRepository.resolveIntent(intent) ?: throwNFE() emit(mangaDataRepository.resolveIntent(intent) ?: throwNFE())
return invoke(manga) }.flatMapLatest { invoke(it) }
}
private suspend fun loadLocal(manga: Manga): Result<Manga>? { private suspend fun loadLocal(manga: Manga): Result<Manga>? {
return runCatchingCancellable { return runCatchingCancellable {
@ -70,5 +77,15 @@ class DoubleMangaLoadUseCase @Inject constructor(
} }
} }
private fun invokeImpl(manga: Manga): Flow<DoubleManga> = combine(
flow { emit(null); emit(loadRemote(manga)) },
flow { emit(null); emit(loadLocal(manga)) },
) { remote, local ->
DoubleManga(
remoteManga = remote,
localManga = local,
)
}
private fun throwNFE(): Nothing = throw NotFoundException("Cannot find manga", "") private fun throwNFE(): Nothing = throw NotFoundException("Cannot find manga", "")
} }

@ -1,5 +1,6 @@
package org.koitharu.kotatsu.details.domain.model package org.koitharu.kotatsu.details.domain.model
import org.koitharu.kotatsu.core.model.findById
import org.koitharu.kotatsu.parsers.model.Manga import org.koitharu.kotatsu.parsers.model.Manga
import org.koitharu.kotatsu.parsers.model.MangaChapter import org.koitharu.kotatsu.parsers.model.MangaChapter
import org.koitharu.kotatsu.parsers.model.MangaSource import org.koitharu.kotatsu.parsers.model.MangaSource
@ -34,6 +35,10 @@ data class DoubleManga(
mergeChapters() mergeChapters()
} }
fun hasChapter(id: Long): Boolean {
return local?.chapters?.findById(id) != null || remote?.chapters?.findById(id) != null
}
fun requireAny(): Manga { fun requireAny(): Manga {
val result = remoteManga?.getOrNull() ?: localManga?.getOrNull() val result = remoteManga?.getOrNull() ?: localManga?.getOrNull()
if (result != null) { if (result != null) {

@ -5,6 +5,7 @@ import android.content.Intent
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import dagger.hilt.android.EntryPointAccessors import dagger.hilt.android.EntryPointAccessors
import org.koitharu.kotatsu.core.cache.ContentCache import org.koitharu.kotatsu.core.cache.ContentCache
import org.koitharu.kotatsu.core.model.findById
import org.koitharu.kotatsu.core.model.parcelable.ParcelableChapter import org.koitharu.kotatsu.core.model.parcelable.ParcelableChapter
import org.koitharu.kotatsu.core.model.parcelable.ParcelableManga import org.koitharu.kotatsu.core.model.parcelable.ParcelableManga
import org.koitharu.kotatsu.core.parser.MangaRepository import org.koitharu.kotatsu.core.parser.MangaRepository
@ -72,7 +73,7 @@ class MangaPrefetchService : CoroutineIntentService() {
val chapter = if (history == null) { val chapter = if (history == null) {
chapters.firstOrNull() chapters.firstOrNull()
} else { } else {
chapters.find { x -> x.id == history.chapterId } ?: chapters.firstOrNull() chapters.findById(history.chapterId) ?: chapters.firstOrNull()
} ?: return } ?: return
runCatchingCancellable { repo.getPages(chapter) } runCatchingCancellable { repo.getPages(chapter) }
} }
@ -122,7 +123,7 @@ class MangaPrefetchService : CoroutineIntentService() {
} }
val entryPoint = EntryPointAccessors.fromApplication( val entryPoint = EntryPointAccessors.fromApplication(
context, context,
PrefetchCompanionEntryPoint::class.java PrefetchCompanionEntryPoint::class.java,
) )
return entryPoint.contentCache.isCachingEnabled && entryPoint.settings.isContentPrefetchEnabled return entryPoint.contentCache.isCachingEnabled && entryPoint.settings.isContentPrefetchEnabled
} }

@ -43,6 +43,7 @@ import org.koitharu.kotatsu.core.util.ext.observe
import org.koitharu.kotatsu.core.util.ext.parentView import org.koitharu.kotatsu.core.util.ext.parentView
import org.koitharu.kotatsu.core.util.ext.resolveDp import org.koitharu.kotatsu.core.util.ext.resolveDp
import org.koitharu.kotatsu.core.util.ext.scaleUpActivityOptionsOf import org.koitharu.kotatsu.core.util.ext.scaleUpActivityOptionsOf
import org.koitharu.kotatsu.core.util.ext.showOrHide
import org.koitharu.kotatsu.core.util.ext.textAndVisible import org.koitharu.kotatsu.core.util.ext.textAndVisible
import org.koitharu.kotatsu.databinding.FragmentDetailsBinding import org.koitharu.kotatsu.databinding.FragmentDetailsBinding
import org.koitharu.kotatsu.details.ui.model.ChapterListItem import org.koitharu.kotatsu.details.ui.model.ChapterListItem
@ -247,11 +248,7 @@ class DetailsFragment :
} }
private fun onLoadingStateChanged(isLoading: Boolean) { private fun onLoadingStateChanged(isLoading: Boolean) {
if (isLoading) { requireViewBinding().progressBar.showOrHide(isLoading)
requireViewBinding().progressBar.show()
} else {
requireViewBinding().progressBar.hide()
}
} }
private fun onBookmarksChanged(bookmarks: List<Bookmark>) { private fun onBookmarksChanged(bookmarks: List<Bookmark>) {

@ -42,6 +42,7 @@ import org.koitharu.kotatsu.core.util.ext.MutableEventFlow
import org.koitharu.kotatsu.core.util.ext.call import org.koitharu.kotatsu.core.util.ext.call
import org.koitharu.kotatsu.core.util.ext.combine import org.koitharu.kotatsu.core.util.ext.combine
import org.koitharu.kotatsu.core.util.ext.computeSize import org.koitharu.kotatsu.core.util.ext.computeSize
import org.koitharu.kotatsu.core.util.ext.onFirst
import org.koitharu.kotatsu.core.util.ext.requireValue import org.koitharu.kotatsu.core.util.ext.requireValue
import org.koitharu.kotatsu.core.util.ext.sanitize import org.koitharu.kotatsu.core.util.ext.sanitize
import org.koitharu.kotatsu.core.util.ext.toFileOrNull import org.koitharu.kotatsu.core.util.ext.toFileOrNull
@ -87,7 +88,8 @@ class DetailsViewModel @Inject constructor(
private val intent = MangaIntent(savedStateHandle) private val intent = MangaIntent(savedStateHandle)
private val mangaId = intent.mangaId private val mangaId = intent.mangaId
private val doubleManga: MutableStateFlow<DoubleManga?> = MutableStateFlow(intent.manga?.let { DoubleManga(it) }) private val doubleManga: MutableStateFlow<DoubleManga?> =
MutableStateFlow(intent.manga?.let { DoubleManga(it) })
private var loadingJob: Job private var loadingJob: Job
val onShowToast = MutableEventFlow<Int>() val onShowToast = MutableEventFlow<Int>()
@ -202,7 +204,14 @@ class DetailsViewModel @Inject constructor(
bookmarks, bookmarks,
networkState, networkState,
) { manga, history, branch, news, bookmarks, isOnline -> ) { manga, history, branch, news, bookmarks, isOnline ->
mapChapters(manga?.remote?.takeIf { isOnline }, manga?.local, history, news, branch, bookmarks) mapChapters(
manga?.remote?.takeIf { isOnline },
manga?.local,
history,
news,
branch,
bookmarks,
)
}, },
isChaptersReversed, isChaptersReversed,
chaptersQuery, chaptersQuery,
@ -324,12 +333,15 @@ class DetailsViewModel @Inject constructor(
} }
private fun doLoad() = launchLoadingJob(Dispatchers.Default) { private fun doLoad() = launchLoadingJob(Dispatchers.Default) {
val result = doubleMangaLoadUseCase(intent) doubleMangaLoadUseCase.invoke(intent)
val manga = result.requireAny() .onFirst {
// find default branch val manga = it.requireAny()
val hist = historyRepository.getOne(manga) // find default branch
selectedBranch.value = manga.getPreferredBranch(hist) val hist = historyRepository.getOne(manga)
doubleManga.value = result selectedBranch.value = manga.getPreferredBranch(hist)
}.collect {
doubleManga.value = it
}
} }
private fun List<ChapterListItem>.filterSearch(query: String): List<ChapterListItem> { private fun List<ChapterListItem>.filterSearch(query: String): List<ChapterListItem> {

@ -14,6 +14,7 @@ import org.koitharu.kotatsu.core.db.entity.toManga
import org.koitharu.kotatsu.core.db.entity.toMangaTag import org.koitharu.kotatsu.core.db.entity.toMangaTag
import org.koitharu.kotatsu.core.db.entity.toMangaTags import org.koitharu.kotatsu.core.db.entity.toMangaTags
import org.koitharu.kotatsu.core.model.MangaHistory import org.koitharu.kotatsu.core.model.MangaHistory
import org.koitharu.kotatsu.core.model.findById
import org.koitharu.kotatsu.core.prefs.AppSettings import org.koitharu.kotatsu.core.prefs.AppSettings
import org.koitharu.kotatsu.core.ui.util.ReversibleHandle import org.koitharu.kotatsu.core.ui.util.ReversibleHandle
import org.koitharu.kotatsu.core.util.ext.mapItems import org.koitharu.kotatsu.core.util.ext.mapItems
@ -107,7 +108,7 @@ class HistoryRepository @Inject constructor(
), ),
) )
trackingRepository.syncWithHistory(manga, chapterId) trackingRepository.syncWithHistory(manga, chapterId)
val chapter = manga.chapters?.find { x -> x.id == chapterId } val chapter = manga.chapters?.findById(chapterId)
if (chapter != null) { if (chapter != null) {
scrobblers.forEach { it.tryScrobble(manga.id, chapter) } scrobblers.forEach { it.tryScrobble(manga.id, chapter) }
} }
@ -181,7 +182,7 @@ class HistoryRepository @Inject constructor(
private suspend fun HistoryEntity.recoverIfNeeded(manga: Manga): HistoryEntity { private suspend fun HistoryEntity.recoverIfNeeded(manga: Manga): HistoryEntity {
val chapters = manga.chapters val chapters = manga.chapters
if (chapters.isNullOrEmpty() || chapters.any { it.id == chapterId }) { if (chapters.isNullOrEmpty() || chapters.findById(chapterId) != null) {
return this return this
} }
val newChapterId = chapters.getOrNull( val newChapterId = chapters.getOrNull(

@ -2,6 +2,7 @@ package org.koitharu.kotatsu.local.data.output
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runInterruptible import kotlinx.coroutines.runInterruptible
import org.koitharu.kotatsu.core.model.findById
import org.koitharu.kotatsu.core.util.ext.deleteAwait import org.koitharu.kotatsu.core.util.ext.deleteAwait
import org.koitharu.kotatsu.core.util.ext.takeIfReadable import org.koitharu.kotatsu.core.util.ext.takeIfReadable
import org.koitharu.kotatsu.core.zip.ZipOutput import org.koitharu.kotatsu.core.zip.ZipOutput
@ -87,7 +88,7 @@ class LocalMangaDirOutput(
suspend fun deleteChapter(chapterId: Long) { suspend fun deleteChapter(chapterId: Long) {
val chapter = checkNotNull(index.getMangaInfo()?.chapters) { val chapter = checkNotNull(index.getMangaInfo()?.chapters) {
"No chapters found" "No chapters found"
}.first { it.id == chapterId } }.findById(chapterId) ?: error("Chapter not found")
val chapterDir = File(rootFile, chapterFileName(chapter)) val chapterDir = File(rootFile, chapterFileName(chapter))
chapterDir.deleteAwait() chapterDir.deleteAwait()
index.removeChapter(chapterId) index.removeChapter(chapterId)

@ -8,6 +8,7 @@ import coil.request.ImageResult
import org.jsoup.HttpStatusException import org.jsoup.HttpStatusException
import org.koitharu.kotatsu.bookmarks.domain.Bookmark import org.koitharu.kotatsu.bookmarks.domain.Bookmark
import org.koitharu.kotatsu.bookmarks.domain.BookmarksRepository import org.koitharu.kotatsu.bookmarks.domain.BookmarksRepository
import org.koitharu.kotatsu.core.model.findById
import org.koitharu.kotatsu.core.parser.MangaDataRepository import org.koitharu.kotatsu.core.parser.MangaDataRepository
import org.koitharu.kotatsu.core.parser.MangaRepository import org.koitharu.kotatsu.core.parser.MangaRepository
import org.koitharu.kotatsu.core.parser.RemoteMangaRepository import org.koitharu.kotatsu.core.parser.RemoteMangaRepository
@ -92,7 +93,7 @@ class CoverRestoreInterceptor @Inject constructor(
private suspend fun restoreBookmarkImpl(bookmark: Bookmark): Boolean { private suspend fun restoreBookmarkImpl(bookmark: Bookmark): Boolean {
val repo = repositoryFactory.create(bookmark.manga.source) as? RemoteMangaRepository ?: return false val repo = repositoryFactory.create(bookmark.manga.source) as? RemoteMangaRepository ?: return false
val chapter = repo.getDetails(bookmark.manga).chapters?.find { it.id == bookmark.chapterId } ?: return false val chapter = repo.getDetails(bookmark.manga).chapters?.findById(bookmark.chapterId) ?: return false
val page = repo.getPages(chapter)[bookmark.page] val page = repo.getPages(chapter)[bookmark.page]
val imageUrl = page.preview.ifNullOrEmpty { page.url } val imageUrl = page.preview.ifNullOrEmpty { page.url }
return if (imageUrl != bookmark.imageUrl) { return if (imageUrl != bookmark.imageUrl) {

@ -2,6 +2,12 @@ package org.koitharu.kotatsu.reader.domain
import android.util.LongSparseArray import android.util.LongSparseArray
import dagger.hilt.android.scopes.ViewModelScoped import dagger.hilt.android.scopes.ViewModelScoped
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.launch
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
import org.koitharu.kotatsu.core.parser.MangaRepository import org.koitharu.kotatsu.core.parser.MangaRepository
@ -17,24 +23,32 @@ class ChaptersLoader @Inject constructor(
private val mangaRepositoryFactory: MangaRepository.Factory, private val mangaRepositoryFactory: MangaRepository.Factory,
) { ) {
private val chapters = LongSparseArray<MangaChapter>() private val chapters = MutableStateFlow(LongSparseArray<MangaChapter>(0))
private val chapterPages = ChapterPages() private val chapterPages = ChapterPages()
private val mutex = Mutex() private val mutex = Mutex()
val size: Int val size: Int // TODO flow
get() = chapters.size() get() = chapters.value.size()
suspend fun init(manga: DoubleManga) = mutex.withLock { fun init(scope: CoroutineScope, manga: Flow<DoubleManga>) = scope.launch {
chapters.clear() manga.collect {
manga.chapters?.forEach { val ch = it.chapters.orEmpty()
chapters.put(it.id, it) val longSparseArray = LongSparseArray<MangaChapter>(ch.size)
ch.forEach { x -> longSparseArray.put(x.id, x) }
mutex.withLock {
chapters.value = longSparseArray
}
} }
} }
suspend fun loadPrevNextChapter(manga: DoubleManga, currentId: Long, isNext: Boolean) { suspend fun loadPrevNextChapter(manga: DoubleManga, currentId: Long, isNext: Boolean) {
val chapters = manga.chapters ?: return val chapters = manga.chapters ?: return
val predicate: (MangaChapter) -> Boolean = { it.id == currentId } val predicate: (MangaChapter) -> Boolean = { it.id == currentId }
val index = if (isNext) chapters.indexOfFirst(predicate) else chapters.indexOfLast(predicate) val index = if (isNext) {
chapters.indexOfFirst(predicate)
} else {
chapters.indexOfLast(predicate)
}
if (index == -1) return if (index == -1) return
val newChapter = chapters.getOrNull(if (isNext) index + 1 else index - 1) ?: return val newChapter = chapters.getOrNull(if (isNext) index + 1 else index - 1) ?: return
val newPages = loadChapter(newChapter.id) val newPages = loadChapter(newChapter.id)
@ -65,7 +79,11 @@ class ChaptersLoader @Inject constructor(
} }
} }
fun peekChapter(chapterId: Long): MangaChapter? = chapters[chapterId] fun peekChapter(chapterId: Long): MangaChapter? = chapters.value[chapterId]
suspend fun awaitChapter(chapterId: Long): MangaChapter? = chapters.mapNotNull { x ->
x[chapterId]
}.firstOrNull()
fun getPages(chapterId: Long): List<ReaderPage> { fun getPages(chapterId: Long): List<ReaderPage> {
return chapterPages.subList(chapterId) return chapterPages.subList(chapterId)
@ -82,7 +100,7 @@ class ChaptersLoader @Inject constructor(
fun snapshot() = chapterPages.toList() fun snapshot() = chapterPages.toList()
private suspend fun loadChapter(chapterId: Long): List<ReaderPage> { private suspend fun loadChapter(chapterId: Long): List<ReaderPage> {
val chapter = checkNotNull(chapters[chapterId]) { "Requested chapter not found" } val chapter = checkNotNull(awaitChapter(chapterId)) { "Requested chapter not found" }
val repo = mangaRepositoryFactory.create(chapter.source) val repo = mangaRepositoryFactory.create(chapter.source)
return repo.getPages(chapter).mapIndexed { index, page -> return repo.getPages(chapter).mapIndexed { index, page ->
ReaderPage(page, index, chapterId) ReaderPage(page, index, chapterId)

@ -87,7 +87,8 @@ class ReaderViewModel @Inject constructor(
private var pageSaveJob: Job? = null private var pageSaveJob: Job? = null
private var bookmarkJob: Job? = null private var bookmarkJob: Job? = null
private var stateChangeJob: Job? = null private var stateChangeJob: Job? = null
private val currentState = MutableStateFlow<ReaderState?>(savedStateHandle[ReaderActivity.EXTRA_STATE]) private val currentState =
MutableStateFlow<ReaderState?>(savedStateHandle[ReaderActivity.EXTRA_STATE])
private val mangaData = MutableStateFlow(intent.manga?.let { DoubleManga(it) }) private val mangaData = MutableStateFlow(intent.manga?.let { DoubleManga(it) })
private val mangaFlow: Flow<Manga?> private val mangaFlow: Flow<Manga?>
get() = mangaData.map { it?.any } get() = mangaData.map { it?.any }
@ -317,8 +318,9 @@ class ReaderViewModel @Inject constructor(
?: throw NotFoundException("Cannot find manga", ""), ?: throw NotFoundException("Cannot find manga", ""),
) )
mangaData.value = manga mangaData.value = manga
manga = doubleMangaLoadUseCase(intent) val mangaFlow = doubleMangaLoadUseCase(intent)
chaptersLoader.init(manga) manga = mangaFlow.first { x -> x.any != null }
chaptersLoader.init(viewModelScope, mangaFlow)
// determine mode // determine mode
val singleManga = manga.requireAny() val singleManga = manga.requireAny()
// obtain state // obtain state
@ -328,7 +330,7 @@ class ReaderViewModel @Inject constructor(
} ?: ReaderState(singleManga, preselectedBranch) } ?: ReaderState(singleManga, preselectedBranch)
} }
val mode = detectReaderModeUseCase.invoke(singleManga, currentState.value) val mode = detectReaderModeUseCase.invoke(singleManga, currentState.value)
val branch = chaptersLoader.peekChapter(currentState.value?.chapterId ?: 0L)?.branch val branch = chaptersLoader.awaitChapter(currentState.value?.chapterId ?: 0L)?.branch
mangaData.value = manga.filterChapters(branch) mangaData.value = manga.filterChapters(branch)
readerMode.value = mode readerMode.value = mode

@ -24,6 +24,7 @@ import org.koitharu.kotatsu.core.util.ext.observe
import org.koitharu.kotatsu.core.util.ext.observeEvent import org.koitharu.kotatsu.core.util.ext.observeEvent
import org.koitharu.kotatsu.core.util.ext.plus import org.koitharu.kotatsu.core.util.ext.plus
import org.koitharu.kotatsu.core.util.ext.showDistinct import org.koitharu.kotatsu.core.util.ext.showDistinct
import org.koitharu.kotatsu.core.util.ext.showOrHide
import org.koitharu.kotatsu.core.util.ext.withArgs import org.koitharu.kotatsu.core.util.ext.withArgs
import org.koitharu.kotatsu.databinding.SheetPagesBinding import org.koitharu.kotatsu.databinding.SheetPagesBinding
import org.koitharu.kotatsu.list.ui.MangaListSpanResolver import org.koitharu.kotatsu.list.ui.MangaListSpanResolver
@ -84,6 +85,7 @@ class PagesThumbnailsSheet :
viewModel.thumbnails.observe(viewLifecycleOwner, ::onThumbnailsChanged) viewModel.thumbnails.observe(viewLifecycleOwner, ::onThumbnailsChanged)
viewModel.branch.observe(viewLifecycleOwner, ::updateTitle) viewModel.branch.observe(viewLifecycleOwner, ::updateTitle)
viewModel.onError.observeEvent(viewLifecycleOwner, SnackbarErrorObserver(binding.recyclerView, this)) viewModel.onError.observeEvent(viewLifecycleOwner, SnackbarErrorObserver(binding.recyclerView, this))
viewModel.isLoading.observe(viewLifecycleOwner) { binding.progressBar.showOrHide(it) }
} }
override fun onDestroyView() { override fun onDestroyView() {

@ -1,19 +1,26 @@
package org.koitharu.kotatsu.reader.ui.thumbnails package org.koitharu.kotatsu.reader.ui.thumbnails
import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
import org.koitharu.kotatsu.core.model.findById
import org.koitharu.kotatsu.core.model.parcelable.ParcelableManga import org.koitharu.kotatsu.core.model.parcelable.ParcelableManga
import org.koitharu.kotatsu.core.parser.MangaRepository import org.koitharu.kotatsu.core.parser.MangaRepository
import org.koitharu.kotatsu.core.ui.BaseViewModel import org.koitharu.kotatsu.core.ui.BaseViewModel
import org.koitharu.kotatsu.core.util.ext.firstNotNull
import org.koitharu.kotatsu.core.util.ext.firstNotNullOrNull
import org.koitharu.kotatsu.core.util.ext.require import org.koitharu.kotatsu.core.util.ext.require
import org.koitharu.kotatsu.details.domain.DoubleMangaLoadUseCase import org.koitharu.kotatsu.details.domain.DoubleMangaLoadUseCase
import org.koitharu.kotatsu.list.ui.model.ListHeader import org.koitharu.kotatsu.list.ui.model.ListHeader
import org.koitharu.kotatsu.list.ui.model.ListModel import org.koitharu.kotatsu.list.ui.model.ListModel
import org.koitharu.kotatsu.list.ui.model.LoadingFooter
import org.koitharu.kotatsu.parsers.util.SuspendLazy
import org.koitharu.kotatsu.reader.domain.ChaptersLoader import org.koitharu.kotatsu.reader.domain.ChaptersLoader
import javax.inject.Inject import javax.inject.Inject
@ -30,13 +37,12 @@ class PagesThumbnailsViewModel @Inject constructor(
val manga = savedStateHandle.require<ParcelableManga>(PagesThumbnailsSheet.ARG_MANGA).manga val manga = savedStateHandle.require<ParcelableManga>(PagesThumbnailsSheet.ARG_MANGA).manga
private val repository = mangaRepositoryFactory.create(manga.source) private val repository = mangaRepositoryFactory.create(manga.source)
private val mangaDetails = SuspendLazy { private val mangaDetails = doubleMangaLoadUseCase(manga).map {
doubleMangaLoadUseCase(manga).let { val b = manga.chapters?.findById(initialChapterId)?.branch
val b = manga.chapters?.find { ch -> ch.id == initialChapterId }?.branch branch.value = b
branch.value = b it.filterChapters(b)
it.filterChapters(b) }.withErrorHandling()
} .stateIn(viewModelScope, SharingStarted.Lazily, null)
}
private var loadingJob: Job? = null private var loadingJob: Job? = null
private var loadingPrevJob: Job? = null private var loadingPrevJob: Job? = null
private var loadingNextJob: Job? = null private var loadingNextJob: Job? = null
@ -46,8 +52,9 @@ class PagesThumbnailsViewModel @Inject constructor(
val branch = MutableStateFlow<String?>(null) val branch = MutableStateFlow<String?>(null)
init { init {
loadingJob = launchJob(Dispatchers.Default) { loadingJob = launchLoadingJob(Dispatchers.Default) {
chaptersLoader.init(mangaDetails.get()) chaptersLoader.init(viewModelScope, mangaDetails.filterNotNull())
mangaDetails.first { x -> x?.hasChapter(initialChapterId) == true }
chaptersLoader.loadSingleChapter(initialChapterId) chaptersLoader.loadSingleChapter(initialChapterId)
updateList() updateList()
} }
@ -55,7 +62,7 @@ class PagesThumbnailsViewModel @Inject constructor(
fun allowLoadAbove() { fun allowLoadAbove() {
if (!isLoadAboveAllowed) { if (!isLoadAboveAllowed) {
loadingJob = launchJob(Dispatchers.Default) { loadingJob = launchLoadingJob(Dispatchers.Default) {
isLoadAboveAllowed = true isLoadAboveAllowed = true
updateList() updateList()
} }
@ -78,23 +85,18 @@ class PagesThumbnailsViewModel @Inject constructor(
private fun loadPrevNextChapter(isNext: Boolean): Job = launchLoadingJob(Dispatchers.Default) { private fun loadPrevNextChapter(isNext: Boolean): Job = launchLoadingJob(Dispatchers.Default) {
val currentId = (if (isNext) chaptersLoader.last() else chaptersLoader.first()).chapterId val currentId = (if (isNext) chaptersLoader.last() else chaptersLoader.first()).chapterId
chaptersLoader.loadPrevNextChapter(mangaDetails.get(), currentId, isNext) chaptersLoader.loadPrevNextChapter(mangaDetails.firstNotNull(), currentId, isNext)
updateList() updateList()
} }
private suspend fun updateList() { private suspend fun updateList() {
val snapshot = chaptersLoader.snapshot() val snapshot = chaptersLoader.snapshot()
val mangaChapters = mangaDetails.tryGet().getOrNull()?.chapters.orEmpty() val mangaChapters = mangaDetails.firstNotNullOrNull()?.chapters.orEmpty()
val hasPrevChapter = isLoadAboveAllowed && snapshot.firstOrNull()?.chapterId != mangaChapters.firstOrNull()?.id
val hasNextChapter = snapshot.lastOrNull()?.chapterId != mangaChapters.lastOrNull()?.id
val pages = buildList(snapshot.size + chaptersLoader.size + 2) { val pages = buildList(snapshot.size + chaptersLoader.size + 2) {
if (hasPrevChapter) {
add(LoadingFooter(-1))
}
var previousChapterId = 0L var previousChapterId = 0L
for (page in snapshot) { for (page in snapshot) {
if (page.chapterId != previousChapterId) { if (page.chapterId != previousChapterId) {
chaptersLoader.peekChapter(page.chapterId)?.let { chaptersLoader.awaitChapter(page.chapterId)?.let {
add(ListHeader(it.name)) add(ListHeader(it.name))
} }
previousChapterId = page.chapterId previousChapterId = page.chapterId
@ -105,9 +107,6 @@ class PagesThumbnailsViewModel @Inject constructor(
page = page, page = page,
) )
} }
if (hasNextChapter) {
add(LoadingFooter(1))
}
} }
thumbnails.value = pages thumbnails.value = pages
} }

@ -8,7 +8,6 @@ import org.koitharu.kotatsu.core.ui.list.OnListItemClickListener
import org.koitharu.kotatsu.core.ui.list.fastscroll.FastScroller import org.koitharu.kotatsu.core.ui.list.fastscroll.FastScroller
import org.koitharu.kotatsu.list.ui.adapter.ListItemType import org.koitharu.kotatsu.list.ui.adapter.ListItemType
import org.koitharu.kotatsu.list.ui.adapter.listHeaderAD import org.koitharu.kotatsu.list.ui.adapter.listHeaderAD
import org.koitharu.kotatsu.list.ui.adapter.loadingFooterAD
import org.koitharu.kotatsu.list.ui.model.ListHeader import org.koitharu.kotatsu.list.ui.model.ListHeader
import org.koitharu.kotatsu.list.ui.model.ListModel import org.koitharu.kotatsu.list.ui.model.ListModel
import org.koitharu.kotatsu.reader.ui.thumbnails.PageThumbnail import org.koitharu.kotatsu.reader.ui.thumbnails.PageThumbnail
@ -22,7 +21,6 @@ class PageThumbnailAdapter(
init { init {
addDelegate(ListItemType.PAGE_THUMB, pageThumbnailAD(coil, lifecycleOwner, clickListener)) addDelegate(ListItemType.PAGE_THUMB, pageThumbnailAD(coil, lifecycleOwner, clickListener))
addDelegate(ListItemType.HEADER, listHeaderAD(null)) addDelegate(ListItemType.HEADER, listHeaderAD(null))
addDelegate(ListItemType.FOOTER_LOADING, loadingFooterAD())
} }
override fun getSectionText(context: Context, position: Int): CharSequence? { override fun getSectionText(context: Context, position: Int): CharSequence? {

@ -19,6 +19,7 @@ import org.koitharu.kotatsu.core.util.ext.enqueueWith
import org.koitharu.kotatsu.core.util.ext.newImageRequest import org.koitharu.kotatsu.core.util.ext.newImageRequest
import org.koitharu.kotatsu.core.util.ext.observe import org.koitharu.kotatsu.core.util.ext.observe
import org.koitharu.kotatsu.core.util.ext.observeEvent import org.koitharu.kotatsu.core.util.ext.observeEvent
import org.koitharu.kotatsu.core.util.ext.showOrHide
import org.koitharu.kotatsu.databinding.ActivityScrobblerConfigBinding import org.koitharu.kotatsu.databinding.ActivityScrobblerConfigBinding
import org.koitharu.kotatsu.details.ui.DetailsActivity import org.koitharu.kotatsu.details.ui.DetailsActivity
import org.koitharu.kotatsu.list.ui.adapter.TypedListSpacingDecoration import org.koitharu.kotatsu.list.ui.adapter.TypedListSpacingDecoration
@ -115,13 +116,7 @@ class ScrobblerConfigActivity : BaseActivity<ActivityScrobblerConfigBinding>(),
} }
private fun onLoadingStateChanged(isLoading: Boolean) { private fun onLoadingStateChanged(isLoading: Boolean) {
viewBinding.progressBar.run { viewBinding.progressBar.showOrHide(isLoading)
if (isLoading) {
show()
} else {
hide()
}
}
} }
private fun showUserDialog() { private fun showUserDialog() {

@ -14,7 +14,8 @@
<FrameLayout <FrameLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="match_parent"
android:minHeight="240dp">
<org.koitharu.kotatsu.core.ui.list.fastscroll.FastScrollRecyclerView <org.koitharu.kotatsu.core.ui.list.fastscroll.FastScrollRecyclerView
android:id="@+id/recyclerView" android:id="@+id/recyclerView"
@ -29,6 +30,19 @@
tools:listitem="@layout/item_page_thumb" tools:listitem="@layout/item_page_thumb"
tools:targetApi="m" /> tools:targetApi="m" />
<com.google.android.material.progressindicator.LinearProgressIndicator
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:indeterminate="true"
android:visibility="gone"
app:hideAnimationBehavior="outward"
app:showAnimationBehavior="inward"
app:trackCornerRadius="0dp"
app:trackThickness="2dp"
tools:visibility="visible" />
</FrameLayout> </FrameLayout>
</LinearLayout> </LinearLayout>

Loading…
Cancel
Save