On Beagle, you are able to perform Operations it can be sum, subtraction, etc, using the context and the DSL, but you can also can customize your operation.
To register your custom operation, it is necessary to use the createOperation method that receives as a parameter:
String
with the operation’s name;Array<out Any?>
, containing the necessary parameters according to the custom operation.The example below shows a isValidCpf
operation where the Text
component text vary according to the verification result, check out:
fun isValidCpf(param: Array<out String?>) : Bind.Expression<Boolean> {
param.filterNotNull().map { value ->
if (value.isEmpty()) return expressionOf("@{${false}}")
val numbers = arrayListOf<Int>()
value.filter { it.isDigit() }.forEach {
numbers.add(it.toString().toInt())
}
if (numbers.size != 11) return expressionOf("@{${false}}")
for (n in 0..9) {
val digits = arrayListOf<Int>()
repeat((0..10).count()) { digits.add(n) }
if (numbers == digits) return expressionOf("@{${false}}")
}
val dv1 = ((0..8).sumOf { (it + 1) * numbers[it] }).rem(11).let {
if (it >= 10) 0 else it
}
val dv2 = ((0..8).sumOf { it * numbers[it] }.let { (it + (dv1 * 9)).rem(11) }).let {
if (it >= 10) 0 else it
}
return expressionOf("@{${numbers[9] == dv1 && numbers[10] == dv2}}")
}
return createOperation("isValidCpf", arrayOf(param))
}
In the example, the custom operation created validates a CPF (Brazil’s security number) and it returns if the result was valid or not. For that, make a validation is necessary because it returned the createOperation method with the operation name and the expected parameter. The result was:
fun screen() = Screen(
navigationBar = NavigationBar(title = "Custom operation", showBackButton = true),
child = Container(
context = ContextData("cpf", "00000000000"),
children = listOf(
Button(
text = "CPF atual: @{cpf}",
onPress = listOf(
SetContext(contextId = "cpf", value = "42249625000")
)
),
Text(
condition(
isValidCpf(arrayOf("@{cpf}")),
constant("cpf is valid"), constant("cpf is not valid")
)
)
)
)
)
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.