Skip to content

Commit d59a7aa

Browse files
committed
Votes to multiline inline keyboard
1 parent bbfe3b9 commit d59a7aa

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

votes/tgbotapi-extensions/src/main/kotlin/by/jprof/telegram/bot/votes/tgbotapi_extensions/VotesExtensions.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,19 @@ fun Votes.toInlineKeyboardMarkup() = InlineKeyboardMarkup(
1616
}
1717
)
1818
)
19+
20+
fun Votes.toInlineKeyboardMarkup(size: Int) = InlineKeyboardMarkup(
21+
if (this.options.isEmpty()) {
22+
listOf(emptyList())
23+
} else {
24+
this
25+
.options
26+
.map {
27+
CallbackDataInlineKeyboardButton(
28+
text = "${this.count(it)} $it",
29+
callbackData = "${this.id}:$it"
30+
)
31+
}
32+
.chunked(size)
33+
}
34+
)

votes/tgbotapi-extensions/src/test/kotlin/by/jprof/telegram/bot/votes/tgbotapi_extensions/VotesExtensionsTest.kt

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ internal class VotesExtensionsTest {
1919
Assertions.assertEquals(keyboard, votes.toInlineKeyboardMarkup())
2020
}
2121

22+
@ParameterizedTest
23+
@MethodSource
24+
fun toInlineKeyboardMarkupChunked(votes: Votes, size: Int, keyboard: InlineKeyboardMarkup) {
25+
Assertions.assertEquals(keyboard, votes.toInlineKeyboardMarkup(size))
26+
}
27+
2228
private fun toInlineKeyboardMarkup(): Stream<Arguments> = sequence {
2329
yield(
2430
Arguments.of(
@@ -72,4 +78,80 @@ internal class VotesExtensionsTest {
7278
)
7379
)
7480
}.asStream()
81+
82+
private fun toInlineKeyboardMarkupChunked(): Stream<Arguments> = sequence {
83+
yield(
84+
Arguments.of(
85+
Votes("test"),
86+
8,
87+
InlineKeyboardMarkup(
88+
listOf(emptyList())
89+
)
90+
)
91+
)
92+
yield(
93+
Arguments.of(
94+
Votes("test", listOf("1")),
95+
8,
96+
InlineKeyboardMarkup(
97+
listOf(
98+
listOf(
99+
CallbackDataInlineKeyboardButton("0 1", "test:1")
100+
)
101+
)
102+
)
103+
)
104+
)
105+
yield(
106+
Arguments.of(
107+
Votes("test", listOf("1", "2")),
108+
8,
109+
InlineKeyboardMarkup(
110+
listOf(
111+
listOf(
112+
CallbackDataInlineKeyboardButton("0 1", "test:1"),
113+
CallbackDataInlineKeyboardButton("0 2", "test:2"),
114+
)
115+
)
116+
)
117+
)
118+
)
119+
yield(
120+
Arguments.of(
121+
Votes("test", listOf("1", "2")),
122+
1,
123+
InlineKeyboardMarkup(
124+
listOf(
125+
listOf(
126+
CallbackDataInlineKeyboardButton("0 1", "test:1"),
127+
),
128+
listOf(
129+
CallbackDataInlineKeyboardButton("0 2", "test:2"),
130+
)
131+
)
132+
)
133+
)
134+
)
135+
yield(
136+
Arguments.of(
137+
Votes(
138+
"test",
139+
listOf("1", "2", "3"),
140+
mapOf("user1" to "1", "user2" to "2", "user3" to "UNEXISTING_OPTION")
141+
),
142+
2,
143+
InlineKeyboardMarkup(
144+
listOf(
145+
listOf(
146+
CallbackDataInlineKeyboardButton("1 1", "test:1"),
147+
CallbackDataInlineKeyboardButton("1 2", "test:2"),
148+
),
149+
listOf(
150+
CallbackDataInlineKeyboardButton("0 3", "test:3"),
151+
)
152+
)
153+
)
154+
)
155+
)
156+
}.asStream()
75157
}

0 commit comments

Comments
 (0)