Skip to content

Commit

Permalink
fix of #761
Browse files Browse the repository at this point in the history
  • Loading branch information
InsanusMokrassar committed Nov 11, 2024
1 parent f85d4a3 commit 41e369c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.inmo.tgbotapi.utils

import dev.inmo.micro_utils.common.withReplaced
import dev.inmo.tgbotapi.types.buttons.Matrix

/**
Expand Down Expand Up @@ -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> {
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 41e369c

Please sign in to comment.