ocHeader static method

String ocHeader(
  1. Module module,
  2. UniAPIOptions options
)

Implementation

static String ocHeader(Module module, UniAPIOptions options) {
  registerCustomType(module.name);
  return CodeTemplate(children: [
    CommentUniAPI(),
    EmptyLine(),
    OCImport(fullImportName: 'Foundation/Foundation.h'),
    EmptyLine(),
    OCForwardDeclaration(
        className: 'FlutterBinaryMessenger', isProtocol: true),
    OCForwardDeclaration(className: typeFlutterError, isClass: true),
    OCCustomNestedImports(module.inputFile, options,
        methods: module.methods,
        excludeImports: [typeUniCallback],
        isForwardDeclaration: true),
    EmptyLine(),
    OneLine(body: 'NS_ASSUME_NONNULL_BEGIN'),
    EmptyLine(),
    ...OCClassUniCallback.genPublicDeclarations(module.methods),
    Comment(
        comments: [uniNativeModuleDesc, ...module.codeComments],
        commentType: CommentType.commentBlock),
    OCClassDeclaration(
        className: module.name,
        isProtocol: true,
        instanceMethods: module.methods.map((method) {
          // 不能污染原来的 Method
          final methodCopy = Method.copy(method);
          final params = <Variable>[];
          // 处理 UniCallback
          for (final param in method.parameters) {
            if (param.type.astType() != typeUniCallback) {
              params.add(param);
            } else {
              // 改成 OC 形式
              registerCustomType(
                  OCClassUniCallback.getName(method.name, param.name));
              params.add(Variable(
                  AstCustomType(
                      OCClassUniCallback.getName(method.name, param.name)),
                  param.name));
            }
          }
          // 处理异步方法
          if (method.isAsync) {
            // 参数处理
            final blockType = AstLambda([
              Variable(method.returnType.realType(),
                  method.returnType.realType() is AstVoid ? '' : Keys.result)
            ], const AstVoid(), declaration: true);
            registerCustomType(blockType.astType());

            params.add(Variable(blockType, 'success'));

            // 处理异步 Ignore Error
            if (method.ignoreError == false) {
              final ignoreErrorBlock = AstLambda([
                Variable(AstCustomType(typeFlutterError), Keys.error)
              ], const AstVoid(), declaration: true);
              registerCustomType(ignoreErrorBlock.astType());
              registerCustomType(typeFlutterError);

              params.add(Variable(ignoreErrorBlock, 'fail'));
            }

            // 处理 异步返回值
            methodCopy.returnType = const AstVoid();
          } else {
            // 处理同步 ignore error
            if (method.ignoreError == false) {
              params.add(Variable(
                  AstCustomType('FlutterError *_Nullable *_Nonnull',
                      keepRaw: true),
                  Keys.error));
            }
          }
          methodCopy.parameters = params;

          return methodCopy;
        }).toList()),
    EmptyLine(),
    OCFunction(
        functionName: '${module.name}Setup',
        isCFlavor: true,
        isDeclaration: true,
        isExternal: true,
        params: getSetupFuncParams(module.name)),
    EmptyLine(),
    OneLine(body: 'NS_ASSUME_NONNULL_END')
  ]).build();
}