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: "Reply already submitted" crashes #1058

Merged
merged 4 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -19,6 +19,7 @@ import android.app.Activity
import android.content.Context
import android.content.Intent
import androidx.annotation.NonNull
import com.amazonaws.amplify.amplify_core.AtomicResult
import com.amplifyframework.core.Amplify
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
Expand Down Expand Up @@ -51,7 +52,8 @@ class AmplifyAnalyticsPinpointPlugin : FlutterPlugin, ActivityAware, MethodCallH
}

// Handle methods received via MethodChannel
override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
override fun onMethodCall(@NonNull call: MethodCall, @NonNull _result: Result) {
val result = AtomicResult(_result, call.method)

when (call.method) {
"addPlugin" ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Flutter
import UIKit
import Amplify
import AmplifyPlugins
import amplify_core

public class SwiftAmplifyAnalyticsPinpointPlugin: NSObject, FlutterPlugin {
private let bridge: AnalyticsBridge
Expand All @@ -36,6 +37,8 @@ public class SwiftAmplifyAnalyticsPinpointPlugin: NSObject, FlutterPlugin {
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
let result = AtomicResult(result, call.method)

innerHandle(method: call.method, callArgs: call.arguments as Any?, result: result)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import androidx.annotation.NonNull
import androidx.annotation.VisibleForTesting
import com.amazonaws.amplify.amplify_api.auth.FlutterAuthProviders
import com.amazonaws.amplify.amplify_api.rest_api.FlutterRestApi
import com.amazonaws.amplify.amplify_core.AtomicResult
import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil.Companion.createSerializedUnrecognizedError
import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil.Companion.handleAddPluginException
import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil.Companion.postExceptionToFlutterChannel
Expand Down Expand Up @@ -74,8 +75,9 @@ class AmplifyApiPlugin : FlutterPlugin, MethodCallHandler {
}

@Suppress("UNCHECKED_CAST")
override fun onMethodCall(call: MethodCall, result: Result) {
override fun onMethodCall(call: MethodCall, _result: Result) {
val methodName = call.method
val result = AtomicResult(_result, call.method)

if (methodName == "cancel") {
onCancel(result, (call.arguments as String))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class SwiftAmplifyApiPlugin: NSObject, FlutterPlugin {
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
let result = AtomicResult(result, call.method)

innerHandle(method: call.method, callArgs: call.arguments as Any, result: result)
}

Expand Down
1 change: 0 additions & 1 deletion packages/amplify_auth_cognito/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,4 @@ dependencies {
testImplementation 'org.mockito:mockito-inline:3.10.0'
testImplementation 'androidx.test:core:1.4.0'
testImplementation 'org.robolectric:robolectric:4.3.1'
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.9'
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import com.amazonaws.amplify.amplify_auth_cognito.types.FlutterResendUserAttribu
import com.amazonaws.amplify.amplify_auth_cognito.types.FlutterSignOutRequest
import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil.Companion.handleAddPluginException
import com.amazonaws.amplify.amplify_auth_cognito.utils.isRedirectActivityDeclared
import com.amazonaws.amplify.amplify_core.AtomicResult
import com.amplifyframework.auth.AuthException
import com.amplifyframework.auth.AuthProvider
import com.amplifyframework.auth.AuthSession
Expand Down Expand Up @@ -151,7 +152,9 @@ public class AuthCognito : FlutterPlugin, ActivityAware, MethodCallHandler, Plug
return args as HashMap<String, Any>
};

override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
override fun onMethodCall(@NonNull call: MethodCall, @NonNull _result: Result) {

val result = AtomicResult(_result, call.method)

if(call.method == "addPlugin"){
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package com.amazonaws.amplify.amplify_auth_cognito.device

import com.amazonaws.amplify.amplify_auth_cognito.AuthErrorHandler
import com.amazonaws.amplify.amplify_auth_cognito.base.AtomicResult
import com.amazonaws.amplify.amplify_core.AtomicResult
import com.amazonaws.mobile.client.AWSMobileClient
import com.amazonaws.mobile.client.Callback
import com.amazonaws.mobile.client.results.ListDevicesResult
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import Foundation
import Flutter
import Amplify
import AmplifyPlugins
import amplify_core

/// Handles calls to the Devices API.
struct DeviceHandler {
Expand All @@ -43,7 +44,6 @@ struct DeviceHandler {
func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
guard DeviceHandler.canHandle(call.method) else { return }

let result = AtomicResult(result)
do {
switch call.method {
case "rememberDevice":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ public class SwiftAuthCognito: NSObject, FlutterPlugin {
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
let result = AtomicResult(result, call.method)

if(call.method == "addPlugin"){
do {
try Amplify.add(plugin: AWSCognitoAuthPlugin() )
Expand Down
4 changes: 4 additions & 0 deletions packages/amplify_core/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ android {
testOptions {
unitTests {
includeAndroidResources = true
returnDefaultValues = true
}
}
buildTypes {
Expand All @@ -63,11 +64,14 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.amplifyframework:core:1.28.2'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.9'

testImplementation 'junit:junit:4.13.2'
testImplementation 'org.mockito:mockito-core:3.10.0'
testImplementation 'org.mockito:mockito-inline:3.10.0'
testImplementation 'androidx.test:core:1.4.0'
testImplementation 'org.robolectric:robolectric:4.3.1'
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.9'
}

apply plugin: 'org.jlleitschuh.gradle.ktlint'
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.amplify.amplify_auth_cognito.base
package com.amazonaws.amplify.amplify_core

import io.flutter.Log
import io.flutter.plugin.common.MethodChannel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.amplify.amplify_auth_cognito.base
package com.amazonaws.amplify.amplify_core

import io.flutter.plugin.common.MethodChannel
import kotlinx.coroutines.Dispatchers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.amplify.amplify_auth_cognito.base
package com.amazonaws.amplify.amplify_core

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,30 @@
import Foundation

/// Thread-safe wrapper for [FlutterResult]. Prevents multiple replies and automically posts results to the main thread.
func AtomicResult(_ result: @escaping FlutterResult) -> FlutterResult {
return atomicResult(result).send
public func AtomicResult(_ result: @escaping FlutterResult, _ methodName: String) -> FlutterResult {
return atomicResult(result, methodName).send
}

private class atomicResult {
let result: FlutterResult

/// The method call which initiated this result.
let methodName: String

/// Whether a reply has already been sent.
var isSent = false

init(_ result: @escaping FlutterResult) {
init(_ result: @escaping FlutterResult, _ methodName: String) {
self.result = result
self.methodName = methodName
}

func send(_ value: Any?) {
DispatchQueue.main.async { [self] in
guard !isSent else { return }
guard !isSent else {
NSLog("AtomicResult(%@): Attempted to send value after initial reply", methodName)
return
}
result(value)
isSent = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import android.os.Handler
import android.os.Looper
import androidx.annotation.NonNull
import androidx.annotation.VisibleForTesting
import com.amazonaws.amplify.amplify_core.AtomicResult
import com.amazonaws.amplify.amplify_core.exception.ExceptionMessages
import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil.Companion.createSerializedError
import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil.Companion.createSerializedUnrecognizedError
Expand Down Expand Up @@ -99,7 +100,8 @@ class AmplifyDataStorePlugin : FlutterPlugin, MethodCallHandler {
LOG.info("Initiated DataStore plugin")
}

override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
override fun onMethodCall(@NonNull call: MethodCall, @NonNull _result: Result) {
val result = AtomicResult(_result, call.method)
var data: Map<String, Any> = HashMap()
try {
if (call.arguments != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public class SwiftAmplifyDataStorePlugin: NSObject, FlutterPlugin {
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
let result = AtomicResult(result, call.method)
var arguments: [String: Any] = [:]
do {
if(call.arguments != nil) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import android.os.Handler
import android.os.Looper
import android.util.Log
import androidx.annotation.NonNull
import com.amazonaws.amplify.amplify_core.AtomicResult
import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil.Companion.createSerializedError
import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil.Companion.postExceptionToFlutterChannel
import com.amplifyframework.AmplifyException
Expand Down Expand Up @@ -67,8 +68,9 @@ class Amplify(
}
}

override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {

override fun onMethodCall(@NonNull call: MethodCall, @NonNull _result: Result) {
val result = AtomicResult(_result, call.method)

when (call.method) {
"configure" ->
try {
Expand Down
2 changes: 2 additions & 0 deletions packages/amplify_flutter/ios/Classes/SwiftAmplify.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public class SwiftAmplify: NSObject, FlutterPlugin {
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
let result = AtomicResult(result, call.method)

switch call.method {
case "configure":
let arguments = call.arguments as! Dictionary<String, AnyObject>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import android.content.Context
import android.src.main.kotlin.com.amazonaws.amplify.amplify_storage_s3.types.TransferProgressStreamHandler
import android.util.Log
import androidx.annotation.NonNull
import com.amazonaws.amplify.amplify_core.AtomicResult
import com.amazonaws.amplify.amplify_core.exception.ExceptionUtil.Companion.handleAddPluginException
import com.amplifyframework.core.Amplify
import com.amplifyframework.storage.s3.AWSS3StoragePlugin
Expand Down Expand Up @@ -55,7 +56,9 @@ class AmplifyStorageS3Plugin : FlutterPlugin, ActivityAware, MethodCallHandler {
transferProgressEventChannel.setStreamHandler(transferProgressStreamHandler);
}

override fun onMethodCall(@NonNull call: MethodCall, @NonNull result: Result) {
override fun onMethodCall(@NonNull call: MethodCall, @NonNull _result: Result) {
val result = AtomicResult(_result, call.method)

if(call.method == "addPlugin"){
try {
Amplify.addPlugin(AWSS3StoragePlugin())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class SwiftAmplifyStorageS3Plugin: NSObject, FlutterPlugin {
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
print("In handle for method \(call.method)")
let result = AtomicResult(result, call.method)

if(call.method == "addPlugin"){
do {
Expand Down