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

Can update comments content

This commit is contained in:
Pierre HUBERT 2019-05-18 18:48:12 +02:00
parent 6d73d38bf8
commit 67b6764f09
6 changed files with 60 additions and 8 deletions

View File

@ -37,13 +37,24 @@ class CommentsHelper {
return apiToComment(response.getObject());
}
/// Update comment content
Future<bool> updateContent(int id, String newContent) async {
return (await APIRequest(uri: "comments/edit", needLogin: true, args: {
"commentID": id.toString(),
"content": newContent,
}).exec())
.isOK;
}
/// 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;
uri: "comments/delete",
needLogin: true,
args: {"commentID": id.toString()},
).exec())
.code ==
200;
}
/// Turn an API entry into a [Comment] object

View File

@ -13,4 +13,7 @@ class APIResponse {
List<dynamic> getArray() => jsonDecode(this.content);
Map<String, dynamic> getObject() => jsonDecode(this.content);
/// Check if the request is successful or not
bool get isOK => code == 200;
}

View File

@ -13,7 +13,7 @@ class Comment implements LikeElement {
final int userID;
final int postID;
final int timeSent;
final String content;
String content;
final String imageURL;
int likes;
bool userLike;

View File

@ -9,12 +9,13 @@ import 'package:flutter/material.dart';
///
/// @author Pierre HUBERT
enum _CommentAction { DELETE }
enum _CommentAction { DELETE, UPDATE }
class CommentTile extends StatelessWidget {
final Comment comment;
final User user;
final void Function(Comment) onUpdateLike;
final void Function(Comment) onUpdateComment;
final void Function(Comment) onDeleteComment;
const CommentTile({
@ -22,10 +23,13 @@ class CommentTile extends StatelessWidget {
@required this.comment,
@required this.user,
@required this.onUpdateLike,
@required this.onUpdateComment,
@required this.onDeleteComment,
}) : assert(comment != null),
assert(user != null),
assert(onUpdateLike != null),
assert(onUpdateComment != null),
assert(onDeleteComment != null),
super(key: key);
@override
@ -46,11 +50,19 @@ class CommentTile extends StatelessWidget {
),
onSelected: _selectedMenuOption,
itemBuilder: (c) => [
// Update comment content
PopupMenuItem(
enabled: comment.isOwner,
child: Text("Delete"),
child: Text(tr("Update")),
value: _CommentAction.UPDATE,
),
// Delete comment
PopupMenuItem(
enabled: comment.isOwner,
child: Text(tr("Delete")),
value: _CommentAction.DELETE,
)
),
],
);
}
@ -128,6 +140,11 @@ class CommentTile extends StatelessWidget {
/// A menu option has been selected
void _selectedMenuOption(_CommentAction value) {
switch (value) {
// Update comment content
case _CommentAction.UPDATE:
onUpdateComment(comment);
break;
// Delete comment
case _CommentAction.DELETE:
onDeleteComment(comment);

View File

@ -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,
onUpdateComment: _updateCommentContent,
onDeleteComment: _deleteComment,
),
);
@ -366,6 +367,24 @@ class _PostTileState extends State<PostTile> {
});
}
/// Update comment content
Future<void> _updateCommentContent(Comment comment) async {
final newContent = await askUserString(
context: context,
title: tr("Update comment content"),
message: tr("New content:"),
defaultValue: comment.content,
hint: tr("New content..."),
);
if (!(await _commentsHelper.updateContent(comment.id, newContent)))
return showSimpleSnack(context, tr("Could not update comment content!"));
setState(() {
comment.content = newContent;
});
}
/// Process the deletion of a user
Future<void> _deleteComment(Comment comment) async {
if (!await showConfirmDialog(

View File

@ -68,6 +68,8 @@ void showSimpleSnack(BuildContext context, String message) {
}
/// Show an alert dialog to ask the user to enter a string
///
/// Returns entered string if the dialog is confirmed, null else
Future<String> askUserString({
@required BuildContext context,
@required String title,