From 0bf85b478a9bec5cf831e291df564627e95587bb Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sun, 19 May 2019 17:42:09 +0200 Subject: [PATCH] Can update post content --- lib/helpers/posts_helper.dart | 13 +++++++++++++ lib/models/post.dart | 4 +++- lib/ui/tiles/post_tile.dart | 35 ++++++++++++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/lib/helpers/posts_helper.dart b/lib/helpers/posts_helper.dart index 76543c4..384cd95 100644 --- a/lib/helpers/posts_helper.dart +++ b/lib/helpers/posts_helper.dart @@ -56,6 +56,19 @@ class PostsHelper { } } + /// Update a post content + Future updateContent(int id, String newContent) async { + return (await APIRequest( + uri: "posts/update_content", + needLogin: true, + args: { + "postID": id.toString(), + "new_content": newContent, + }, + ).exec()) + .isOK; + } + /// Delete a post Future delete(int id) async { return (await APIRequest( diff --git a/lib/models/post.dart b/lib/models/post.dart index 96e8d21..81ecdc6 100644 --- a/lib/models/post.dart +++ b/lib/models/post.dart @@ -15,7 +15,7 @@ class Post implements LikeElement { final int userPageID; final int groupID; final int timeSent; - final String content; + String content; final PostVisibilityLevel visibilityLevel; final PostKind kind; final int fileSize; @@ -70,6 +70,8 @@ class Post implements LikeElement { bool get hasComments => comments != null; + bool get canUpdate => access == UserAccessLevels.FULL; + bool get canDelete => access == UserAccessLevels.FULL || access == UserAccessLevels.INTERMEDIATE; diff --git a/lib/ui/tiles/post_tile.dart b/lib/ui/tiles/post_tile.dart index 9f157cc..dee42fd 100644 --- a/lib/ui/tiles/post_tile.dart +++ b/lib/ui/tiles/post_tile.dart @@ -31,7 +31,7 @@ const TextStyle _userNameStyle = TextStyle( fontSize: 16.0); /// Post actions -enum _PostActions { DELETE } +enum _PostActions { DELETE, UPDATE_CONTENT } class PostTile extends StatefulWidget { final Post post; @@ -99,6 +99,13 @@ class _PostTileState extends State { PopupMenuButton<_PostActions>( itemBuilder: (c) => [ + // Update post content + PopupMenuItem( + child: Text(tr("Update content")), + value: _PostActions.UPDATE_CONTENT, + enabled: widget.post.canUpdate, + ), + // Delete post PopupMenuItem( child: Text(tr("Delete")), @@ -422,12 +429,38 @@ class _PostTileState extends State { /// Method called each time the user has selected an option void _selectedPostMenuAction(_PostActions value) { switch (value) { + // Update post content + case _PostActions.UPDATE_CONTENT: + updateContent(); + break; + + // Delete post case _PostActions.DELETE: confirmDelete(); break; } } + /// Update post content + Future updateContent() async { + final newContent = await askUserString( + context: context, + title: tr("Update post content"), + message: tr("Please enter message content: "), + defaultValue: widget.post.content, + hint: tr("Post content"), + ); + + if (newContent == null) return; + + if (!await _postsHelper.updateContent(widget.post.id, newContent)) { + showSimpleSnack(context, tr("Could not update post content!")); + return; + } + + setState(() => widget.post.content = newContent); + } + /// Perform the deletion of the post Future confirmDelete() async { // Ask user confirmation