-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: initialize native ios sdk in flutter (#160)
- Loading branch information
Showing
2 changed files
with
70 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters