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

commonMain.dev.inmo.tgbotapi.utils.Matrix.kt Maven / Gradle / Ivy

Go to download

Core part of tgbotapi with all (and only) required functionality for working with Telegram Bot API

There is a newer version: 24.0.1
Show newest version
package dev.inmo.tgbotapi.utils

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

/**
 * @see dev.inmo.tgbotapi.extensions.utils.types.buttons.InlineKeyboardRowBuilder
 * @see dev.inmo.tgbotapi.extensions.utils.types.buttons.ReplyKeyboardRowBuilder
 */
inline fun  row(block: RowBuilder.() -> Unit): List {
    return RowBuilder().apply(block).row
}

/**
 * @see dev.inmo.tgbotapi.extensions.utils.types.buttons.InlineKeyboardRowBuilder
 * @see dev.inmo.tgbotapi.extensions.utils.types.buttons.ReplyKeyboardRowBuilder
 */
inline fun  MatrixBuilder.row(block: RowBuilder.() -> Unit) {
    add(RowBuilder().apply(block).row)
}

/**
 * @see dev.inmo.tgbotapi.extensions.utils.types.buttons.InlineKeyboardRowBuilder
 * @see dev.inmo.tgbotapi.extensions.utils.types.buttons.ReplyKeyboardRowBuilder
 */
inline fun  MatrixBuilder.row(vararg elements: T) {
    add(elements.toList())
}

/**
 * @see dev.inmo.tgbotapi.extensions.utils.types.buttons.InlineKeyboardBuilder
 * @see dev.inmo.tgbotapi.extensions.utils.types.buttons.ReplyKeyboardBuilder
 */
inline fun  matrix(block: MatrixBuilder.() -> Unit): Matrix {
    return MatrixBuilder().also(block).matrix
}

/**
 * @see dev.inmo.tgbotapi.extensions.utils.types.buttons.InlineKeyboardBuilder
 * @see dev.inmo.tgbotapi.extensions.utils.types.buttons.ReplyKeyboardBuilder
 */
inline fun  flatMatrix(block: RowBuilder.() -> Unit): Matrix {
    return MatrixBuilder().apply {
        row(block)
    }.matrix
}

/**
 * @see dev.inmo.tgbotapi.extensions.utils.types.buttons.InlineKeyboardBuilder
 * @see dev.inmo.tgbotapi.extensions.utils.types.buttons.ReplyKeyboardBuilder
 */
inline fun  flatMatrix(vararg elements: T): Matrix {
    return MatrixBuilder().apply {
        row { elements.forEach { +it } }
    }.matrix
}

/**
 * @see dev.inmo.tgbotapi.extensions.utils.types.buttons.InlineKeyboardRowBuilder
 * @see dev.inmo.tgbotapi.extensions.utils.types.buttons.ReplyKeyboardRowBuilder
 */
operator fun  RowBuilder.plus(t: T) = add(t)

open class RowBuilder {
    private val mutRow: MutableList = ArrayList()
    val row: List
        get() = mutRow

    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 {
    private val mutMatrix: MutableList> = ArrayList()
    val matrix: Matrix
        get() = mutMatrix.toList()

    fun add(t: List) = mutMatrix.add(t)
    operator fun plus(t: List) = add(t)
    operator fun T.unaryPlus() = add(listOf(this))

    fun modifyRow(i: Int, block: RowBuilder.() -> Unit) {
        val exists = matrix[i]
        val rowBuilder = RowBuilder()
        exists.forEach { rowBuilder.add(it) }
        mutMatrix[i] = rowBuilder.apply(block).row
    }
    fun modifyRow(row: List, block: RowBuilder.() -> Unit): Boolean {
        val i = mutMatrix.indexOf(row).takeIf { it > -1 } ?: return false
        modifyRow(i, block)
        return true
    }
    fun remove(i: Int): List {
        return mutMatrix.removeAt(i)
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy