writeFlutterApi method

  1. @override
void writeFlutterApi(
  1. GObjectOptions generatorOptions,
  2. Root root,
  3. Indent indent,
  4. Api api, {
  5. required String dartPackageName,
})
override

Writes a single Flutter Api to indent.

Implementation

@override
void writeFlutterApi(
  GObjectOptions generatorOptions,
  Root root,
  Indent indent,
  Api api, {
  required String dartPackageName,
}) {
  final String module = _getModule(generatorOptions, dartPackageName);
  final String className = _getClassName(module, api.name);

  for (final Method method in api.methods) {
    _writeFlutterApiRespondClass(indent, module, api, method);
  }

  final String methodPrefix = _getMethodPrefix(module, api.name);
  indent.newln();
  addDocumentationComments(
      indent,
      <String>[
        '$className:',
        '',
        ...api.documentationComments,
      ],
      _docCommentSpec);

  indent.newln();
  _writeDeclareFinalType(indent, module, api.name);

  indent.newln();
  addDocumentationComments(
      indent,
      <String>[
        '${methodPrefix}_new:',
        '@messenger: an #FlBinaryMessenger.',
        '@suffix: (allow-none): a suffix to add to the API or %NULL for none.',
        '',
        'Creates a new object to access the ${api.name} API.',
        '',
        'Returns: a new #$className',
      ],
      _docCommentSpec);
  indent.writeln(
      '$className* ${methodPrefix}_new(FlBinaryMessenger* messenger, const gchar* suffix);');

  for (final Method method in api.methods) {
    final String methodName = _getMethodName(method.name);
    final String responseName = _getResponseName(api.name, method.name);
    final String responseClassName = _getClassName(module, responseName);

    final List<String> asyncArgs = <String>['$className* api'];
    for (final Parameter param in method.parameters) {
      final String paramName = _snakeCaseFromCamelCase(param.name);
      asyncArgs.add('${_getType(module, param.type)} $paramName');
      if (_isNumericListType(param.type)) {
        asyncArgs.add('size_t ${paramName}_length');
      }
    }
    asyncArgs.addAll(<String>[
      'GCancellable* cancellable',
      'GAsyncReadyCallback callback',
      'gpointer user_data'
    ]);
    indent.newln();
    final List<String> methodParameterCommentLines = <String>[];
    for (final Parameter param in method.parameters) {
      final String paramName = _snakeCaseFromCamelCase(param.name);
      methodParameterCommentLines.add(
          '@$paramName: ${param.type.isNullable ? '(allow-none): ' : ''}parameter for this method.');
      if (_isNumericListType(param.type)) {
        methodParameterCommentLines
            .add('@${paramName}_length: length of $paramName.');
      }
    }
    addDocumentationComments(
        indent,
        <String>[
          '${methodPrefix}_$methodName:',
          '@api: a #$className.',
          ...methodParameterCommentLines,
          '@cancellable: (allow-none): a #GCancellable or %NULL.',
          '@callback: (scope async): (allow-none): a #GAsyncReadyCallback to call when the call is complete or %NULL to ignore the response.',
          '@user_data: (closure): user data to pass to @callback.',
          '',
          ...method.documentationComments
        ],
        _docCommentSpec);
    indent.writeln(
        "void ${methodPrefix}_$methodName(${asyncArgs.join(', ')});");

    final List<String> finishArgs = <String>[
      '$className* api',
      'GAsyncResult* result',
      'GError** error'
    ];
    indent.newln();
    addDocumentationComments(
        indent,
        <String>[
          '${methodPrefix}_${methodName}_finish:',
          '@api: a #$className.',
          '@result: a #GAsyncResult.',
          '@error: (allow-none): #GError location to store the error occurring, or %NULL to ignore.',
          '',
          'Completes a ${methodPrefix}_$methodName() call.',
          '',
          'Returns: a #$responseClassName or %NULL on error.',
        ],
        _docCommentSpec);
    indent.writeln(
        "$responseClassName* ${methodPrefix}_${methodName}_finish(${finishArgs.join(', ')});");
  }
}