All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.vendeli.tgbot.interfaces.KeyboardBuilder.kt Maven / Gradle / Ivy

There is a newer version: 6.6.0
Show newest version
package eu.vendeli.tgbot.interfaces

abstract class KeyboardBuilder {
    protected abstract val buttons: MutableList

    /**
     * Adds a line break, so that the following buttons will be on a new line.
     */
    fun newLine() {
        buttons.add(null)
    }

    /**
     * Same as [newLine].
     *
     * @see [newLine]
     */
    fun br() = newLine()

    /**
     * The function that collects and returns [Keyboard]
     *
     * @return
     */
    internal fun build(): List> {
        val builtButtons = mutableListOf>()

        val buttonsIterator = buttons.iterator()
        var currentLine: MutableList = mutableListOf()

        while (buttonsIterator.hasNext()) {
            val item = buttonsIterator.next()
            if (item == null) {
                builtButtons.add(currentLine)
                currentLine = mutableListOf()
                continue
            }
            currentLine.add(item)
        }
        if (currentLine.isNotEmpty()) builtButtons.add(currentLine)

        return builtButtons
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy