Skip to content

Commit 44ecc89

Browse files
authored
fix(datastore): properly handle multiple configures on Android (#5740)
1 parent ea9a678 commit 44ecc89

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

packages/amplify_datastore/android/src/main/kotlin/com/amazonaws/amplify/amplify_datastore/AmplifyDataStorePlugin.kt

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,14 @@ class AmplifyDataStorePlugin :
8585
NativeApiBridge {
8686
private lateinit var channel: MethodChannel
8787
private lateinit var eventChannel: EventChannel
88-
private lateinit var observeCancelable: Cancelable
88+
private var observeCancelable: Cancelable? = null
8989
private lateinit var hubEventChannel: EventChannel
9090

9191
private val dataStoreObserveEventStreamHandler: DataStoreObserveEventStreamHandler
9292
private val dataStoreHubEventStreamHandler: DataStoreHubEventStreamHandler
9393
private val uiThreadHandler: Handler
9494
private val LOG = Amplify.Logging.forNamespace("amplify:flutter:datastore")
9595
private var isSettingUpObserve = AtomicBoolean()
96-
private var nativeAuthPlugin: NativeAuthPlugin? = null
9796
private var nativeApiPlugin: NativeApiPlugin? = null
9897
private val coroutineScope = CoroutineScope(CoroutineName("AmplifyFlutterPlugin"))
9998
private val dispatcher: CoroutineDispatcher = Dispatchers.IO
@@ -114,6 +113,8 @@ class AmplifyDataStorePlugin :
114113
* be instantiated only once but still maintain a reference to the active method channel.
115114
*/
116115
var flutterAuthProviders: FlutterAuthProviders? = null
116+
var nativeAuthPlugin: NativeAuthPlugin? = null
117+
var hasAddedUserAgent :Boolean = false
117118
}
118119

119120
val modelProvider = FlutterModelProvider.instance
@@ -185,6 +186,14 @@ class AmplifyDataStorePlugin :
185186
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
186187
channel.setMethodCallHandler(null)
187188

189+
eventChannel.setStreamHandler(null)
190+
hubEventChannel.setStreamHandler(null)
191+
192+
observeCancelable?.cancel()
193+
observeCancelable = null
194+
195+
dataStoreHubEventStreamHandler.onCancel(null)
196+
188197
nativeAuthPlugin = null
189198
NativeAuthBridge.setUp(binding.binaryMessenger, null)
190199

@@ -438,7 +447,7 @@ class AmplifyDataStorePlugin :
438447

439448
queryPredicate = QueryPredicateBuilder.fromSerializedMap(
440449
request["queryPredicate"].safeCastToMap()
441-
) ?: QueryPredicates.all()
450+
) ?: QueryPredicates.all()
442451
} catch (e: Exception) {
443452
uiThreadHandler.post {
444453
postExceptionToFlutterChannel(
@@ -502,7 +511,7 @@ class AmplifyDataStorePlugin :
502511
}
503512

504513
fun onSetUpObserve(flutterResult: Result) {
505-
if (this::observeCancelable.isInitialized || isSettingUpObserve.getAndSet(true)) {
514+
if (observeCancelable != null || isSettingUpObserve.getAndSet(true)) {
506515
flutterResult.success(true)
507516
return
508517
}
@@ -932,16 +941,27 @@ class AmplifyDataStorePlugin :
932941
throw NotImplementedError("Not yet implemented")
933942
}
934943

944+
fun addUserAgent(
945+
version: String,
946+
) {
947+
if(hasAddedUserAgent) return
948+
949+
@OptIn(AmplifyFlutterApi::class)
950+
Amplify.addUserAgentPlatform(UserAgent.Platform.FLUTTER, "$version /datastore")
951+
952+
hasAddedUserAgent = true
953+
}
954+
935955
override fun configure(
936956
version: String,
937957
config: String,
938958
callback: (kotlin.Result<Unit>) -> Unit
939959
) {
940960
coroutineScope.launch(dispatcher) {
941961
try {
942-
@OptIn(AmplifyFlutterApi::class)
943-
Amplify.addUserAgentPlatform(UserAgent.Platform.FLUTTER, "$version /datastore")
962+
addUserAgent(version)
944963
Amplify.configure(AmplifyOutputs(config), context)
964+
945965
withContext(Dispatchers.Main) {
946966
callback(kotlin.Result.success(Unit))
947967
}

0 commit comments

Comments
 (0)