Improve loading both local and remote manga
This commit is contained in:
@@ -17,6 +17,8 @@ fun Collection<Manga>.distinctById() = distinctBy { it.id }
|
||||
@JvmName("chaptersIds")
|
||||
fun Collection<MangaChapter>.ids() = mapToSet { it.id }
|
||||
|
||||
fun Collection<MangaChapter>.findById(id: Long) = find { x -> x.id == id }
|
||||
|
||||
fun Collection<ChapterListItem>.countChaptersByBranch(): Int {
|
||||
if (size <= 1) {
|
||||
return size
|
||||
@@ -30,7 +32,7 @@ fun Collection<ChapterListItem>.countChaptersByBranch(): Int {
|
||||
}
|
||||
|
||||
fun Manga.findChapter(id: Long): MangaChapter? {
|
||||
return chapters?.find { it.id == id }
|
||||
return chapters?.findById(id)
|
||||
}
|
||||
|
||||
fun Manga.getPreferredBranch(history: MangaHistory?): String? {
|
||||
@@ -39,7 +41,7 @@ fun Manga.getPreferredBranch(history: MangaHistory?): String? {
|
||||
return null
|
||||
}
|
||||
if (history != null) {
|
||||
val currentChapter = ch.find { it.id == history.chapterId }
|
||||
val currentChapter = ch.findById(history.chapterId)
|
||||
if (currentChapter != null) {
|
||||
return currentChapter.branch
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.combine
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.firstOrNull
|
||||
import kotlinx.coroutines.flow.flow
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.onCompletion
|
||||
@@ -72,7 +74,7 @@ fun <T1, T2, T3, T4, T5, T6, R> combine(
|
||||
flow4: Flow<T4>,
|
||||
flow5: Flow<T5>,
|
||||
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<*> ->
|
||||
transform(
|
||||
args[0] as T1,
|
||||
@@ -83,3 +85,7 @@ fun <T1, T2, T3, T4, T5, T6, R> combine(
|
||||
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.inputmethod.InputMethodManager
|
||||
import android.widget.Checkable
|
||||
import android.widget.CompoundButton
|
||||
import androidx.core.view.children
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import androidx.viewpager2.widget.ViewPager2
|
||||
import com.google.android.material.progressindicator.BaseProgressIndicator
|
||||
import com.google.android.material.slider.Slider
|
||||
import com.google.android.material.tabs.TabLayout
|
||||
import kotlin.math.roundToInt
|
||||
@@ -155,3 +156,11 @@ fun TabLayout.setTabsEnabled(enabled: Boolean) {
|
||||
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
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.coroutineScope
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
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.parser.MangaDataRepository
|
||||
import org.koitharu.kotatsu.core.parser.MangaIntent
|
||||
@@ -23,24 +26,28 @@ class DoubleMangaLoadUseCase @Inject constructor(
|
||||
private val recoverUseCase: RecoverMangaUseCase,
|
||||
) {
|
||||
|
||||
suspend operator fun invoke(manga: Manga): DoubleManga = coroutineScope {
|
||||
val remoteDeferred = async(Dispatchers.Default) { loadRemote(manga) }
|
||||
val localDeferred = async(Dispatchers.Default) { loadLocal(manga) }
|
||||
DoubleManga(
|
||||
remoteManga = remoteDeferred.await(),
|
||||
localManga = localDeferred.await(),
|
||||
)
|
||||
}
|
||||
operator fun invoke(manga: Manga): Flow<DoubleManga> = flow<DoubleManga> {
|
||||
var lastValue: DoubleManga? = null
|
||||
var emitted = false
|
||||
invokeImpl(manga).collect {
|
||||
lastValue = it
|
||||
if (it.any != null) {
|
||||
emitted = true
|
||||
emit(it)
|
||||
}
|
||||
}
|
||||
if (!emitted) {
|
||||
lastValue?.requireAny()
|
||||
}
|
||||
}.flowOn(Dispatchers.Default)
|
||||
|
||||
suspend operator fun invoke(mangaId: Long): DoubleManga {
|
||||
val manga = mangaDataRepository.findMangaById(mangaId) ?: throwNFE()
|
||||
return invoke(manga)
|
||||
}
|
||||
operator fun invoke(mangaId: Long): Flow<DoubleManga> = flow {
|
||||
emit(mangaDataRepository.findMangaById(mangaId) ?: throwNFE())
|
||||
}.flatMapLatest { invoke(it) }
|
||||
|
||||
suspend operator fun invoke(intent: MangaIntent): DoubleManga {
|
||||
val manga = mangaDataRepository.resolveIntent(intent) ?: throwNFE()
|
||||
return invoke(manga)
|
||||
}
|
||||
operator fun invoke(intent: MangaIntent): Flow<DoubleManga> = flow {
|
||||
emit(mangaDataRepository.resolveIntent(intent) ?: throwNFE())
|
||||
}.flatMapLatest { invoke(it) }
|
||||
|
||||
private suspend fun loadLocal(manga: Manga): Result<Manga>? {
|
||||
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", "")
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
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.MangaChapter
|
||||
import org.koitharu.kotatsu.parsers.model.MangaSource
|
||||
@@ -34,6 +35,10 @@ data class DoubleManga(
|
||||
mergeChapters()
|
||||
}
|
||||
|
||||
fun hasChapter(id: Long): Boolean {
|
||||
return local?.chapters?.findById(id) != null || remote?.chapters?.findById(id) != null
|
||||
}
|
||||
|
||||
fun requireAny(): Manga {
|
||||
val result = remoteManga?.getOrNull() ?: localManga?.getOrNull()
|
||||
if (result != null) {
|
||||
|
||||
@@ -5,6 +5,7 @@ import android.content.Intent
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
import dagger.hilt.android.EntryPointAccessors
|
||||
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.ParcelableManga
|
||||
import org.koitharu.kotatsu.core.parser.MangaRepository
|
||||
@@ -72,7 +73,7 @@ class MangaPrefetchService : CoroutineIntentService() {
|
||||
val chapter = if (history == null) {
|
||||
chapters.firstOrNull()
|
||||
} else {
|
||||
chapters.find { x -> x.id == history.chapterId } ?: chapters.firstOrNull()
|
||||
chapters.findById(history.chapterId) ?: chapters.firstOrNull()
|
||||
} ?: return
|
||||
runCatchingCancellable { repo.getPages(chapter) }
|
||||
}
|
||||
@@ -122,7 +123,7 @@ class MangaPrefetchService : CoroutineIntentService() {
|
||||
}
|
||||
val entryPoint = EntryPointAccessors.fromApplication(
|
||||
context,
|
||||
PrefetchCompanionEntryPoint::class.java
|
||||
PrefetchCompanionEntryPoint::class.java,
|
||||
)
|
||||
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.resolveDp
|
||||
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.databinding.FragmentDetailsBinding
|
||||
import org.koitharu.kotatsu.details.ui.model.ChapterListItem
|
||||
@@ -247,11 +248,7 @@ class DetailsFragment :
|
||||
}
|
||||
|
||||
private fun onLoadingStateChanged(isLoading: Boolean) {
|
||||
if (isLoading) {
|
||||
requireViewBinding().progressBar.show()
|
||||
} else {
|
||||
requireViewBinding().progressBar.hide()
|
||||
}
|
||||
requireViewBinding().progressBar.showOrHide(isLoading)
|
||||
}
|
||||
|
||||
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.combine
|
||||
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.sanitize
|
||||
import org.koitharu.kotatsu.core.util.ext.toFileOrNull
|
||||
@@ -87,7 +88,8 @@ class DetailsViewModel @Inject constructor(
|
||||
|
||||
private val intent = MangaIntent(savedStateHandle)
|
||||
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
|
||||
|
||||
val onShowToast = MutableEventFlow<Int>()
|
||||
@@ -202,7 +204,14 @@ class DetailsViewModel @Inject constructor(
|
||||
bookmarks,
|
||||
networkState,
|
||||
) { 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,
|
||||
chaptersQuery,
|
||||
@@ -324,12 +333,15 @@ class DetailsViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
private fun doLoad() = launchLoadingJob(Dispatchers.Default) {
|
||||
val result = doubleMangaLoadUseCase(intent)
|
||||
val manga = result.requireAny()
|
||||
// find default branch
|
||||
val hist = historyRepository.getOne(manga)
|
||||
selectedBranch.value = manga.getPreferredBranch(hist)
|
||||
doubleManga.value = result
|
||||
doubleMangaLoadUseCase.invoke(intent)
|
||||
.onFirst {
|
||||
val manga = it.requireAny()
|
||||
// find default branch
|
||||
val hist = historyRepository.getOne(manga)
|
||||
selectedBranch.value = manga.getPreferredBranch(hist)
|
||||
}.collect {
|
||||
doubleManga.value = it
|
||||
}
|
||||
}
|
||||
|
||||
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.toMangaTags
|
||||
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.ui.util.ReversibleHandle
|
||||
import org.koitharu.kotatsu.core.util.ext.mapItems
|
||||
@@ -107,7 +108,7 @@ class HistoryRepository @Inject constructor(
|
||||
),
|
||||
)
|
||||
trackingRepository.syncWithHistory(manga, chapterId)
|
||||
val chapter = manga.chapters?.find { x -> x.id == chapterId }
|
||||
val chapter = manga.chapters?.findById(chapterId)
|
||||
if (chapter != null) {
|
||||
scrobblers.forEach { it.tryScrobble(manga.id, chapter) }
|
||||
}
|
||||
@@ -181,7 +182,7 @@ class HistoryRepository @Inject constructor(
|
||||
|
||||
private suspend fun HistoryEntity.recoverIfNeeded(manga: Manga): HistoryEntity {
|
||||
val chapters = manga.chapters
|
||||
if (chapters.isNullOrEmpty() || chapters.any { it.id == chapterId }) {
|
||||
if (chapters.isNullOrEmpty() || chapters.findById(chapterId) != null) {
|
||||
return this
|
||||
}
|
||||
val newChapterId = chapters.getOrNull(
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.koitharu.kotatsu.local.data.output
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
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.takeIfReadable
|
||||
import org.koitharu.kotatsu.core.zip.ZipOutput
|
||||
@@ -87,7 +88,7 @@ class LocalMangaDirOutput(
|
||||
suspend fun deleteChapter(chapterId: Long) {
|
||||
val chapter = checkNotNull(index.getMangaInfo()?.chapters) {
|
||||
"No chapters found"
|
||||
}.first { it.id == chapterId }
|
||||
}.findById(chapterId) ?: error("Chapter not found")
|
||||
val chapterDir = File(rootFile, chapterFileName(chapter))
|
||||
chapterDir.deleteAwait()
|
||||
index.removeChapter(chapterId)
|
||||
|
||||
@@ -8,6 +8,7 @@ import coil.request.ImageResult
|
||||
import org.jsoup.HttpStatusException
|
||||
import org.koitharu.kotatsu.bookmarks.domain.Bookmark
|
||||
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.MangaRepository
|
||||
import org.koitharu.kotatsu.core.parser.RemoteMangaRepository
|
||||
@@ -92,7 +93,7 @@ class CoverRestoreInterceptor @Inject constructor(
|
||||
|
||||
private suspend fun restoreBookmarkImpl(bookmark: Bookmark): Boolean {
|
||||
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 imageUrl = page.preview.ifNullOrEmpty { page.url }
|
||||
return if (imageUrl != bookmark.imageUrl) {
|
||||
|
||||
@@ -2,6 +2,12 @@ package org.koitharu.kotatsu.reader.domain
|
||||
|
||||
import android.util.LongSparseArray
|
||||
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.withLock
|
||||
import org.koitharu.kotatsu.core.parser.MangaRepository
|
||||
@@ -17,24 +23,32 @@ class ChaptersLoader @Inject constructor(
|
||||
private val mangaRepositoryFactory: MangaRepository.Factory,
|
||||
) {
|
||||
|
||||
private val chapters = LongSparseArray<MangaChapter>()
|
||||
private val chapters = MutableStateFlow(LongSparseArray<MangaChapter>(0))
|
||||
private val chapterPages = ChapterPages()
|
||||
private val mutex = Mutex()
|
||||
|
||||
val size: Int
|
||||
get() = chapters.size()
|
||||
val size: Int // TODO flow
|
||||
get() = chapters.value.size()
|
||||
|
||||
suspend fun init(manga: DoubleManga) = mutex.withLock {
|
||||
chapters.clear()
|
||||
manga.chapters?.forEach {
|
||||
chapters.put(it.id, it)
|
||||
fun init(scope: CoroutineScope, manga: Flow<DoubleManga>) = scope.launch {
|
||||
manga.collect {
|
||||
val ch = it.chapters.orEmpty()
|
||||
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) {
|
||||
val chapters = manga.chapters ?: return
|
||||
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
|
||||
val newChapter = chapters.getOrNull(if (isNext) index + 1 else index - 1) ?: return
|
||||
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> {
|
||||
return chapterPages.subList(chapterId)
|
||||
@@ -82,7 +100,7 @@ class ChaptersLoader @Inject constructor(
|
||||
fun snapshot() = chapterPages.toList()
|
||||
|
||||
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)
|
||||
return repo.getPages(chapter).mapIndexed { index, page ->
|
||||
ReaderPage(page, index, chapterId)
|
||||
|
||||
@@ -87,7 +87,8 @@ class ReaderViewModel @Inject constructor(
|
||||
private var pageSaveJob: Job? = null
|
||||
private var bookmarkJob: 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 mangaFlow: Flow<Manga?>
|
||||
get() = mangaData.map { it?.any }
|
||||
@@ -317,8 +318,9 @@ class ReaderViewModel @Inject constructor(
|
||||
?: throw NotFoundException("Cannot find manga", ""),
|
||||
)
|
||||
mangaData.value = manga
|
||||
manga = doubleMangaLoadUseCase(intent)
|
||||
chaptersLoader.init(manga)
|
||||
val mangaFlow = doubleMangaLoadUseCase(intent)
|
||||
manga = mangaFlow.first { x -> x.any != null }
|
||||
chaptersLoader.init(viewModelScope, mangaFlow)
|
||||
// determine mode
|
||||
val singleManga = manga.requireAny()
|
||||
// obtain state
|
||||
@@ -328,7 +330,7 @@ class ReaderViewModel @Inject constructor(
|
||||
} ?: ReaderState(singleManga, preselectedBranch)
|
||||
}
|
||||
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)
|
||||
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.plus
|
||||
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.databinding.SheetPagesBinding
|
||||
import org.koitharu.kotatsu.list.ui.MangaListSpanResolver
|
||||
@@ -84,6 +85,7 @@ class PagesThumbnailsSheet :
|
||||
viewModel.thumbnails.observe(viewLifecycleOwner, ::onThumbnailsChanged)
|
||||
viewModel.branch.observe(viewLifecycleOwner, ::updateTitle)
|
||||
viewModel.onError.observeEvent(viewLifecycleOwner, SnackbarErrorObserver(binding.recyclerView, this))
|
||||
viewModel.isLoading.observe(viewLifecycleOwner) { binding.progressBar.showOrHide(it) }
|
||||
}
|
||||
|
||||
override fun onDestroyView() {
|
||||
|
||||
@@ -1,19 +1,26 @@
|
||||
package org.koitharu.kotatsu.reader.ui.thumbnails
|
||||
|
||||
import androidx.lifecycle.SavedStateHandle
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
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.parser.MangaRepository
|
||||
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.details.domain.DoubleMangaLoadUseCase
|
||||
import org.koitharu.kotatsu.list.ui.model.ListHeader
|
||||
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 javax.inject.Inject
|
||||
|
||||
@@ -30,13 +37,12 @@ class PagesThumbnailsViewModel @Inject constructor(
|
||||
val manga = savedStateHandle.require<ParcelableManga>(PagesThumbnailsSheet.ARG_MANGA).manga
|
||||
|
||||
private val repository = mangaRepositoryFactory.create(manga.source)
|
||||
private val mangaDetails = SuspendLazy {
|
||||
doubleMangaLoadUseCase(manga).let {
|
||||
val b = manga.chapters?.find { ch -> ch.id == initialChapterId }?.branch
|
||||
branch.value = b
|
||||
it.filterChapters(b)
|
||||
}
|
||||
}
|
||||
private val mangaDetails = doubleMangaLoadUseCase(manga).map {
|
||||
val b = manga.chapters?.findById(initialChapterId)?.branch
|
||||
branch.value = b
|
||||
it.filterChapters(b)
|
||||
}.withErrorHandling()
|
||||
.stateIn(viewModelScope, SharingStarted.Lazily, null)
|
||||
private var loadingJob: Job? = null
|
||||
private var loadingPrevJob: Job? = null
|
||||
private var loadingNextJob: Job? = null
|
||||
@@ -46,8 +52,9 @@ class PagesThumbnailsViewModel @Inject constructor(
|
||||
val branch = MutableStateFlow<String?>(null)
|
||||
|
||||
init {
|
||||
loadingJob = launchJob(Dispatchers.Default) {
|
||||
chaptersLoader.init(mangaDetails.get())
|
||||
loadingJob = launchLoadingJob(Dispatchers.Default) {
|
||||
chaptersLoader.init(viewModelScope, mangaDetails.filterNotNull())
|
||||
mangaDetails.first { x -> x?.hasChapter(initialChapterId) == true }
|
||||
chaptersLoader.loadSingleChapter(initialChapterId)
|
||||
updateList()
|
||||
}
|
||||
@@ -55,7 +62,7 @@ class PagesThumbnailsViewModel @Inject constructor(
|
||||
|
||||
fun allowLoadAbove() {
|
||||
if (!isLoadAboveAllowed) {
|
||||
loadingJob = launchJob(Dispatchers.Default) {
|
||||
loadingJob = launchLoadingJob(Dispatchers.Default) {
|
||||
isLoadAboveAllowed = true
|
||||
updateList()
|
||||
}
|
||||
@@ -78,23 +85,18 @@ class PagesThumbnailsViewModel @Inject constructor(
|
||||
|
||||
private fun loadPrevNextChapter(isNext: Boolean): Job = launchLoadingJob(Dispatchers.Default) {
|
||||
val currentId = (if (isNext) chaptersLoader.last() else chaptersLoader.first()).chapterId
|
||||
chaptersLoader.loadPrevNextChapter(mangaDetails.get(), currentId, isNext)
|
||||
chaptersLoader.loadPrevNextChapter(mangaDetails.firstNotNull(), currentId, isNext)
|
||||
updateList()
|
||||
}
|
||||
|
||||
private suspend fun updateList() {
|
||||
val snapshot = chaptersLoader.snapshot()
|
||||
val mangaChapters = mangaDetails.tryGet().getOrNull()?.chapters.orEmpty()
|
||||
val hasPrevChapter = isLoadAboveAllowed && snapshot.firstOrNull()?.chapterId != mangaChapters.firstOrNull()?.id
|
||||
val hasNextChapter = snapshot.lastOrNull()?.chapterId != mangaChapters.lastOrNull()?.id
|
||||
val mangaChapters = mangaDetails.firstNotNullOrNull()?.chapters.orEmpty()
|
||||
val pages = buildList(snapshot.size + chaptersLoader.size + 2) {
|
||||
if (hasPrevChapter) {
|
||||
add(LoadingFooter(-1))
|
||||
}
|
||||
var previousChapterId = 0L
|
||||
for (page in snapshot) {
|
||||
if (page.chapterId != previousChapterId) {
|
||||
chaptersLoader.peekChapter(page.chapterId)?.let {
|
||||
chaptersLoader.awaitChapter(page.chapterId)?.let {
|
||||
add(ListHeader(it.name))
|
||||
}
|
||||
previousChapterId = page.chapterId
|
||||
@@ -105,9 +107,6 @@ class PagesThumbnailsViewModel @Inject constructor(
|
||||
page = page,
|
||||
)
|
||||
}
|
||||
if (hasNextChapter) {
|
||||
add(LoadingFooter(1))
|
||||
}
|
||||
}
|
||||
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.list.ui.adapter.ListItemType
|
||||
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.ListModel
|
||||
import org.koitharu.kotatsu.reader.ui.thumbnails.PageThumbnail
|
||||
@@ -22,7 +21,6 @@ class PageThumbnailAdapter(
|
||||
init {
|
||||
addDelegate(ListItemType.PAGE_THUMB, pageThumbnailAD(coil, lifecycleOwner, clickListener))
|
||||
addDelegate(ListItemType.HEADER, listHeaderAD(null))
|
||||
addDelegate(ListItemType.FOOTER_LOADING, loadingFooterAD())
|
||||
}
|
||||
|
||||
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.observe
|
||||
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.details.ui.DetailsActivity
|
||||
import org.koitharu.kotatsu.list.ui.adapter.TypedListSpacingDecoration
|
||||
@@ -115,13 +116,7 @@ class ScrobblerConfigActivity : BaseActivity<ActivityScrobblerConfigBinding>(),
|
||||
}
|
||||
|
||||
private fun onLoadingStateChanged(isLoading: Boolean) {
|
||||
viewBinding.progressBar.run {
|
||||
if (isLoading) {
|
||||
show()
|
||||
} else {
|
||||
hide()
|
||||
}
|
||||
}
|
||||
viewBinding.progressBar.showOrHide(isLoading)
|
||||
}
|
||||
|
||||
private fun showUserDialog() {
|
||||
|
||||
@@ -14,7 +14,8 @@
|
||||
|
||||
<FrameLayout
|
||||
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
|
||||
android:id="@+id/recyclerView"
|
||||
@@ -29,6 +30,19 @@
|
||||
tools:listitem="@layout/item_page_thumb"
|
||||
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>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
Reference in New Issue
Block a user