From: David ‘Bombe’ Roden Date: Thu, 4 Jan 2018 20:24:47 +0000 (+0100) Subject: Paginate things a bit differently X-Git-Tag: v79^2~238 X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=commitdiff_plain;h=d4d31bed957e29d50bd313ab973a034eba5a07f3 Paginate things a bit differently --- diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/Pagination.kt b/src/main/kotlin/net/pterodactylus/sone/utils/Pagination.kt index 6c436e2..3330e78 100644 --- a/src/main/kotlin/net/pterodactylus/sone/utils/Pagination.kt +++ b/src/main/kotlin/net/pterodactylus/sone/utils/Pagination.kt @@ -39,6 +39,8 @@ class Pagination(private val originalItems: List, pageSize: Int): Iter override fun iterator() = items.iterator() + fun turnTo(page: Int) = apply { this.page = page } + } fun Iterable.paginate(pageSize: Int) = Pagination(toList(), pageSize) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/BookmarksPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/BookmarksPage.kt index 8d33977..d138263 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/BookmarksPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/BookmarksPage.kt @@ -1,7 +1,7 @@ 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 @@ -15,7 +15,7 @@ class BookmarksPage(template: Template, webInterface: WebInterface) : override fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { webInterface.core.bookmarkedPosts.let { posts -> - val pagination = Pagination(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 } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPage.kt index e9911e6..bf1ed2a 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPage.kt @@ -2,7 +2,7 @@ package net.pterodactylus.sone.web.pages 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 @@ -33,7 +33,9 @@ class ImageBrowserPage(template: Template, webInterface: WebInterface): .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 } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt index 0257cb1..1e39712 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt @@ -2,7 +2,7 @@ package net.pterodactylus.sone.web.pages 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 @@ -13,26 +13,26 @@ import net.pterodactylus.util.template.TemplateContext * 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 + } + } } } diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPage.kt index a3969b2..aa4b2db 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPage.kt @@ -1,7 +1,7 @@ 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 @@ -16,7 +16,7 @@ class KnownSonesPage(template: Template, webInterface: WebInterface): 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 } @@ -35,7 +35,8 @@ class KnownSonesPage(template: Template, webInterface: WebInterface): 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 diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt index c774ceb..5218ce8 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt @@ -1,7 +1,7 @@ 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 @@ -21,12 +21,12 @@ class NewPage(template: Template, webInterface: WebInterface): .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 + } } }