Skip to content
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

Handle error cases on iOS correctly, and simplify code #221

Merged
merged 3 commits into from Nov 15, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 12 additions & 26 deletions ios/Classes/handlers/PushActivationEventHandlers.swift
Expand Up @@ -19,7 +19,7 @@ public class PushActivationEventHandlers: NSObject, ARTPushRegistererDelegate {
return instance!
}

// FlutterResults to return result as Future<void> or throws an error. This is the convenient API.
// FlutterResult to return result as Future<void> or throws an error. This is the convenient API.
public var flutterResultForActivate: FlutterResult? = nil;
public var flutterResultForDeactivate: FlutterResult? = nil;
// There is no result stored for didAblyPushRegistrationFail, because there is dart side method call to return values to.
Expand All @@ -34,43 +34,29 @@ public class PushActivationEventHandlers: NSObject, ARTPushRegistererDelegate {
defer {
flutterResultForActivate = nil
}

guard let result = flutterResultForActivate else {
let error = FlutterError(code: "didActivateAblyPush_error", message: "Failed to asynchronously return a value because flutterResultForActivate was nil", details: nil)
methodChannel.invokeMethod(AblyPlatformMethod_pushOnActivate, arguments: error, result: nil)
return
}
if let error = error {
result(FlutterError(code: String(error.code), message: error.message, details: error))

methodChannel.invokeMethod(AblyPlatformMethod_pushOnActivate, arguments: error, result: nil)
if let result = flutterResultForActivate {
result(error)
} else {
result(nil)
print("Did not return a value asynchronously because flutterResultForActivate was nil. The app might have been restarted since calling activate.")
}
methodChannel.invokeMethod(AblyPlatformMethod_pushOnActivate, arguments: error, result: nil)
}

public func didDeactivateAblyPush(_ error: ARTErrorInfo?) {
defer {
flutterResultForDeactivate = nil
}

guard let result = flutterResultForDeactivate else {
let error = FlutterError(code: "didDeactivateAblyPush_error", message: "Failed to asynchronously return a value because flutterResultForDeactivate was nil", details: nil)
methodChannel.invokeMethod(AblyPlatformMethod_pushOnDeactivate, arguments: error, result: nil)
return
}
if let error = error {
result(FlutterError(code: String(error.code), message: error.message, details: error))

methodChannel.invokeMethod(AblyPlatformMethod_pushOnDeactivate, arguments: error, result: nil)
if let result = flutterResultForDeactivate {
result(error)
} else {
result(nil)
print("Did not return a value asynchronously because flutterResultForDeactivate was nil. The app might have been restarted since calling deactivate.")
}
methodChannel.invokeMethod(AblyPlatformMethod_pushOnDeactivate, arguments: error, result: nil)
}

public func didAblyPushRegistrationFail(_ error: ARTErrorInfo?) {
if let error = error {
methodChannel.invokeMethod(AblyPlatformMethod_pushOnUpdateFailed, arguments: error, result: nil)
} else {
methodChannel.invokeMethod(AblyPlatformMethod_pushOnUpdateFailed, arguments: FlutterError(code: "40000", message: "Ably push update failed, but no error was provided", details: nil), result: nil)
}
methodChannel.invokeMethod(AblyPlatformMethod_pushOnUpdateFailed, arguments: error, result: nil)
}
}