Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(datastore): properly handle multiple configures on Android #5740

Merged
merged 3 commits into from
Jan 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading