On Beagle, it’s possible to create a customized action from a CustomAction
.
You must create a class that implements an Action
. This same class will request the implementation of execute’s method and will dictate the actions o action.
The listed msg
attribute is only a parameter example that must be stated on this class constructor.
The following example shows an action with Alert
:
class CustomAction: Action {
let msg: String?
init(msg: String? = nil) {
self.msg = msg
}
func execute(controller: BeagleController, origin: UIView) {
let alert = UIAlertController(
title: nil,
message: msg,
preferredStyle: .alert
)
controller.present(alert, animated: true)
}
}
To register the action, just create a method with all your custom action and calls the registerCustomAction
Beagle’s method to make the registry.
Call the method created on BeagleConfig, that can be seen on this example.
private func registerCustomAction() {
Beagle.registerCustomAction("CustomAction", actionType: CustomAction.self)
}
To create this action, your code must be like:
@RegisterAction
data class CustomAction(
val msg: String? = null
) : Action
Below, see an example of an Action in a Button’s type component:
{
"_beagleComponent_": "beagle:button",
"text": "Beagle Button",
"onPress": [
{
"_beagleAction_": "custom:customAction",
"msg": "I am a Toast."
}
]
}
Button(
text = "Beagle Button",
onPress = listOf(CustomAction("I am a Toast"))
)
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.