writeHostApi method

  1. @override
void writeHostApi(
  1. KotlinOptions generatorOptions,
  2. Root root,
  3. Indent indent,
  4. AstHostApi api, {
  5. required String dartPackageName,
})
override

Write the kotlin code that represents a host Api, api. Example: interface Foo { Int add(x: Int, y: Int); companion object { fun setUp(binaryMessenger: BinaryMessenger, api: Api) {...} } }

Implementation

@override
void writeHostApi(
  KotlinOptions generatorOptions,
  Root root,
  Indent indent,
  AstHostApi api, {
  required String dartPackageName,
}) {
  final String apiName = api.name;

  const List<String> generatedMessages = <String>[
    ' Generated interface from Pigeon that represents a handler of messages from Flutter.'
  ];
  addDocumentationComments(indent, api.documentationComments, _docCommentSpec,
      generatorComments: generatedMessages);

  indent.write('interface $apiName ');
  indent.addScoped('{', '}', () {
    for (final Method method in api.methods) {
      _writeMethodDeclaration(
        indent,
        name: method.name,
        documentationComments: method.documentationComments,
        returnType: method.returnType,
        parameters: method.parameters,
        isAsynchronous: method.isAsynchronous,
      );
    }

    indent.newln();
    indent.write('companion object ');
    indent.addScoped('{', '}', () {
      indent.writeln('/** The codec used by $apiName. */');
      indent.write('val codec: MessageCodec<Any?> by lazy ');
      indent.addScoped('{', '}', () {
        indent.writeln(
            '${generatorOptions.fileSpecificClassNameComponent}$_codecName');
      });
      indent.writeln(
          '/** Sets up an instance of `$apiName` to handle messages through the `binaryMessenger`. */');
      indent.writeln('@JvmOverloads');
      indent.write(
          'fun setUp(binaryMessenger: BinaryMessenger, api: $apiName?, messageChannelSuffix: String = "") ');
      indent.addScoped('{', '}', () {
        indent.writeln(
            r'val separatedMessageChannelSuffix = if (messageChannelSuffix.isNotEmpty()) ".$messageChannelSuffix" else ""');
        for (final Method method in api.methods) {
          _writeHostMethodMessageHandler(
            indent,
            api: api,
            name: method.name,
            channelName: makeChannelName(api, method, dartPackageName),
            taskQueueType: method.taskQueueType,
            parameters: method.parameters,
            returnType: method.returnType,
            isAsynchronous: method.isAsynchronous,
          );
        }
      });
    });
  });
}