writeHostApi method

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

Writes a single Host Api to indent.

Implementation

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

  _writeCodecAndGetter(generatorOptions, root, indent, api);

  const String channelName = 'channel';
  indent.write(
      'void SetUp$apiName(id<FlutterBinaryMessenger> binaryMessenger, NSObject<$apiName> *api) ');
  indent.addScoped('{', '}', () {
    indent.writeln('SetUp${apiName}WithSuffix(binaryMessenger, api, @"");');
  });
  indent.newln();
  indent.write(
      'void SetUp${apiName}WithSuffix(id<FlutterBinaryMessenger> binaryMessenger, NSObject<$apiName> *api, NSString *messageChannelSuffix) ');
  indent.addScoped('{', '}', () {
    indent.writeln(
        'messageChannelSuffix = messageChannelSuffix.length > 0 ? [NSString stringWithFormat: @".%@", messageChannelSuffix] : @"";');
    for (final Method func in api.methods) {
      addDocumentationComments(
          indent, func.documentationComments, _docCommentSpec);

      indent.writeScoped('{', '}', () {
        String? taskQueue;
        if (func.taskQueueType != TaskQueueType.serial) {
          taskQueue = 'taskQueue';
          indent.writeln(
              'NSObject<FlutterTaskQueue> *$taskQueue = [binaryMessenger makeBackgroundTaskQueue];');
        }
        _writeChannelAllocation(
          generatorOptions,
          indent,
          api,
          func,
          channelName,
          taskQueue,
          dartPackageName: dartPackageName,
        );
        indent.write('if (api) ');
        indent.addScoped('{', '}', () {
          _writeChannelApiBinding(
              generatorOptions, root, indent, apiName, func, channelName);
        }, addTrailingNewline: false);
        indent.add(' else ');
        indent.addScoped('{', '}', () {
          indent.writeln('[$channelName setMessageHandler:nil];');
        });
      });
    }
  });
}