startApplication method

Future<void> startApplication()
inherited

the SimpleFrameApp subclass can override with application-specific code if necessary

Implementation

Future<void> startApplication() async {
  currentState = ApplicationState.starting;
  if (mounted) setState(() {});

  // try to get the Frame into a known state by making sure there's no main loop running
  frame!.sendBreakSignal();
  await Future.delayed(const Duration(milliseconds: 500));

  // clear the previous content from the display and show a temporary loading screen while
  // we send over our scripts and resources
  await showLoadingScreen();
  await Future.delayed(const Duration(milliseconds: 100));

  // only if there are lua files to send to Frame (e.g. frame_app.lua companion app, other helper functions, minified versions)
  List<String> luaFiles = _filterLuaFiles(
      (await AssetManifest.loadFromAssetBundle(rootBundle)).listAssets());

  if (luaFiles.isNotEmpty) {
    for (var pathFile in luaFiles) {
      String fileName = pathFile.split('/').last;
      // send the lua script to the Frame
      await frame!.uploadScript(fileName, pathFile);
    }

    // kick off the main application loop: if there is only one lua file, use it;
    // otherwise require a file called "assets/frame_app.min.lua", or "assets/frame_app.lua".
    // In that case, the main app file should add require() statements for any dependent modules
    if (luaFiles.length != 1 &&
        !luaFiles.contains('assets/frame_app.min.lua') &&
        !luaFiles.contains('assets/frame_app.lua')) {
      _log.fine('Multiple Lua files uploaded, but no main file to require()');
    } else {
      if (luaFiles.length == 1) {
        String fileName = luaFiles[0]
            .split('/')
            .last; // e.g. "assets/my_file.min.lua" -> "my_file.min.lua"
        int lastDotIndex = fileName.lastIndexOf(".lua");
        String bareFileName = fileName.substring(
            0, lastDotIndex); // e.g. "my_file.min.lua" -> "my_file.min"

        await frame!
            .sendString('require("$bareFileName")', awaitResponse: true);
      } else if (luaFiles.contains('assets/frame_app.min.lua')) {
        await frame!
            .sendString('require("frame_app.min")', awaitResponse: true);
      } else if (luaFiles.contains('assets/frame_app.lua')) {
        await frame!.sendString('require("frame_app")', awaitResponse: true);
      }

      // load all the Sprites from assets/sprites
      await _uploadSprites(_filterSpriteAssets(
          (await AssetManifest.loadFromAssetBundle(rootBundle))
              .listAssets()));
    }
  } else {
    await frame!.clearDisplay();
    await Future.delayed(const Duration(milliseconds: 100));
  }

  currentState = ApplicationState.ready;
  if (mounted) setState(() {});
}