diff --git a/aws-datastore/src/main/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverter.java b/aws-datastore/src/main/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverter.java index 2c55ed35a8..68d2f4d555 100644 --- a/aws-datastore/src/main/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverter.java +++ b/aws-datastore/src/main/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverter.java @@ -34,6 +34,7 @@ import com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteColumn; import com.amplifyframework.datastore.storage.sqlite.adapter.SQLiteTable; import com.amplifyframework.logging.Logger; +import com.amplifyframework.util.UserAgent; import com.google.gson.Gson; @@ -130,6 +131,9 @@ public static Object convertRawValueToTarget( case DATE: return value instanceof String ? value : ((Temporal.Date) value).format(); case DATE_TIME: + if (UserAgent.isFlutter() && value instanceof String) { + return value; + } OffsetDateTime offsetDateTime; if (value instanceof String) { offsetDateTime = OffsetDateTime.parse((String) value); @@ -141,6 +145,9 @@ public static Object convertRawValueToTarget( .ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSSSSSSS'Z'"); return offsetDateTime.toInstant().atOffset(ZoneOffset.UTC).format(dateTimeFormatter); case TIME: + if (UserAgent.isFlutter() && value instanceof String) { + return value; + } String timeValue; if (value instanceof String) { timeValue = (String) value; diff --git a/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java b/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java new file mode 100644 index 0000000000..16673d3a4b --- /dev/null +++ b/aws-datastore/src/test/java/com/amplifyframework/datastore/storage/sqlite/SQLiteModelFieldTypeConverterTest.java @@ -0,0 +1,103 @@ +/* + * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). + * You may not use this file except in compliance with the License. + * A copy of the License is located at + * + * http://aws.amazon.com/apache2.0 + * + * or in the "license" file accompanying this file. This file is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either + * express or implied. See the License for the specific language governing + * permissions and limitations under the License. + */ + +package com.amplifyframework.datastore.storage.sqlite; + +import com.amplifyframework.AmplifyException; +import com.amplifyframework.core.model.types.JavaFieldType; +import com.amplifyframework.util.GsonFactory; +import com.amplifyframework.util.UserAgent; + +import com.google.gson.Gson; +import org.junit.Before; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +public class SQLiteModelFieldTypeConverterTest { + + /** + * Reset the user agent before each test. + */ + @Before + public void reset() { + UserAgent.reset(); + } + + /** + * Test TIME conversion for Android (default platform). + */ + @Test + public void testConvertRawValueToTargetTimeAndroid() { + final String value = "16:00:00.050020000"; + final JavaFieldType fieldType = JavaFieldType.TIME; + final Gson gson = GsonFactory.instance(); + final String expected = "16:00:00.050020000Z"; + final Object actual = SQLiteModelFieldTypeConverter.convertRawValueToTarget( + value, + fieldType, + gson); + assertEquals(expected, actual); + } + + /** + * Test TIME converter for Flutter. + * @throws AmplifyException Not expected. + */ + @Test + public void testConvertRawValueToTargetTimeFlutter() throws AmplifyException { + setUserAgent(); + final String value = "16:00:00.050020000"; + final JavaFieldType fieldType = JavaFieldType.TIME; + final Gson gson = GsonFactory.instance(); + final Object actual = SQLiteModelFieldTypeConverter.convertRawValueToTarget( + value, + fieldType, + gson); + final String expected = "16:00:00.050020000"; + assertEquals(expected, actual); + } + + /** + * Set user agent to Flutter. + * @throws AmplifyException not expected. + */ + private void setUserAgent() throws AmplifyException { + Map map = new HashMap<>(); + map.put(UserAgent.Platform.FLUTTER, "1.0"); + UserAgent.configure(map); + } + + /** + * Test DATE_TIME converter for Flutter. + * @throws AmplifyException Not expected. + */ + @Test + public void testConvertRawValueToTargetDateTimeFlutter() throws AmplifyException { + setUserAgent(); + final String value = "2020-01-01T16:00:00.050020000"; + final JavaFieldType fieldType = JavaFieldType.DATE_TIME; + final Gson gson = GsonFactory.instance(); + final Object actual = SQLiteModelFieldTypeConverter.convertRawValueToTarget( + value, + fieldType, + gson); + final String expected = "2020-01-01T16:00:00.050020000"; + assertEquals(expected, actual); + } +} diff --git a/core/src/main/java/com/amplifyframework/util/UserAgent.java b/core/src/main/java/com/amplifyframework/util/UserAgent.java index fc300aabb6..bf0b57e9f5 100644 --- a/core/src/main/java/com/amplifyframework/util/UserAgent.java +++ b/core/src/main/java/com/amplifyframework/util/UserAgent.java @@ -116,6 +116,14 @@ private static String forAndroid() { .toString(); } + /** + * Returns true if running on Flutter. + * @return Returns true if running on Flutter. + */ + public static boolean isFlutter() { + return string().contains(Platform.FLUTTER.libraryName); + } + /** * Enum to represent various platforms that use Amplify library for tracking * usage metrics.