Skip to content

Commit

Permalink
feat: updated client to support react native user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
mrehan27 committed Aug 25, 2022
1 parent 68010f8 commit 7588526
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ abstract class BaseTest {
protected lateinit var dateUtilStub: DateUtilStub

protected fun createConfig(
client: Client = Client.Android,
client: Client = Client.Android(sdkVersion = "1.0.0-alpha.6"),
siteId: String = this.siteId,
apiKey: String = "xyz",
region: Region = Region.EU,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DeviceStoreStub {
override val isPushEnabled: Boolean
get() = true
},
version = "1.0.0-alpha.6"
version = cioConfig.client.sdkVersion
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class CustomerIOFirebaseMessagingService : FirebaseMessagingService() {
}

private fun handleNewToken(token: String) {
CustomerIO.instance().registerDeviceToken(deviceToken = token)
CustomerIO.instanceOrNull()?.registerDeviceToken(deviceToken = token)
}

private fun handleMessageReceived(
Expand Down
18 changes: 17 additions & 1 deletion sdk/src/main/java/io/customer/sdk/CustomerIO.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@ class CustomerIO internal constructor(
companion object {
private var instance: CustomerIO? = null

/**
* Safe method to get SDK instance that may be called before SDK initialization.
* e.g. services, classes triggered by other libraries or Android framework.
* <p/>
* This method should only be used when SDK initialization could be delayed.
* @see [instance] to be used otherwise.
*
* @return instance of SDK if initialized; null otherwise.
*/
@JvmStatic
fun instanceOrNull(): CustomerIO? = try {
instance()
} catch (ex: Exception) {
null
}

@JvmStatic
fun instance(): CustomerIO {
return instance
Expand All @@ -105,7 +121,7 @@ class CustomerIO internal constructor(
private var region: Region = Region.US,
private val appContext: Application
) {
private var client: Client = Client.Android
private var client: Client = Client.Android(Version.version)
private var timeout = 6000L
private var shouldAutoRecordScreenViews: Boolean = false
private var autoTrackDeviceAttributes: Boolean = true
Expand Down
54 changes: 31 additions & 23 deletions sdk/src/main/java/io/customer/sdk/data/store/Client.kt
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
package io.customer.sdk.data.store

import java.util.*

/**
* Date class to hold information about the package client app is using.
*
* This class only holds info that can have multiple values e.g. source
* package type (Android, ReactNative, etc.) and not the information that
* cannot be change (e.g. the SDK version)
* Sealed class to hold information about the SDK wrapper and package that the
* client app is using.
*
* @property source name of the client source to append with user-agent
* @property identifier name only used to identify client, can be anything;
* default value is lowercase of the name
* @property source name of the client to append with user-agent.
* @property sdkVersion version of the SDK used.
*/
sealed class Client(
val source: String,
private val identifier: String? = source.lowercase(Locale.ENGLISH)
val sdkVersion: String
) {
object Android : Client(source = "Android")
object ReactNative : Client(source = "ReactNative")
object Expo : Client(source = "Expo")
class Other(value: String) : Client(source = value, identifier = null)
override fun toString(): String = "$source Client/$sdkVersion"

/**
* Simpler class for Android clients.
*/
class Android(sdkVersion: String) : Client(source = "Android", sdkVersion = sdkVersion)

/**
* Simpler class for ReactNative clients.
*/
class ReactNative(sdkVersion: String) : Client(source = "ReactNative", sdkVersion = sdkVersion)

/**
* Simpler class for Expo clients.
*/
class Expo(sdkVersion: String) : Client(source = "Expo", sdkVersion = sdkVersion)

companion object {
fun fromSource(source: String): Client {
val identifier = source.lowercase(Locale.ENGLISH)
return listOf(Android, ReactNative, Expo).find { client ->
client.identifier == identifier
} ?: Other(value = source)
}
}
/**
* Other class to allow adding custom sources for clients that are not
* supported above.
* <p/>
* Use this only if the client platform is not available in the above list.
*/
class Other(
source: String,
sdkVersion: String
) : Client(source = source, sdkVersion = sdkVersion)
}
3 changes: 1 addition & 2 deletions sdk/src/main/java/io/customer/sdk/data/store/DeviceStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ class DeviceStoreImp(

override fun buildUserAgent(): String {
return buildString {
append("Customer.io ${sdkConfig.client.source} Client/")
append(customerIOVersion)
append("Customer.io ${sdkConfig.client}")
append(" ($deviceManufacturer $deviceModel; $deviceOSVersion)")
append(" $customerPackageName/${customerAppVersion ?: "0.0.0"}")
}
Expand Down
38 changes: 38 additions & 0 deletions sdk/src/sharedTest/java/io/customer/sdk/data/store/ClientTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.customer.sdk.data.store

import androidx.test.ext.junit.runners.AndroidJUnit4
import io.customer.commontest.BaseTest
import org.amshove.kluent.shouldBeEqualTo
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class ClientTest : BaseTest() {
@Test
fun initialize_givenAndroid_expectAndroidClient() {
val androidClient = Client.Android(sdkVersion = "2.6.3")

androidClient.toString().shouldBeEqualTo(expected = "Android Client/2.6.3")
}

@Test
fun initialize_givenExpo_expectExpoClient() {
val expoClient = Client.Expo(sdkVersion = "3.9.7")

expoClient.toString().shouldBeEqualTo(expected = "Expo Client/3.9.7")
}

@Test
fun initialize_givenReactNative_expectReactNativeClient() {
val reactNativeClient = Client.ReactNative(sdkVersion = "7.3.2")

reactNativeClient.toString().shouldBeEqualTo(expected = "ReactNative Client/7.3.2")
}

@Test
fun initialize_givenOther_expectOtherClient() {
val iOSClient = Client.Other(source = "iOS", sdkVersion = "4.6.7")

iOSClient.toString().shouldBeEqualTo(expected = "iOS Client/4.6.7")
}
}

0 comments on commit 7588526

Please sign in to comment.