Skip to content

Commit

Permalink
fix(datastore): properly handle multiple configures on Android (#5740)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyllark authored Jan 13, 2025
1 parent ea9a678 commit 44ecc89
Showing 1 changed file with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,14 @@ class AmplifyDataStorePlugin :
NativeApiBridge {
private lateinit var channel: MethodChannel
private lateinit var eventChannel: EventChannel
private lateinit var observeCancelable: Cancelable
private var observeCancelable: Cancelable? = null
private lateinit var hubEventChannel: EventChannel

private val dataStoreObserveEventStreamHandler: DataStoreObserveEventStreamHandler
private val dataStoreHubEventStreamHandler: DataStoreHubEventStreamHandler
private val uiThreadHandler: Handler
private val LOG = Amplify.Logging.forNamespace("amplify:flutter:datastore")
private var isSettingUpObserve = AtomicBoolean()
private var nativeAuthPlugin: NativeAuthPlugin? = null
private var nativeApiPlugin: NativeApiPlugin? = null
private val coroutineScope = CoroutineScope(CoroutineName("AmplifyFlutterPlugin"))
private val dispatcher: CoroutineDispatcher = Dispatchers.IO
Expand All @@ -114,6 +113,8 @@ class AmplifyDataStorePlugin :
* be instantiated only once but still maintain a reference to the active method channel.
*/
var flutterAuthProviders: FlutterAuthProviders? = null
var nativeAuthPlugin: NativeAuthPlugin? = null
var hasAddedUserAgent :Boolean = false
}

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

eventChannel.setStreamHandler(null)
hubEventChannel.setStreamHandler(null)

observeCancelable?.cancel()
observeCancelable = null

dataStoreHubEventStreamHandler.onCancel(null)

nativeAuthPlugin = null
NativeAuthBridge.setUp(binding.binaryMessenger, null)

Expand Down Expand Up @@ -438,7 +447,7 @@ class AmplifyDataStorePlugin :

queryPredicate = QueryPredicateBuilder.fromSerializedMap(
request["queryPredicate"].safeCastToMap()
) ?: QueryPredicates.all()
) ?: QueryPredicates.all()
} catch (e: Exception) {
uiThreadHandler.post {
postExceptionToFlutterChannel(
Expand Down Expand Up @@ -502,7 +511,7 @@ class AmplifyDataStorePlugin :
}

fun onSetUpObserve(flutterResult: Result) {
if (this::observeCancelable.isInitialized || isSettingUpObserve.getAndSet(true)) {
if (observeCancelable != null || isSettingUpObserve.getAndSet(true)) {
flutterResult.success(true)
return
}
Expand Down Expand Up @@ -932,16 +941,27 @@ class AmplifyDataStorePlugin :
throw NotImplementedError("Not yet implemented")
}

fun addUserAgent(
version: String,
) {
if(hasAddedUserAgent) return

@OptIn(AmplifyFlutterApi::class)
Amplify.addUserAgentPlatform(UserAgent.Platform.FLUTTER, "$version /datastore")

hasAddedUserAgent = true
}

override fun configure(
version: String,
config: String,
callback: (kotlin.Result<Unit>) -> Unit
) {
coroutineScope.launch(dispatcher) {
try {
@OptIn(AmplifyFlutterApi::class)
Amplify.addUserAgentPlatform(UserAgent.Platform.FLUTTER, "$version /datastore")
addUserAgent(version)
Amplify.configure(AmplifyOutputs(config), context)

withContext(Dispatchers.Main) {
callback(kotlin.Result.success(Unit))
}
Expand Down

0 comments on commit 44ecc89

Please sign in to comment.