fun onConfigureDataStore()

in packages/amplify_datastore/android/src/main/kotlin/com/amazonaws/amplify/amplify_datastore/AmplifyDataStorePlugin.kt [140:224]


    fun onConfigureDataStore(flutterResult: Result, request: Map<String, Any>) {
        if (!request.containsKey("modelSchemas") || !request.containsKey(
                "modelProviderVersion"
            ) || request["modelSchemas"] !is List<*>
        ) {
            uiThreadHandler.post {
                postExceptionToFlutterChannel(
                    flutterResult, "DataStoreException",
                    createSerializedError(
                        ExceptionMessages.missingExceptionMessage,
                        ExceptionMessages.missingRecoverySuggestion,
                        "Received invalid request from Dart, modelSchemas and/or modelProviderVersion" +
                                " are not available. Request: " + request.toString()
                    )
                )
            }
            return
        }

        // Register schemas to the native model provider
        registerSchemas(request)

        val syncExpressions: List<Map<String, Any>> =
            request["syncExpressions"].safeCastToList() ?: emptyList()
        val defaultDataStoreConfiguration = DataStoreConfiguration.defaults()
        val syncInterval: Long =
            (request["syncInterval"] as? Int)?.toLong()
                ?: defaultDataStoreConfiguration.syncIntervalInMinutes
        val syncMaxRecords: Int =
            (request["syncMaxRecords"] as? Int)
                ?: defaultDataStoreConfiguration.syncMaxRecords
        val syncPageSize: Int =
            (request["syncPageSize"] as? Int)
                ?: defaultDataStoreConfiguration.syncPageSize

        val dataStoreConfigurationBuilder = DataStoreConfiguration.builder()

        try {
            buildSyncExpressions(syncExpressions, dataStoreConfigurationBuilder)
        } catch (e: Exception) {
            uiThreadHandler.post {
                postExceptionToFlutterChannel(
                    flutterResult, "DataStoreException",
                    createSerializedUnrecognizedError(e)
                )
            }
        }

        var errorHandler : DataStoreErrorHandler;
        errorHandler = if( (request["hasErrorHandler"] as? Boolean? == true) ) {
            DataStoreErrorHandler {
                val args = hashMapOf(
                        "errorCode" to "DataStoreException",
                        "errorMessage" to ExceptionMessages.defaultFallbackExceptionMessage,
                        "details" to createSerializedError(it)
                )
                channel.invokeMethod("errorHandler", args)
            }
        } else {
            DataStoreErrorHandler {
                LOG.error(it.toString())
            }
        }
        dataStoreConfigurationBuilder.errorHandler(errorHandler)

        val dataStorePlugin = AWSDataStorePlugin
            .builder()
            .modelProvider(modelProvider)
            .dataStoreConfiguration(
                dataStoreConfigurationBuilder
                    .syncInterval(syncInterval, TimeUnit.MINUTES)
                    .syncMaxRecords(syncMaxRecords)
                    .syncPageSize(syncPageSize)
                    .build()
            )
            .build()

        try {
            Amplify.addPlugin(dataStorePlugin)
        } catch (e: Exception) {
            handleAddPluginException("Datastore", e, flutterResult)
            return
        }
        flutterResult.success(null)
    }