Skip to content

Commit

Permalink
Merge branch 'feature/fix-initialisation-error' into feat(typing)/all…
Browse files Browse the repository at this point in the history
…ow-non-string-trait-types

# Conflicts:
#	FlagsmithClient/src/main/java/com/flagsmith/entities/Trait.kt
  • Loading branch information
gazreese committed Feb 12, 2024
2 parents aa8ecda + 12f7d0a commit 402780d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
12 changes: 8 additions & 4 deletions FlagsmithClient/src/main/java/com/flagsmith/Flagsmith.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ class Flagsmith constructor(
private lateinit var retrofit: FlagsmithRetrofitService
private var cache: Cache? = null
private var lastUsedIdentity: String? = null
private val analytics: FlagsmithAnalytics? =
if (!enableAnalytics) null
else if (context != null) FlagsmithAnalytics(context, retrofit, analyticsFlushPeriod)
else throw IllegalArgumentException("Flagsmith requires a context to use the analytics feature")
private var analytics: FlagsmithAnalytics? = null

private val eventService: FlagsmithEventService? =
if (!enableRealtimeUpdates) null
Expand Down Expand Up @@ -93,6 +90,13 @@ class Flagsmith constructor(
writeTimeoutSeconds = writeTimeoutSeconds, timeTracker = this, klass = FlagsmithRetrofitService::class.java)
retrofit = pair.first
cache = pair.second

if (enableAnalytics) {
if (context == null || context.applicationContext == null) {
throw IllegalArgumentException("Flagsmith requires a context to use the analytics feature")
}
analytics = FlagsmithAnalytics(context, retrofit, analyticsFlushPeriod)
}
}

companion object {
Expand Down
18 changes: 13 additions & 5 deletions FlagsmithClient/src/main/java/com/flagsmith/entities/Trait.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@ import com.google.gson.annotations.SerializedName
data class Trait(
val identifier: String? = null,
@SerializedName(value = "trait_key") val key: String,
@SerializedName(value = "trait_value") val value: dynamic
)
@SerializedName(value = "trait_value") val value: String
) {
constructor(key: String, value: String) : this(null, key, value)
}

data class TraitWithIdentity(
@SerializedName(value = "trait_key") val key: String,
@SerializedName(value = "trait_value") val value: dynamic,
val identity: Identity
)
@SerializedName(value = "trait_value") val value: Any,
val identity: Identity,
) {
// Add a constructor that takes a string and sets the value to it
constructor(key: String, value: String, identity: Identity) : this(key, value as Any, identity)

// Add a constructor that takes an int and sets the value to it
constructor(key: String, value: Int, identity: Identity) : this(key, value as Any, identity)
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class FeatureFlagCachingTests {
flagsmithWithCache = Flagsmith(
environmentKey = "",
baseUrl = "http://localhost:${mockServer.localPort}",
enableAnalytics = false,
enableAnalytics = true, // Mix up the analytics flag to test initialisation
context = mockApplicationContext,
defaultFlags = defaultFlags,
cacheConfig = FlagsmithCacheConfig(enableCache = true)
Expand Down Expand Up @@ -112,6 +112,7 @@ class FeatureFlagCachingTests {
`when`(mockContextResources.getBoolean(anyInt())).thenReturn(false)
`when`(mockContextResources.getDimension(anyInt())).thenReturn(100f)
`when`(mockContextResources.getIntArray(anyInt())).thenReturn(intArrayOf(1, 2, 3))
`when`(mockApplicationContext.applicationContext).thenReturn(mockApplicationContext)
}

@After
Expand Down

0 comments on commit 402780d

Please sign in to comment.