dartCode static method

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

Implementation

static String dartCode(Module module, UniAPIOptions options) {
  return CodeTemplate(children: [
    CommentUniAPI(),
    EmptyLine(),
    DartImport(fullClassName: 'package:flutter/services.dart'),
    EmptyLine(),
    DartCustomNestedImports(module.inputFile, options,
        methods: module.methods),
    EmptyLine(),
    Comment(
        comments: [uniFlutterModuleDesc, ...module.codeComments],
        commentType: CommentType.commentTripleBackSlash),
    DartClass(
        className: module.name,
        isAbstract: true,
        methods: module.methods,
        injectedJavaCodes: (depth) {
          final ret = <CodeUnit>[];

          for (final method in module.methods) {
            if (method.name == module.name) {
              continue;
            }

            ret.add(DartFunction(
              depth: depth + 1,
              functionName: method.name,
              isAbstract: true,
              params: method.parameters,
              returnType: method.returnType,
            ));
            ret.add(EmptyLine());
          }

          ret.add(DartFunction(
              depth: depth + 1,
              functionName: 'setup',
              isStatic: true,
              params: [Variable(AstCustomType(module.name), 'impl')],
              body: (depth) {
                final funcBody = <CodeUnit>[];
                for (final method in module.methods) {
                  funcBody.add(OneLine(depth: depth + 1, body: '{'));
                  funcBody.add(OneLine(
                      depth: depth + 2,
                      body: kEnableNullSafety
                          ? "const BasicMessageChannel<Object?> channel = BasicMessageChannel<Object?>('${makeChannelName(module, method)}', StandardMessageCodec());"
                          : "const BasicMessageChannel<Object> channel = BasicMessageChannel<Object>('${makeChannelName(module, method)}', StandardMessageCodec());"));

                  funcBody.add(OneLine(
                      depth: depth + 2,
                      body: kEnableNullSafety
                          ? 'channel.setMessageHandler((Object? message) async {'
                          : 'channel.setMessageHandler((Object message) async {'));
                  funcBody.add(OneLine(
                      depth: depth + 3,
                      body: 'if (message == null) return null;'));
                  funcBody.add(OneLine(
                      depth: depth + 3,
                      body: kEnableNullSafety
                          ? 'Map<Object?, Object?> wrapped = message as Map<Object?, Object?>;'
                          : 'Map<Object, Object> wrapped = message as Map<Object, Object>;'));
                  final methodArgs = <String>[];
                  for (final param in method.parameters) {
                    methodArgs.add(
                        '${param.type.realType().convertDartJson2Obj(vname: "wrapped['${param.name}']")}');
                  }
                  final call =
                      '${method.isAsync ? 'await ' : ''}impl.${method.name}(${methodArgs.join(', ')})';
                  if (method.returnType.realType() is AstVoid) {
                    funcBody.add(OneLine(depth: depth + 3, body: '$call;'));
                  } else {
                    funcBody.add(OneLine(
                        depth: depth + 3, body: 'var implResult = $call;'));
                    if (method.returnType.maybeNull) {
                      funcBody.add(OneLine(
                          depth: depth + 3,
                          body: 'if (implResult == null ) return null;'));
                    }
                    funcBody.add(OneLine(
                        depth: depth + 3,
                        body:
                            'return ${method.returnType.realType().convertDartObj2Json('implResult')};'));
                  }
                  funcBody.add(OneLine(depth: depth + 2, body: '});'));
                  funcBody.add(OneLine(depth: depth + 1, body: '}'));
                }

                return funcBody;
              }));

          return ret;
        })
  ]).build();
}