⌘K

Action

Actions let your app do something when a user interacts—like pressing a button—or when something happens automatically. In Nativeblocks, an action is just a piece of code that runs because something triggered it.

Every action uses the INativeAction protocol. You provide all the info it needs, and Nativeblocks runs your code for you.


How does an Action usually work?

Here’s what happens step by step:

  1. Something happens (for example, a button gets tapped).
  2. Nativeblocks looks up which action to use from the NativeActionProvider.
  3. It calls your handle(actionProps:) function so your code can do its job.

INativeAction

This is just a protocol—a kind of rule—so your code knows what an action should look like. You implement your own actions using it.

public protocol INativeAction

What’s required?

handle(actionProps:)

This is the main function Nativeblocks will call when it’s time to run your action.

func handle(actionProps: ActionProps)
  • actionProps: All the data you get so your action can do whatever is needed.

ActionProps

When it’s time to run your action, Nativeblocks hands you an ActionProps struct, packed with everything you need to know about what happened and what you might want to change.

public struct ActionProps

Here’s what you get inside ActionProps—and what it means:

  • listItemIndex
    The position of the item in a list, if your action was triggered from a list.
    public var listItemIndex: Int? = nil
    
  • variables
    Any variables you might want to read or change during the action.
    public var variables: [String: NativeVariableModel]? = nil
    
  • onVariableChange
    If your action needs to update a variable, use this callback.
    public var onVariableChange: ((NativeVariableModel) -> Void)? = nil
    
  • blocks
    A list of blocks that might be useful to your action (blocks are just building pieces of your screen).
    public var blocks: [String: NativeBlockModel]? = nil
    
  • onChangeBlock
    Call this if you want to change something about a block.
    public var onChangeBlock: ((NativeBlockModel) -> Void)? = nil
    
  • trigger
    What caused this action to happen, including more details if you need them.
    public var trigger: NativeActionTriggerModel? = nil
    
  • onHandleNextTrigger
    If your action needs to run the next "trigger" step, call this.
    public var onHandleNextTrigger: ((NativeActionTriggerModel) -> Void)? = nil
    
  • onHandleSuccessNextTrigger
    Call this if your action was successful and you want to keep going.
    public var onHandleSuccessNextTrigger: ((NativeActionTriggerModel) -> Void)? = nil
    
  • onHandleFailureNextTrigger
    Call this if something fails and you want to react to it.
    public var onHandleFailureNextTrigger: ((NativeActionTriggerModel) -> Void)? = nil
    

NativeActionModel

This is the main way to describe an action and all its basic info.

public struct NativeActionModel: Hashable, Codable
  • id
    The unique id for the action.
    public let id: String?
    
  • key
    Some extra key used for identifying your action.
    public let key: String?
    
  • event
    What event will make this action run.
    public let event: String?
    
  • triggers
    All the steps (triggers) inside this action.
    public let triggers: [NativeActionTriggerModel?]?
    

NativeActionTriggerModel

A "trigger" is a part of an action—think of it like a step or branch. It has extra info and knows what to do when run.

public struct NativeActionTriggerModel: Hashable, Codable
  • id
    The id of this trigger.
    public let id: String?
    
  • parentId
    If this trigger is part of another trigger, this is its parent’s id.
    public let parentId: String?
    
  • keyType
    What kind of trigger (for example, "show", "hide", or something custom).
    public let keyType: String?
    
  • then
    What should happen afterwards (see below for the possible options).
    public let then: NativeActionTriggerThen?
    
  • properties
    Settings or options for this trigger.
    public let properties: [String: NativeActionTriggerPropertyModel]?
    
  • data
    Extra data you can use inside this trigger.
    public let data: [String: NativeActionTriggerDataModel]?
    
  • subTriggers
    If this trigger has child steps, they go here.
    public var subTriggers: [NativeActionTriggerModel?]?
    

NativeActionTriggerDataModel

Think of this as one piece of data for a trigger—a key name, a value, and the type.

public struct NativeActionTriggerDataModel: Hashable, Codable
  • key
    The name for this data value.
    public let key: String
    
  • value
    The actual data.
    public let value: String
    
  • type
    What kind of value this is. Could be "string", "number", or something else.
    public let type: String
    

NativeActionTriggerPropertyModel

Almost the same as the data model above, but for settings or properties of a trigger.

public struct NativeActionTriggerPropertyModel: Hashable, Codable
  • key
    The property’s name.
    public let key: String
    
  • value
    What the property is set to.
    public let value: String
    
  • type
    What kind of property this is (could be "string", "bool", etc).
    public let type: String
    

NativeActionTriggerThen

This tells the system what should happen after a trigger runs.

public enum NativeActionTriggerThen: String, Hashable, Codable

Here are the possible options:

  • success
    Do something when the trigger works.
    case success = "SUCCESS"
    
  • failure
    Do something when the trigger fails.
    case failure = "FAILURE"
    
  • next
    Just move on to the next trigger or step.
    case next = "NEXT"
    
  • end
    Don’t do any more triggers.
    case end = "END"