dartCallbackManagerCode static method

String dartCallbackManagerCode(
  1. UniAPIOptions options, {
  2. List<Model>? models,
})

Implementation

static String dartCallbackManagerCode(UniAPIOptions options,
    {List<Model>? models}) {
  final imports = <CodeUnit>[];
  models?.forEach((model) {
    imports.add(DartImport(
        fullClassName: relative(
            productPath(options.dartOutput, model.inputFile.path),
            from: dirname(
                '${options.dartOutput}/$projectName/$dotDartFileUniCallbackManager'))));
  });

  const generateDepth = 3;
  final generateList = <CodeUnit>[];
  generateList.add(OneLine(
      depth: generateDepth,
      body: 'if (callback.callbackName != callbackName) continue;'));
  generateList.add(OneLine(
      depth: generateDepth,
      body:
          'UniCallbackDisposable disposable = UniCallbackDisposable(callback);'));
  models?.forEach((model) {
    generateList.add(OneLine(
        depth: generateDepth,
        body: 'if (callback.getType() == ${model.name}) {'));
    generateList.add(OneLine(
        depth: generateDepth + 1,
        body:
            '(callback as UniCallback<${model.name}>).onEvent(${model.name}${model.dartDecodedFuncName(model.isEnum, 'data')}, disposable);'));
    generateList.add(OneLine(depth: generateDepth + 1, body: 'continue;'));
    generateList.add(OneLine(depth: generateDepth, body: '}'));
  });
  final generateListStr = CodeUnit.join(generateList);

  return CodeTemplate(children: [
    CommentUniAPI(),
    EmptyLine(),
    ...imports,
    EmptyLine(),
    OneLine(
        body: dartUniCallbackManagerContent(generateListStr,
            nullSafty: options.dartNullSafety,
            channelSuffix: options.objcUniAPIPrefix.suffix()))
  ]).build();
}