mirror of
https://github.com/Iconica-Development/flutter_timeline.git
synced 2025-05-19 10:33:44 +02:00
feat: option to delete reactions
This commit is contained in:
parent
e523f52118
commit
7e89ba9c85
5 changed files with 115 additions and 51 deletions
|
@ -28,7 +28,11 @@ class TimelineUserStoryConfiguration {
|
|||
final Widget Function(BuildContext context, Widget filterBar, Widget child)?
|
||||
mainPageBuilder;
|
||||
|
||||
final Widget Function(BuildContext context, Widget child, TimelineCategory category)? postScreenBuilder;
|
||||
final Widget Function(
|
||||
BuildContext context,
|
||||
Widget child,
|
||||
TimelineCategory category,
|
||||
)? postScreenBuilder;
|
||||
|
||||
final Widget Function(BuildContext context, Widget child)?
|
||||
postCreationScreenBuilder;
|
||||
|
|
|
@ -60,6 +60,30 @@ class FirebaseTimelineService with ChangeNotifier implements TimelineService {
|
|||
notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<TimelinePost> deletePostReaction(
|
||||
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;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<TimelinePost> fetchPostDetails(TimelinePost post) async {
|
||||
var reactions = post.reactions ?? [];
|
||||
|
|
|
@ -10,6 +10,7 @@ import 'package:flutter_timeline_interface/src/model/timeline_reaction.dart';
|
|||
|
||||
abstract class TimelineService with ChangeNotifier {
|
||||
Future<void> deletePost(TimelinePost post);
|
||||
Future<TimelinePost> deletePostReaction(TimelinePost post, String reactionId);
|
||||
Future<TimelinePost> createPost(TimelinePost post);
|
||||
Future<List<TimelinePost>> fetchPosts(String? category);
|
||||
Future<TimelinePost> fetchPost(TimelinePost post);
|
||||
|
|
|
@ -20,6 +20,7 @@ class TimelineTranslations {
|
|||
'Indicate whether people are allowed to respond',
|
||||
this.checkPost = 'Check post overview',
|
||||
this.deletePost = 'Delete post',
|
||||
this.deleteReaction = 'Delete Reaction',
|
||||
this.viewPost = 'View post',
|
||||
this.likesTitle = 'Likes',
|
||||
this.commentsTitle = 'Comments',
|
||||
|
@ -45,6 +46,7 @@ class TimelineTranslations {
|
|||
final String postAt;
|
||||
|
||||
final String deletePost;
|
||||
final String deleteReaction;
|
||||
final String viewPost;
|
||||
final String likesTitle;
|
||||
final String commentsTitle;
|
||||
|
|
|
@ -312,7 +312,38 @@ class _TimelinePostScreenState extends State<TimelinePostScreen> {
|
|||
for (var reaction
|
||||
in post.reactions ?? <TimelinePostReaction>[]) ...[
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
GestureDetector(
|
||||
onLongPress: () async {
|
||||
if (reaction.creatorId == widget.userId ||
|
||||
widget.options.allowAllDeletion) {
|
||||
// Show popup menu for deletion
|
||||
var value = await showMenu<String>(
|
||||
context: context,
|
||||
position: const RelativeRect.fromLTRB(
|
||||
100.0,
|
||||
200.0,
|
||||
100.0,
|
||||
100.0,
|
||||
),
|
||||
items: [
|
||||
PopupMenuItem<String>(
|
||||
value: 'delete',
|
||||
child: Text(
|
||||
widget.options.translations.deleteReaction,
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
if (value == 'delete') {
|
||||
// Call service to delete reaction
|
||||
updatePost(
|
||||
await widget.service
|
||||
.deletePostReaction(post, reaction.id),
|
||||
);
|
||||
}
|
||||
}
|
||||
},
|
||||
child: Row(
|
||||
crossAxisAlignment: reaction.imageUrl != null
|
||||
? CrossAxisAlignment.start
|
||||
: CrossAxisAlignment.center,
|
||||
|
@ -338,8 +369,8 @@ class _TimelinePostScreenState extends State<TimelinePostScreen> {
|
|||
children: [
|
||||
Text(
|
||||
reaction.creator?.fullName ??
|
||||
widget
|
||||
.options.translations.anonymousUser,
|
||||
widget.options.translations
|
||||
.anonymousUser,
|
||||
style: theme.textTheme.titleSmall,
|
||||
),
|
||||
Padding(
|
||||
|
@ -357,7 +388,8 @@ class _TimelinePostScreenState extends State<TimelinePostScreen> {
|
|||
child: Text.rich(
|
||||
TextSpan(
|
||||
text: reaction.creator?.fullName ??
|
||||
widget.options.translations.anonymousUser,
|
||||
widget
|
||||
.options.translations.anonymousUser,
|
||||
style: theme.textTheme.titleSmall,
|
||||
children: [
|
||||
const TextSpan(text: ' '),
|
||||
|
@ -373,6 +405,7 @@ class _TimelinePostScreenState extends State<TimelinePostScreen> {
|
|||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
if (post.reactions?.isEmpty ?? true) ...[
|
||||
const SizedBox(height: 16),
|
||||
|
|
Loading…
Reference in a new issue