Skip to content

Commit

Permalink
fix: support for expo client
Browse files Browse the repository at this point in the history
  • Loading branch information
mrehan27 committed Oct 31, 2022
1 parent c95ddb7 commit b641fd8
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 92 deletions.
Expand Up @@ -30,7 +30,7 @@ object CustomerIOReactNativeInstance {
context = context,
environment = preferencesStorage.loadEnvironmentSettings(),
configuration = preferencesStorage.loadConfigurationSettings(),
sdkVersion = preferencesStorage.loadSDKVersion(),
packageConfig = preferencesStorage.loadPackageConfigurations(),
)
logger.info("Customer.io instance initialized successfully from preferences")
} catch (ex: Exception) {
Expand All @@ -43,7 +43,7 @@ object CustomerIOReactNativeInstance {
context: Context,
environment: Map<String, Any?>,
configuration: Map<String, Any?>?,
sdkVersion: String?,
packageConfig: Map<String, Any?>?,
): CustomerIO {
val siteId = environment.getString(Keys.Environment.SITE_ID)
val apiKey = environment.getString(Keys.Environment.API_KEY)
Expand All @@ -60,7 +60,7 @@ object CustomerIOReactNativeInstance {
region = region,
appContext = context.applicationContext as Application,
).apply {
setClient(Client.ReactNative(sdkVersion = sdkVersion ?: "n/a"))
setClient(client = getUserAgentClient(packageConfig = packageConfig))
setupConfig(configuration)
addCustomerIOModule(module = configureModuleMessagingPushFCM(configuration))
if (!organizationId.isNullOrBlank()) {
Expand All @@ -69,6 +69,23 @@ object CustomerIOReactNativeInstance {
}.build()
}

private fun getUserAgentClient(packageConfig: Map<String, Any?>?): Client {
val sourceSDK = packageConfig?.getProperty<String>(
Keys.PackageConfig.SOURCE_SDK
)?.takeIfNotBlank()
val sourceSDKVersion = packageConfig?.getProperty<String>(
Keys.PackageConfig.SOURCE_SDK_VERSION
)?.takeIfNotBlank() ?: packageConfig?.getProperty<String>(
Keys.PackageConfig.SOURCE_SDK_VERSION_COMPAT
)?.takeIfNotBlank() ?: "n/a"
return when {
sourceSDK?.equals(
other = "expo", ignoreCase = true,
) == true -> Client.Expo(sdkVersion = sourceSDKVersion)
else -> Client.ReactNative(sdkVersion = sourceSDKVersion)
}
}

private fun CustomerIO.Builder.setupConfig(config: Map<String, Any?>?): CustomerIO.Builder {
if (config == null) return this

Expand Down
Expand Up @@ -39,26 +39,27 @@ class CustomerIOReactNativeModule(
fun initialize(
environment: ReadableMap,
configuration: ReadableMap? = null,
sdkVersion: String? = null,
packageConfiguration: ReadableMap? = null,
) {
if (isInstanceValid()) {
logger.info("Customer.io instance already initialized, reinitializing")
}

val env = environment.toMap()
val config = configuration?.toMap()
val packageConfig = packageConfiguration?.toMap()

preferencesStorage.saveSettings(
environment = env,
configuration = config,
sdkVersion = sdkVersion
packageConfig = packageConfig
)
try {
customerIO = CustomerIOReactNativeInstance.initialize(
context = reactApplicationContext,
environment = env,
configuration = config,
sdkVersion = sdkVersion,
packageConfig = packageConfig,
)
logger.info("Customer.io instance initialized successfully from app")
} catch (ex: Exception) {
Expand Down
Expand Up @@ -16,4 +16,10 @@ internal object Keys {
const val BACKGROUND_QUEUE_MIN_NUMBER_OF_TASKS = "backgroundQueueMinNumberOfTasks"
const val BACKGROUND_QUEUE_SECONDS_DELAY = "backgroundQueueSecondsDelay"
}

object PackageConfig {
const val SOURCE_SDK = "source"
const val SOURCE_SDK_VERSION = "version"
const val SOURCE_SDK_VERSION_COMPAT = "sdkVersion"
}
}
Expand Up @@ -23,6 +23,9 @@ class PreferencesStorage(context: Context) {
BACKGROUND_QUEUE_SECONDS_DELAY,
)
}
private val packageConfigKeys = with(Keys.PackageConfig) {
arrayOf(SOURCE_SDK, SOURCE_SDK_VERSION)
}

init {
sharedPref = context.getSharedPreferences(
Expand All @@ -33,7 +36,7 @@ class PreferencesStorage(context: Context) {
fun saveSettings(
environment: Map<String, Any?>,
configuration: Map<String, Any?>?,
sdkVersion: String?,
packageConfig: Map<String, Any?>?,
) = with(sharedPref.edit()) {
for (key in environmentKeys) {
putString(key, environment[key]?.toString()?.encodeToBase64())
Expand All @@ -43,7 +46,11 @@ class PreferencesStorage(context: Context) {
putString(key, configuration[key]?.toString()?.encodeToBase64())
}
}
putString(SDK_VERSION_KEY, sdkVersion?.encodeToBase64())
if (packageConfig != null) {
for (key in packageConfigKeys) {
putString(key, packageConfig[key]?.toString()?.encodeToBase64())
}
}
apply()
}

Expand All @@ -62,7 +69,10 @@ class PreferencesStorage(context: Context) {
return map
}

fun loadSDKVersion() = sharedPref.getString(SDK_VERSION_KEY, null)?.decodeFromBase64()
fun loadPackageConfigurations() = loadSettings(
keys = packageConfigKeys.plus(Keys.PackageConfig.SOURCE_SDK_VERSION_COMPAT),
)

fun loadEnvironmentSettings() = loadSettings(environmentKeys)
fun loadConfigurationSettings() = loadSettings(configKeys) { key, value ->
with(Keys.Config) {
Expand All @@ -81,8 +91,6 @@ class PreferencesStorage(context: Context) {
}

companion object {
private const val SDK_VERSION_KEY = "sdkVersion"

private fun String.encodeToBase64(): String {
return Base64.encodeToString(toByteArray(Charsets.UTF_8), Base64.NO_WRAP)
}
Expand Down
4 changes: 2 additions & 2 deletions customerio-reactnative.podspec
Expand Up @@ -16,6 +16,6 @@ Pod::Spec.new do |s|
s.source_files = "ios/**/*.{h,m,mm,swift}"

s.dependency "React-Core"
s.dependency "CustomerIOTracking", '~> 1.2.0'
s.dependency "CustomerIOMessagingInApp", '~> 1.2.0'
s.dependency "CustomerIOTracking", '~> 1.2.1'
s.dependency "CustomerIOMessagingInApp", '~> 1.2.1'
end
2 changes: 1 addition & 1 deletion ios/CustomerioReactnative.m
Expand Up @@ -4,7 +4,7 @@ @interface RCT_EXTERN_MODULE(CustomerioReactnative, NSObject)

RCT_EXTERN_METHOD(initialize: (nonnull NSDictionary *) env
configData : (NSDictionary *) configData
pversion: (nonnull NSString *) pversion)
packageConfig: (nonnull NSDictionary *) packageConfig)

RCT_EXTERN_METHOD(identify: (nonnull NSString *) identifier
body : (NSDictionary *) body)
Expand Down
17 changes: 14 additions & 3 deletions ios/CustomerioReactnative.swift
Expand Up @@ -13,13 +13,24 @@ class CustomerioReactnative: NSObject {
/**
Initialize the package before sending any calls to the package
*/
@objc(initialize:configData:pversion:)
func initialize(env: Dictionary<String, AnyHashable>, configData: Dictionary<String, AnyHashable>, pversion: String) -> Void {
@objc(initialize:configData:packageConfig:)
func initialize(env: Dictionary<String, AnyHashable>, configData: Dictionary<String, AnyHashable>, packageConfig: Dictionary<String, AnyHashable>) -> Void {

guard let siteId = env["siteId"] as? String, let apiKey = env["apiKey"] as? String, let region = env["region"] as? String, let organizationId = env["organizationId"] as? String else {
return
}

guard let pversion = packageConfig["version"] as? String, let source = packageConfig["source"] as? String else {
return
}

var sdkSource = SdkWrapperConfig.Source.reactNative
if source.lowercased() == "expo" {
sdkSource = SdkWrapperConfig.Source.expo
}

CustomerIO.initialize(siteId: siteId, apiKey: apiKey, region: Region.getLocation(from: region)) { config in
config._sdkWrapperConfig = SdkWrapperConfig(source: SdkWrapperConfig.Source.reactNative, version: pversion )
config._sdkWrapperConfig = SdkWrapperConfig(source: sdkSource, version: pversion )
config.autoTrackDeviceAttributes = configData["autoTrackDeviceAttributes"] as! Bool
config.logLevel = CioLogLevel.getLogValue(for: configData["logLevel"] as! Int)
config.autoTrackPushEvents = configData["autoTrackPushEvents"] as! Bool
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -7,6 +7,7 @@
"types": "lib/typescript/index.d.ts",
"react-native": "src/index",
"source": "src/index",
"expoVersion": "",
"files": [
"src",
"lib",
Expand Down
8 changes: 7 additions & 1 deletion src/CustomerioConfig.tsx
Expand Up @@ -26,7 +26,13 @@ class CustomerIOEnv {
organizationId: string = ""
}

class PackageConfig {
version: string = ""
source: string = ""
}

export {
CustomerioConfig,
CustomerIOEnv
CustomerIOEnv,
PackageConfig
}

0 comments on commit b641fd8

Please sign in to comment.