Skip to content

Commit

Permalink
patch: resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
qingzhuozhen committed Mar 17, 2022
1 parent 147eb46 commit 2ec5db8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 43 deletions.
3 changes: 1 addition & 2 deletions id/src/main/java/com/amplitude/id/IdentityContainer.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.amplitude.id

class IdentityContainer private constructor(val configuration: IdentityConfiguration) {
private val identityStorage: IdentityStorage
val identityManager: IdentityManager

companion object {
Expand All @@ -20,7 +19,7 @@ class IdentityContainer private constructor(val configuration: IdentityConfigura
}

init {
identityStorage = configuration.identityStorageProvider.getIdentityStorage(configuration)
val identityStorage = configuration.identityStorageProvider.getIdentityStorage(configuration)
identityManager = IdentityManagerImpl(identityStorage)
}
}
42 changes: 3 additions & 39 deletions id/src/main/java/com/amplitude/id/IdentityManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ import java.util.concurrent.locks.ReentrantReadWriteLock
import kotlin.concurrent.read
import kotlin.concurrent.write

internal const val ID_OP_SET = "\$set"
internal const val ID_OP_UNSET = "\$unset"
internal const val ID_OP_CLEAR_ALL = "\$clearAll"

interface IdentityListener {

fun onUserIdChange(userId: String?)
Expand All @@ -19,8 +15,7 @@ interface IdentityListener {

data class Identity(
val userId: String? = null,
val deviceId: String? = null,
val userProperties: Map<String, Any?> = mapOf(),
val deviceId: String? = null
)

enum class IdentityUpdateType {
Expand All @@ -33,8 +28,6 @@ interface IdentityManager {

fun setUserId(userId: String?): Editor
fun setDeviceId(deviceId: String?): Editor
fun setUserProperties(userProperties: Map<String, Any?>): Editor
fun updateUserProperties(actions: Map<String, Map<String, Any?>>): Editor
fun commit()
}

Expand All @@ -46,7 +39,7 @@ interface IdentityManager {
fun isInitialized(): Boolean
}

internal class IdentityManagerImpl(val identityStorage: IdentityStorage): IdentityManager {
internal class IdentityManagerImpl(private val identityStorage: IdentityStorage): IdentityManager {

private val identityLock = ReentrantReadWriteLock(true)
private var identity = Identity()
Expand All @@ -65,7 +58,6 @@ internal class IdentityManagerImpl(val identityStorage: IdentityStorage): Identi

private var userId: String? = originalIdentity.userId
private var deviceId: String? = originalIdentity.deviceId
private var userProperties: Map<String, Any?> = originalIdentity.userProperties

override fun setUserId(userId: String?): IdentityManager.Editor {
this.userId = userId
Expand All @@ -77,36 +69,8 @@ internal class IdentityManagerImpl(val identityStorage: IdentityStorage): Identi
return this
}

override fun setUserProperties(userProperties: Map<String, Any?>): IdentityManager.Editor {
this.userProperties = userProperties
return this
}

override fun updateUserProperties(actions: Map<String, Map<String, Any?>>): IdentityManager.Editor {
val actingProperties = this.userProperties.toMutableMap()
for (actionEntry in actions.entries) {
val action = actionEntry.key
val properties = actionEntry.value
when (action) {
ID_OP_SET -> {
actingProperties.putAll(properties)
}
ID_OP_UNSET -> {
for (entry in properties.entries) {
actingProperties.remove(entry.key)
}
}
ID_OP_CLEAR_ALL -> {
actingProperties.clear()
}
}
}
this.userProperties = actingProperties
return this
}

override fun commit() {
val newIdentity = Identity(userId, deviceId, userProperties)
val newIdentity = Identity(userId, deviceId)
setIdentity(newIdentity)
}
}
Expand Down
4 changes: 2 additions & 2 deletions samples/kotlin-jvm-app/src/main/kotlin/main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ fun main() {
runBlocking {
val amplitude = Amplitude(
Configuration(
apiKey = "API-Key",
apiKey = "bbdf6e7b53f5d0e48b40f2eb51cd8ab4",
storageProvider = FileStorageProvider()
)
)
val event = BaseEvent()
event.eventType = "Kotlin JVM Test"
event.eventType = "Kotlin JVM Test with retry"
event.userId = "kotlin-test-user"
event.deviceId = "kotlin-test-device"
amplitude.track(event)
Expand Down

0 comments on commit 2ec5db8

Please sign in to comment.