Skip to content

Commit

Permalink
format swift
Browse files Browse the repository at this point in the history
  • Loading branch information
tarrinneal committed May 6, 2024
1 parent dca5bfc commit 0e97d19
Show file tree
Hide file tree
Showing 3 changed files with 1,586 additions and 499 deletions.
41 changes: 30 additions & 11 deletions packages/pigeon/example/app/ios/Runner/Messages.g.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ private func wrapError(_ error: Any) -> [Any?] {
}

private func createConnectionError(withChannelName channelName: String) -> FlutterError {
return FlutterError(code: "channel-error", message: "Unable to establish connection on channel: '\(channelName)'.", details: "")
return FlutterError(
code: "channel-error", message: "Unable to establish connection on channel: '\(channelName)'.",
details: "")
}

private func isNullish(_ value: Any?) -> Bool {
Expand Down Expand Up @@ -127,7 +129,6 @@ class MessagesPigeonCodec: FlutterStandardMessageCodec {
static let shared = MessagesPigeonCodec(readerWriter: MessagesPigeonCodecReaderWriter())
}


/// Generated protocol from Pigeon that represents a handler of messages from Flutter.
protocol ExampleHostApi {
func getHostLanguage() throws -> String
Expand All @@ -139,9 +140,14 @@ protocol ExampleHostApi {
class ExampleHostApiSetup {
static var codec: FlutterStandardMessageCodec { MessagesPigeonCodec.shared }
/// Sets up an instance of `ExampleHostApi` to handle messages through the `binaryMessenger`.
static func setUp(binaryMessenger: FlutterBinaryMessenger, api: ExampleHostApi?, messageChannelSuffix: String = "") {
static func setUp(
binaryMessenger: FlutterBinaryMessenger, api: ExampleHostApi?, messageChannelSuffix: String = ""
) {
let channelSuffix = messageChannelSuffix.count > 0 ? ".\(messageChannelSuffix)" : ""
let getHostLanguageChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.getHostLanguage\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
let getHostLanguageChannel = FlutterBasicMessageChannel(
name:
"dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.getHostLanguage\(channelSuffix)",
binaryMessenger: binaryMessenger, codec: codec)
if let api = api {
getHostLanguageChannel.setMessageHandler { _, reply in
do {
Expand All @@ -154,7 +160,9 @@ class ExampleHostApiSetup {
} else {
getHostLanguageChannel.setMessageHandler(nil)
}
let addChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.add\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
let addChannel = FlutterBasicMessageChannel(
name: "dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.add\(channelSuffix)",
binaryMessenger: binaryMessenger, codec: codec)
if let api = api {
addChannel.setMessageHandler { message, reply in
let args = message as! [Any?]
Expand All @@ -170,7 +178,9 @@ class ExampleHostApiSetup {
} else {
addChannel.setMessageHandler(nil)
}
let sendMessageChannel = FlutterBasicMessageChannel(name: "dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.sendMessage\(channelSuffix)", binaryMessenger: binaryMessenger, codec: codec)
let sendMessageChannel = FlutterBasicMessageChannel(
name: "dev.flutter.pigeon.pigeon_example_package.ExampleHostApi.sendMessage\(channelSuffix)",
binaryMessenger: binaryMessenger, codec: codec)
if let api = api {
sendMessageChannel.setMessageHandler { message, reply in
let args = message as! [Any?]
Expand All @@ -191,7 +201,8 @@ class ExampleHostApiSetup {
}
/// Generated protocol from Pigeon that represents Flutter messages that can be called from Swift.
protocol MessageFlutterApiProtocol {
func flutterMethod(aString aStringArg: String?, completion: @escaping (Result<String, FlutterError>) -> Void)
func flutterMethod(
aString aStringArg: String?, completion: @escaping (Result<String, FlutterError>) -> Void)
}
class MessageFlutterApi: MessageFlutterApiProtocol {
private let binaryMessenger: FlutterBinaryMessenger
Expand All @@ -203,9 +214,13 @@ class MessageFlutterApi: MessageFlutterApiProtocol {
var codec: MessagesPigeonCodec {
return MessagesPigeonCodec.shared
}
func flutterMethod(aString aStringArg: String?, completion: @escaping (Result<String, FlutterError>) -> Void) {
let channelName: String = "dev.flutter.pigeon.pigeon_example_package.MessageFlutterApi.flutterMethod\(messageChannelSuffix)"
let channel = FlutterBasicMessageChannel(name: channelName, binaryMessenger: binaryMessenger, codec: codec)
func flutterMethod(
aString aStringArg: String?, completion: @escaping (Result<String, FlutterError>) -> Void
) {
let channelName: String =
"dev.flutter.pigeon.pigeon_example_package.MessageFlutterApi.flutterMethod\(messageChannelSuffix)"
let channel = FlutterBasicMessageChannel(
name: channelName, binaryMessenger: binaryMessenger, codec: codec)
channel.sendMessage([aStringArg] as [Any?]) { response in
guard let listResponse = response as? [Any?] else {
completion(.failure(createConnectionError(withChannelName: channelName)))
Expand All @@ -217,7 +232,11 @@ class MessageFlutterApi: MessageFlutterApiProtocol {
let details: String? = nilOrValue(listResponse[2])
completion(.failure(FlutterError(code: code, message: message, details: details)))
} else if listResponse[0] == nil {
completion(.failure(FlutterError(code: "null-error", message: "Flutter api returned null value for non-null return value.", details: "")))
completion(
.failure(
FlutterError(
code: "null-error",
message: "Flutter api returned null value for non-null return value.", details: "")))
} else {
let result = listResponse[0] as! String
completion(.success(result))
Expand Down

0 comments on commit 0e97d19

Please sign in to comment.