diff --git a/packages/pigeon/CHANGELOG.md b/packages/pigeon/CHANGELOG.md index 0e5faeed09..5497e80ca0 100644 --- a/packages/pigeon/CHANGELOG.md +++ b/packages/pigeon/CHANGELOG.md @@ -1,3 +1,9 @@ +## 0.1.22 + +* Java code generator enhancements: + * Added linter tests to CI. + * Fixed some linter issues in the Java code. + ## 0.1.21 * Fixed decode method on generated Flutter classes that use null-safety and have diff --git a/packages/pigeon/README.md b/packages/pigeon/README.md index f5977670a6..30fa261dfe 100644 --- a/packages/pigeon/README.md +++ b/packages/pigeon/README.md @@ -68,6 +68,48 @@ channels supports Note: Generics for List and Map aren't supported yet. +## Asynchronous Handlers + +By default Pigeon will generate synchronous handlers for messages. If you want +to be able to respond to a message asynchronously you can use the `@async` +annotation as of version 0.1.20. + +Example: + +```dart +class Value { + int number; +} + +@HostApi() +abstract class Api2Host { + @async + Value calculate(Value value); +} +``` + +Generates: + +```objc +// Objc +@protocol Api2Host +-(void)calculate:(nullable Value *)input + completion:(void(^)(Value *_Nullable, FlutterError *_Nullable))completion; +@end +``` + +```java +// Java +public interface Result { + void success(T result); +} + +/** Generated interface from Pigeon that represents a handler of messages from Flutter.*/ +public interface Api2Host { + void calculate(Value arg, Result result); +} +``` + ## Feedback File an issue in [flutter/flutter](https://github.com/flutter/flutter) with the diff --git a/packages/pigeon/example/README.md b/packages/pigeon/example/README.md index 8b8409d84c..0c8fdb8ee2 100644 --- a/packages/pigeon/example/README.md +++ b/packages/pigeon/example/README.md @@ -120,7 +120,7 @@ A downloadable example of using Pigeon to create a Flutter Plugin with Swift and Kotlin can be found at [gaaclarke/flutter_plugin_example](https://github.com/gaaclarke/pigeon_plugin_example). -## Swift Add-to-app Example +## Swift / Kotlin Add-to-app Example A full example of using Pigeon for add-to-app with Swift on iOS can be found at -[gaaclarke/GiantsA2A](https://github.com/gaaclarke/GiantsA2A). +[samples/add_to_app/books](https://github.com/flutter/samples/tree/master/add_to_app/books). diff --git a/packages/pigeon/lib/generator_tools.dart b/packages/pigeon/lib/generator_tools.dart index d196e26a21..294962ca5d 100644 --- a/packages/pigeon/lib/generator_tools.dart +++ b/packages/pigeon/lib/generator_tools.dart @@ -8,7 +8,7 @@ import 'dart:mirrors'; import 'ast.dart'; /// The current version of pigeon. This must match the version in pubspec.yaml. -const String pigeonVersion = '0.1.21'; +const String pigeonVersion = '0.1.22'; /// Read all the content from [stdin] to a String. String readStdin() { diff --git a/packages/pigeon/lib/java_generator.dart b/packages/pigeon/lib/java_generator.dart index c79a18e445..51d21f78b2 100644 --- a/packages/pigeon/lib/java_generator.dart +++ b/packages/pigeon/lib/java_generator.dart @@ -15,7 +15,7 @@ const Map _javaTypeForDartTypeMap = { 'Int64List': 'long[]', 'Float64List': 'double[]', 'List': 'List', - 'Map': 'Map', + 'Map': 'Map', }; /// Options that control how Java code will be generated. @@ -169,38 +169,18 @@ void _writeFlutterApi(Indent indent, Api api) { if (func.argType != 'void') { indent.writeln('Map inputMap = argInput.toMap();'); } - indent.write('if (callback != null)'); - indent.scoped('{', '}', () { - indent.write('channel.send($sendArgument, channelReply -> '); - indent.scoped('{', '});', () { - if (func.returnType == 'void') { - indent.writeln('callback.reply(null);'); - } else { - indent.writeln('Map outputMap = (Map)channelReply;'); - indent.writeln('@SuppressWarnings("ConstantConditions")'); - indent.writeln( - '${func.returnType} output = ${func.returnType}.fromMap(outputMap);'); - indent.writeln('callback.reply(output);'); - } - }); + indent.write('channel.send($sendArgument, channelReply -> '); + indent.scoped('{', '});', () { + if (func.returnType == 'void') { + indent.writeln('callback.reply(null);'); + } else { + indent.writeln('Map outputMap = (Map)channelReply;'); + indent.writeln('@SuppressWarnings("ConstantConditions")'); + indent.writeln( + '${func.returnType} output = ${func.returnType}.fromMap(outputMap);'); + indent.writeln('callback.reply(output);'); + } }); - indent.write(' else '); - indent.scoped('{', '}', () { - indent.writeln('channel.send($sendArgument, null);'); - }); - }); - - if (func.argType == 'void') { - indent.write('public void ${func.name}() '); - } else { - indent.write('public void ${func.name}(${func.argType} argInput) '); - } - indent.scoped('{', '}', () { - if (func.argType == 'void') { - indent.writeln('${func.name}(null);'); - } else { - indent.writeln('${func.name}(argInput, null);'); - } }); } }); diff --git a/packages/pigeon/pubspec.yaml b/packages/pigeon/pubspec.yaml index e534e2d57d..7560a04a3c 100644 --- a/packages/pigeon/pubspec.yaml +++ b/packages/pigeon/pubspec.yaml @@ -1,5 +1,5 @@ name: pigeon -version: 0.1.21 # This must match the version in lib/generator_tools.dart +version: 0.1.22 # This must match the version in lib/generator_tools.dart description: Code generator tool to make communication between Flutter and the host platform type-safe and easier. homepage: https://github.com/flutter/packages/tree/master/packages/pigeon dependencies: diff --git a/packages/pigeon/run_tests.sh b/packages/pigeon/run_tests.sh index 643a1cafe0..d3dbe0a0f0 100755 --- a/packages/pigeon/run_tests.sh +++ b/packages/pigeon/run_tests.sh @@ -124,7 +124,7 @@ fi pub get dart analyze bin dart analyze lib -pub run test test/ +dart --no-sound-null-safety test ############################################################################### # Execute without arguments test diff --git a/packages/pigeon/test/java_generator_test.dart b/packages/pigeon/test/java_generator_test.dart index c453231dc8..398ca69ce0 100644 --- a/packages/pigeon/test/java_generator_test.dart +++ b/packages/pigeon/test/java_generator_test.dart @@ -257,7 +257,7 @@ void main() { generateJava(javaOptions, root, sink); final String code = sink.toString(); expect(code, contains('public static class Foobar')); - expect(code, contains('private Map field1;')); + expect(code, contains('private Map field1;')); }); test('gen nested', () {