From 57ff5e44ba0bac51ea7a937ad0c3cda8d6a0cde8 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Wed, 22 May 2024 20:56:10 +0800 Subject: [PATCH] :sparkles: Embedded reply post and repost --- lib/translations.dart | 4 + lib/widgets/account/account_avatar.dart | 20 +++-- lib/widgets/posts/post_item.dart | 98 +++++++++++++++++++++++++ 3 files changed, 117 insertions(+), 5 deletions(-) diff --git a/lib/translations.dart b/lib/translations.dart index dc76088..04277d7 100644 --- a/lib/translations.dart +++ b/lib/translations.dart @@ -47,6 +47,8 @@ class SolianMessages extends Translations { 'postContentPlaceholder': 'What\'s happened?!', 'postReaction': 'Reactions of the Post', 'postActionList': 'Actions of Post', + 'postRepliedNotify': 'Replied a post from @username.', + 'postRepostedNotify': 'Reposted a post from @username.', 'postEditingNotify': 'You\'re editing as post from you.', 'postReplyingNotify': 'You\'re replying a post from @username.', 'postRepostingNotify': 'You\'re reposting a post from @username.', @@ -105,6 +107,8 @@ class SolianMessages extends Translations { 'postContentPlaceholder': '发生什么事了?!', 'postReaction': '帖子的反应', 'postActionList': '帖子的操作', + 'postRepliedNotify': '回了一个 @username 的帖子', + 'postRepostedNotify': '转了一个 @username 的帖子', 'postEditingNotify': '你正在编辑一个你发布的帖子', 'postReplyingNotify': '你正在回一个来自 @username 的帖子', 'postRepostingNotify': '你正在转一个来自 @username 的帖子', diff --git a/lib/widgets/account/account_avatar.dart b/lib/widgets/account/account_avatar.dart index c0ab0a3..f781af1 100644 --- a/lib/widgets/account/account_avatar.dart +++ b/lib/widgets/account/account_avatar.dart @@ -6,7 +6,8 @@ class AccountAvatar extends StatelessWidget { final Color? color; final double? radius; - const AccountAvatar({super.key, required this.content, this.color, this.radius}); + const AccountAvatar( + {super.key, required this.content, this.color, this.radius}); @override Widget build(BuildContext context) { @@ -21,10 +22,19 @@ class AccountAvatar extends StatelessWidget { key: Key('a$content'), radius: radius, backgroundColor: color, - backgroundImage: !isEmpty ? NetworkImage( - direct ? content : '${ServiceFinder.services['paperclip']}/api/attachments/$content', - ) : null, - child: isEmpty ? const Icon(Icons.account_circle) : null, + backgroundImage: !isEmpty + ? NetworkImage( + direct + ? content + : '${ServiceFinder.services['paperclip']}/api/attachments/$content', + ) + : null, + child: isEmpty + ? Icon( + Icons.account_circle, + size: radius != null ? radius! * 1.2 : 24, + ) + : null, ); } } diff --git a/lib/widgets/posts/post_item.dart b/lib/widgets/posts/post_item.dart index fb17347..ed52878 100644 --- a/lib/widgets/posts/post_item.dart +++ b/lib/widgets/posts/post_item.dart @@ -10,11 +10,13 @@ import 'package:timeago/timeago.dart' show format; class PostItem extends StatefulWidget { final Post item; + final bool isCompact; final bool isReactable; const PostItem({ super.key, required this.item, + this.isCompact = false, this.isReactable = true, }); @@ -31,10 +33,104 @@ class _PostItemState extends State { super.initState(); } + Widget buildReply(BuildContext context) { + return Column( + children: [ + Row( + children: [ + Icon( + Icons.reply, + size: 18, + color: Theme.of(context).colorScheme.onSurface.withOpacity(0.75), + ), + Text( + 'postRepliedNotify'.trParams( + {'username': '@${widget.item.author.name}'}, + ), + style: TextStyle( + color: + Theme.of(context).colorScheme.onSurface.withOpacity(0.75), + ), + ).paddingOnly(left: 6), + ], + ).paddingOnly(left: 12), + Card( + elevation: 1, + child: PostItem( + item: widget.item.replyTo!, + isCompact: true, + ).paddingSymmetric(vertical: 8), + ), + ], + ); + } + + Widget buildRepost(BuildContext context) { + return Column( + children: [ + Row( + children: [ + Icon( + Icons.redo, + size: 18, + color: Theme.of(context).colorScheme.onSurface.withOpacity(0.75), + ), + Text( + 'postRepostedNotify'.trParams( + {'username': '@${widget.item.author.name}'}, + ), + style: TextStyle( + color: + Theme.of(context).colorScheme.onSurface.withOpacity(0.75), + ), + ).paddingOnly(left: 6), + ], + ).paddingOnly(left: 12), + Card( + elevation: 1, + child: PostItem( + item: widget.item.repostTo!, + isCompact: true, + ).paddingSymmetric(vertical: 8), + ), + ], + ); + } + @override Widget build(BuildContext context) { final hasAttachment = item.attachments?.isNotEmpty ?? false; + if (widget.isCompact) { + return Column( + children: [ + Row( + children: [ + AccountAvatar(content: item.author.avatar.toString(), radius: 10), + Text( + item.author.nick, + style: const TextStyle(fontWeight: FontWeight.bold), + ).paddingOnly(left: 6), + Text(format(item.createdAt, locale: 'en_short')) + .paddingOnly(left: 4), + ], + ).paddingSymmetric(horizontal: 12), + Markdown( + shrinkWrap: true, + physics: const NeverScrollableScrollPhysics(), + data: item.content, + padding: const EdgeInsets.all(0), + ).paddingOnly( + left: 16, + right: 12, + top: 2, + bottom: hasAttachment ? 4 : 0, + ), + AttachmentList(attachmentsId: item.attachments ?? List.empty()), + ], + ); + } + return Column( children: [ Row( @@ -54,6 +150,8 @@ class _PostItemState extends State { .paddingOnly(left: 4), ], ), + if (widget.item.replyTo != null) buildReply(context), + if (widget.item.repostTo != null) buildRepost(context), Markdown( shrinkWrap: true, physics: const NeverScrollableScrollPhysics(),