Skip to content

Commit

Permalink
Fix initialisation error on the Flagsmith SDK when enabling analytics -
Browse files Browse the repository at this point in the history
fixes #39 (#40)

* Update the tests to reproduce the initialisation error

* Fix the initialisation error

* Remove TODO comment
  • Loading branch information
gazreese authored Feb 14, 2024
1 parent c794997 commit c747fb0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 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
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 c747fb0

Please sign in to comment.