diff --git a/packages/flutter_timeline/pubspec.yaml b/packages/flutter_timeline/pubspec.yaml index 201604b..69e1e87 100644 --- a/packages/flutter_timeline/pubspec.yaml +++ b/packages/flutter_timeline/pubspec.yaml @@ -13,7 +13,7 @@ environment: dependencies: flutter: sdk: flutter - go_router: ^12.1.1 + go_router: any flutter_timeline_view: git: url: https://github.com/Iconica-Development/flutter_timeline diff --git a/packages/flutter_timeline_firebase/lib/src/service/firebase_timeline_service.dart b/packages/flutter_timeline_firebase/lib/src/service/firebase_timeline_service.dart index 07047ad..ed95443 100644 --- a/packages/flutter_timeline_firebase/lib/src/service/firebase_timeline_service.dart +++ b/packages/flutter_timeline_firebase/lib/src/service/firebase_timeline_service.dart @@ -65,23 +65,30 @@ class FirebaseTimelineService with ChangeNotifier implements TimelineService { TimelinePost post, String reactionId, ) async { - var updatedPost = post.copyWith( - reaction: post.reaction - 1, - reactions: (post.reactions ?? []) - ..removeWhere((element) => element.id == reactionId), - ); - _posts = _posts - .map( - (p) => p.id == post.id ? updatedPost : p, - ) - .toList(); - var postRef = _db.collection(_options.timelineCollectionName).doc(post.id); - await postRef.update({ - 'reaction': FieldValue.increment(-1), - 'reactions': FieldValue.arrayRemove([reactionId]), - }); - notifyListeners(); - return updatedPost; + if (post.reactions != null && post.reactions!.isNotEmpty) { + var reaction = + post.reactions!.firstWhere((element) => element.id == reactionId); + var updatedPost = post.copyWith( + reaction: post.reaction - 1, + reactions: (post.reactions ?? [])..remove(reaction), + ); + _posts = _posts + .map( + (p) => p.id == post.id ? updatedPost : p, + ) + .toList(); + var postRef = + _db.collection(_options.timelineCollectionName).doc(post.id); + await postRef.update({ + 'reaction': FieldValue.increment(-1), + 'reactions': FieldValue.arrayRemove( + [reaction.toJsonWithMicroseconds()], + ), + }); + notifyListeners(); + return updatedPost; + } + return post; } @override diff --git a/packages/flutter_timeline_interface/lib/src/model/timeline_reaction.dart b/packages/flutter_timeline_interface/lib/src/model/timeline_reaction.dart index 0b8b231..f880362 100644 --- a/packages/flutter_timeline_interface/lib/src/model/timeline_reaction.dart +++ b/packages/flutter_timeline_interface/lib/src/model/timeline_reaction.dart @@ -15,6 +15,7 @@ class TimelinePostReaction { this.reaction, this.imageUrl, this.creator, + this.createdAtString, }); factory TimelinePostReaction.fromJson( @@ -29,6 +30,7 @@ class TimelinePostReaction { reaction: json['reaction'] as String?, imageUrl: json['image_url'] as String?, createdAt: DateTime.parse(json['created_at'] as String), + createdAtString: json['created_at'] as String, ); /// The unique identifier of the reaction. @@ -52,6 +54,9 @@ class TimelinePostReaction { /// Reaction creation date. final DateTime createdAt; + /// Reaction creation date as String with microseconds. + final String? createdAtString; + TimelinePostReaction copyWith({ String? id, String? postId, @@ -79,4 +84,13 @@ class TimelinePostReaction { 'created_at': createdAt.toIso8601String(), }, }; + + Map toJsonWithMicroseconds() => { + id: { + 'creator_id': creatorId, + 'reaction': reaction, + 'image_url': imageUrl, + 'created_at': createdAtString, + }, + }; }