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.asFlow
import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
@ -161,8 +162,7 @@ open class BrowseSourcePresenter(
// Create a new pager. // Create a new pager.
pager = createPager( pager = createPager(
query, query,
filters.takeIf { it.isNotEmpty() || query.isBlank() } filters.takeIf { it.isNotEmpty() || query.isBlank() } ?: source.getFilterList(),
?: source.getFilterList(),
) )
val sourceId = source.id val sourceId = source.id
@ -174,24 +174,33 @@ open class BrowseSourcePresenter(
// Prepare the pager. // Prepare the pager.
pagerJob?.cancel() pagerJob?.cancel()
pagerJob = presenterScope.launchIO { pagerJob = presenterScope.launchIO {
pager.results().onEach { (page, second) -> pager.asFlow()
try { .map { (first, second) ->
val mangas = second first to second
.map { networkToLocalManga(it, sourceId) } .map { networkToLocalManga(it, sourceId) }
.filter { !preferences.hideInLibraryItems().get() || !it.favorite } .filter { !preferences.hideInLibraryItems().get() || !it.favorite }
if (mangas.isEmpty() && page == 1) { }
withUIContext { view?.onAddPageError(NoResultsException()) } .onEach { initializeMangas(it.second) }
return@onEach .map { (first, second) ->
first to second.map {
BrowseSourceItem(
it,
browseAsList,
sourceListType,
outlineCovers,
)
} }
initializeMangas(mangas) }
val items = mangas.map { .catch { error ->
BrowseSourceItem(it, browseAsList, sourceListType, outlineCovers)
}
withUIContext { view?.onAddPage(page, items) }
} catch (error: Exception) {
Logger.e(error) { "Unable to prepare a page" } 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. // Request first page.

View file

@ -16,7 +16,7 @@ abstract class Pager(var currentPage: Int = 1) {
protected val results = MutableSharedFlow<Pair<Int, List<SManga>>>() 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() return results.asSharedFlow()
} }