mirror of
				https://gitlab.com/comunic/comunicmobile
				synced 2025-11-04 04:04:18 +00:00 
			
		
		
		
	Can update comments content
This commit is contained in:
		@@ -37,13 +37,24 @@ class CommentsHelper {
 | 
				
			|||||||
    return apiToComment(response.getObject());
 | 
					    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
 | 
					  /// Attempt to delete a comment
 | 
				
			||||||
  Future<bool> delete(int id) async {
 | 
					  Future<bool> delete(int id) async {
 | 
				
			||||||
    return (await APIRequest(
 | 
					    return (await APIRequest(
 | 
				
			||||||
          uri: "comments/delete",
 | 
					          uri: "comments/delete",
 | 
				
			||||||
          needLogin: true,
 | 
					          needLogin: true,
 | 
				
			||||||
          args: {"commentID": id.toString()},
 | 
					          args: {"commentID": id.toString()},
 | 
				
			||||||
    ).exec()).code == 200;
 | 
					        ).exec())
 | 
				
			||||||
 | 
					            .code ==
 | 
				
			||||||
 | 
					        200;
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /// Turn an API entry into a [Comment] object
 | 
					  /// Turn an API entry into a [Comment] object
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,4 +13,7 @@ class APIResponse {
 | 
				
			|||||||
  List<dynamic> getArray() => jsonDecode(this.content);
 | 
					  List<dynamic> getArray() => jsonDecode(this.content);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Map<String, dynamic> getObject() => jsonDecode(this.content);
 | 
					  Map<String, dynamic> getObject() => jsonDecode(this.content);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  /// Check if the request is successful or not
 | 
				
			||||||
 | 
					  bool get isOK => code == 200;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,7 +13,7 @@ class Comment implements LikeElement {
 | 
				
			|||||||
  final int userID;
 | 
					  final int userID;
 | 
				
			||||||
  final int postID;
 | 
					  final int postID;
 | 
				
			||||||
  final int timeSent;
 | 
					  final int timeSent;
 | 
				
			||||||
  final String content;
 | 
					  String content;
 | 
				
			||||||
  final String imageURL;
 | 
					  final String imageURL;
 | 
				
			||||||
  int likes;
 | 
					  int likes;
 | 
				
			||||||
  bool userLike;
 | 
					  bool userLike;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,12 +9,13 @@ import 'package:flutter/material.dart';
 | 
				
			|||||||
///
 | 
					///
 | 
				
			||||||
/// @author Pierre HUBERT
 | 
					/// @author Pierre HUBERT
 | 
				
			||||||
 | 
					
 | 
				
			||||||
enum _CommentAction { DELETE }
 | 
					enum _CommentAction { DELETE, UPDATE }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
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) onUpdateComment;
 | 
				
			||||||
  final void Function(Comment) onDeleteComment;
 | 
					  final void Function(Comment) onDeleteComment;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const CommentTile({
 | 
					  const CommentTile({
 | 
				
			||||||
@@ -22,10 +23,13 @@ class CommentTile extends StatelessWidget {
 | 
				
			|||||||
    @required this.comment,
 | 
					    @required this.comment,
 | 
				
			||||||
    @required this.user,
 | 
					    @required this.user,
 | 
				
			||||||
    @required this.onUpdateLike,
 | 
					    @required this.onUpdateLike,
 | 
				
			||||||
 | 
					    @required this.onUpdateComment,
 | 
				
			||||||
    @required this.onDeleteComment,
 | 
					    @required this.onDeleteComment,
 | 
				
			||||||
  })  : assert(comment != null),
 | 
					  })  : assert(comment != null),
 | 
				
			||||||
        assert(user != null),
 | 
					        assert(user != null),
 | 
				
			||||||
        assert(onUpdateLike != null),
 | 
					        assert(onUpdateLike != null),
 | 
				
			||||||
 | 
					        assert(onUpdateComment != null),
 | 
				
			||||||
 | 
					        assert(onDeleteComment != null),
 | 
				
			||||||
        super(key: key);
 | 
					        super(key: key);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  @override
 | 
					  @override
 | 
				
			||||||
@@ -46,11 +50,19 @@ class CommentTile extends StatelessWidget {
 | 
				
			|||||||
      ),
 | 
					      ),
 | 
				
			||||||
      onSelected: _selectedMenuOption,
 | 
					      onSelected: _selectedMenuOption,
 | 
				
			||||||
      itemBuilder: (c) => [
 | 
					      itemBuilder: (c) => [
 | 
				
			||||||
 | 
					            // Update comment content
 | 
				
			||||||
            PopupMenuItem(
 | 
					            PopupMenuItem(
 | 
				
			||||||
              enabled: comment.isOwner,
 | 
					              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,
 | 
					              value: _CommentAction.DELETE,
 | 
				
			||||||
            )
 | 
					            ),
 | 
				
			||||||
          ],
 | 
					          ],
 | 
				
			||||||
    );
 | 
					    );
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
@@ -128,6 +140,11 @@ class CommentTile extends StatelessWidget {
 | 
				
			|||||||
  /// A menu option has been selected
 | 
					  /// A menu option has been selected
 | 
				
			||||||
  void _selectedMenuOption(_CommentAction value) {
 | 
					  void _selectedMenuOption(_CommentAction value) {
 | 
				
			||||||
    switch (value) {
 | 
					    switch (value) {
 | 
				
			||||||
 | 
					      // Update comment content
 | 
				
			||||||
 | 
					      case _CommentAction.UPDATE:
 | 
				
			||||||
 | 
					        onUpdateComment(comment);
 | 
				
			||||||
 | 
					        break;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // Delete comment
 | 
					      // Delete comment
 | 
				
			||||||
      case _CommentAction.DELETE:
 | 
					      case _CommentAction.DELETE:
 | 
				
			||||||
        onDeleteComment(comment);
 | 
					        onDeleteComment(comment);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -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,
 | 
				
			||||||
 | 
					            onUpdateComment: _updateCommentContent,
 | 
				
			||||||
            onDeleteComment: _deleteComment,
 | 
					            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
 | 
					  /// Process the deletion of a user
 | 
				
			||||||
  Future<void> _deleteComment(Comment comment) async {
 | 
					  Future<void> _deleteComment(Comment comment) async {
 | 
				
			||||||
    if (!await showConfirmDialog(
 | 
					    if (!await showConfirmDialog(
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -68,6 +68,8 @@ void showSimpleSnack(BuildContext context, String message) {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Show an alert dialog to ask the user to enter a string
 | 
					/// 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({
 | 
					Future<String> askUserString({
 | 
				
			||||||
  @required BuildContext context,
 | 
					  @required BuildContext context,
 | 
				
			||||||
  @required String title,
 | 
					  @required String title,
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user