From e9af59b0d0cab7fc016c484ff9fb7dc82d44aa52 Mon Sep 17 00:00:00 2001 From: Muhammad Rehan Date: Wed, 6 Nov 2024 15:15:59 +0500 Subject: [PATCH] chore: initialize native ios sdk in flutter (#160) --- .../Bridge/CustomerIOSDKConfigMapper.swift | 59 +++++++++++++++++++ ios/Classes/SwiftCustomerIoPlugin.swift | 33 ++++------- 2 files changed, 70 insertions(+), 22 deletions(-) create mode 100644 ios/Classes/Bridge/CustomerIOSDKConfigMapper.swift diff --git a/ios/Classes/Bridge/CustomerIOSDKConfigMapper.swift b/ios/Classes/Bridge/CustomerIOSDKConfigMapper.swift new file mode 100644 index 0000000..aa31776 --- /dev/null +++ b/ios/Classes/Bridge/CustomerIOSDKConfigMapper.swift @@ -0,0 +1,59 @@ +import CioDataPipelines + +enum SDKConfigBuilderError: Error { + case missingCdpApiKey +} + +extension SDKConfigBuilder { + private enum Config: String { + case migrationSiteId + case cdpApiKey + case region + case logLevel + case autoTrackDeviceAttributes + case trackApplicationLifecycleEvents + case flushAt + case flushInterval + case apiHost + case cdnHost + } + + @available(iOSApplicationExtension, unavailable) + static func create(from config: [String: Any?]) throws -> SDKConfigBuilder { + guard let cdpApiKey = config[Config.cdpApiKey.rawValue] as? String else { + throw SDKConfigBuilderError.missingCdpApiKey + } + + let builder = SDKConfigBuilder(cdpApiKey: cdpApiKey) + Config.migrationSiteId.ifNotNil(in: config, thenPassItTo: builder.migrationSiteId) + Config.region.ifNotNil(in: config, thenPassItTo: builder.region, transformingBy: Region.getRegion) + Config.logLevel.ifNotNil(in: config, thenPassItTo: builder.logLevel, transformingBy: CioLogLevel.getLogLevel) + Config.autoTrackDeviceAttributes.ifNotNil(in: config, thenPassItTo: builder.autoTrackDeviceAttributes) + Config.trackApplicationLifecycleEvents.ifNotNil(in: config, thenPassItTo: builder.trackApplicationLifecycleEvents) + Config.flushAt.ifNotNil(in: config, thenPassItTo: builder.flushAt) { (value: NSNumber) in value.intValue } + Config.flushInterval.ifNotNil(in: config, thenPassItTo: builder.flushInterval) { (value: NSNumber) in value.doubleValue } + Config.apiHost.ifNotNil(in: config, thenPassItTo: builder.apiHost) + Config.cdnHost.ifNotNil(in: config, thenPassItTo: builder.cdnHost) + + return builder + } +} + +extension RawRepresentable where RawValue == String { + func ifNotNil( + in config: [String: Any?]?, + thenPassItTo handler: (Raw) -> Any + ) { + ifNotNil(in: config, thenPassItTo: handler) { $0 } + } + + func ifNotNil( + in config: [String: Any?]?, + thenPassItTo handler: (Transformed) -> Any, + transformingBy transform: (Raw) -> Transformed? + ) { + if let value = config?[self.rawValue] as? Raw, let result = transform(value) { + _ = handler(result) + } + } +} diff --git a/ios/Classes/SwiftCustomerIoPlugin.swift b/ios/Classes/SwiftCustomerIoPlugin.swift index dc98f14..ebcb1fa 100644 --- a/ios/Classes/SwiftCustomerIoPlugin.swift +++ b/ios/Classes/SwiftCustomerIoPlugin.swift @@ -1,5 +1,6 @@ import Flutter import UIKit +import CioDataPipelines import CioInternalCommon import CioMessagingInApp @@ -7,6 +8,8 @@ public class SwiftCustomerIoPlugin: NSObject, FlutterPlugin { private var methodChannel: FlutterMethodChannel! private var inAppMessagingChannelHandler: CusomterIOInAppMessaging! + + private let logger: CioInternalCommon.Logger = DIGraphShared.shared.logger public static func register(with registrar: FlutterPluginRegistrar) { let instance = SwiftCustomerIoPlugin() @@ -175,29 +178,15 @@ public class SwiftCustomerIoPlugin: NSObject, FlutterPlugin { } private func initialize(params : Dictionary){ - // TODO: Fix initialize implementation - /* - guard let siteId = params[Keys.Environment.siteId] as? String, - let apiKey = params[Keys.Environment.apiKey] as? String, - let regionStr = params[Keys.Environment.region] as? String - else { - return - } - - let region = Region.getRegion(from: regionStr) - - CustomerIO.initialize(siteId: siteId, apiKey: apiKey, region: region){ - config in - config.modify(params: params) - } - - - if let enableInApp = params[Keys.Environment.enableInApp] as? Bool { - if enableInApp{ - initializeInApp() - } + do { + let sdkConfigBuilder = try SDKConfigBuilder.create(from: params) + CustomerIO.initialize(withConfig: sdkConfigBuilder.build()) + + // 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)") } - */ } /**