Skip to content

Commit

Permalink
chore: initialize native ios sdk in flutter (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrehan27 authored Nov 6, 2024
1 parent 9eb5f45 commit e9af59b
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 22 deletions.
59 changes: 59 additions & 0 deletions ios/Classes/Bridge/CustomerIOSDKConfigMapper.swift
Original file line number Diff line number Diff line change
@@ -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<Raw>(
in config: [String: Any?]?,
thenPassItTo handler: (Raw) -> Any
) {
ifNotNil(in: config, thenPassItTo: handler) { $0 }
}

func ifNotNil<Raw, Transformed>(
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)
}
}
}
33 changes: 11 additions & 22 deletions ios/Classes/SwiftCustomerIoPlugin.swift
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import Flutter
import UIKit
import CioDataPipelines
import CioInternalCommon
import CioMessagingInApp

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()
Expand Down Expand Up @@ -175,29 +178,15 @@ public class SwiftCustomerIoPlugin: NSObject, FlutterPlugin {
}

private func initialize(params : Dictionary<String, AnyHashable>){
// 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)")
}
*/
}

/**
Expand Down

0 comments on commit e9af59b

Please sign in to comment.