Skip to content

Commit

Permalink
feat: expose device token (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shahroz16 committed Jul 7, 2023
1 parent 85022d1 commit deaa916
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 3 deletions.
8 changes: 6 additions & 2 deletions Makefile
Expand Up @@ -28,6 +28,10 @@ lint:
lint-install:
./scripts/get-ktlint.sh

# Generate public API binary
generate-public-api:
./gradlew apiDump

# Run kotlin binary validator
run-binary-validator:
./scripts/binary-validation.sh
validate-public-api:
./scripts/binary-validation.sh
2 changes: 1 addition & 1 deletion lefthook.yml
Expand Up @@ -10,7 +10,7 @@ pre-commit:
# Format kotlin code and then `git add` modified files from formatter.
run: make format && git add {staged_files}
binary-validation:
run: make run-binary-validator
run: make validate-public-api

# Commands to run before pushing code
pre-push:
Expand Down
3 changes: 3 additions & 0 deletions sdk/api/sdk.api
Expand Up @@ -5,6 +5,7 @@ public final class io/customer/sdk/CustomerIO : io/customer/sdk/CustomerIOInstan
public fun getDeviceAttributes ()Ljava/util/Map;
public final fun getDiGraph ()Lio/customer/sdk/di/CustomerIOComponent;
public fun getProfileAttributes ()Ljava/util/Map;
public fun getRegisteredDeviceToken ()Ljava/lang/String;
public fun getSdkVersion ()Ljava/lang/String;
public fun getSiteId ()Ljava/lang/String;
public fun identify (Ljava/lang/String;)V
Expand Down Expand Up @@ -129,6 +130,7 @@ public abstract interface class io/customer/sdk/CustomerIOInstance {
public abstract fun deleteDeviceToken ()V
public abstract fun getDeviceAttributes ()Ljava/util/Map;
public abstract fun getProfileAttributes ()Ljava/util/Map;
public abstract fun getRegisteredDeviceToken ()Ljava/lang/String;
public abstract fun getSdkVersion ()Ljava/lang/String;
public abstract fun getSiteId ()Ljava/lang/String;
public abstract fun identify (Ljava/lang/String;)V
Expand Down Expand Up @@ -339,6 +341,7 @@ public abstract interface class io/customer/sdk/module/CustomerIOModuleConfig$Bu
public abstract interface class io/customer/sdk/repository/DeviceRepository {
public abstract fun addCustomDeviceAttributes (Ljava/util/Map;)V
public abstract fun deleteDeviceToken ()V
public abstract fun getDeviceToken ()Ljava/lang/String;
public abstract fun registerDeviceToken (Ljava/lang/String;Ljava/util/Map;)V
}

Expand Down
5 changes: 5 additions & 0 deletions sdk/src/main/java/io/customer/sdk/CustomerIO.kt
Expand Up @@ -37,6 +37,8 @@ interface CustomerIOInstance {
var profileAttributes: CustomAttributes
var deviceAttributes: CustomAttributes

val registeredDeviceToken: String?

fun identify(identifier: String)

fun identify(
Expand Down Expand Up @@ -538,6 +540,9 @@ class CustomerIO internal constructor(
deviceRepository.addCustomDeviceAttributes(value)
}

override val registeredDeviceToken: String?
get() = deviceRepository.getDeviceToken()

private fun recordScreenViews(activity: Activity, attributes: CustomAttributes) {
val packageManager = activity.packageManager
return try {
Expand Down
Expand Up @@ -13,6 +13,7 @@ interface DeviceRepository {
fun registerDeviceToken(deviceToken: String, attributes: CustomAttributes)
fun deleteDeviceToken()
fun addCustomDeviceAttributes(attributes: CustomAttributes)
fun getDeviceToken(): String?
}

internal class DeviceRepositoryImpl(
Expand Down Expand Up @@ -63,6 +64,10 @@ internal class DeviceRepositoryImpl(
registerDeviceToken(existingDeviceToken, attributes)
}

override fun getDeviceToken(): String? {
return sitePreferenceRepository.getDeviceToken()
}

private fun createDeviceAttributes(customAddedAttributes: CustomAttributes): Map<String, Any> {
if (!config.autoTrackDeviceAttributes) return customAddedAttributes

Expand Down
22 changes: 22 additions & 0 deletions sdk/src/sharedTest/java/io/customer/sdk/CustomerIOTest.kt
Expand Up @@ -103,6 +103,28 @@ class CustomerIOTest : BaseTest() {
updatedConfig.trackingApiHostname shouldBeEqualTo givenTrackingApiUrl
}

@Test
fun deviceToken_givenGetValue_expectDeviceRepositoryGetDeviceToken() {
val givenDeviceToken = String.random
whenever(deviceRepositoryMock.getDeviceToken()).thenReturn(givenDeviceToken)
val customerIO = CustomerIO(di)

val actual = customerIO.registeredDeviceToken

actual shouldBeEqualTo givenDeviceToken
}

@Test
fun deviceToken_testRegisterDeviceTokenWhenPreviouslyNull() {
val givenDeviceToken = String.random

CustomerIO.instance().registeredDeviceToken shouldBe null

CustomerIO.instance().registerDeviceToken(givenDeviceToken)

CustomerIO.instance().registeredDeviceToken shouldBeEqualTo givenDeviceToken
}

@Test
fun deviceAttributes_givenSetValue_expectMakeRequestToAddAttributes() {
val givenAttributes = mapOf(String.random to String.random)
Expand Down

0 comments on commit deaa916

Please sign in to comment.