Paginate things a bit differently
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 4 Jan 2018 20:24:47 +0000 (21:24 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Thu, 4 Jan 2018 20:33:25 +0000 (21:33 +0100)
src/main/kotlin/net/pterodactylus/sone/utils/Pagination.kt
src/main/kotlin/net/pterodactylus/sone/web/pages/BookmarksPage.kt
src/main/kotlin/net/pterodactylus/sone/web/pages/ImageBrowserPage.kt
src/main/kotlin/net/pterodactylus/sone/web/pages/IndexPage.kt
src/main/kotlin/net/pterodactylus/sone/web/pages/KnownSonesPage.kt
src/main/kotlin/net/pterodactylus/sone/web/pages/NewPage.kt

index 6c436e2..3330e78 100644 (file)
@@ -39,6 +39,8 @@ class Pagination<out T>(private val originalItems: List<T>, pageSize: Int): Iter
 
        override fun iterator() = items.iterator()
 
+       fun turnTo(page: Int) = apply { this.page = page }
+
 }
 
 fun <T> Iterable<T>.paginate(pageSize: Int) = Pagination<T>(toList(), pageSize)
index 8d33977..d138263 100644 (file)
@@ -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<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 }
index e9911e6..bf1ed2a 100644 (file)
@@ -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
                                                }
index 0257cb1..1e39712 100644 (file)
@@ -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
+                                                       }
+                               }
        }
 
 }
index a3969b2..aa4b2db 100644 (file)
@@ -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
index c774ceb..5218ce8 100644 (file)
@@ -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
+                                                                       }
                                                }
                        }