add pagination to recentChapters (#246)

* add pagination to recentChapters

* Use kotlin native library

Co-authored-by: Mitchell Syer <Mitchellptbo@gmail.com>

Co-authored-by: Mitchell Syer <Mitchellptbo@gmail.com>
This commit is contained in:
Aria Moradi
2021-11-08 18:45:52 +03:30
committed by GitHub
parent 420d14fc37
commit 30f7cdc1ba
5 changed files with 164 additions and 13 deletions

View File

@@ -0,0 +1,40 @@
package suwayomi.tachidesk.manga.model.dataclass
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
import kotlin.math.min
data class PaginatedList<T>(
val page: List<T>,
val hasNextPage: Boolean,
)
const val PaginationFactor = 50
fun <T> paginatedFrom(
pageNum: Int,
paginationFactor: Int = PaginationFactor,
lister: () -> List<T>
): PaginatedList<T> {
val list = lister()
val lastIndex = list.size - 1
val lowerIndex = pageNum * paginationFactor
val higherIndex = (pageNum + 1) * paginationFactor - 1
if (lowerIndex > lastIndex) {
return PaginatedList(emptyList(), false)
}
val sliced = list.slice(lowerIndex..min(lastIndex, higherIndex))
return PaginatedList(
sliced,
higherIndex < lastIndex
)
}