runOnTap method

Future<void> runOnTap({
  1. String? luaScript,
  2. void callback()?,
})

Runs a callback when the Frame is tapped. Can include Lua code to be run on Frame upon tap and/or a Dart callback to be run locally upon tap. Clears any existing callbacks and replaces them with the new ones passed in, so pass in null for both arguments if you want to remove any existing callbacks.

Args: luaScript (String?): The Lua script to run on tap. callback (void Function()?): The callback to run on tap.

Implementation

Future<void> runOnTap({String? luaScript, void Function()? callback}) async {
  if (_tappedSubscription != null) {
    _tappedSubscription!.cancel();
    _tappedSubscription = null;
  }

  if (callback != null) {
    _tappedSubscription = tappedStream().listen((_) => callback());
  }

  String luaCode = '';
  if (luaScript != null && callback != null) {
    luaCode =
        "function on_tap();frame.bluetooth.send('\\x${FrameDataTypePrefixes.tap.valueAsHex}');$luaScript;end;frame.imu.tap_callback(on_tap)";
  } else if (luaScript == null && callback != null) {
    luaCode =
        "function on_tap();frame.bluetooth.send('\\x${FrameDataTypePrefixes.tap.valueAsHex}');end;frame.imu.tap_callback(on_tap)";
  } else if (luaScript != null && callback == null) {
    luaCode =
        "function on_tap();$luaScript;end;frame.imu.tap_callback(on_tap)";
  } else {
    luaCode = "frame.imu.tap_callback(nil)";
  }

  await frame.runLua(luaCode, checked: callback == null);
}