refactor(browse): Move stuff around

This commit is contained in:
Ahmad Ansori Palembani 2024-12-04 17:30:02 +07:00
parent cb265d2225
commit 2e12817735
Signed by: null2264
GPG key ID: BA64F8B60AF3EFB6
2 changed files with 25 additions and 16 deletions

View file

@ -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
}
initializeMangas(mangas)
val items = mangas.map {
BrowseSourceItem(it, browseAsList, sourceListType, outlineCovers)
.onEach { initializeMangas(it.second) }
.map { (first, second) ->
first to second.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.

View file

@ -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()
}