From 82640a0c5d0ed3949b798d7fd0438cf2984c20ce Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 28 Nov 2022 20:21:03 +0600 Subject: [PATCH] start adding of ratings buttons --- .../commonMain/kotlin/buttons/RootButtons.kt | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 ratings/source/src/commonMain/kotlin/buttons/RootButtons.kt diff --git a/ratings/source/src/commonMain/kotlin/buttons/RootButtons.kt b/ratings/source/src/commonMain/kotlin/buttons/RootButtons.kt new file mode 100644 index 0000000..0f369d0 --- /dev/null +++ b/ratings/source/src/commonMain/kotlin/buttons/RootButtons.kt @@ -0,0 +1,42 @@ +package dev.inmo.plaguposter.ratings.source.buttons + +import dev.inmo.micro_utils.pagination.FirstPagePagination +import dev.inmo.micro_utils.pagination.Pagination +import dev.inmo.micro_utils.pagination.isFirstPage +import dev.inmo.micro_utils.pagination.utils.paginate +import dev.inmo.plaguposter.ratings.repo.RatingsRepo +import dev.inmo.plaguposter.ratings.utils.postsByRatings +import dev.inmo.tgbotapi.extensions.utils.types.buttons.dataButton +import dev.inmo.tgbotapi.extensions.utils.types.buttons.inlineKeyboard +import dev.inmo.tgbotapi.types.buttons.inline.dataInlineButton +import dev.inmo.tgbotapi.utils.row + +const val RootButtonsShowRatingData = "ratings_buttons_show" +const val RootButtonsToPageData = "ratings_buttons_to_page" + +suspend fun RatingsRepo.buildRootButtons( + pagination: Pagination = FirstPagePagination(16), + rowSize: Int = 4 +) { + val postsByRatings = postsByRatings().toList().paginate(pagination) + inlineKeyboard { + if (postsByRatings.pagesNumber > 1) { + row { + if (postsByRatings.page > 0) { + dataButton("<", "$RootButtonsToPageData ${postsByRatings.page - 1}") + } + dataButton("${postsByRatings.page}: \uD83D\uDD04", "$RootButtonsToPageData ${postsByRatings.page}") + if (postsByRatings.pagesNumber - postsByRatings.page > 1) { + dataButton(">", "$RootButtonsToPageData ${postsByRatings.page + 1}") + } + } + } + postsByRatings.results.chunked(rowSize).map { + row { + it.forEach { (rating, posts) -> + dataButton("${rating.double}: ${posts.size}", "$RootButtonsShowRatingData ${rating.double}") + } + } + } + } +}