From d32ec679a811ed8ed59a288fb777e1167e2df267 Mon Sep 17 00:00:00 2001 From: Colin Maier - MBA Date: Thu, 6 Jul 2023 09:40:34 +0200 Subject: [PATCH] Transform DateTimes in YustDatabaseServiceMocked --- .../yust_database_service_mocked.dart | 43 +++++++++++++------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/lib/src/services/yust_database_service_mocked.dart b/lib/src/services/yust_database_service_mocked.dart index 653d4353..f86b302d 100644 --- a/lib/src/services/yust_database_service_mocked.dart +++ b/lib/src/services/yust_database_service_mocked.dart @@ -177,7 +177,8 @@ class YustDatabaseServiceMocked extends YustDatabaseService { await doc.onSave(); final jsonDocs = _getJSONCollection(docSetup.collectionName); final index = jsonDocs.indexWhere((d) => d['id'] == doc.id); - final docJsonClone = jsonDecode(jsonEncode(doc.toJson())); + final docJsonClone = + jsonDecode(jsonEncode(doc.toJson()), reviver: _decodeDateTime); if (index == -1 && !doNotCreate) { jsonDocs.add(docJsonClone); await onChange?.call( @@ -186,7 +187,8 @@ class YustDatabaseServiceMocked extends YustDatabaseService { docJsonClone, ); } else { - final oldDoc = jsonDecode(jsonEncode(jsonDocs[index])); + final oldDoc = + jsonDecode(jsonEncode(jsonDocs[index]), reviver: _decodeDateTime); if (updateMask == null) { jsonDocs[index] = docJsonClone; await onChange?.call( @@ -221,8 +223,10 @@ class YustDatabaseServiceMocked extends YustDatabaseService { }) async { final jsonDocs = _getJSONCollection(docSetup.collectionName); final index = jsonDocs.indexWhere((doc) => doc['id'] == id); - final jsonDocClone = jsonDecode(jsonEncode(jsonDocs[index])); - final oldDoc = jsonDecode(jsonEncode(jsonDocs[index])); + final jsonDocClone = + jsonDecode(jsonEncode(jsonDocs[index]), reviver: _decodeDateTime); + final oldDoc = + jsonDecode(jsonEncode(jsonDocs[index]), reviver: _decodeDateTime); for (final t in fieldTransforms) { final unescapedPath = t.fieldPath.replaceAll('`', ''); @@ -287,7 +291,8 @@ class YustDatabaseServiceMocked extends YustDatabaseService { List> collection, YustDocSetup docSetup) { return collection // We clone the maps here by using jsonDecode/jsonEncode - .map((e) => docSetup.fromJson(jsonDecode(jsonEncode(e)))) + .map((e) => docSetup + .fromJson(jsonDecode(jsonEncode(e), reviver: _decodeDateTime))) .toList(); } @@ -305,13 +310,13 @@ class YustDatabaseServiceMocked extends YustDatabaseService { List> _orderBy( List> collection, - List? orderBy, + List? orderBys, ) { - for (final o in (orderBy ?? []).reversed) { - collection.sort((a, b) { - final compare = (_readValueInJsonDoc(a, o.field) as Comparable) - .compareTo(_readValueInJsonDoc(b, o.field) as Comparable); - final order = o.descending ? -1 : 1; + for (final orderBy in (orderBys ?? []).reversed) { + collection.sort((docA, docB) { + final compare = (_readValueInJsonDoc(docA, orderBy.field) as Comparable) + .compareTo(_readValueInJsonDoc(docB, orderBy.field) as Comparable); + final order = orderBy.descending ? -1 : 1; return order * compare; }); } @@ -337,7 +342,9 @@ class YustDatabaseServiceMocked extends YustDatabaseService { for (final segment in segments.sublist(0, segments.length - 1)) { subDoc = subDoc[segment]; } - return subDoc[segments.last]; + final value = subDoc[segments.last]; + final decoded = jsonDecode(value, reviver: _decodeDateTime); + return decoded; } String _createDocumentId() { @@ -352,4 +359,16 @@ class YustDatabaseServiceMocked extends YustDatabaseService { return '$parentPath/${docSetup.collectionName}/${doc?.id ?? id ?? ''}'; } + + dynamic _decodeDateTime(dynamic key, dynamic item) { + if (item is String) { + if (RegExp(r'^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}').hasMatch(item)) { + final dateTime = DateTime.tryParse(item); + if (dateTime != null) { + return dateTime; + } + } + } + return item; + } }