override fun iterator() = items.iterator()
+ fun turnTo(page: Int) = apply { this.page = page }
+
}
fun <T> Iterable<T>.paginate(pageSize: Int) = Pagination<T>(toList(), pageSize)
package net.pterodactylus.sone.web.pages
import net.pterodactylus.sone.data.Post
-import net.pterodactylus.sone.utils.Pagination
+import net.pterodactylus.sone.utils.paginate
import net.pterodactylus.sone.web.WebInterface
import net.pterodactylus.sone.web.page.FreenetRequest
import net.pterodactylus.util.template.Template
override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
webInterface.core.bookmarkedPosts.let { posts ->
- val pagination = Pagination<Post>(posts.filter { it.isLoaded }.sortedByDescending { it.time }, webInterface.core.preferences.postsPerPage)
+ val pagination = posts.filter(Post::isLoaded).sortedByDescending { it.time }.paginate(webInterface.core.preferences.postsPerPage)
templateContext["pagination"] = pagination
templateContext["posts"] = pagination.items
templateContext["postsNotLoaded"] = posts.any { !it.isLoaded }
import net.pterodactylus.sone.data.Album
import net.pterodactylus.sone.data.Sone
-import net.pterodactylus.sone.utils.Pagination
+import net.pterodactylus.sone.utils.paginate
import net.pterodactylus.sone.utils.parameters
import net.pterodactylus.sone.web.WebInterface
import net.pterodactylus.sone.web.page.FreenetRequest
.filterNot(Album::isEmpty)
.sortedBy(Album::getTitle)
.also { albums ->
- Pagination(albums, webInterface.core.preferences.imagesPerPage).apply { page = freenetRequest.parameters["page"]?.toIntOrNull() ?: 0 }.also { pagination ->
+ albums.paginate(webInterface.core.preferences.imagesPerPage)
+ .turnTo(freenetRequest.parameters["page"]?.toIntOrNull() ?: 0)
+ .also { pagination ->
templateContext["albumPagination"] = pagination
templateContext["albums"] = pagination.items
}
import net.pterodactylus.sone.data.Sone
import net.pterodactylus.sone.notify.PostVisibilityFilter
-import net.pterodactylus.sone.utils.Pagination
+import net.pterodactylus.sone.utils.paginate
import net.pterodactylus.sone.utils.parameters
import net.pterodactylus.sone.web.WebInterface
import net.pterodactylus.sone.web.page.FreenetRequest
* The index page shows the main page of Sone. This page will contain the posts
* of all friends of the current user.
*/
-class IndexPage(template: Template, webInterface: WebInterface, private val postVisibilityFilter: PostVisibilityFilter):
+class IndexPage(template: Template, webInterface: WebInterface, private val postVisibilityFilter: PostVisibilityFilter) :
LoggedInPage("index.html", template, "Page.Index.Title", webInterface) {
override fun handleRequest(freenetRequest: FreenetRequest, currentSone: Sone, templateContext: TemplateContext) {
- (currentSone.posts +
- currentSone.friends
- .mapNotNull(webInterface.core::getSone)
- .flatMap { it.posts } +
- webInterface.core.getDirectedPosts(currentSone.id)
- ).distinct()
- .filter { postVisibilityFilter.isVisible(currentSone).apply(it) }
- .sortedByDescending { it.time }
- .let { posts ->
- Pagination(posts, webInterface.core.preferences.postsPerPage).apply {
- page = freenetRequest.parameters["page"]?.toIntOrNull() ?: 0
- }.let { pagination ->
- templateContext["pagination"] = pagination
- templateContext["posts"] = pagination.items
- }
- }
+ (currentSone.posts +
+ currentSone.friends
+ .mapNotNull(webInterface.core::getSone)
+ .flatMap { it.posts } +
+ webInterface.core.getDirectedPosts(currentSone.id)
+ ).distinct()
+ .filter { postVisibilityFilter.isVisible(currentSone).apply(it) }
+ .sortedByDescending { it.time }
+ .let { posts ->
+ posts.paginate(webInterface.core.preferences.postsPerPage)
+ .turnTo(freenetRequest.parameters["page"]?.toIntOrNull() ?: 0)
+ .let { pagination ->
+ templateContext["pagination"] = pagination
+ templateContext["posts"] = pagination.items
+ }
+ }
}
}
package net.pterodactylus.sone.web.pages
import net.pterodactylus.sone.data.Sone
-import net.pterodactylus.sone.utils.Pagination
+import net.pterodactylus.sone.utils.paginate
import net.pterodactylus.sone.utils.parameters
import net.pterodactylus.sone.web.WebInterface
import net.pterodactylus.sone.web.page.FreenetRequest
override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) {
getCurrentSone(freenetRequest.toadletContext).let { currentSone ->
- Pagination(webInterface.core.sones
+ webInterface.core.sones
.filterNot { freenetRequest.parameters["filter"] == "followed" && currentSone != null && !currentSone.hasFriend(it.id) }
.filterNot { freenetRequest.parameters["filter"] == "not-followed" && currentSone != null && currentSone.hasFriend(it.id) }
.filterNot { freenetRequest.parameters["filter"] == "new" && it.isKnown }
else -> comparator
}
}
- ), 25).apply { page = freenetRequest.parameters["page"]?.toIntOrNull() ?: 0 }
+ ).paginate(25)
+ .turnTo(freenetRequest.parameters["page"]?.toIntOrNull() ?: 0)
.let { pagination ->
templateContext["pagination"] = pagination
templateContext["knownSones"] = pagination.items
package net.pterodactylus.sone.web.pages
-import net.pterodactylus.sone.utils.Pagination
import net.pterodactylus.sone.utils.mapPresent
+import net.pterodactylus.sone.utils.paginate
import net.pterodactylus.sone.utils.parameters
import net.pterodactylus.sone.web.WebInterface
import net.pterodactylus.sone.web.page.FreenetRequest
.distinct()
.sortedByDescending { it.time }
.let { posts ->
- Pagination(posts, webInterface.core.preferences.postsPerPage).apply {
- page = freenetRequest.parameters["page"]?.toIntOrNull() ?: 0
- }.let { pagination ->
- templateContext["pagination"] = pagination
- templateContext["posts"] = pagination.items
- }
+ posts.paginate(webInterface.core.preferences.postsPerPage)
+ .turnTo(freenetRequest.parameters["page"]?.toIntOrNull() ?: 0)
+ .let { pagination ->
+ templateContext["pagination"] = pagination
+ templateContext["posts"] = pagination.items
+ }
}
}