mirror of
https://github.com/Iconica-Development/flutter_timeline.git
synced 2025-05-19 02:23:46 +02:00
fix: delete reaction uses reaction instead of id
This commit is contained in:
parent
d60244fa3e
commit
c8cc325a95
3 changed files with 39 additions and 18 deletions
|
@ -13,7 +13,7 @@ environment:
|
||||||
dependencies:
|
dependencies:
|
||||||
flutter:
|
flutter:
|
||||||
sdk: flutter
|
sdk: flutter
|
||||||
go_router: ^12.1.1
|
go_router: any
|
||||||
flutter_timeline_view:
|
flutter_timeline_view:
|
||||||
git:
|
git:
|
||||||
url: https://github.com/Iconica-Development/flutter_timeline
|
url: https://github.com/Iconica-Development/flutter_timeline
|
||||||
|
|
|
@ -65,23 +65,30 @@ class FirebaseTimelineService with ChangeNotifier implements TimelineService {
|
||||||
TimelinePost post,
|
TimelinePost post,
|
||||||
String reactionId,
|
String reactionId,
|
||||||
) async {
|
) async {
|
||||||
var updatedPost = post.copyWith(
|
if (post.reactions != null && post.reactions!.isNotEmpty) {
|
||||||
reaction: post.reaction - 1,
|
var reaction =
|
||||||
reactions: (post.reactions ?? [])
|
post.reactions!.firstWhere((element) => element.id == reactionId);
|
||||||
..removeWhere((element) => element.id == reactionId),
|
var updatedPost = post.copyWith(
|
||||||
);
|
reaction: post.reaction - 1,
|
||||||
_posts = _posts
|
reactions: (post.reactions ?? [])..remove(reaction),
|
||||||
.map(
|
);
|
||||||
(p) => p.id == post.id ? updatedPost : p,
|
_posts = _posts
|
||||||
)
|
.map(
|
||||||
.toList();
|
(p) => p.id == post.id ? updatedPost : p,
|
||||||
var postRef = _db.collection(_options.timelineCollectionName).doc(post.id);
|
)
|
||||||
await postRef.update({
|
.toList();
|
||||||
'reaction': FieldValue.increment(-1),
|
var postRef =
|
||||||
'reactions': FieldValue.arrayRemove([reactionId]),
|
_db.collection(_options.timelineCollectionName).doc(post.id);
|
||||||
});
|
await postRef.update({
|
||||||
notifyListeners();
|
'reaction': FieldValue.increment(-1),
|
||||||
return updatedPost;
|
'reactions': FieldValue.arrayRemove(
|
||||||
|
[reaction.toJsonWithMicroseconds()],
|
||||||
|
),
|
||||||
|
});
|
||||||
|
notifyListeners();
|
||||||
|
return updatedPost;
|
||||||
|
}
|
||||||
|
return post;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|
|
@ -15,6 +15,7 @@ class TimelinePostReaction {
|
||||||
this.reaction,
|
this.reaction,
|
||||||
this.imageUrl,
|
this.imageUrl,
|
||||||
this.creator,
|
this.creator,
|
||||||
|
this.createdAtString,
|
||||||
});
|
});
|
||||||
|
|
||||||
factory TimelinePostReaction.fromJson(
|
factory TimelinePostReaction.fromJson(
|
||||||
|
@ -29,6 +30,7 @@ class TimelinePostReaction {
|
||||||
reaction: json['reaction'] as String?,
|
reaction: json['reaction'] as String?,
|
||||||
imageUrl: json['image_url'] as String?,
|
imageUrl: json['image_url'] as String?,
|
||||||
createdAt: DateTime.parse(json['created_at'] as String),
|
createdAt: DateTime.parse(json['created_at'] as String),
|
||||||
|
createdAtString: json['created_at'] as String,
|
||||||
);
|
);
|
||||||
|
|
||||||
/// The unique identifier of the reaction.
|
/// The unique identifier of the reaction.
|
||||||
|
@ -52,6 +54,9 @@ class TimelinePostReaction {
|
||||||
/// Reaction creation date.
|
/// Reaction creation date.
|
||||||
final DateTime createdAt;
|
final DateTime createdAt;
|
||||||
|
|
||||||
|
/// Reaction creation date as String with microseconds.
|
||||||
|
final String? createdAtString;
|
||||||
|
|
||||||
TimelinePostReaction copyWith({
|
TimelinePostReaction copyWith({
|
||||||
String? id,
|
String? id,
|
||||||
String? postId,
|
String? postId,
|
||||||
|
@ -79,4 +84,13 @@ class TimelinePostReaction {
|
||||||
'created_at': createdAt.toIso8601String(),
|
'created_at': createdAt.toIso8601String(),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Map<String, dynamic> toJsonWithMicroseconds() => <String, dynamic>{
|
||||||
|
id: {
|
||||||
|
'creator_id': creatorId,
|
||||||
|
'reaction': reaction,
|
||||||
|
'image_url': imageUrl,
|
||||||
|
'created_at': createdAtString,
|
||||||
|
},
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue