mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-11-03 19:54:12 +00:00 
			
		
		
		
	Can delete comments
This commit is contained in:
		@@ -9,16 +9,20 @@ import 'package:flutter/material.dart';
 | 
			
		||||
///
 | 
			
		||||
/// @author Pierre HUBERT
 | 
			
		||||
 | 
			
		||||
enum _CommentAction { DELETE }
 | 
			
		||||
 | 
			
		||||
class CommentTile extends StatelessWidget {
 | 
			
		||||
  final Comment comment;
 | 
			
		||||
  final User user;
 | 
			
		||||
  final void Function(Comment) onUpdateLike;
 | 
			
		||||
  final void Function(Comment) onDeleteComment;
 | 
			
		||||
 | 
			
		||||
  const CommentTile({
 | 
			
		||||
    Key key,
 | 
			
		||||
    @required this.comment,
 | 
			
		||||
    @required this.user,
 | 
			
		||||
    @required this.onUpdateLike,
 | 
			
		||||
    @required this.onDeleteComment,
 | 
			
		||||
  })  : assert(comment != null),
 | 
			
		||||
        assert(user != null),
 | 
			
		||||
        assert(onUpdateLike != null),
 | 
			
		||||
@@ -27,9 +31,7 @@ class CommentTile extends StatelessWidget {
 | 
			
		||||
  @override
 | 
			
		||||
  Widget build(BuildContext context) {
 | 
			
		||||
    return ListTile(
 | 
			
		||||
      leading: AccountImageWidget(
 | 
			
		||||
        user: user,
 | 
			
		||||
      ),
 | 
			
		||||
      leading: _buildAccountImageWidget(),
 | 
			
		||||
      title: Text(
 | 
			
		||||
        user.displayName,
 | 
			
		||||
      ),
 | 
			
		||||
@@ -37,6 +39,22 @@ class CommentTile extends StatelessWidget {
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Widget _buildAccountImageWidget() {
 | 
			
		||||
    return PopupMenuButton<_CommentAction>(
 | 
			
		||||
      child: AccountImageWidget(
 | 
			
		||||
        user: user,
 | 
			
		||||
      ),
 | 
			
		||||
      onSelected: _selectedMenuOption,
 | 
			
		||||
      itemBuilder: (c) => [
 | 
			
		||||
            PopupMenuItem(
 | 
			
		||||
              enabled: comment.isOwner,
 | 
			
		||||
              child: Text("Delete"),
 | 
			
		||||
              value: _CommentAction.DELETE,
 | 
			
		||||
            )
 | 
			
		||||
          ],
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Widget _buildCommentContent() {
 | 
			
		||||
    return Column(
 | 
			
		||||
      crossAxisAlignment: CrossAxisAlignment.stretch,
 | 
			
		||||
@@ -106,4 +124,14 @@ class CommentTile extends StatelessWidget {
 | 
			
		||||
      ),
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// A menu option has been selected
 | 
			
		||||
  void _selectedMenuOption(_CommentAction value) {
 | 
			
		||||
    switch (value) {
 | 
			
		||||
      // Delete comment
 | 
			
		||||
      case _CommentAction.DELETE:
 | 
			
		||||
        onDeleteComment(comment);
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -195,6 +195,7 @@ class _PostTileState extends State<PostTile> {
 | 
			
		||||
            comment: widget.post.comments[num],
 | 
			
		||||
            user: widget.usersInfo.getUser(widget.post.comments[num].userID),
 | 
			
		||||
            onUpdateLike: _updateCommentLike,
 | 
			
		||||
            onDeleteComment: _deleteComment,
 | 
			
		||||
          ),
 | 
			
		||||
    );
 | 
			
		||||
 | 
			
		||||
@@ -290,7 +291,7 @@ class _PostTileState extends State<PostTile> {
 | 
			
		||||
  Future<void> _pickImageForComment() async {
 | 
			
		||||
    // Ask the user to confirm image removal if there is already one selected
 | 
			
		||||
    if (_hasImage) {
 | 
			
		||||
      if (await askUserConfirmation(
 | 
			
		||||
      if (await showConfirmDialog(
 | 
			
		||||
        context: context,
 | 
			
		||||
        title: tr("Remove selected image"),
 | 
			
		||||
        message: tr("Do you want to unselected currently selected image ?"),
 | 
			
		||||
@@ -364,4 +365,22 @@ class _PostTileState extends State<PostTile> {
 | 
			
		||||
      element.userLike ? element.likes++ : element.likes--;
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Process the deletion of a user
 | 
			
		||||
  Future<void> _deleteComment(Comment comment) async {
 | 
			
		||||
    if (!await showConfirmDialog(
 | 
			
		||||
        context: context,
 | 
			
		||||
        message: tr("Do you really want to delete this comment ?"),
 | 
			
		||||
        title: tr("Delete comment"))) return;
 | 
			
		||||
 | 
			
		||||
    if (!await _commentsHelper.delete(comment.id)) {
 | 
			
		||||
      showSimpleSnack(context, tr("Could not delete the comment!"));
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Remove the comment from the list
 | 
			
		||||
    setState(() {
 | 
			
		||||
      widget.post.comments.remove(comment);
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user