|
4 | 4 | package com.amazonaws.amplify.amplify_native_legacy_wrapper
|
5 | 5 |
|
6 | 6 | import android.content.Context
|
7 |
| -import com.amazonaws.mobile.client.AWSMobileClient |
8 |
| -import com.amazonaws.mobile.client.Callback |
9 |
| -import com.amazonaws.mobile.config.AWSConfiguration |
| 7 | +import com.amazonaws.amplify.amplify_native_legacy_wrapper.pigeons.LegacyNativePlugin |
| 8 | +import com.amplifyframework.AmplifyException |
| 9 | +import com.amplifyframework.auth.AuthException |
| 10 | +import com.amplifyframework.auth.cognito.AWSCognitoAuthPlugin |
| 11 | +import com.amplifyframework.kotlin.core.Amplify |
| 12 | +import com.amplifyframework.core.AmplifyConfiguration |
10 | 13 | import io.flutter.embedding.engine.plugins.FlutterPlugin
|
11 | 14 | import kotlinx.coroutines.runBlocking
|
12 | 15 | import org.json.JSONObject
|
13 | 16 |
|
14 | 17 |
|
15 | 18 | /** AmplifyNativeLegacyWrapperPlugin */
|
16 |
| -class AmplifyNativeLegacyWrapperPlugin: FlutterPlugin, LegacyNativePluginPigeon.LegacyNativePlugin { |
| 19 | +class AmplifyNativeLegacyWrapperPlugin: FlutterPlugin, LegacyNativePlugin { |
17 | 20 |
|
18 | 21 | private lateinit var context: Context
|
19 |
| - private val awsMobileClient = AWSMobileClient.getInstance() |
20 | 22 |
|
21 | 23 | override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
|
22 | 24 | context = flutterPluginBinding.applicationContext
|
23 |
| - LegacyNativePluginPigeon.LegacyNativePlugin.setup(flutterPluginBinding.binaryMessenger, this) |
| 25 | + LegacyNativePlugin.setUp(flutterPluginBinding.binaryMessenger, this) |
24 | 26 | }
|
25 | 27 |
|
26 | 28 | override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
|
27 |
| - LegacyNativePluginPigeon.LegacyNativePlugin.setup(binding.binaryMessenger, null) |
| 29 | + LegacyNativePlugin.setUp(binding.binaryMessenger, null) |
28 | 30 | }
|
29 | 31 |
|
30 |
| - override fun configure(config: String, result: LegacyNativePluginPigeon.Result<Void>) { |
31 |
| - val mobileClientConfig = JSONObject(config).getJSONObject("auth").getJSONObject("plugins").getJSONObject("awsCognitoAuthPlugin") |
32 |
| - awsMobileClient.initialize( |
33 |
| - context, |
34 |
| - AWSConfiguration(mobileClientConfig), |
35 |
| - ResultCallback(result) |
36 |
| - ) |
37 |
| - } |
38 |
| - |
39 |
| - override fun signOut(result: LegacyNativePluginPigeon.Result<Void>) { |
40 |
| - awsMobileClient.signOut(null, ResultCallback(result)) |
| 32 | + override fun configure(config: String, callback: (Result<Unit>) -> Unit) { |
| 33 | + try { |
| 34 | + val configuration = AmplifyConfiguration.builder(JSONObject(config)) |
| 35 | + .devMenuEnabled(false) |
| 36 | + .build() |
| 37 | + Amplify.addPlugin(AWSCognitoAuthPlugin()) |
| 38 | + Amplify.configure(configuration, context) |
| 39 | + callback(Result.success(Unit)) |
| 40 | + } catch (e: AmplifyException) { |
| 41 | + callback(Result.failure(e)) |
| 42 | + } |
41 | 43 | }
|
42 | 44 |
|
43 |
| - override fun signIn( |
44 |
| - username: String, |
45 |
| - password: String, |
46 |
| - result: LegacyNativePluginPigeon.Result<Void> |
47 |
| - ) { |
| 45 | + override fun signIn(username: String, password: String, callback: (Result<Unit>) -> Unit) { |
48 | 46 | runBlocking {
|
49 |
| - awsMobileClient.signIn( |
50 |
| - username, |
51 |
| - password, |
52 |
| - HashMap(), |
53 |
| - ResultCallback(result) |
54 |
| - ) |
| 47 | + try { |
| 48 | + Amplify.Auth.signIn(username, password) |
| 49 | + callback(Result.success(Unit)) |
| 50 | + } catch (error: AuthException) { |
| 51 | + callback(Result.failure(error)) |
| 52 | + } |
55 | 53 | }
|
56 | 54 | }
|
57 | 55 |
|
58 |
| -} |
59 |
| - |
60 |
| -class ResultCallback<T>(private val result: LegacyNativePluginPigeon.Result<Void>) : Callback<T> { |
61 |
| - override fun onResult(res: T) { |
62 |
| - this.result.success(null) |
| 56 | + override fun signOut(callback: (Result<Unit>) -> Unit) { |
| 57 | + runBlocking { |
| 58 | + try { |
| 59 | + Amplify.Auth.signOut() |
| 60 | + callback(Result.success(Unit)) |
| 61 | + } catch (error: AuthException) { |
| 62 | + callback(Result.failure(error)) |
| 63 | + } |
| 64 | + } |
63 | 65 | }
|
64 | 66 |
|
65 |
| - override fun onError(e: Exception) { |
66 |
| - this.result.error(e) |
| 67 | + override fun rememberDevice(callback: (Result<Unit>) -> Unit) { |
| 68 | + runBlocking { |
| 69 | + try { |
| 70 | + Amplify.Auth.rememberDevice() |
| 71 | + callback(Result.success(Unit)) |
| 72 | + } catch (error: AuthException) { |
| 73 | + callback(Result.failure(error)) |
| 74 | + } |
| 75 | + } |
67 | 76 | }
|
68 | 77 |
|
69 | 78 | }
|
0 commit comments