mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-11-04 04:04:18 +00:00 
			
		
		
		
	Can delete posts
This commit is contained in:
		@@ -4,6 +4,7 @@ import 'package:comunic/enums/likes_type.dart';
 | 
			
		||||
import 'package:comunic/enums/post_kind.dart';
 | 
			
		||||
import 'package:comunic/helpers/comments_helper.dart';
 | 
			
		||||
import 'package:comunic/helpers/likes_helper.dart';
 | 
			
		||||
import 'package:comunic/helpers/posts_helper.dart';
 | 
			
		||||
import 'package:comunic/lists/users_list.dart';
 | 
			
		||||
import 'package:comunic/models/comment.dart';
 | 
			
		||||
import 'package:comunic/models/like_element.dart';
 | 
			
		||||
@@ -29,16 +30,22 @@ const TextStyle _userNameStyle = TextStyle(
 | 
			
		||||
    fontWeight: FontWeight.w600,
 | 
			
		||||
    fontSize: 16.0);
 | 
			
		||||
 | 
			
		||||
/// Post actions
 | 
			
		||||
enum _PostActions { DELETE }
 | 
			
		||||
 | 
			
		||||
class PostTile extends StatefulWidget {
 | 
			
		||||
  final Post post;
 | 
			
		||||
  final UsersList usersInfo;
 | 
			
		||||
  final void Function(Post) onDeletedPost;
 | 
			
		||||
 | 
			
		||||
  const PostTile({
 | 
			
		||||
    Key key,
 | 
			
		||||
    @required this.post,
 | 
			
		||||
    @required this.usersInfo,
 | 
			
		||||
    @required this.onDeletedPost,
 | 
			
		||||
  })  : assert(post != null),
 | 
			
		||||
        assert(usersInfo != null),
 | 
			
		||||
        assert(onDeletedPost != null),
 | 
			
		||||
        super(key: key);
 | 
			
		||||
 | 
			
		||||
  @override
 | 
			
		||||
@@ -47,6 +54,7 @@ class PostTile extends StatefulWidget {
 | 
			
		||||
 | 
			
		||||
class _PostTileState extends State<PostTile> {
 | 
			
		||||
  // Helpers
 | 
			
		||||
  final _postsHelper = PostsHelper();
 | 
			
		||||
  final _likesHelper = LikesHelper();
 | 
			
		||||
  final _commentsHelper = CommentsHelper();
 | 
			
		||||
 | 
			
		||||
@@ -89,8 +97,16 @@ class _PostTileState extends State<PostTile> {
 | 
			
		||||
          ),
 | 
			
		||||
        ),
 | 
			
		||||
 | 
			
		||||
        PopupMenuButton(
 | 
			
		||||
          itemBuilder: (c) => [],
 | 
			
		||||
        PopupMenuButton<_PostActions>(
 | 
			
		||||
          itemBuilder: (c) => [
 | 
			
		||||
                // Delete post
 | 
			
		||||
                PopupMenuItem(
 | 
			
		||||
                  child: Text(tr("Delete")),
 | 
			
		||||
                  value: _PostActions.DELETE,
 | 
			
		||||
                  enabled: widget.post.canDelete,
 | 
			
		||||
                ),
 | 
			
		||||
              ],
 | 
			
		||||
          onSelected: _selectedPostMenuAction,
 | 
			
		||||
        )
 | 
			
		||||
      ],
 | 
			
		||||
    );
 | 
			
		||||
@@ -402,4 +418,30 @@ class _PostTileState extends State<PostTile> {
 | 
			
		||||
      widget.post.comments.remove(comment);
 | 
			
		||||
    });
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Method called each time the user has selected an option
 | 
			
		||||
  void _selectedPostMenuAction(_PostActions value) {
 | 
			
		||||
    switch (value) {
 | 
			
		||||
      case _PostActions.DELETE:
 | 
			
		||||
        confirmDelete();
 | 
			
		||||
        break;
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  /// Perform the deletion of the post
 | 
			
		||||
  Future<void> confirmDelete() async {
 | 
			
		||||
    // Ask user confirmation
 | 
			
		||||
    if (!await showConfirmDialog(
 | 
			
		||||
      context: context,
 | 
			
		||||
      message: tr(
 | 
			
		||||
          "Do you really want to delete this post ? The operation can not be reverted !"),
 | 
			
		||||
    )) return;
 | 
			
		||||
 | 
			
		||||
    if (!await _postsHelper.delete(widget.post.id)) {
 | 
			
		||||
      showSimpleSnack(context, tr("Could not delete the post!"));
 | 
			
		||||
      return;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    widget.onDeletedPost(widget.post);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,6 +1,7 @@
 | 
			
		||||
import 'package:comunic/helpers/users_helper.dart';
 | 
			
		||||
import 'package:comunic/lists/posts_list.dart';
 | 
			
		||||
import 'package:comunic/lists/users_list.dart';
 | 
			
		||||
import 'package:comunic/models/post.dart';
 | 
			
		||||
import 'package:comunic/ui/screens/conversation_screen.dart';
 | 
			
		||||
import 'package:comunic/ui/tiles/post_tile.dart';
 | 
			
		||||
import 'package:comunic/utils/intl_utils.dart';
 | 
			
		||||
@@ -73,6 +74,7 @@ class _PostsListWidgetState extends State<PostsListWidget> {
 | 
			
		||||
      itemBuilder: (c, i) => PostTile(
 | 
			
		||||
            post: _list[i],
 | 
			
		||||
            usersInfo: _users,
 | 
			
		||||
            onDeletedPost: _removePost,
 | 
			
		||||
          ),
 | 
			
		||||
    );
 | 
			
		||||
  }
 | 
			
		||||
@@ -83,4 +85,6 @@ class _PostsListWidgetState extends State<PostsListWidget> {
 | 
			
		||||
    if (_list == null) return buildCenteredProgressBar();
 | 
			
		||||
    return _buildListView();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  void _removePost(Post post) => setState(() => _list.remove(post));
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user