writeGeneralCodec method

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

Writes the custom codec to indent.

Implementation

@override
void writeGeneralCodec(
  ObjcOptions generatorOptions,
  Root root,
  Indent indent, {
  required String dartPackageName,
}) {
  const String codecName = 'PigeonCodec';
  final List<EnumeratedType> enumeratedTypes =
      getEnumeratedTypes(root).toList();
  final String readerWriterName =
      '${generatorOptions.prefix}${toUpperCamelCase(generatorOptions.fileSpecificClassNameComponent ?? '')}${codecName}ReaderWriter';
  final String readerName =
      '${generatorOptions.prefix}${toUpperCamelCase(generatorOptions.fileSpecificClassNameComponent ?? '')}${codecName}Reader';
  final String writerName =
      '${generatorOptions.prefix}${toUpperCamelCase(generatorOptions.fileSpecificClassNameComponent ?? '')}${codecName}Writer';

  if (root.requiresOverflowClass) {
    _writeCodecOverflowUtilities(
        generatorOptions, root, indent, enumeratedTypes,
        dartPackageName: dartPackageName);
  }

  indent.writeln('@interface $readerName : FlutterStandardReader');
  indent.writeln('@end');
  indent.writeln('@implementation $readerName');
  indent.write('- (nullable id)readValueOfType:(UInt8)type ');
  indent.addScoped('{', '}', () {
    indent.writeScoped('switch (type) {', '}', () {
      for (final EnumeratedType customType in enumeratedTypes) {
        if (customType.enumeration < maximumCodecFieldKey) {
          indent.write('case ${customType.enumeration}: ');
          _writeCodecDecode(
              indent, customType, generatorOptions.prefix ?? '');
        }
      }
      if (root.requiresOverflowClass) {
        indent.write('case $maximumCodecFieldKey: ');
        _writeCodecDecode(
          indent,
          _enumeratedOverflow,
          generatorOptions.prefix,
        );
      }
      indent.writeln('default:');
      indent.nest(1, () {
        indent.writeln('return [super readValueOfType:type];');
      });
    });
  });
  indent.writeln('@end');
  indent.newln();
  indent.writeln('@interface $writerName : FlutterStandardWriter');
  indent.writeln('@end');
  indent.writeln('@implementation $writerName');
  indent.write('- (void)writeValue:(id)value ');
  indent.addScoped('{', '}', () {
    indent.write('');
    for (final EnumeratedType customType in enumeratedTypes) {
      final String encodeString = customType.type == CustomTypes.customClass
          ? '[value toList]'
          : '(value == nil ? [NSNull null] : [NSNumber numberWithInteger:box.value])';
      final String valueString = customType.enumeration < maximumCodecFieldKey
          ? encodeString
          : '[wrap toList]';
      final String className = customType.type == CustomTypes.customClass
          ? _className(generatorOptions.prefix, customType.name)
          : _enumName(customType.name,
              prefix: generatorOptions.prefix, box: true);
      indent.addScoped(
          'if ([value isKindOfClass:[$className class]]) {', '} else ', () {
        if (customType.type == CustomTypes.customEnum) {
          indent.writeln('$className *box = ($className *)value;');
        }
        final int enumeration = customType.enumeration < maximumCodecFieldKey
            ? customType.enumeration
            : maximumCodecFieldKey;
        if (customType.enumeration >= maximumCodecFieldKey) {
          indent.writeln(
              '${_className(generatorOptions.prefix, _overflowClassName)} *wrap = [${_className(generatorOptions.prefix, _overflowClassName)} makeWithType:${customType.enumeration - maximumCodecFieldKey} wrapped:$encodeString];');
        }
        indent.writeln('[self writeByte:$enumeration];');
        indent.writeln('[self writeValue:$valueString];');
      }, addTrailingNewline: false);
    }
    indent.addScoped('{', '}', () {
      indent.writeln('[super writeValue:value];');
    });
  });
  indent.writeln('@end');
  indent.newln();
  indent.format('''
@interface $readerWriterName : FlutterStandardReaderWriter
@end
@implementation $readerWriterName
- (FlutterStandardWriter *)writerWithData:(NSMutableData *)data {
\treturn [[$writerName alloc] initWithData:data];
}
- (FlutterStandardReader *)readerWithData:(NSData *)data {
\treturn [[$readerName alloc] initWithData:data];
}
@end''');
  indent.newln();

  indent.write(
      'NSObject<FlutterMessageCodec> *${generatorOptions.prefix}Get${toUpperCamelCase(generatorOptions.fileSpecificClassNameComponent ?? '')}Codec(void) ');
  indent.addScoped('{', '}', () {
    indent
        .writeln('static FlutterStandardMessageCodec *sSharedObject = nil;');

    indent.writeln('static dispatch_once_t sPred = 0;');
    indent.write('dispatch_once(&sPred, ^');
    indent.addScoped('{', '});', () {
      indent.writeln(
          '$readerWriterName *readerWriter = [[$readerWriterName alloc] init];');
      indent.writeln(
          'sSharedObject = [FlutterStandardMessageCodec codecWithReaderWriter:readerWriter];');
    });

    indent.writeln('return sSharedObject;');
  });
}