mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI.git
synced 2024-11-21 15:53:47 +00:00
fix of #761
This commit is contained in:
parent
f85d4a36e1
commit
41e369cea5
@ -25,4 +25,23 @@ data class InlineKeyboardMarkup(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
operator fun plus(other: InlineKeyboardMarkup): InlineKeyboardMarkup {
|
||||
return InlineKeyboardMarkup(
|
||||
keyboard + other.keyboard
|
||||
)
|
||||
}
|
||||
|
||||
operator fun minus(other: InlineKeyboardMarkup): InlineKeyboardMarkup {
|
||||
val otherButtons = other.keyboard.flatten()
|
||||
return InlineKeyboardMarkup(
|
||||
keyboard.mapNotNull { row ->
|
||||
row.filter { button ->
|
||||
button !in otherButtons
|
||||
}.takeIf {
|
||||
it.isNotEmpty()
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -22,4 +22,32 @@ data class ReplyKeyboardMarkup(
|
||||
error("Field $inputFieldPlaceholderField length must be in $inputFieldPlaceholderLimit, but was ${inputFieldPlaceholder.length}")
|
||||
}
|
||||
}
|
||||
|
||||
fun add(other: ReplyKeyboardMarkup, placeholderDelimiter: String = "\n"): ReplyKeyboardMarkup {
|
||||
return ReplyKeyboardMarkup(
|
||||
keyboard = keyboard + other.keyboard,
|
||||
resizeKeyboard = resizeKeyboard ?.or(other.resizeKeyboard ?: false) ?: other.resizeKeyboard,
|
||||
oneTimeKeyboard = oneTimeKeyboard ?.or(other.oneTimeKeyboard ?: false) ?: other.oneTimeKeyboard,
|
||||
inputFieldPlaceholder = inputFieldPlaceholder ?.plus(other.inputFieldPlaceholder ?.let { placeholderDelimiter + it } ?: "") ?: other.inputFieldPlaceholder,
|
||||
selective = selective ?.or(other.selective ?: false) ?: other.selective,
|
||||
persistent = persistent ?.or(other.persistent ?: false) ?: other.persistent,
|
||||
)
|
||||
}
|
||||
|
||||
operator fun plus(other: ReplyKeyboardMarkup): ReplyKeyboardMarkup {
|
||||
return add(other)
|
||||
}
|
||||
|
||||
operator fun minus(other: ReplyKeyboardMarkup): ReplyKeyboardMarkup {
|
||||
val otherButtons = other.keyboard.flatten()
|
||||
return copy(
|
||||
keyboard.mapNotNull { row ->
|
||||
row.filter { button ->
|
||||
button !in otherButtons
|
||||
}.takeIf {
|
||||
it.isNotEmpty()
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
package dev.inmo.tgbotapi.utils
|
||||
|
||||
import dev.inmo.micro_utils.common.withReplaced
|
||||
import dev.inmo.tgbotapi.types.buttons.Matrix
|
||||
|
||||
/**
|
||||
@ -67,6 +68,18 @@ open class RowBuilder<T> {
|
||||
|
||||
fun add(t: T) = mutRow.add(t)
|
||||
operator fun T.unaryPlus() = add(this)
|
||||
|
||||
fun replace(i: Int, new: T) {
|
||||
mutRow[i] = new
|
||||
}
|
||||
fun replace(old: T, new: T): Boolean {
|
||||
val i = mutRow.indexOf(old).takeIf { it > -1 } ?: return false
|
||||
replace(i, new)
|
||||
return mutRow[i] == new
|
||||
}
|
||||
fun remove(i: Int): T {
|
||||
return mutRow.removeAt(i)
|
||||
}
|
||||
}
|
||||
|
||||
open class MatrixBuilder<T> {
|
||||
@ -77,4 +90,18 @@ open class MatrixBuilder<T> {
|
||||
fun add(t: List<T>) = mutMatrix.add(t)
|
||||
operator fun plus(t: List<T>) = add(t)
|
||||
operator fun T.unaryPlus() = add(listOf(this))
|
||||
|
||||
fun modifyRow(i: Int, block: RowBuilder<T>.() -> Unit) {
|
||||
val exists = matrix[i]
|
||||
val rowBuilder = RowBuilder<T>()
|
||||
exists.forEach { rowBuilder.add(it) }
|
||||
mutMatrix[i] = rowBuilder.apply(block).row
|
||||
}
|
||||
fun modifyRow(row: List<T>, block: RowBuilder<T>.() -> Unit) {
|
||||
val i = mutMatrix.indexOf(row).takeIf { it > -1 } ?: return false
|
||||
modifyRow(i, block)
|
||||
}
|
||||
fun remove(i: Int): List<T> {
|
||||
return mutMatrix.removeAt(i)
|
||||
}
|
||||
}
|
||||
|
@ -49,6 +49,19 @@ inline fun flatInlineKeyboard(
|
||||
block: InlineKeyboardRowBuilder.() -> Unit
|
||||
) = inlineKeyboard { row<InlineKeyboardButton>(block) }
|
||||
|
||||
/**
|
||||
* Factory-function for [InlineKeyboardBuilder]. It will [apply] [block] to internally created [InlineKeyboardMarkup]
|
||||
* and [InlineKeyboardBuilder.build] [InlineKeyboardMarkup] then
|
||||
*
|
||||
* @see InlineKeyboardBuilder.row
|
||||
*/
|
||||
inline fun InlineKeyboardMarkup.modified(
|
||||
block: InlineKeyboardBuilder.() -> Unit
|
||||
) = InlineKeyboardBuilder().apply {
|
||||
keyboard.forEach { add(it) }
|
||||
block()
|
||||
}.build()
|
||||
|
||||
|
||||
/**
|
||||
* Creates and put [PayInlineKeyboardButton]
|
||||
|
Loading…
Reference in New Issue
Block a user