Skip to content

Commit

Permalink
chore: refactor ios inapp classes and fix naming
Browse files Browse the repository at this point in the history
  • Loading branch information
mrehan27 committed Nov 28, 2024
1 parent 84a5fa7 commit b34a01f
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 80 deletions.
4 changes: 4 additions & 0 deletions ios/Classes/CustomerIOPlugin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#import <Flutter/Flutter.h>

@interface CustomerIOPlugin : NSObject<FlutterPlugin>
@end
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
4 changes: 0 additions & 4 deletions ios/Classes/CustomerIoPlugin.h

This file was deleted.

32 changes: 32 additions & 0 deletions ios/Classes/MessagingInApp/CustomerIOInAppEventListener.swift
Original file line number Diff line number Diff line change
@@ -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])
}
}
Original file line number Diff line number Diff line change
@@ -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?

Expand Down Expand Up @@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -176,49 +176,16 @@ 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)")
} catch {
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 {
Expand All @@ -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])
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ flutter:
package: io.customer.customer_io
pluginClass: CustomerIOPlugin
ios:
pluginClass: CustomerIoPlugin
pluginClass: CustomerIOPlugin
native_sdk_version: 3.5.1

0 comments on commit b34a01f

Please sign in to comment.