writeInstanceManagerApi method
void
writeInstanceManagerApi(
- SwiftOptions generatorOptions,
- Root root,
- Indent indent, {
- required String dartPackageName,
override
Writes the implementation of the API for the InstanceManager
to
indent
.
Implementation
@override
void writeInstanceManagerApi(
SwiftOptions generatorOptions,
Root root,
Indent indent, {
required String dartPackageName,
}) {
final String instanceManagerApiName =
'${swiftInstanceManagerClassName(generatorOptions)}Api';
final String removeStrongReferenceName =
makeRemoveStrongReferenceChannelName(
dartPackageName,
);
indent.writeScoped('private class $instanceManagerApiName {', '}', () {
addDocumentationComments(
indent,
<String>[' The codec used for serializing messages.'],
_docCommentSpec,
);
indent.writeln(
'var codec: FlutterStandardMessageCodec { ${_getCodecName(generatorOptions)}.shared }',
);
indent.newln();
addDocumentationComments(
indent,
<String>[' Handles sending and receiving messages with Dart.'],
_docCommentSpec,
);
indent.writeln('unowned let binaryMessenger: FlutterBinaryMessenger');
indent.newln();
indent.writeScoped(
'init(binaryMessenger: FlutterBinaryMessenger) {',
'}',
() {
indent.writeln('self.binaryMessenger = binaryMessenger');
},
);
indent.newln();
addDocumentationComments(
indent,
<String>[
' Sets up an instance of `$instanceManagerApiName` to handle messages through the `binaryMessenger`.',
],
_docCommentSpec,
);
indent.writeScoped(
'static func setUpMessageHandlers(binaryMessenger: FlutterBinaryMessenger, instanceManager: ${swiftInstanceManagerClassName(generatorOptions)}?) {',
'}',
() {
indent.writeln(
'let codec = ${_getCodecName(generatorOptions)}.shared',
);
const String setHandlerCondition =
'let instanceManager = instanceManager';
_writeHostMethodMessageHandler(
indent,
name: 'removeStrongReference',
channelName: removeStrongReferenceName,
parameters: <Parameter>[
Parameter(
name: 'identifier',
type: const TypeDeclaration(
baseName: 'int',
isNullable: false,
),
),
],
returnType: const TypeDeclaration.voidDeclaration(),
swiftFunction: 'method(withIdentifier:)',
setHandlerCondition: setHandlerCondition,
isAsynchronous: false,
onCreateCall: (
List<String> safeArgNames, {
required String apiVarName,
}) {
return 'let _: AnyObject? = try instanceManager.removeInstance(${safeArgNames.single})';
},
);
_writeHostMethodMessageHandler(
indent,
name: 'clear',
channelName: makeClearChannelName(dartPackageName),
parameters: <Parameter>[],
returnType: const TypeDeclaration.voidDeclaration(),
setHandlerCondition: setHandlerCondition,
swiftFunction: null,
isAsynchronous: false,
onCreateCall: (
List<String> safeArgNames, {
required String apiVarName,
}) {
return 'try instanceManager.removeAllObjects()';
},
);
},
);
indent.newln();
addDocumentationComments(
indent,
<String>[
' Sends a message to the Dart `InstanceManager` to remove the strong reference of the instance associated with `identifier`.',
],
_docCommentSpec,
);
_writeFlutterMethod(
indent,
generatorOptions: generatorOptions,
name: 'removeStrongReference',
parameters: <Parameter>[
Parameter(
name: 'identifier',
type: const TypeDeclaration(baseName: 'int', isNullable: false),
)
],
returnType: const TypeDeclaration.voidDeclaration(),
channelName: removeStrongReferenceName,
swiftFunction: null,
);
});
}