1
0
mirror of https://gitlab.com/comunic/comunicmobile synced 2024-11-25 22:39:22 +00:00

Can delete comments

This commit is contained in:
Pierre HUBERT 2019-05-18 16:48:19 +02:00
parent 55088f2b23
commit 6d73d38bf8
5 changed files with 64 additions and 5 deletions

View File

@ -37,6 +37,15 @@ class CommentsHelper {
return apiToComment(response.getObject()); return apiToComment(response.getObject());
} }
/// Attempt to delete a comment
Future<bool> delete(int id) async {
return (await APIRequest(
uri: "comments/delete",
needLogin: true,
args: {"commentID": id.toString()},
).exec()).code == 200;
}
/// Turn an API entry into a [Comment] object /// Turn an API entry into a [Comment] object
static Comment apiToComment(Map<String, dynamic> entry) { static Comment apiToComment(Map<String, dynamic> entry) {
return Comment( return Comment(

View File

@ -1,5 +1,6 @@
import 'package:comunic/models/like_element.dart'; import 'package:comunic/models/like_element.dart';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import 'package:comunic/utils/account_utils.dart' as account;
/// Comments /// Comments
/// ///
@ -37,4 +38,6 @@ class Comment implements LikeElement {
bool get hasContent => content != null && content.length > 0; bool get hasContent => content != null && content.length > 0;
bool get hasImage => imageURL != null; bool get hasImage => imageURL != null;
bool get isOwner => userID == account.userID();
} }

View File

@ -9,16 +9,20 @@ import 'package:flutter/material.dart';
/// ///
/// @author Pierre HUBERT /// @author Pierre HUBERT
enum _CommentAction { DELETE }
class CommentTile extends StatelessWidget { class CommentTile extends StatelessWidget {
final Comment comment; final Comment comment;
final User user; final User user;
final void Function(Comment) onUpdateLike; final void Function(Comment) onUpdateLike;
final void Function(Comment) onDeleteComment;
const CommentTile({ const CommentTile({
Key key, Key key,
@required this.comment, @required this.comment,
@required this.user, @required this.user,
@required this.onUpdateLike, @required this.onUpdateLike,
@required this.onDeleteComment,
}) : assert(comment != null), }) : assert(comment != null),
assert(user != null), assert(user != null),
assert(onUpdateLike != null), assert(onUpdateLike != null),
@ -27,9 +31,7 @@ class CommentTile extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return ListTile( return ListTile(
leading: AccountImageWidget( leading: _buildAccountImageWidget(),
user: user,
),
title: Text( title: Text(
user.displayName, 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() { Widget _buildCommentContent() {
return Column( return Column(
crossAxisAlignment: CrossAxisAlignment.stretch, 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;
}
}
} }

View File

@ -195,6 +195,7 @@ class _PostTileState extends State<PostTile> {
comment: widget.post.comments[num], comment: widget.post.comments[num],
user: widget.usersInfo.getUser(widget.post.comments[num].userID), user: widget.usersInfo.getUser(widget.post.comments[num].userID),
onUpdateLike: _updateCommentLike, onUpdateLike: _updateCommentLike,
onDeleteComment: _deleteComment,
), ),
); );
@ -290,7 +291,7 @@ class _PostTileState extends State<PostTile> {
Future<void> _pickImageForComment() async { Future<void> _pickImageForComment() async {
// Ask the user to confirm image removal if there is already one selected // Ask the user to confirm image removal if there is already one selected
if (_hasImage) { if (_hasImage) {
if (await askUserConfirmation( if (await showConfirmDialog(
context: context, context: context,
title: tr("Remove selected image"), title: tr("Remove selected image"),
message: tr("Do you want to unselected currently 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--; 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);
});
}
} }

View File

@ -122,7 +122,7 @@ Future<String> askUserString({
/// Show an alert dialog to get user confirmation for something /// Show an alert dialog to get user confirmation for something
/// ///
/// Return value of this function is never null /// Return value of this function is never null
Future<bool> askUserConfirmation({ Future<bool> showConfirmDialog({
@required BuildContext context, @required BuildContext context,
String title, String title,
@required String message, @required String message,