mirror of
https://github.com/null2264/yokai.git
synced 2025-06-21 10:44:42 +00:00
refactor(browse): Move stuff around
This commit is contained in:
parent
cb265d2225
commit
2e12817735
2 changed files with 25 additions and 16 deletions
|
@ -33,6 +33,7 @@ import kotlinx.coroutines.Job
|
|||
import kotlinx.coroutines.flow.asFlow
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
import kotlinx.coroutines.flow.filter
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
|
@ -161,8 +162,7 @@ open class BrowseSourcePresenter(
|
|||
// Create a new pager.
|
||||
pager = createPager(
|
||||
query,
|
||||
filters.takeIf { it.isNotEmpty() || query.isBlank() }
|
||||
?: source.getFilterList(),
|
||||
filters.takeIf { it.isNotEmpty() || query.isBlank() } ?: source.getFilterList(),
|
||||
)
|
||||
|
||||
val sourceId = source.id
|
||||
|
@ -174,24 +174,33 @@ open class BrowseSourcePresenter(
|
|||
// Prepare the pager.
|
||||
pagerJob?.cancel()
|
||||
pagerJob = presenterScope.launchIO {
|
||||
pager.results().onEach { (page, second) ->
|
||||
try {
|
||||
val mangas = second
|
||||
pager.asFlow()
|
||||
.map { (first, second) ->
|
||||
first to second
|
||||
.map { networkToLocalManga(it, sourceId) }
|
||||
.filter { !preferences.hideInLibraryItems().get() || !it.favorite }
|
||||
if (mangas.isEmpty() && page == 1) {
|
||||
withUIContext { view?.onAddPageError(NoResultsException()) }
|
||||
return@onEach
|
||||
}
|
||||
.onEach { initializeMangas(it.second) }
|
||||
.map { (first, second) ->
|
||||
first to second.map {
|
||||
BrowseSourceItem(
|
||||
it,
|
||||
browseAsList,
|
||||
sourceListType,
|
||||
outlineCovers,
|
||||
)
|
||||
}
|
||||
initializeMangas(mangas)
|
||||
val items = mangas.map {
|
||||
BrowseSourceItem(it, browseAsList, sourceListType, outlineCovers)
|
||||
}
|
||||
withUIContext { view?.onAddPage(page, items) }
|
||||
} catch (error: Exception) {
|
||||
}
|
||||
.catch { error ->
|
||||
Logger.e(error) { "Unable to prepare a page" }
|
||||
}
|
||||
}.collect()
|
||||
.collectLatest { (page, mangas) ->
|
||||
if (mangas.isEmpty() && page == 1) {
|
||||
withUIContext { view?.onAddPageError(NoResultsException()) }
|
||||
return@collectLatest
|
||||
}
|
||||
withUIContext { view?.onAddPage(page, mangas) }
|
||||
}
|
||||
}
|
||||
|
||||
// Request first page.
|
||||
|
|
|
@ -16,7 +16,7 @@ abstract class Pager(var currentPage: Int = 1) {
|
|||
|
||||
protected val results = MutableSharedFlow<Pair<Int, List<SManga>>>()
|
||||
|
||||
fun results(): SharedFlow<Pair<Int, List<SManga>>> {
|
||||
fun asFlow(): SharedFlow<Pair<Int, List<SManga>>> {
|
||||
return results.asSharedFlow()
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue