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

chore: refactor ios in-app classes and fix naming #184

Merged
merged 1 commit into from
Nov 28, 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
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
Loading