From c747fb0007e1932e014318b03c54de6c18e26c44 Mon Sep 17 00:00:00 2001 From: Gareth Reese <8297652+gazreese@users.noreply.github.com> Date: Wed, 14 Feb 2024 14:31:15 +0200 Subject: [PATCH] Fix initialisation error on the Flagsmith SDK when enabling analytics - fixes #39 (#40) * Update the tests to reproduce the initialisation error * Fix the initialisation error * Remove TODO comment --- .../src/main/java/com/flagsmith/Flagsmith.kt | 12 ++++++++---- .../java/com/flagsmith/FeatureFlagCachingTests.kt | 3 ++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/FlagsmithClient/src/main/java/com/flagsmith/Flagsmith.kt b/FlagsmithClient/src/main/java/com/flagsmith/Flagsmith.kt index e4d87f5..603ae51 100644 --- a/FlagsmithClient/src/main/java/com/flagsmith/Flagsmith.kt +++ b/FlagsmithClient/src/main/java/com/flagsmith/Flagsmith.kt @@ -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 @@ -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 { diff --git a/FlagsmithClient/src/test/java/com/flagsmith/FeatureFlagCachingTests.kt b/FlagsmithClient/src/test/java/com/flagsmith/FeatureFlagCachingTests.kt index 3c3ed7b..d96b529 100644 --- a/FlagsmithClient/src/test/java/com/flagsmith/FeatureFlagCachingTests.kt +++ b/FlagsmithClient/src/test/java/com/flagsmith/FeatureFlagCachingTests.kt @@ -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) @@ -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