diff --git a/lib/widgets/comment.dart b/lib/widgets/comment.dart index c100405..dee70ec 100644 --- a/lib/widgets/comment.dart +++ b/lib/widgets/comment.dart @@ -98,6 +98,8 @@ class Comment extends HookWidget { } bool get isOP => commentTree.comment.creatorId == postCreatorId; + bool get isMine => + commentTree.comment.creatorId == commentTree.comment.userId; @override Widget build(BuildContext context) { @@ -106,12 +108,32 @@ class Comment extends HookWidget { final showRaw = useState(false); final collapsed = useState(false); final myVote = useState(commentTree.comment.myVote ?? VoteType.none); + final isDeleted = useState(commentTree.comment.deleted); final delayedVoting = useDelayedLoading(); + final delayedDeletion = useDelayedLoading(); final loggedInAction = useLoggedInAction(commentTree.comment.instanceUrl); final newReplies = useState(const []); final comment = commentTree.comment; + handleDelete(Jwt token) async { + final api = LemmyApi(token.payload.iss).v1; + + delayedDeletion.start(); + Navigator.of(context).pop(); + try { + final res = await api.deleteComment( + editId: comment.id, deleted: !isDeleted.value, auth: token.raw); + isDeleted.value = res.deleted; + // ignore: avoid_catches_without_on_clauses + } catch (e) { + Scaffold.of(context).showSnackBar( + SnackBar(content: Text('Failed to delete/restore comment'))); + return; + } + delayedDeletion.cancel(); + } + void _openMoreMenu(BuildContext context) { pop() => Navigator.of(context).pop(); @@ -160,6 +182,12 @@ class Comment extends HookWidget { pop(); }, ), + if (isMine) + ListTile( + leading: Icon(isDeleted.value ? Icons.restore : Icons.delete), + title: Text(isDeleted.value ? 'Restore' : 'Delete'), + onTap: loggedInAction(handleDelete), + ), ListTile( leading: Icon(Icons.info_outline), title: Text('Nerd stuff'), @@ -209,7 +237,7 @@ class Comment extends HookWidget { }(); final body = () { - if (comment.deleted) { + if (isDeleted.value) { return Flexible( child: Text( 'comment deleted by creator', @@ -254,7 +282,7 @@ class Comment extends HookWidget { final actions = collapsed.value ? SizedBox.shrink() : Row(children: [ - if (selectable.value && !comment.deleted && !comment.removed) + if (selectable.value && !isDeleted.value && !comment.removed) _CommentAction( icon: Icons.content_copy, tooltip: 'copy', @@ -268,14 +296,16 @@ class Comment extends HookWidget { _CommentAction( icon: Icons.more_horiz, onPressed: () => _openMoreMenu(context), + loading: delayedDeletion.loading, tooltip: 'more', ), _SaveComment(commentTree.comment), - _CommentAction( - icon: Icons.reply, - onPressed: loggedInAction((_) => reply()), - tooltip: 'reply', - ), + if (!isDeleted.value && !comment.removed) + _CommentAction( + icon: Icons.reply, + onPressed: loggedInAction((_) => reply()), + tooltip: 'reply', + ), _CommentAction( icon: Icons.arrow_upward, iconColor: myVote.value == VoteType.up ? theme.accentColor : null,