Skip to content

[pigeon] Enable example app build in CI #8119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 23 additions & 14 deletions packages/pigeon/example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class PigeonApiImplementation : public ExampleHostApi {
}
void SendMessage(const MessageData& message,
std::function<void(ErrorOr<bool> reply)> result) {
if (message.code == Code.kOne) {
if (message.code() == Code::kOne) {
result(FlutterError("code", "message", "details"));
return;
}
Expand Down Expand Up @@ -281,10 +281,10 @@ private class PigeonFlutterApi {
}

func callFlutterMethod(
aString aStringArg: String?, completion: @escaping (Result<String, Error>) -> Void
aString aStringArg: String?, completion: @escaping (Result<String, PigeonError>) -> Void
) {
flutterAPI.flutterMethod(aString: aStringArg) {
completion(.success($0))
completion($0)
}
}
}
Expand All @@ -294,16 +294,15 @@ private class PigeonFlutterApi {

<?code-excerpt "android/app/src/main/kotlin/dev/flutter/pigeon_example_app/MainActivity.kt (kotlin-class-flutter)"?>
```kotlin
private class PigeonFlutterApi {

private class PigeonFlutterApi(binding: FlutterPlugin.FlutterPluginBinding) {
var flutterApi: MessageFlutterApi? = null

constructor(binding: FlutterPlugin.FlutterPluginBinding) {
flutterApi = MessageFlutterApi(binding.getBinaryMessenger())
init {
flutterApi = MessageFlutterApi(binding.binaryMessenger)
}

fun callFlutterMethod(aString: String, callback: (Result<String>) -> Unit) {
flutterApi!!.flutterMethod(aString) { echo -> callback(Result.success(echo)) }
flutterApi!!.flutterMethod(aString) { echo -> callback(echo) }
}
}
```
Expand All @@ -312,12 +311,22 @@ private class PigeonFlutterApi {

<?code-excerpt "windows/runner/flutter_window.cpp (cpp-method-flutter)"?>
```c++
void TestPlugin::CallFlutterMethod(
String aString, std::function<void(ErrorOr<int64_t> reply)> result) {
MessageFlutterApi->FlutterMethod(
aString, [result](String echo) { result(echo); },
[result](const FlutterError& error) { result(error); });
}
class PigeonFlutterApi {
public:
PigeonFlutterApi(flutter::BinaryMessenger* messenger)
: flutterApi_(std::make_unique<MessageFlutterApi>(messenger)) {}

void CallFlutterMethod(
const std::string& a_string,
std::function<void(ErrorOr<std::string> reply)> result) {
flutterApi_->FlutterMethod(
&a_string, [result](const std::string& echo) { result(echo); },
[result](const FlutterError& error) { result(error); });
}

private:
std::unique_ptr<MessageFlutterApi> flutterApi_;
};
```

### GObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import ExampleHostApi
import FlutterError
import MessageData
import MessageFlutterApi
import androidx.annotation.NonNull
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.embedding.engine.plugins.FlutterPlugin
Expand Down Expand Up @@ -37,22 +36,21 @@ private class PigeonApiImplementation : ExampleHostApi {
// #enddocregion kotlin-class

// #docregion kotlin-class-flutter
private class PigeonFlutterApi {

private class PigeonFlutterApi(binding: FlutterPlugin.FlutterPluginBinding) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and the constructor->init change were an Android Studio-suggested refactor; I figured I might as well update it to follow recommendations while I was there, but I can back this part out if we don't want it.

var flutterApi: MessageFlutterApi? = null

constructor(binding: FlutterPlugin.FlutterPluginBinding) {
flutterApi = MessageFlutterApi(binding.getBinaryMessenger())
init {
flutterApi = MessageFlutterApi(binding.binaryMessenger)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a Kotlin style suggestion from AS.

}

fun callFlutterMethod(aString: String, callback: (Result<String>) -> Unit) {
flutterApi!!.flutterMethod(aString) { echo -> callback(Result.success(echo)) }
flutterApi!!.flutterMethod(aString) { echo -> callback(echo) }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per discussion in another PR, this code is wrong, and we just hadn't noticed because it wasn't building.

}
}
// #enddocregion kotlin-class-flutter

class MainActivity : FlutterActivity() {
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AS flagged that we shouldn't be using nullability annotations in Kotlin.

super.configureFlutterEngine(flutterEngine)

val api = PigeonApiImplementation()
Expand Down
6 changes: 3 additions & 3 deletions packages/pigeon/example/app/ios/Runner/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ private class PigeonFlutterApi {
}

func callFlutterMethod(
aString aStringArg: String?, completion: @escaping (Result<String, Error>) -> Void
aString aStringArg: String?, completion: @escaping (Result<String, PigeonError>) -> Void
) {
flutterAPI.flutterMethod(aString: aStringArg) {
completion(.success($0))
completion($0)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As with the Kotlin file, this was wrong and didn't compile.

}
}
}
// #enddocregion swift-class-flutter

@UIApplicationMain
@main
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was a Flutter tool auto-migration; I didn't see any reason to undo it.

@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ private class PigeonApiImplementation: ExampleHostApi {
func getHostLanguage() throws -> String {
return "Swift"
}

func add(_ a: Int64, to b: Int64) throws -> Int64 {
if a < 0 || b < 0 {
throw PigeonError(code: "code", message: "message", details: "details")
}
return a + b
}

func sendMessage(message: MessageData, completion: @escaping (Result<Bool, Error>) -> Void) {
if message.code == Code.one {
completion(.failure(PigeonError(code: "code", message: "message", details: "details")))
return
}
completion(.success(true))
}
}

class MainFlutterWindow: NSWindow {
Expand Down
37 changes: 27 additions & 10 deletions packages/pigeon/example/app/windows/runner/flutter_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,21 @@

#include "flutter_window.h"

#include <flutter/binary_messenger.h>

#include <memory>
#include <optional>

#include "flutter/generated_plugin_registrant.h"
#include "messages.g.h"

namespace {
using pigeon_example::Code;
using pigeon_example::ErrorOr;
using pigeon_example::ExampleHostApi;
using pigeon_example::FlutterError;
using pigeon_example::MessageData;
using pigeon_example::MessageFlutterApi;

// #docregion cpp-class
class PigeonApiImplementation : public ExampleHostApi {
Expand All @@ -29,14 +35,34 @@ class PigeonApiImplementation : public ExampleHostApi {
}
void SendMessage(const MessageData& message,
std::function<void(ErrorOr<bool> reply)> result) {
if (message.code == Code.kOne) {
if (message.code() == Code::kOne) {
result(FlutterError("code", "message", "details"));
return;
}
result(true);
}
};
// #enddocregion cpp-class

// #docregion cpp-method-flutter
class PigeonFlutterApi {
public:
PigeonFlutterApi(flutter::BinaryMessenger* messenger)
: flutterApi_(std::make_unique<MessageFlutterApi>(messenger)) {}

void CallFlutterMethod(
const std::string& a_string,
std::function<void(ErrorOr<std::string> reply)> result) {
flutterApi_->FlutterMethod(
&a_string, [result](const std::string& echo) { result(echo); },
[result](const FlutterError& error) { result(error); });
}

private:
std::unique_ptr<MessageFlutterApi> flutterApi_;
};
// #enddocregion cpp-method-flutter

} // namespace

FlutterWindow::FlutterWindow(const flutter::DartProject& project)
Expand All @@ -49,15 +75,6 @@ bool FlutterWindow::OnCreate() {
return false;
}

// #docregion cpp-method-flutter
void TestPlugin::CallFlutterMethod(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code was not fully C++, and was in the wrong place (as in, this isn't a valid place for this definition to exist in C++). I pulled it out into a class to match Swift and Kotlin, and updated it to compile.

String aString, std::function<void(ErrorOr<int64_t> reply)> result) {
MessageFlutterApi->FlutterMethod(
aString, [result](String echo) { result(echo); },
[result](const FlutterError& error) { result(error); });
}
// #enddocregion cpp-method-flutter

RECT frame = GetClientArea();

// The size here must match the window dimensions to avoid unnecessary surface
Expand Down
11 changes: 0 additions & 11 deletions packages/pigeon/example/pubspec.yaml

This file was deleted.