From 1809f2557d3683737fa53d00bdb51287aaa15965 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 13 Oct 2024 20:36:10 +0800 Subject: [PATCH] :alien: Support server-side truncate content --- lib/screens/posts/post_detail.dart | 1 - lib/widgets/posts/post_item.dart | 105 +++++-------------------- lib/widgets/posts/post_list.dart | 1 - lib/widgets/posts/post_owned_list.dart | 2 - 4 files changed, 21 insertions(+), 88 deletions(-) diff --git a/lib/screens/posts/post_detail.dart b/lib/screens/posts/post_detail.dart index 9b6fc09..a98f0c1 100644 --- a/lib/screens/posts/post_detail.dart +++ b/lib/screens/posts/post_detail.dart @@ -65,7 +65,6 @@ class _PostDetailScreenState extends State { isClickable: false, isOverrideEmbedClickable: true, isFullDate: true, - isFullContent: true, isShowReply: false, isContentSelectable: true, padding: AppTheme.isLargeScreen(context) diff --git a/lib/widgets/posts/post_item.dart b/lib/widgets/posts/post_item.dart index 8267864..4b4c7cf 100644 --- a/lib/widgets/posts/post_item.dart +++ b/lib/widgets/posts/post_item.dart @@ -31,13 +31,11 @@ class PostItem extends StatefulWidget { final bool isShowEmbed; final bool isOverrideEmbedClickable; final bool isFullDate; - final bool isFullContent; final bool isContentSelectable; final bool showFeaturedReply; final String? attachmentParent; final EdgeInsets? padding; - final Color? backgroundColor; final Function? onComment; @@ -51,12 +49,10 @@ class PostItem extends StatefulWidget { this.isShowEmbed = true, this.isOverrideEmbedClickable = false, this.isFullDate = false, - this.isFullContent = false, this.isContentSelectable = false, this.showFeaturedReply = false, this.attachmentParent, this.padding, - this.backgroundColor, this.onComment, }); @@ -76,8 +72,6 @@ class _PostItemState extends State { super.initState(); } - double _contentHeight = 0; - @override Widget build(BuildContext context) { final List attachments = item.body['attachments'] is List @@ -95,32 +89,24 @@ class _PostItemState extends State { ).paddingOnly(bottom: 8), _PostHeaderWidget( isCompact: widget.isCompact, + isFullDate: widget.isFullDate, item: item, ).paddingSymmetric(horizontal: 12), _PostHeaderDividerWidget(item: item).paddingSymmetric(horizontal: 12), SizedContainer( maxWidth: 640, - maxHeight: widget.isFullContent ? double.infinity : 80, - child: _MeasureSize( - onChange: (size) { - setState(() => _contentHeight = size.height); - }, - child: SingleChildScrollView( - physics: const NeverScrollableScrollPhysics(), - child: MarkdownTextContent( - parentId: 'p${item.id}', - content: item.body['content'], - isAutoWarp: item.type == 'story', - isSelectable: widget.isContentSelectable, - ), - ).paddingOnly( - left: 12, - right: 12, - bottom: hasAttachment ? 4 : 0, - ), + child: MarkdownTextContent( + parentId: 'p${item.id}', + content: item.body['content'], + isAutoWarp: item.type == 'story', + isSelectable: widget.isContentSelectable, ), + ).paddingOnly( + left: 12, + right: 12, + bottom: hasAttachment ? 4 : 0, ), - if (_contentHeight >= 80 && !widget.isFullContent) + if (widget.item.body?['content_truncated'] == true) Opacity( opacity: 0.8, child: InkWell(child: Text('readMore'.tr)), @@ -165,30 +151,20 @@ class _PostItemState extends State { children: [ _PostHeaderWidget( isCompact: widget.isCompact, + isFullDate: widget.isFullDate, item: item, ), _PostHeaderDividerWidget(item: item), SizedContainer( maxWidth: 640, - maxHeight: widget.isFullContent ? double.infinity : 320, - child: _MeasureSize( - onChange: (size) { - setState(() => _contentHeight = size.height); - }, - child: SingleChildScrollView( - physics: const NeverScrollableScrollPhysics(), - child: MarkdownTextContent( - parentId: 'p${item.id}-embed', - content: item.body['content'], - isAutoWarp: item.type == 'story', - isSelectable: widget.isContentSelectable, - isLargeText: - item.type == 'article' && widget.isFullContent, - ), - ), + child: MarkdownTextContent( + parentId: 'p${item.id}-embed', + content: item.body['content'], + isAutoWarp: item.type == 'story', + isSelectable: widget.isContentSelectable, ), ), - if (_contentHeight >= 320 && !widget.isFullContent) + if (widget.item.body?['content_truncated'] == true) Opacity( opacity: 0.8, child: InkWell(child: Text('readMore'.tr)), @@ -593,10 +569,12 @@ class _PostFooterWidget extends StatelessWidget { class _PostHeaderWidget extends StatelessWidget { final bool isCompact; + final bool isFullDate; final Post item; const _PostHeaderWidget({ required this.isCompact, + required this.isFullDate, required this.item, }); @@ -629,6 +607,7 @@ class _PostHeaderWidget extends StatelessWidget { if (isCompact) RelativeDate( item.publishedAt?.toLocal() ?? DateTime.now(), + isFull: isFullDate, ).paddingOnly(top: 1), ], ), @@ -684,45 +663,3 @@ class _PostThumbnail extends StatelessWidget { ); } } - -typedef _OnWidgetSizeChange = void Function(Size size); - -class _MeasureSizeRenderObject extends RenderProxyBox { - Size? oldSize; - _OnWidgetSizeChange onChange; - - _MeasureSizeRenderObject(this.onChange); - - @override - void performLayout() { - super.performLayout(); - - Size newSize = child!.size; - if (oldSize == newSize) return; - - oldSize = newSize; - WidgetsBinding.instance.addPostFrameCallback((_) { - onChange(newSize); - }); - } -} - -class _MeasureSize extends SingleChildRenderObjectWidget { - final _OnWidgetSizeChange onChange; - - const _MeasureSize({ - required this.onChange, - required Widget super.child, - }); - - @override - RenderObject createRenderObject(BuildContext context) { - return _MeasureSizeRenderObject(onChange); - } - - @override - void updateRenderObject( - BuildContext context, covariant _MeasureSizeRenderObject renderObject) { - renderObject.onChange = onChange; - } -} diff --git a/lib/widgets/posts/post_list.dart b/lib/widgets/posts/post_list.dart index d9fe325..05b881c 100644 --- a/lib/widgets/posts/post_list.dart +++ b/lib/widgets/posts/post_list.dart @@ -86,7 +86,6 @@ class PostListEntryWidget extends StatelessWidget { isClickable: isNestedClickable, showFeaturedReply: showFeaturedReply, padding: padding, - backgroundColor: backgroundColor, onComment: () { AppRouter.instance .pushNamed( diff --git a/lib/widgets/posts/post_owned_list.dart b/lib/widgets/posts/post_owned_list.dart index 27c8cf3..459c69a 100644 --- a/lib/widgets/posts/post_owned_list.dart +++ b/lib/widgets/posts/post_owned_list.dart @@ -31,8 +31,6 @@ class PostOwnedListEntry extends StatelessWidget { isClickable: false, isShowReply: false, isReactable: false, - isFullContent: isFullContent, - backgroundColor: backgroundColor, ).paddingSymmetric(vertical: 8), ], ),