diff --git a/ios/Classes/CustomerIOPlugin.h b/ios/Classes/CustomerIOPlugin.h new file mode 100644 index 0000000..c9d15df --- /dev/null +++ b/ios/Classes/CustomerIOPlugin.h @@ -0,0 +1,4 @@ +#import <Flutter/Flutter.h> + +@interface CustomerIOPlugin : NSObject<FlutterPlugin> +@end diff --git a/ios/Classes/CustomerIoPlugin.m b/ios/Classes/CustomerIOPlugin.m similarity index 78% rename from ios/Classes/CustomerIoPlugin.m rename to ios/Classes/CustomerIOPlugin.m index a3353ca..498db87 100644 --- a/ios/Classes/CustomerIoPlugin.m +++ b/ios/Classes/CustomerIOPlugin.m @@ -1,4 +1,4 @@ -#import "CustomerIoPlugin.h" +#import "CustomerIOPlugin.h" #if __has_include(<customer_io/customer_io-Swift.h>) #import <customer_io/customer_io-Swift.h> #else @@ -8,8 +8,8 @@ #import "customer_io-Swift.h" #endif -@implementation CustomerIoPlugin +@implementation CustomerIOPlugin + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar { - [SwiftCustomerIoPlugin registerWithRegistrar:registrar]; + [SwiftCustomerIOPlugin registerWithRegistrar:registrar]; } @end diff --git a/ios/Classes/CustomerIoPlugin.h b/ios/Classes/CustomerIoPlugin.h deleted file mode 100644 index c98ba4b..0000000 --- a/ios/Classes/CustomerIoPlugin.h +++ /dev/null @@ -1,4 +0,0 @@ -#import <Flutter/Flutter.h> - -@interface CustomerIoPlugin : NSObject<FlutterPlugin> -@end diff --git a/ios/Classes/MessagingInApp/CustomerIOInAppEventListener.swift b/ios/Classes/MessagingInApp/CustomerIOInAppEventListener.swift new file mode 100644 index 0000000..cd179fc --- /dev/null +++ b/ios/Classes/MessagingInApp/CustomerIOInAppEventListener.swift @@ -0,0 +1,32 @@ +import CioMessagingInApp + +class CustomerIOInAppEventListener { + private let invokeDartMethod: (String, Any?) -> Void + + init(invokeDartMethod: @escaping (String, Any?) -> Void) { + self.invokeDartMethod = invokeDartMethod + } +} + +extension CustomerIOInAppEventListener: InAppEventListener { + func errorWithMessage(message: InAppMessage) { + invokeDartMethod("errorWithMessage", ["messageId": message.messageId, "deliveryId": message.deliveryId]) + } + + func messageActionTaken(message: InAppMessage, actionValue: String, actionName: String) { + invokeDartMethod("messageActionTaken", [ + "messageId": message.messageId, + "deliveryId": message.deliveryId, + "actionValue": actionValue, + "actionName": actionName + ]) + } + + func messageDismissed(message: InAppMessage) { + invokeDartMethod("messageDismissed", ["messageId": message.messageId, "deliveryId": message.deliveryId]) + } + + func messageShown(message: InAppMessage) { + invokeDartMethod("messageShown", ["messageId": message.messageId, "deliveryId": message.deliveryId]) + } +} diff --git a/ios/Classes/CustomerIOInAppMessaging.swift b/ios/Classes/MessagingInApp/CustomerIOInAppMessaging.swift similarity index 55% rename from ios/Classes/CustomerIOInAppMessaging.swift rename to ios/Classes/MessagingInApp/CustomerIOInAppMessaging.swift index 82ec4ed..9038980 100644 --- a/ios/Classes/CustomerIOInAppMessaging.swift +++ b/ios/Classes/MessagingInApp/CustomerIOInAppMessaging.swift @@ -1,8 +1,9 @@ import Foundation import Flutter +import CioInternalCommon import CioMessagingInApp -public class CusomterIOInAppMessaging: NSObject, FlutterPlugin { +public class CustomerIOInAppMessaging: NSObject, FlutterPlugin { private var methodChannel: FlutterMethodChannel? @@ -41,4 +42,23 @@ public class CusomterIOInAppMessaging: NSObject, FlutterPlugin { methodChannel?.setMethodCallHandler(nil) methodChannel = nil } + + func configureModule(params: [String: AnyHashable]) { + if let inAppConfig = try? MessagingInAppConfigBuilder.build(from: params) { + MessagingInApp.initialize(withConfig: inAppConfig) + MessagingInApp.shared.setEventListener(CustomerIOInAppEventListener(invokeDartMethod: invokeDartMethod)) + } + } + + func invokeDartMethod(_ method: String, _ args: Any?) { + // When sending messages from native code to Flutter, it's required to do it on main thread. + // Learn more: + // * https://docs.flutter.dev/platform-integration/platform-channels#channels-and-platform-threading + // * https://linear.app/customerio/issue/MBL-358/ + DIGraphShared.shared.threadUtil.runMain { [weak self] in + guard let self else { return } + + self.methodChannel?.invokeMethod(method, arguments: args) + } + } } diff --git a/ios/Classes/SwiftCustomerIoPlugin.swift b/ios/Classes/SwiftCustomerIOPlugin.swift similarity index 74% rename from ios/Classes/SwiftCustomerIoPlugin.swift rename to ios/Classes/SwiftCustomerIOPlugin.swift index f8363c1..556fc55 100644 --- a/ios/Classes/SwiftCustomerIoPlugin.swift +++ b/ios/Classes/SwiftCustomerIOPlugin.swift @@ -4,19 +4,19 @@ import CioDataPipelines import CioInternalCommon import CioMessagingInApp -public class SwiftCustomerIoPlugin: NSObject, FlutterPlugin { +public class SwiftCustomerIOPlugin: NSObject, FlutterPlugin { private var methodChannel: FlutterMethodChannel! - private var inAppMessagingChannelHandler: CusomterIOInAppMessaging! + private var inAppMessagingChannelHandler: CustomerIOInAppMessaging! private var messagingPushChannelHandler: CustomerIOMessagingPush! private let logger: CioInternalCommon.Logger = DIGraphShared.shared.logger public static func register(with registrar: FlutterPluginRegistrar) { - let instance = SwiftCustomerIoPlugin() + let instance = SwiftCustomerIOPlugin() instance.methodChannel = FlutterMethodChannel(name: "customer_io", binaryMessenger: registrar.messenger()) registrar.addMethodCallDelegate(instance, channel: instance.methodChannel) - instance.inAppMessagingChannelHandler = CusomterIOInAppMessaging(with: registrar) + instance.inAppMessagingChannelHandler = CustomerIOInAppMessaging(with: registrar) instance.messagingPushChannelHandler = CustomerIOMessagingPush(with: registrar) } @@ -176,15 +176,9 @@ public class SwiftCustomerIoPlugin: NSObject, FlutterPlugin { // Initialize native SDK with provided config let sdkConfigBuilder = try SDKConfigBuilder.create(from: params) CustomerIO.initialize(withConfig: sdkConfigBuilder.build()) - - if let inAppConfig = try? MessagingInAppConfigBuilder.build(from: params) { - MessagingInApp.initialize(withConfig: inAppConfig) - MessagingInApp.shared.setEventListener(CustomerIOInAppEventListener( - invokeMethod: {method,args in - self.invokeMethod(method, args) - }) - ) - } + + // Initialize in-app messaging with provided config + inAppMessagingChannelHandler.configureModule(params: params) // TODO: Initialize in-app module with given config logger.debug("Customer.io SDK initialized with config: \(params)") @@ -192,33 +186,6 @@ public class SwiftCustomerIoPlugin: NSObject, FlutterPlugin { logger.error("Initializing Customer.io SDK failed with error: \(error)") } } - - /** - Initialize in-app using customerio plugin - */ - private func initializeInApp(){ - // TODO: Fix initializeInApp implementation - /* - DispatchQueue.main.async { - MessagingInApp.shared.initialize(eventListener: CustomerIOInAppEventListener( - invokeMethod: {method,args in - self.invokeMethod(method, args) - }) - ) - } - */ - } - - func invokeMethod(_ method: String, _ args: Any?) { - // When sending messages from native code to Flutter, it's required to do it on main thread. - // Learn more: - // * https://docs.flutter.dev/platform-integration/platform-channels#channels-and-platform-threading - // * https://linear.app/customerio/issue/MBL-358/ - DispatchQueue.main.async { - self.methodChannel.invokeMethod(method, arguments: args) - } - } - } private extension FlutterMethodCall { @@ -238,34 +205,3 @@ private extension FlutterMethodCall { } } - -class CustomerIOInAppEventListener { - private let invokeMethod: (String, Any?) -> Void - - init(invokeMethod: @escaping (String, Any?) -> Void) { - self.invokeMethod = invokeMethod - } -} - -extension CustomerIOInAppEventListener: InAppEventListener { - func errorWithMessage(message: InAppMessage) { - invokeMethod("errorWithMessage", ["messageId": message.messageId, "deliveryId": message.deliveryId]) - } - - func messageActionTaken(message: InAppMessage, actionValue: String, actionName: String) { - invokeMethod("messageActionTaken", [ - "messageId": message.messageId, - "deliveryId": message.deliveryId, - "actionValue": actionValue, - "actionName": actionName - ]) - } - - func messageDismissed(message: InAppMessage) { - invokeMethod("messageDismissed", ["messageId": message.messageId, "deliveryId": message.deliveryId]) - } - - func messageShown(message: InAppMessage) { - invokeMethod("messageShown", ["messageId": message.messageId, "deliveryId": message.deliveryId]) - } -} diff --git a/pubspec.yaml b/pubspec.yaml index c77b0b5..3718439 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -41,5 +41,5 @@ flutter: package: io.customer.customer_io pluginClass: CustomerIOPlugin ios: - pluginClass: CustomerIoPlugin + pluginClass: CustomerIOPlugin native_sdk_version: 3.5.1