Skip to content

Commit

Permalink
Identifies feature flags using name and email (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
newmskywalker authored Oct 26, 2024
1 parent 805eed8 commit 6bb0ebe
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
13 changes: 10 additions & 3 deletions android/app-newm/src/main/java/io/newm/AppLaunchGhostActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ import io.newm.shared.NewmAppLogger
import io.newm.shared.public.featureflags.FeatureFlagManager
import io.newm.shared.public.usecases.UserDetailsUseCase
import io.newm.shared.public.usecases.UserSessionUseCase
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.timeout
import kotlinx.coroutines.launch
import org.koin.android.ext.android.inject
import kotlin.time.Duration.Companion.seconds

class AppLaunchGhostActivity : ComponentActivity() {
private val tag = "AppLaunchGhostActivity"
Expand All @@ -21,6 +25,7 @@ class AppLaunchGhostActivity : ComponentActivity() {
private val featureFlagMager: FeatureFlagManager by inject()
private val logger: NewmAppLogger by inject()

@OptIn(FlowPreview::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
installSplashScreen().apply {
Expand All @@ -30,9 +35,11 @@ class AppLaunchGhostActivity : ComponentActivity() {
if (userSession.isLoggedIn()) {
lifecycleScope.launch {
try {
val user =
userDetailsUseCase.fetchLoggedInUserDetailsFlow().filterNotNull().first()
featureFlagMager.setUserId(user.id)
userDetailsUseCase.fetchLoggedInUserDetailsFlow()
.filterNotNull()
.onEach(featureFlagMager::setUser)
.timeout(3.seconds)
.first()
} catch (e: Exception) {
logger.error(
tag,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.launchdarkly.sdk.android.LDConfig.Builder.AutoEnvAttributes
import io.newm.shared.config.NewmSharedBuildConfig
import io.newm.shared.public.featureflags.FeatureFlag
import io.newm.shared.public.featureflags.FeatureFlagManager
import io.newm.shared.public.models.User
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -38,8 +39,11 @@ class AndroidFeatureFlagManager(
return client.boolVariation(flag.key, default)
}

override suspend fun setUserId(id: String) {
val ldContext = LDContext.builder(ContextKind.DEFAULT, id).build()
override suspend fun setUser(user: User) {
val ldContext = LDContext.builder(ContextKind.DEFAULT, user.id)
.set("email", user.email)
.name(user.nickname ?: user.email)
.build()

client.identify(ldContext)
.asDeferred()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.newm.shared.public.featureflags

import io.newm.shared.public.models.User

interface FeatureFlagManager {
fun isEnabled(flag: FeatureFlag, default: Boolean = false): Boolean
suspend fun setUserId(id: String)
suspend fun setUser(user: User)
}

0 comments on commit 6bb0ebe

Please sign in to comment.