Surface/lib/widgets/post/post_item.dart

1109 lines
36 KiB
Dart
Raw Normal View History

2024-12-08 07:30:55 +00:00
import 'dart:io';
import 'dart:math' as math;
2024-11-10 10:37:34 +00:00
import 'package:easy_localization/easy_localization.dart';
2024-12-13 16:14:23 +00:00
import 'package:file_saver/file_saver.dart';
2024-12-08 07:30:55 +00:00
import 'package:flutter/foundation.dart';
2024-11-08 17:11:56 +00:00
import 'package:flutter/material.dart';
2024-11-10 10:37:34 +00:00
import 'package:go_router/go_router.dart';
2024-12-12 15:51:27 +00:00
import 'package:google_fonts/google_fonts.dart';
2024-11-09 03:16:14 +00:00
import 'package:material_symbols_icons/symbols.dart';
2024-12-12 15:51:27 +00:00
import 'package:path_provider/path_provider.dart';
2024-12-01 12:08:04 +00:00
import 'package:popover/popover.dart';
import 'package:provider/provider.dart';
2024-12-12 15:51:27 +00:00
import 'package:qr_flutter/qr_flutter.dart';
2024-11-09 03:16:14 +00:00
import 'package:relative_time/relative_time.dart';
2024-12-12 15:51:27 +00:00
import 'package:responsive_framework/responsive_framework.dart';
import 'package:screenshot/screenshot.dart';
2024-12-08 07:30:55 +00:00
import 'package:share_plus/share_plus.dart';
2024-11-09 03:16:14 +00:00
import 'package:styled_widget/styled_widget.dart';
2024-11-28 14:04:38 +00:00
import 'package:surface/providers/sn_network.dart';
import 'package:surface/providers/userinfo.dart';
2024-11-08 17:11:56 +00:00
import 'package:surface/types/post.dart';
import 'package:surface/types/reaction.dart';
2024-11-09 03:16:14 +00:00
import 'package:surface/widgets/account/account_image.dart';
2024-11-09 04:04:03 +00:00
import 'package:surface/widgets/attachment/attachment_list.dart';
2024-11-28 14:04:38 +00:00
import 'package:surface/widgets/dialog.dart';
2024-11-09 03:16:14 +00:00
import 'package:surface/widgets/markdown_content.dart';
import 'package:gap/gap.dart';
2024-11-11 12:06:00 +00:00
import 'package:surface/widgets/post/post_comment_list.dart';
2024-12-05 16:21:48 +00:00
import 'package:surface/widgets/post/post_meta_editor.dart';
2024-11-12 12:47:40 +00:00
import 'package:surface/widgets/post/post_reaction.dart';
2024-12-01 12:08:04 +00:00
import 'package:surface/widgets/post/publisher_popover.dart';
2024-12-07 09:43:44 +00:00
import 'package:surface/widgets/universal_image.dart';
2024-11-08 17:11:56 +00:00
class PostItem extends StatelessWidget {
final SnPost data;
2024-11-12 12:47:40 +00:00
final bool showReactions;
2024-11-11 12:06:00 +00:00
final bool showComments;
2024-11-26 13:56:01 +00:00
final bool showMenu;
2024-12-07 09:43:44 +00:00
final bool showFullPost;
final double? maxWidth;
2024-11-12 12:47:40 +00:00
final Function(SnPost data)? onChanged;
2024-11-28 14:04:38 +00:00
final Function()? onDeleted;
2024-12-07 09:43:44 +00:00
2024-11-11 12:06:00 +00:00
const PostItem({
super.key,
required this.data,
2024-11-12 12:47:40 +00:00
this.showReactions = true,
2024-11-11 12:06:00 +00:00
this.showComments = true,
2024-11-26 13:56:01 +00:00
this.showMenu = true,
2024-12-07 09:43:44 +00:00
this.showFullPost = false,
this.maxWidth,
2024-11-12 12:47:40 +00:00
this.onChanged,
2024-11-28 14:04:38 +00:00
this.onDeleted,
2024-11-11 12:06:00 +00:00
});
2024-11-08 17:11:56 +00:00
2024-11-12 12:47:40 +00:00
void _onChanged(SnPost data) {
if (onChanged != null) onChanged!(data);
}
void _doShare(BuildContext context) {
final box = context.findRenderObject() as RenderBox?;
final url = 'https://solsynth.dev/posts/${data.id}';
if (!kIsWeb && (Platform.isAndroid || Platform.isIOS)) {
Share.shareUri(Uri.parse(url), sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size);
} else {
Share.share(url, sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size);
}
}
void _doShareViaPicture(BuildContext context) async {
final box = context.findRenderObject() as RenderBox?;
context.showSnackbar('postSharingViaPicture'.tr());
final controller = ScreenshotController();
final capturedImage = await controller.captureFromLongWidget(
InheritedTheme.captureAll(
context,
MediaQuery(
data: MediaQuery.of(context),
child: Material(
child: MultiProvider(
providers: [
Provider<SnNetworkProvider>(create: (_) => context.read()),
],
child: ResponsiveBreakpoints.builder(
breakpoints: ResponsiveBreakpoints.of(context).breakpoints,
child: PostShareImageWidget(data: data),
),
),
),
),
),
pixelRatio: 3,
context: context,
);
if (kIsWeb) return;
final directory = await getTemporaryDirectory();
2024-12-13 16:14:23 +00:00
final imageFile = await File(
'${directory.path}/sn-share-via-image-${DateTime.now().millisecondsSinceEpoch}.png',
).create();
2024-12-13 16:14:23 +00:00
await imageFile.writeAsBytes(capturedImage);
2024-12-13 16:14:23 +00:00
if(!kIsWeb && (Platform.isAndroid || Platform.isIOS)) {
await Share.shareXFiles(
[XFile(imageFile.path)],
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
);
} else {
await FileSaver.instance.saveFile(name: 'Solar Network Post #${data.id}', file: imageFile);
}
2024-12-13 16:14:23 +00:00
await imageFile.delete();
}
2024-11-08 17:11:56 +00:00
@override
Widget build(BuildContext context) {
2024-12-07 09:43:44 +00:00
final sn = context.read<SnNetworkProvider>();
2024-12-12 15:51:27 +00:00
final ua = context.read<UserProvider>();
final isAuthor = ua.isAuthorized && data.publisher.accountId == ua.user?.id;
2024-12-12 15:51:27 +00:00
2024-12-07 09:43:44 +00:00
// Article headline preview
if (!showFullPost && data.type == 'article') {
return Container(
constraints: BoxConstraints(maxWidth: maxWidth ?? double.infinity),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
_PostContentHeader(
data: data,
2024-12-12 15:51:27 +00:00
isAuthor: isAuthor,
onShare: () => _doShare(context),
onShareImage: () => _doShareViaPicture(context),
2024-12-07 09:43:44 +00:00
onDeleted: () {
if (onDeleted != null) {}
},
2024-12-08 09:03:10 +00:00
).padding(horizontal: 12, top: 8, bottom: 8),
2024-12-07 09:43:44 +00:00
Container(
width: double.infinity,
margin: const EdgeInsets.only(bottom: 4, left: 12, right: 12),
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(Radius.circular(8)),
border: Border.all(
color: Theme.of(context).dividerColor,
width: 1,
),
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (data.preload?.thumbnail != null)
AspectRatio(
aspectRatio: 16 / 9,
child: ClipRRect(
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(8),
topRight: Radius.circular(8),
),
child: AutoResizeUniversalImage(
sn.getAttachmentUrl(data.preload!.thumbnail!.rid),
fit: BoxFit.cover,
),
),
),
const Gap(8),
_PostHeadline(data: data).padding(horizontal: 14),
const Gap(4),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (data.visibility > 0) _PostVisibilityHint(data: data),
_PostTruncatedHint(data: data),
],
).padding(horizontal: 12),
const Gap(8),
],
),
),
Text('postArticle').tr().fontSize(13).opacity(0.75).padding(horizontal: 24, bottom: 8),
if (data.tags.isNotEmpty) _PostTagsList(data: data).padding(horizontal: 16, bottom: 6),
_PostBottomAction(
data: data,
showComments: showComments,
showReactions: showReactions,
onShare: () => _doShare(context),
onShareImage: () => _doShareViaPicture(context),
2024-12-07 09:43:44 +00:00
onChanged: _onChanged,
).padding(left: 8, right: 14),
],
),
2024-12-08 09:03:10 +00:00
).center();
2024-12-07 09:43:44 +00:00
}
2024-11-08 17:11:56 +00:00
return Column(
crossAxisAlignment: CrossAxisAlignment.center,
2024-11-09 03:16:14 +00:00
children: [
Container(
constraints: BoxConstraints(maxWidth: maxWidth ?? double.infinity),
child: Column(
2024-11-24 16:51:34 +00:00
crossAxisAlignment: CrossAxisAlignment.start,
children: [
2024-11-28 14:04:38 +00:00
_PostContentHeader(
2024-12-13 17:36:23 +00:00
isAuthor: isAuthor,
2024-11-28 14:04:38 +00:00
data: data,
showMenu: showMenu,
onShare: () => _doShare(context),
onShareImage: () => _doShareViaPicture(context),
2024-11-28 14:04:38 +00:00
onDeleted: () {
if (onDeleted != null) onDeleted!();
},
).padding(horizontal: 12, vertical: 8),
2024-12-07 09:43:44 +00:00
if (data.body['title'] != null || data.body['description'] != null)
_PostHeadline(
data: data,
isEnlarge: data.type == 'article' && showFullPost,
).padding(horizontal: 16, bottom: 8),
_PostContentBody(
data: data,
isEnlarge: data.type == 'article' && showFullPost,
).padding(horizontal: 16, bottom: 6),
if (data.repostTo != null)
_PostQuoteContent(child: data.repostTo!).padding(
horizontal: 12,
2024-12-07 14:27:07 +00:00
bottom: data.preload?.attachments?.isNotEmpty ?? false ? 12 : 0,
),
2024-12-05 16:21:48 +00:00
if (data.visibility > 0)
_PostVisibilityHint(data: data).padding(
horizontal: 16,
vertical: 4,
),
2024-11-24 16:51:34 +00:00
if (data.body['content_truncated'] == true)
_PostTruncatedHint(data: data).padding(
horizontal: 16,
vertical: 4,
),
2024-12-07 09:43:44 +00:00
if (data.tags.isNotEmpty) _PostTagsList(data: data).padding(horizontal: 16, bottom: 6),
],
2024-11-13 14:05:40 +00:00
),
),
if ((data.preload?.attachments?.isNotEmpty ?? false) && data.type != 'article')
2024-11-11 12:06:00 +00:00
AttachmentList(
data: data.preload!.attachments!,
bordered: true,
maxHeight: 560,
listPadding: const EdgeInsets.symmetric(horizontal: 12),
),
Container(
constraints: BoxConstraints(maxWidth: maxWidth ?? double.infinity),
child: Column(
children: [
_PostBottomAction(
data: data,
showComments: showComments,
showReactions: showReactions,
onShare: () => _doShare(context),
onShareImage: () => _doShareViaPicture(context),
onChanged: _onChanged,
2024-11-24 16:51:34 +00:00
).padding(left: 8, right: 14),
],
),
),
],
);
}
}
class PostShareImageWidget extends StatelessWidget {
const PostShareImageWidget({
2024-12-12 15:51:27 +00:00
super.key,
required this.data,
});
final SnPost data;
@override
Widget build(BuildContext context) {
2024-12-13 15:49:47 +00:00
final sn = context.read<SnNetworkProvider>();
2024-12-12 15:51:27 +00:00
return SizedBox(
width: 480,
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
2024-12-13 15:49:47 +00:00
if (data.preload?.thumbnail != null)
AspectRatio(
aspectRatio: 16 / 9,
child: ClipRRect(
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(8),
topRight: Radius.circular(8),
),
child: AutoResizeUniversalImage(
sn.getAttachmentUrl(data.preload!.thumbnail!.rid),
fit: BoxFit.cover,
),
),
).padding(bottom: 8),
2024-12-12 15:51:27 +00:00
_PostContentHeader(
2024-12-13 17:36:23 +00:00
isAuthor: false,
2024-12-12 15:51:27 +00:00
data: data,
onDeleted: () {},
onShare: () {},
onShareImage: () {},
2024-12-12 15:51:27 +00:00
showMenu: false,
isRelativeDate: false,
).padding(horizontal: 16, bottom: 8),
_PostHeadline(
data: data,
isEnlarge: data.type == 'article',
).padding(horizontal: 16, bottom: 8),
_PostContentBody(
data: data,
isEnlarge: data.type == 'article',
).padding(horizontal: 16, bottom: 8),
if (data.repostTo != null)
_PostQuoteContent(
child: data.repostTo!,
isRelativeDate: false,
isFlatted: true,
).padding(horizontal: 16, bottom: 8),
if (data.type != 'article' && (data.preload?.attachments?.isNotEmpty ?? false))
AttachmentList(
data: data.preload!.attachments!,
isFlatted: true,
).padding(horizontal: 16, bottom: 8),
Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (data.visibility > 0) _PostVisibilityHint(data: data),
if (data.body['content_truncated'] == true) _PostTruncatedHint(data: data),
],
).padding(horizontal: 16),
2024-12-12 15:51:27 +00:00
_PostBottomAction(
data: data,
showComments: true,
showReactions: true,
onShare: () {},
onShareImage: () {},
2024-12-12 15:51:27 +00:00
onChanged: (SnPost data) {},
).padding(left: 8, right: 14),
const Divider(height: 1),
const Gap(12),
SizedBox(
height: 100,
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'${data.aliasPrefix} / ${data.alias ?? '#${data.id}'}',
style: GoogleFonts.robotoMono(fontSize: 17),
),
const Gap(2),
Text(
switch (data.type) {
'article' => 'postArticle'.tr(),
_ => 'postStory'.tr(),
},
style: GoogleFonts.robotoMono(fontSize: 12),
),
],
),
),
if(data.body['content_truncated'] == true)
Text(
'postImageShareReadMore'.tr(),
style: GoogleFonts.robotoMono(fontSize: 11),
),
2024-12-12 15:51:27 +00:00
Text(
'postImageShareAds',
style: GoogleFonts.robotoMono(fontSize: 13),
).tr(),
],
),
),
QrImageView(
padding: EdgeInsets.zero,
data: 'https://solsynth.dev/posts/${data.id}',
errorCorrectionLevel: QrErrorCorrectLevel.H,
2024-12-12 15:51:27 +00:00
version: QrVersions.auto,
size: 100,
gapless: true,
embeddedImage: AssetImage('assets/icon/icon-light-radius.png'),
embeddedImageStyle: QrEmbeddedImageStyle(
size: Size(28, 28),
2024-12-12 15:51:27 +00:00
),
eyeStyle: QrEyeStyle(
eyeShape: QrEyeShape.square,
2024-12-12 15:51:27 +00:00
color: Theme.of(context).colorScheme.onSurface,
),
dataModuleStyle: QrDataModuleStyle(
dataModuleShape: QrDataModuleShape.square,
color: Theme.of(context).colorScheme.onSurface,
),
)
],
),
).padding(left: 16, right: 32, vertical: 8),
],
).padding(vertical: 16),
);
}
}
class _PostBottomAction extends StatelessWidget {
final SnPost data;
2024-11-11 12:06:00 +00:00
final bool showComments;
2024-11-12 12:47:40 +00:00
final bool showReactions;
final Function(SnPost data) onChanged;
final Function() onShare, onShareImage;
2024-12-07 09:43:44 +00:00
2024-11-12 12:47:40 +00:00
const _PostBottomAction({
required this.data,
required this.showComments,
required this.showReactions,
required this.onChanged,
required this.onShare,
required this.onShareImage,
2024-11-12 12:47:40 +00:00
});
@override
Widget build(BuildContext context) {
final iconColor = Theme.of(context).colorScheme.onSurface.withAlpha(
(255 * 0.8).round(),
);
final String? mostTypicalReaction = data.metric.reactionList.isNotEmpty
2024-12-07 09:43:44 +00:00
? data.metric.reactionList.entries.reduce((a, b) => a.value > b.value ? a : b).key
: null;
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
2024-11-26 13:56:01 +00:00
if (showReactions || showComments)
Row(
children: [
if (showReactions)
InkWell(
child: Row(
children: [
2024-12-07 09:43:44 +00:00
if (mostTypicalReaction == null || kTemplateReactions[mostTypicalReaction] == null)
Icon(Symbols.add_reaction, size: 20, color: iconColor)
else
Text(
kTemplateReactions[mostTypicalReaction]!.icon,
style: TextStyle(
fontSize: 16,
height: 1.2,
letterSpacing: 0,
),
),
2024-11-26 13:56:01 +00:00
const Gap(8),
2024-12-07 09:43:44 +00:00
if (data.totalUpvote > 0 && data.totalUpvote >= data.totalDownvote)
2024-11-26 13:56:01 +00:00
Text('postReactionUpvote').plural(
data.totalUpvote,
)
else if (data.totalDownvote > 0)
Text('postReactionDownvote').plural(
data.totalDownvote,
)
else
Text('postReact').tr(),
],
).padding(horizontal: 8, vertical: 8),
onTap: () {
showModalBottomSheet(
context: context,
builder: (context) => PostReactionPopup(
data: data,
onChanged: (value, attr, delta) {
onChanged(data.copyWith(
2024-12-07 09:43:44 +00:00
totalUpvote: attr == 1 ? data.totalUpvote + delta : data.totalUpvote,
totalDownvote: attr == 2 ? data.totalDownvote + delta : data.totalDownvote,
2024-11-26 13:56:01 +00:00
metric: data.metric.copyWith(reactionList: value),
));
},
),
);
},
),
if (showComments)
InkWell(
child: Row(
children: [
Icon(Symbols.comment, size: 20, color: iconColor),
const Gap(8),
Text('postComments').plural(data.metric.replyCount),
],
).padding(horizontal: 8, vertical: 8),
onTap: () {
showModalBottomSheet(
context: context,
useRootNavigator: true,
builder: (context) => PostCommentListPopup(
postId: data.id,
commentCount: data.metric.replyCount,
),
);
},
),
].expand((ele) => [ele, const Gap(8)]).toList()
..removeLast(),
),
InkWell(
onTap: onShare,
onLongPress: onShareImage,
2024-11-11 12:06:00 +00:00
child: Icon(
Symbols.share,
size: 20,
color: iconColor,
).padding(horizontal: 8, vertical: 8),
),
2024-11-09 03:16:14 +00:00
],
2024-11-08 17:11:56 +00:00
);
}
}
2024-11-24 16:51:34 +00:00
class _PostHeadline extends StatelessWidget {
final SnPost data;
final bool isEnlarge;
2024-12-07 09:43:44 +00:00
const _PostHeadline({
super.key,
required this.data,
this.isEnlarge = false,
});
2024-11-24 16:51:34 +00:00
@override
Widget build(BuildContext context) {
if (isEnlarge) {
final sn = context.read<SnNetworkProvider>();
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (data.preload?.thumbnail != null)
Container(
margin: const EdgeInsets.only(bottom: 8),
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(Radius.circular(8)),
border: Border.all(
color: Theme.of(context).dividerColor,
width: 1,
),
),
child: AspectRatio(
aspectRatio: 16 / 9,
child: ClipRRect(
borderRadius: const BorderRadius.all(Radius.circular(8)),
child: AutoResizeUniversalImage(
sn.getAttachmentUrl(data.preload!.thumbnail!.rid),
fit: BoxFit.cover,
),
),
),
),
if (data.body['title'] != null)
Text(
data.body['title'],
style: Theme.of(context).textTheme.titleMedium,
textScaler: TextScaler.linear(1.4),
),
if (data.body['description'] != null)
Text(
data.body['description'],
style: Theme.of(context).textTheme.bodyMedium,
textScaler: TextScaler.linear(1.1),
),
if (data.body['description'] != null) const Gap(8) else const Gap(4),
Row(
children: [
Text(
'articleWrittenAt'.tr(
args: [DateFormat('y/M/d HH:mm').format(data.createdAt)],
),
style: TextStyle(fontSize: 13),
),
const Gap(8),
if (data.editedAt != null)
Text(
'articleEditedAt'.tr(
args: [DateFormat('y/M/d HH:mm').format(data.editedAt!)],
),
style: TextStyle(fontSize: 13),
),
],
).opacity(0.75),
const Gap(8),
const Divider(height: 1, thickness: 1),
const Gap(8),
],
);
}
2024-11-24 16:51:34 +00:00
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (data.body['title'] != null)
Text(
data.body['title'],
style: Theme.of(context).textTheme.titleMedium,
),
if (data.body['description'] != null)
Text(
data.body['description'],
style: Theme.of(context).textTheme.bodyMedium,
),
],
);
}
}
2024-11-09 03:16:14 +00:00
class _PostContentHeader extends StatelessWidget {
final SnPost data;
2024-12-12 15:51:27 +00:00
final bool isAuthor;
2024-11-13 14:05:40 +00:00
final bool isCompact;
2024-12-12 15:51:27 +00:00
final bool isRelativeDate;
2024-11-26 13:56:01 +00:00
final bool showMenu;
2024-11-28 14:04:38 +00:00
final Function onDeleted;
final Function() onShare, onShareImage;
2024-12-07 09:43:44 +00:00
2024-11-13 14:05:40 +00:00
const _PostContentHeader({
required this.data,
2024-12-13 17:36:23 +00:00
required this.isAuthor,
2024-11-13 14:05:40 +00:00
this.isCompact = false,
2024-12-12 15:51:27 +00:00
this.isRelativeDate = true,
2024-11-26 13:56:01 +00:00
this.showMenu = true,
2024-11-28 14:04:38 +00:00
required this.onDeleted,
required this.onShare,
required this.onShareImage,
2024-11-13 14:05:40 +00:00
});
2024-11-09 03:16:14 +00:00
2024-11-28 14:04:38 +00:00
Future<void> _deletePost(BuildContext context) async {
final confirm = await context.showConfirmDialog(
'postDelete'.tr(args: ['#${data.id}']),
'postDeleteDescription'.tr(),
);
if (!confirm) return;
if (!context.mounted) return;
try {
final sn = context.read<SnNetworkProvider>();
await sn.client.delete('/cgi/co/posts/${data.id}', queryParameters: {
'publisherId': data.publisherId,
});
if (!context.mounted) return;
context.showSnackbar('postDeleted'.tr(args: ['#${data.id}']));
} catch (err) {
if (!context.mounted) return;
context.showErrorDialog(err);
}
}
2024-11-09 03:16:14 +00:00
@override
Widget build(BuildContext context) {
return Row(
children: [
2024-12-01 12:08:04 +00:00
GestureDetector(
child: AccountImage(
content: data.publisher.avatar,
radius: isCompact ? 12 : 20,
),
onTap: () {
showPopover(
2024-12-01 14:30:32 +00:00
backgroundColor: Theme.of(context).colorScheme.surface,
2024-12-01 12:08:04 +00:00
context: context,
transition: PopoverTransition.other,
bodyBuilder: (context) => SizedBox(
width: math.min(400, MediaQuery.of(context).size.width - 10),
2024-12-01 12:08:04 +00:00
child: PublisherPopoverCard(
data: data.publisher,
2024-12-01 14:30:32 +00:00
),
2024-12-01 12:08:04 +00:00
),
direction: PopoverDirection.bottom,
arrowHeight: 5,
arrowWidth: 15,
arrowDxOffset: -190,
);
},
2024-11-13 14:05:40 +00:00
),
Gap(isCompact ? 8 : 12),
if (isCompact)
Row(
2024-11-09 03:16:14 +00:00
children: [
Text(data.publisher.nick).bold(),
2024-11-13 14:05:40 +00:00
const Gap(4),
2024-11-09 03:16:14 +00:00
Row(
children: [
Text('@${data.publisher.name}').fontSize(13),
const Gap(4),
2024-12-12 15:51:27 +00:00
Text(
isRelativeDate
? RelativeTime(context).format(data.publishedAt ?? data.createdAt)
: DateFormat('y/M/d HH:mm').format(data.publishedAt ?? data.createdAt),
).fontSize(13),
2024-11-09 03:16:14 +00:00
],
).opacity(0.8),
],
2024-11-13 14:05:40 +00:00
)
else
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(data.publisher.nick).bold(),
Row(
children: [
Text('@${data.publisher.name}').fontSize(13),
const Gap(4),
2024-12-12 15:51:27 +00:00
Text(
isRelativeDate
? RelativeTime(context).format(data.publishedAt ?? data.createdAt)
: DateFormat('y/M/d HH:mm').format(data.publishedAt ?? data.createdAt),
).fontSize(13),
2024-11-13 14:05:40 +00:00
],
).opacity(0.8),
],
),
2024-11-09 03:16:14 +00:00
),
2024-11-26 13:56:01 +00:00
if (showMenu)
2024-11-13 14:05:40 +00:00
PopupMenuButton(
icon: const Icon(Symbols.more_horiz),
style: const ButtonStyle(
visualDensity: VisualDensity(horizontal: -4, vertical: -4),
),
itemBuilder: (BuildContext context) => <PopupMenuEntry>[
if (isAuthor)
PopupMenuItem(
child: Row(
children: [
const Icon(Symbols.edit),
const Gap(16),
Text('edit').tr(),
],
),
onTap: () {
GoRouter.of(context).pushNamed(
'postEditor',
pathParameters: {'mode': data.typePlural},
queryParameters: {'editing': data.id.toString()},
);
},
),
if (isAuthor)
PopupMenuItem(
child: Row(
children: [
const Icon(Symbols.delete),
const Gap(16),
Text('delete').tr(),
],
),
2024-11-28 14:04:38 +00:00
onTap: () => _deletePost(context),
2024-11-13 14:05:40 +00:00
),
if (isAuthor) const PopupMenuDivider(),
PopupMenuItem(
child: Row(
children: [
2024-11-13 14:05:40 +00:00
const Icon(Symbols.reply),
const Gap(16),
2024-11-18 14:52:22 +00:00
Text('replyPost').tr(),
],
),
onTap: () {
GoRouter.of(context).pushNamed(
'postEditor',
pathParameters: {'mode': data.typePlural},
2024-11-13 14:05:40 +00:00
queryParameters: {'replying': data.id.toString()},
);
},
),
PopupMenuItem(
child: Row(
children: [
2024-11-13 14:05:40 +00:00
const Icon(Symbols.forward),
const Gap(16),
2024-11-13 14:05:40 +00:00
Text('repost').tr(),
],
),
2024-11-13 14:05:40 +00:00
onTap: () {
GoRouter.of(context).pushNamed(
'postEditor',
pathParameters: {'mode': data.typePlural},
queryParameters: {'reposting': data.id.toString()},
);
},
),
2024-11-13 14:05:40 +00:00
const PopupMenuDivider(),
PopupMenuItem(
onTap: onShare,
child: Row(
children: [
const Icon(Symbols.share),
const Gap(16),
Text('postShare').tr(),
],
),
),
PopupMenuItem(
onTap: onShareImage,
child: Row(
children: [
const Icon(Symbols.share_reviews),
const Gap(16),
Text('postShareImage').tr(),
],
),
),
const PopupMenuDivider(),
2024-11-13 14:05:40 +00:00
PopupMenuItem(
child: Row(
children: [
const Icon(Symbols.flag),
const Gap(16),
Text('report').tr(),
],
),
2024-12-07 16:53:22 +00:00
onTap: () {
showDialog(
context: context,
builder: (context) => _PostAbuseReportDialog(
data: data,
),
).then((value) {
if (value == true && context.mounted) {
2024-12-07 17:01:20 +00:00
context.showSnackbar('abuseReportSubmitted'.tr());
2024-12-07 16:53:22 +00:00
}
});
},
2024-11-10 10:37:34 +00:00
),
2024-11-13 14:05:40 +00:00
],
),
2024-11-09 03:16:14 +00:00
],
);
2024-11-09 03:16:14 +00:00
}
}
class _PostContentBody extends StatelessWidget {
final SnPost data;
final bool isEnlarge;
2024-12-07 09:43:44 +00:00
const _PostContentBody({
required this.data,
this.isEnlarge = false,
});
2024-11-08 17:11:56 +00:00
@override
Widget build(BuildContext context) {
if (data.body['content'] == null) return const SizedBox.shrink();
return MarkdownTextContent(
textScaler: isEnlarge ? TextScaler.linear(1.1) : null,
content: data.body['content'],
attachments: data.preload?.attachments,
);
2024-11-08 17:11:56 +00:00
}
}
2024-11-13 14:05:40 +00:00
class _PostQuoteContent extends StatelessWidget {
final SnPost child;
2024-12-12 15:51:27 +00:00
final bool isRelativeDate;
final bool isFlatted;
2024-12-07 09:43:44 +00:00
2024-12-12 15:51:27 +00:00
const _PostQuoteContent({
super.key,
this.isRelativeDate = true,
this.isFlatted = false,
required this.child,
});
2024-11-13 14:05:40 +00:00
@override
Widget build(BuildContext context) {
2024-12-07 14:27:07 +00:00
return GestureDetector(
child: Container(
decoration: BoxDecoration(
borderRadius: const BorderRadius.all(Radius.circular(8)),
border: Border.all(
color: Theme.of(context).dividerColor,
width: 1,
),
),
2024-12-11 16:48:03 +00:00
padding: const EdgeInsets.only(top: 8),
2024-12-07 14:27:07 +00:00
child: Column(
children: [
2024-12-11 16:48:03 +00:00
Column(
children: [
_PostContentHeader(
2024-12-13 17:36:23 +00:00
isAuthor: false,
2024-12-11 16:48:03 +00:00
data: child,
isCompact: true,
2024-12-12 15:51:27 +00:00
isRelativeDate: isRelativeDate,
2024-12-11 16:48:03 +00:00
showMenu: false,
onShare: () {},
onShareImage: () {},
2024-12-11 16:48:03 +00:00
onDeleted: () {},
).padding(bottom: 4),
_PostContentBody(data: child),
if (child.visibility > 0) _PostVisibilityHint(data: child).padding(top: 4),
],
).padding(horizontal: 16),
if (child.type != 'article' && (child.preload?.attachments?.isNotEmpty ?? false))
2024-12-11 16:48:03 +00:00
ClipRRect(
borderRadius: const BorderRadius.only(
bottomLeft: Radius.circular(8),
bottomRight: Radius.circular(8),
),
child: AttachmentList(
data: child.preload!.attachments!,
2024-12-12 15:51:27 +00:00
isFlatted: isFlatted,
2024-12-11 16:48:03 +00:00
listPadding: const EdgeInsets.symmetric(horizontal: 12),
),
).padding(
top: 8,
bottom: (child.preload?.attachments?.length ?? 0) > 1 ? 12 : 0,
2024-12-12 15:51:27 +00:00
)
else
const Gap(8),
2024-12-07 14:27:07 +00:00
],
2024-11-13 14:05:40 +00:00
),
),
2024-12-07 14:27:07 +00:00
onTap: () {
GoRouter.of(context).pushNamed('postDetail', pathParameters: {
'slug': child.id.toString(),
});
},
2024-11-13 14:05:40 +00:00
);
}
}
2024-11-24 16:51:34 +00:00
2024-11-26 16:06:11 +00:00
class _PostTagsList extends StatelessWidget {
final SnPost data;
2024-12-07 09:43:44 +00:00
2024-11-26 16:06:11 +00:00
const _PostTagsList({super.key, required this.data});
@override
Widget build(BuildContext context) {
return Wrap(
spacing: 4,
runSpacing: 4,
children: data.tags
.map(
(ele) => InkWell(
child: Text(
'#${ele.alias}',
style: TextStyle(
decoration: TextDecoration.underline,
),
).fontSize(13),
onTap: () {},
),
)
.toList(),
).opacity(0.8);
}
}
2024-12-05 16:21:48 +00:00
class _PostVisibilityHint extends StatelessWidget {
final SnPost data;
2024-12-07 09:43:44 +00:00
2024-12-05 16:21:48 +00:00
const _PostVisibilityHint({super.key, required this.data});
static const List<IconData> kVisibilityIcons = [
Symbols.public,
Symbols.group,
Symbols.person_check,
Symbols.person_remove,
Symbols.lock,
];
@override
Widget build(BuildContext context) {
return Row(
children: [
Icon(kVisibilityIcons[data.visibility], size: 20),
const Gap(4),
Text(kPostVisibilityLevel[data.visibility] ?? 'postVisibilityAll').tr(),
],
).opacity(0.75);
}
}
2024-11-24 16:51:34 +00:00
class _PostTruncatedHint extends StatelessWidget {
final SnPost data;
2024-12-07 09:43:44 +00:00
2024-11-24 16:51:34 +00:00
const _PostTruncatedHint({super.key, required this.data});
static const int kHumanReadSpeed = 238;
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
if (data.body['content_length'] != null)
Row(
children: [
const Icon(Symbols.timer, size: 20),
const Gap(4),
Text('postReadEstimate').tr(args: [
'${Duration(
seconds: (data.body['content_length'] as num).toDouble() * 60 ~/ kHumanReadSpeed,
).inSeconds}s',
]),
],
).padding(right: 8),
if (data.body['content_length'] != null)
Row(
children: [
const Icon(Symbols.height, size: 20),
const Gap(4),
Text(
'postTotalLength'.plural(data.body['content_length']),
)
],
),
],
).opacity(0.75),
);
2024-11-24 16:51:34 +00:00
}
}
2024-12-07 16:53:22 +00:00
class _PostAbuseReportDialog extends StatefulWidget {
final SnPost data;
const _PostAbuseReportDialog({super.key, required this.data});
@override
State<_PostAbuseReportDialog> createState() => _PostAbuseReportDialogState();
}
class _PostAbuseReportDialogState extends State<_PostAbuseReportDialog> {
bool _isBusy = false;
final _reasonController = TextEditingController();
@override
dispose() {
_reasonController.dispose();
super.dispose();
}
Future<void> _performAction() async {
setState(() => _isBusy = true);
try {
final sn = context.read<SnNetworkProvider>();
2024-12-07 17:12:45 +00:00
await sn.client.post(
2024-12-07 16:53:22 +00:00
'/cgi/id/reports/abuse',
data: {
'resource': 'post:${widget.data.id}',
'reason': _reasonController.text,
},
);
if (!mounted) return;
Navigator.pop(context, true);
} catch (err) {
if (!mounted) return;
context.showErrorDialog(err);
} finally {
setState(() => _isBusy = false);
}
}
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text('postAbuseReport'.tr()),
content: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text('postAbuseReportDescription'.tr()),
const Gap(12),
TextField(
controller: _reasonController,
maxLength: null,
decoration: InputDecoration(
border: const UnderlineInputBorder(),
2024-12-07 17:01:20 +00:00
labelText: 'abuseReportReason'.tr(),
2024-12-07 16:53:22 +00:00
),
onTapOutside: (_) => FocusManager.instance.primaryFocus?.unfocus(),
),
],
),
actions: [
TextButton(
onPressed: _isBusy ? null : () => Navigator.pop(context),
child: Text('dialogDismiss').tr(),
),
TextButton(
onPressed: _isBusy ? null : _performAction,
child: Text('submit').tr(),
),
],
);
}
}