Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Identifies feature flags using name and email #337

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Loading