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: initialize native ios sdk in flutter #160

Merged
merged 4 commits into from
Nov 6, 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
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
Loading