writeInstanceManagerApi method

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

Writes the implementation of the API for the InstanceManager to indent.

Implementation

@override
void writeInstanceManagerApi(
  KotlinOptions generatorOptions,
  Root root,
  Indent indent, {
  required String dartPackageName,
}) {
  final String instanceManagerApiName =
      '${kotlinInstanceManagerClassName(generatorOptions)}Api';

  addDocumentationComments(
    indent,
    <String>[
      ' Generated API for managing the Dart and native `InstanceManager`s.',
    ],
    _docCommentSpec,
  );
  indent.writeScoped(
    'private class $instanceManagerApiName(val binaryMessenger: BinaryMessenger) {',
    '}',
    () {
      indent.writeScoped('companion object {', '}', () {
        addDocumentationComments(
          indent,
          <String>[' The codec used by $instanceManagerApiName.'],
          _docCommentSpec,
        );
        indent.writeScoped(
          'val codec: MessageCodec<Any?> by lazy {',
          '}',
          () {
            indent.writeln(
              '${generatorOptions.fileSpecificClassNameComponent}$_codecName()',
            );
          },
        );
        indent.newln();

        addDocumentationComments(
          indent,
          <String>[
            ' Sets up an instance of `$instanceManagerApiName` to handle messages from the',
            ' `binaryMessenger`.',
          ],
          _docCommentSpec,
        );
        indent.writeScoped(
          'fun setUpMessageHandlers(binaryMessenger: BinaryMessenger, instanceManager: ${kotlinInstanceManagerClassName(generatorOptions)}?) {',
          '}',
          () {
            const String setHandlerCondition = 'instanceManager != null';
            _writeHostMethodMessageHandler(
              indent,
              name: 'removeStrongReference',
              channelName:
                  makeRemoveStrongReferenceChannelName(dartPackageName),
              taskQueueType: TaskQueueType.serial,
              parameters: <Parameter>[
                Parameter(
                  name: 'identifier',
                  type: const TypeDeclaration(
                    baseName: 'int',
                    isNullable: false,
                  ),
                ),
              ],
              returnType: const TypeDeclaration.voidDeclaration(),
              setHandlerCondition: setHandlerCondition,
              onCreateCall: (
                List<String> safeArgNames, {
                required String apiVarName,
              }) {
                return 'instanceManager.remove<Any?>(${safeArgNames.single})';
              },
            );
            _writeHostMethodMessageHandler(
              indent,
              name: 'clear',
              channelName: makeClearChannelName(dartPackageName),
              taskQueueType: TaskQueueType.serial,
              parameters: <Parameter>[],
              returnType: const TypeDeclaration.voidDeclaration(),
              setHandlerCondition: setHandlerCondition,
              onCreateCall: (
                List<String> safeArgNames, {
                required String apiVarName,
              }) {
                return 'instanceManager.clear()';
              },
            );
          },
        );
      });
      indent.newln();

      _writeFlutterMethod(
        indent,
        generatorOptions: generatorOptions,
        name: 'removeStrongReference',
        parameters: <Parameter>[
          Parameter(
            name: 'identifier',
            type: const TypeDeclaration(baseName: 'int', isNullable: false),
          )
        ],
        returnType: const TypeDeclaration.voidDeclaration(),
        channelName: makeRemoveStrongReferenceChannelName(dartPackageName),
        dartPackageName: dartPackageName,
      );
    },
  );
}