2020-08-27 23:27:27 +02:00
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
2020-09-16 22:53:04 +02:00
|
|
|
import 'package:flutter/cupertino.dart';
|
2020-08-27 23:27:27 +02:00
|
|
|
import 'package:flutter/gestures.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2020-09-17 15:25:47 +02:00
|
|
|
import 'package:flutter_hooks/flutter_hooks.dart';
|
2020-08-29 21:01:01 +02:00
|
|
|
import 'package:intl/intl.dart';
|
2021-04-05 20:14:39 +02:00
|
|
|
import 'package:lemmy_api_client/v3.dart';
|
2020-09-09 21:23:48 +02:00
|
|
|
import 'package:url_launcher/url_launcher.dart' as ul;
|
2020-08-27 23:27:27 +02:00
|
|
|
|
2020-09-17 16:46:40 +02:00
|
|
|
import '../hooks/delayed_loading.dart';
|
2020-09-18 21:56:42 +02:00
|
|
|
import '../hooks/logged_in_action.dart';
|
2021-03-01 14:21:45 +01:00
|
|
|
import '../l10n/l10n.dart';
|
2020-09-10 13:11:16 +02:00
|
|
|
import '../pages/full_post.dart';
|
2020-09-02 23:05:34 +02:00
|
|
|
import '../url_launcher.dart';
|
2021-01-27 20:53:09 +01:00
|
|
|
import '../util/cleanup_url.dart';
|
2021-01-02 01:13:13 +01:00
|
|
|
import '../util/extensions/api.dart';
|
2021-02-12 17:53:51 +01:00
|
|
|
import '../util/extensions/datetime.dart';
|
2020-09-09 17:41:54 +02:00
|
|
|
import '../util/goto.dart';
|
2020-12-04 13:57:37 +01:00
|
|
|
import '../util/more_icon.dart';
|
2021-03-18 19:24:29 +01:00
|
|
|
import '../util/share.dart';
|
2021-02-18 09:19:00 +01:00
|
|
|
import 'avatar.dart';
|
2020-09-09 21:23:48 +02:00
|
|
|
import 'bottom_modal.dart';
|
2020-09-11 20:38:50 +02:00
|
|
|
import 'fullscreenable_image.dart';
|
2020-10-26 18:47:49 +01:00
|
|
|
import 'info_table_popup.dart';
|
2020-08-31 12:22:29 +02:00
|
|
|
import 'markdown_text.dart';
|
2020-09-17 22:50:18 +02:00
|
|
|
import 'save_post_button.dart';
|
2020-08-30 16:49:59 +02:00
|
|
|
|
2020-08-29 21:01:01 +02:00
|
|
|
enum MediaType {
|
|
|
|
image,
|
|
|
|
gallery,
|
|
|
|
video,
|
|
|
|
other,
|
2020-09-28 23:28:41 +02:00
|
|
|
none,
|
2020-08-29 21:01:01 +02:00
|
|
|
}
|
|
|
|
|
2021-04-09 00:11:44 +02:00
|
|
|
MediaType whatType(String? url) {
|
2020-09-28 23:28:41 +02:00
|
|
|
if (url == null || url.isEmpty) return MediaType.none;
|
2020-08-29 21:01:01 +02:00
|
|
|
|
2020-08-30 19:29:12 +02:00
|
|
|
// TODO: make detection more nuanced
|
2020-09-11 19:21:59 +02:00
|
|
|
if (url.endsWith('.jpg') ||
|
|
|
|
url.endsWith('.jpeg') ||
|
|
|
|
url.endsWith('.png') ||
|
|
|
|
url.endsWith('.gif') ||
|
|
|
|
url.endsWith('.webp') ||
|
|
|
|
url.endsWith('.bmp') ||
|
|
|
|
url.endsWith('.wbpm')) {
|
2020-08-29 21:01:01 +02:00
|
|
|
return MediaType.image;
|
|
|
|
}
|
|
|
|
return MediaType.other;
|
|
|
|
}
|
|
|
|
|
2020-09-30 19:05:00 +02:00
|
|
|
/// A post overview card
|
2021-01-24 20:01:55 +01:00
|
|
|
class PostWidget extends HookWidget {
|
2020-08-27 23:27:27 +02:00
|
|
|
final PostView post;
|
2020-12-31 14:58:23 +01:00
|
|
|
final String instanceHost;
|
2020-09-02 00:05:42 +02:00
|
|
|
final bool fullPost;
|
2020-08-27 23:27:27 +02:00
|
|
|
|
2021-01-24 20:01:55 +01:00
|
|
|
PostWidget(this.post, {this.fullPost = false})
|
|
|
|
: instanceHost = post.instanceHost;
|
2020-08-27 23:27:27 +02:00
|
|
|
|
2020-08-28 13:30:42 +02:00
|
|
|
// == ACTIONS ==
|
|
|
|
|
2020-09-09 21:23:48 +02:00
|
|
|
static void showMoreMenu(BuildContext context, PostView post) {
|
2021-02-09 15:12:13 +01:00
|
|
|
showBottomModal(
|
2020-09-09 21:23:48 +02:00
|
|
|
context: context,
|
2021-02-09 15:12:13 +01:00
|
|
|
builder: (context) => Column(
|
|
|
|
children: [
|
|
|
|
ListTile(
|
|
|
|
leading: const Icon(Icons.open_in_browser),
|
|
|
|
title: const Text('Open in browser'),
|
|
|
|
onTap: () async => await ul.canLaunch(post.post.apId)
|
|
|
|
? ul.launch(post.post.apId)
|
2021-03-10 08:34:30 +01:00
|
|
|
: ScaffoldMessenger.of(context).showSnackBar(
|
2021-02-09 15:12:13 +01:00
|
|
|
const SnackBar(content: Text("can't open in browser"))),
|
|
|
|
),
|
|
|
|
ListTile(
|
|
|
|
leading: const Icon(Icons.info_outline),
|
|
|
|
title: const Text('Nerd stuff'),
|
|
|
|
onTap: () {
|
2021-02-24 20:52:18 +01:00
|
|
|
showInfoTablePopup(context: context, table: {
|
2021-02-09 15:12:13 +01:00
|
|
|
'% of upvotes':
|
|
|
|
'${(100 * (post.counts.upvotes / (post.counts.upvotes + post.counts.downvotes))).toInt()}%',
|
2021-02-24 20:52:18 +01:00
|
|
|
...post.toJson(),
|
2021-02-09 15:12:13 +01:00
|
|
|
});
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
2020-09-09 21:23:48 +02:00
|
|
|
),
|
|
|
|
);
|
2020-08-28 13:30:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// == UI ==
|
|
|
|
|
2020-08-27 23:27:27 +02:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-08-31 21:03:50 +02:00
|
|
|
final theme = Theme.of(context);
|
2021-02-04 15:47:13 +01:00
|
|
|
|
2021-04-09 00:11:44 +02:00
|
|
|
void _openLink(String url) =>
|
|
|
|
linkLauncher(context: context, url: url, instanceHost: instanceHost);
|
2020-08-31 21:03:50 +02:00
|
|
|
|
2020-09-09 20:52:31 +02:00
|
|
|
final urlDomain = () {
|
2021-01-24 20:01:55 +01:00
|
|
|
if (whatType(post.post.url) == MediaType.none) return null;
|
2020-09-09 20:52:31 +02:00
|
|
|
|
2021-04-09 00:11:44 +02:00
|
|
|
return urlHost(post.post.url!);
|
2020-09-09 20:52:31 +02:00
|
|
|
}();
|
|
|
|
|
2020-08-31 21:03:50 +02:00
|
|
|
/// assemble info section
|
2021-01-27 20:53:09 +01:00
|
|
|
Widget info() => Column(
|
|
|
|
children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(10),
|
|
|
|
child: Row(
|
2020-08-31 21:35:54 +02:00
|
|
|
children: [
|
2021-01-27 20:53:09 +01:00
|
|
|
Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
if (post.community.icon != null)
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(right: 10),
|
|
|
|
child: InkWell(
|
2021-02-24 20:52:18 +01:00
|
|
|
borderRadius: BorderRadius.circular(20),
|
2021-01-27 20:53:09 +01:00
|
|
|
onTap: () => goToCommunity.byId(
|
|
|
|
context, instanceHost, post.community.id),
|
2021-02-18 09:19:00 +01:00
|
|
|
child: Avatar(
|
|
|
|
url: post.community.icon,
|
|
|
|
noBlank: true,
|
|
|
|
radius: 20,
|
2020-08-27 23:27:27 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2021-01-27 20:53:09 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
RichText(
|
|
|
|
overflow:
|
|
|
|
TextOverflow.ellipsis, // TODO: fix overflowing
|
|
|
|
text: TextSpan(
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 15,
|
2021-04-09 00:11:44 +02:00
|
|
|
color: theme.textTheme.bodyText1?.color),
|
2021-01-27 20:53:09 +01:00
|
|
|
children: [
|
|
|
|
const TextSpan(
|
|
|
|
text: '!',
|
|
|
|
style:
|
|
|
|
TextStyle(fontWeight: FontWeight.w300)),
|
|
|
|
TextSpan(
|
|
|
|
text: post.community.name,
|
|
|
|
style: const TextStyle(
|
|
|
|
fontWeight: FontWeight.w600),
|
|
|
|
recognizer: TapGestureRecognizer()
|
|
|
|
..onTap = () => goToCommunity.byId(
|
|
|
|
context,
|
|
|
|
instanceHost,
|
|
|
|
post.community.id)),
|
|
|
|
const TextSpan(
|
|
|
|
text: '@',
|
|
|
|
style:
|
|
|
|
TextStyle(fontWeight: FontWeight.w300)),
|
|
|
|
TextSpan(
|
|
|
|
text: post.post.originInstanceHost,
|
|
|
|
style: const TextStyle(
|
|
|
|
fontWeight: FontWeight.w600),
|
|
|
|
recognizer: TapGestureRecognizer()
|
|
|
|
..onTap = () => goToInstance(context,
|
|
|
|
post.post.originInstanceHost)),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
2020-08-27 23:27:27 +02:00
|
|
|
),
|
2021-01-27 20:53:09 +01:00
|
|
|
Row(
|
2020-08-31 21:03:50 +02:00
|
|
|
children: [
|
2021-01-27 20:53:09 +01:00
|
|
|
RichText(
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
text: TextSpan(
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 13,
|
2021-04-09 00:11:44 +02:00
|
|
|
color: theme.textTheme.bodyText1?.color),
|
2021-01-27 20:53:09 +01:00
|
|
|
children: [
|
2021-03-01 14:21:45 +01:00
|
|
|
TextSpan(
|
2021-04-09 00:11:44 +02:00
|
|
|
text: L10n.of(context)!.by,
|
2021-03-01 14:21:45 +01:00
|
|
|
style: const TextStyle(
|
|
|
|
fontWeight: FontWeight.w300),
|
|
|
|
),
|
2021-01-27 20:53:09 +01:00
|
|
|
TextSpan(
|
2021-01-27 21:11:36 +01:00
|
|
|
text: ' ${post.creator.originDisplayName}',
|
2021-01-27 20:53:09 +01:00
|
|
|
style: const TextStyle(
|
|
|
|
fontWeight: FontWeight.w600),
|
|
|
|
recognizer: TapGestureRecognizer()
|
2021-04-05 20:14:39 +02:00
|
|
|
..onTap = () => goToUser.fromPersonSafe(
|
2021-01-27 20:53:09 +01:00
|
|
|
context,
|
2021-02-24 20:52:18 +01:00
|
|
|
post.creator,
|
2021-01-27 20:53:09 +01:00
|
|
|
),
|
|
|
|
),
|
|
|
|
TextSpan(
|
|
|
|
text:
|
2021-02-12 17:53:51 +01:00
|
|
|
' · ${post.post.published.fancyShort}'),
|
2021-01-27 20:53:09 +01:00
|
|
|
if (post.post.locked)
|
|
|
|
const TextSpan(text: ' · 🔒'),
|
|
|
|
if (post.post.stickied)
|
|
|
|
const TextSpan(text: ' · 📌'),
|
|
|
|
if (post.post.nsfw) const TextSpan(text: ' · '),
|
|
|
|
if (post.post.nsfw)
|
2021-03-01 14:21:45 +01:00
|
|
|
TextSpan(
|
2021-04-09 00:11:44 +02:00
|
|
|
text: L10n.of(context)!.nsfw,
|
2021-03-01 14:21:45 +01:00
|
|
|
style:
|
|
|
|
const TextStyle(color: Colors.red)),
|
2021-01-27 20:53:09 +01:00
|
|
|
if (urlDomain != null)
|
|
|
|
TextSpan(text: ' · $urlDomain'),
|
|
|
|
if (post.post.removed)
|
|
|
|
const TextSpan(text: ' · REMOVED'),
|
|
|
|
if (post.post.deleted)
|
|
|
|
const TextSpan(text: ' · DELETED'),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
)
|
2020-08-31 21:03:50 +02:00
|
|
|
],
|
2020-08-31 21:35:54 +02:00
|
|
|
),
|
2021-01-27 20:53:09 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
const Spacer(),
|
|
|
|
if (!fullPost)
|
|
|
|
Column(
|
|
|
|
children: [
|
|
|
|
IconButton(
|
|
|
|
onPressed: () => showMoreMenu(context, post),
|
|
|
|
icon: Icon(moreIcon),
|
|
|
|
padding: const EdgeInsets.all(0),
|
|
|
|
visualDensity: VisualDensity.compact,
|
|
|
|
)
|
|
|
|
],
|
2020-08-31 21:35:54 +02:00
|
|
|
)
|
|
|
|
],
|
2020-08-31 21:03:50 +02:00
|
|
|
),
|
2021-01-27 20:53:09 +01:00
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
2020-08-31 21:35:54 +02:00
|
|
|
|
|
|
|
/// assemble title section
|
|
|
|
Widget title() => Padding(
|
|
|
|
padding: const EdgeInsets.only(left: 10, right: 10, bottom: 10),
|
|
|
|
child: Row(
|
|
|
|
children: [
|
2020-09-09 20:52:31 +02:00
|
|
|
Expanded(
|
|
|
|
flex: 100,
|
2020-08-31 21:35:54 +02:00
|
|
|
child: Text(
|
2021-01-24 20:01:55 +01:00
|
|
|
post.post.name,
|
2020-08-31 21:35:54 +02:00
|
|
|
textAlign: TextAlign.left,
|
|
|
|
softWrap: true,
|
2021-01-03 19:43:39 +01:00
|
|
|
style: const TextStyle(
|
|
|
|
fontSize: 18, fontWeight: FontWeight.w600),
|
2020-08-31 21:35:54 +02:00
|
|
|
),
|
|
|
|
),
|
2021-01-24 20:01:55 +01:00
|
|
|
if (whatType(post.post.url) == MediaType.other &&
|
|
|
|
post.post.thumbnailUrl != null) ...[
|
2021-01-03 19:43:39 +01:00
|
|
|
const Spacer(),
|
2020-08-31 21:35:54 +02:00
|
|
|
InkWell(
|
2021-02-24 20:52:18 +01:00
|
|
|
borderRadius: BorderRadius.circular(20),
|
2021-04-09 00:11:44 +02:00
|
|
|
onTap: () => _openLink(post.post.url!),
|
2021-01-27 20:53:09 +01:00
|
|
|
child: Stack(
|
|
|
|
children: [
|
|
|
|
ClipRRect(
|
|
|
|
borderRadius: BorderRadius.circular(20),
|
|
|
|
child: CachedNetworkImage(
|
2021-04-09 00:11:44 +02:00
|
|
|
imageUrl: post.post.thumbnailUrl!,
|
2021-01-27 20:53:09 +01:00
|
|
|
width: 70,
|
|
|
|
height: 70,
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
errorWidget: (context, url, error) =>
|
|
|
|
Text(error.toString()),
|
|
|
|
),
|
2020-08-31 21:35:54 +02:00
|
|
|
),
|
2021-01-27 20:53:09 +01:00
|
|
|
const Positioned(
|
|
|
|
top: 8,
|
|
|
|
right: 8,
|
|
|
|
child: Icon(
|
|
|
|
Icons.launch,
|
|
|
|
size: 20,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
2020-08-31 21:35:54 +02:00
|
|
|
)
|
2020-09-09 20:52:31 +02:00
|
|
|
]
|
2020-08-31 21:35:54 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
2020-08-27 23:27:27 +02:00
|
|
|
|
2020-08-31 21:03:50 +02:00
|
|
|
/// assemble link preview
|
|
|
|
Widget linkPreview() {
|
2021-01-24 20:01:55 +01:00
|
|
|
assert(post.post.url != null);
|
2020-08-27 23:27:27 +02:00
|
|
|
|
2020-08-31 21:03:50 +02:00
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.all(10),
|
|
|
|
child: InkWell(
|
2021-04-09 00:11:44 +02:00
|
|
|
onTap: () => _openLink(post.post.url!),
|
2020-08-31 21:03:50 +02:00
|
|
|
child: Container(
|
|
|
|
decoration: BoxDecoration(
|
2020-09-03 00:07:07 +02:00
|
|
|
border: Border.all(
|
2021-04-09 00:11:44 +02:00
|
|
|
color: Theme.of(context).iconTheme.color!.withAlpha(170)),
|
2020-08-31 21:03:50 +02:00
|
|
|
borderRadius: BorderRadius.circular(5)),
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(10),
|
|
|
|
child: Column(
|
|
|
|
children: [
|
2021-01-27 20:53:09 +01:00
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
const Spacer(),
|
|
|
|
Text('$urlDomain ',
|
|
|
|
style: theme.textTheme.caption
|
2021-04-09 00:11:44 +02:00
|
|
|
?.apply(fontStyle: FontStyle.italic)),
|
2021-01-27 20:53:09 +01:00
|
|
|
const Icon(Icons.launch, size: 12),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
Row(
|
|
|
|
children: [
|
|
|
|
Flexible(
|
2021-02-24 20:52:18 +01:00
|
|
|
child: Text(
|
|
|
|
post.post.embedTitle ?? '',
|
|
|
|
style: theme.textTheme.subtitle1
|
2021-04-09 00:11:44 +02:00
|
|
|
?.apply(fontWeightDelta: 2),
|
2021-02-24 20:52:18 +01:00
|
|
|
maxLines: 2,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
),
|
|
|
|
)
|
2021-01-27 20:53:09 +01:00
|
|
|
],
|
|
|
|
),
|
2021-01-24 20:01:55 +01:00
|
|
|
if (post.post.embedDescription != null &&
|
2021-04-09 00:11:44 +02:00
|
|
|
post.post.embedDescription!.isNotEmpty)
|
2021-01-27 20:53:09 +01:00
|
|
|
Row(
|
|
|
|
children: [
|
2021-02-24 20:52:18 +01:00
|
|
|
Flexible(
|
|
|
|
child: Text(
|
2021-04-09 00:11:44 +02:00
|
|
|
post.post.embedDescription!,
|
2021-02-24 20:52:18 +01:00
|
|
|
maxLines: 4,
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
),
|
|
|
|
)
|
2021-01-27 20:53:09 +01:00
|
|
|
],
|
|
|
|
),
|
2020-08-31 21:03:50 +02:00
|
|
|
],
|
|
|
|
),
|
2020-08-27 23:27:27 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2020-08-31 21:03:50 +02:00
|
|
|
);
|
|
|
|
}
|
2020-08-27 23:27:27 +02:00
|
|
|
|
2020-08-31 21:03:50 +02:00
|
|
|
/// assemble image
|
|
|
|
Widget postImage() {
|
2021-01-24 20:01:55 +01:00
|
|
|
assert(post.post.url != null);
|
2020-08-31 21:03:50 +02:00
|
|
|
|
2020-09-11 20:38:50 +02:00
|
|
|
return FullscreenableImage(
|
2021-04-09 00:11:44 +02:00
|
|
|
url: post.post.url!,
|
2020-09-11 20:38:50 +02:00
|
|
|
child: CachedNetworkImage(
|
2021-04-09 00:11:44 +02:00
|
|
|
imageUrl: post.post.url!,
|
2021-01-03 19:43:39 +01:00
|
|
|
errorWidget: (_, __, ___) => const Icon(Icons.warning),
|
2020-09-11 20:38:50 +02:00
|
|
|
progressIndicatorBuilder: (context, url, progress) =>
|
|
|
|
CircularProgressIndicator(value: progress.progress),
|
2020-08-31 21:03:50 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2020-08-29 21:01:01 +02:00
|
|
|
|
2020-08-31 21:03:50 +02:00
|
|
|
/// assemble actions section
|
2020-08-31 21:35:54 +02:00
|
|
|
Widget actions() => Padding(
|
|
|
|
padding: const EdgeInsets.fromLTRB(10, 5, 10, 5),
|
|
|
|
child: Row(
|
|
|
|
children: [
|
2021-01-03 19:43:39 +01:00
|
|
|
const Icon(Icons.comment),
|
2021-03-03 13:16:05 +01:00
|
|
|
const SizedBox(width: 6),
|
2020-08-31 21:35:54 +02:00
|
|
|
Expanded(
|
|
|
|
flex: 999,
|
|
|
|
child: Text(
|
2021-04-09 00:11:44 +02:00
|
|
|
L10n.of(context)!.number_of_comments(post.counts.comments),
|
2020-08-31 21:35:54 +02:00
|
|
|
overflow: TextOverflow.fade,
|
|
|
|
softWrap: false,
|
|
|
|
),
|
2020-08-31 21:03:50 +02:00
|
|
|
),
|
2021-01-03 19:43:39 +01:00
|
|
|
const Spacer(),
|
2020-09-02 00:05:42 +02:00
|
|
|
if (!fullPost)
|
|
|
|
IconButton(
|
2021-01-30 17:38:11 +01:00
|
|
|
icon: const Icon(Icons.share),
|
2021-03-20 15:50:49 +01:00
|
|
|
onPressed: () => share(post.post.apId, context: context),
|
2021-03-18 19:24:29 +01:00
|
|
|
),
|
2020-09-17 22:50:18 +02:00
|
|
|
if (!fullPost) SavePostButton(post),
|
2020-09-17 16:46:40 +02:00
|
|
|
_Voting(post),
|
2020-08-31 21:35:54 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
2020-08-31 21:03:50 +02:00
|
|
|
|
|
|
|
return Container(
|
|
|
|
decoration: BoxDecoration(
|
2021-02-24 20:52:18 +01:00
|
|
|
boxShadow: const [BoxShadow(blurRadius: 10, color: Colors.black45)],
|
2020-09-10 13:11:16 +02:00
|
|
|
color: theme.cardColor,
|
2021-01-03 19:43:39 +01:00
|
|
|
borderRadius: const BorderRadius.all(Radius.circular(20)),
|
2020-08-31 21:03:50 +02:00
|
|
|
),
|
2021-02-24 20:52:18 +01:00
|
|
|
child: GestureDetector(
|
2020-09-10 13:11:16 +02:00
|
|
|
onTap: fullPost
|
|
|
|
? null
|
2020-12-31 14:58:23 +01:00
|
|
|
: () => goTo(context, (context) => FullPostPage.fromPostView(post)),
|
2021-02-24 20:52:18 +01:00
|
|
|
child: Material(
|
|
|
|
type: MaterialType.transparency,
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
info(),
|
|
|
|
title(),
|
|
|
|
if (whatType(post.post.url) != MediaType.other &&
|
|
|
|
whatType(post.post.url) != MediaType.none)
|
|
|
|
postImage()
|
2021-04-09 00:11:44 +02:00
|
|
|
else if (post.post.url != null && post.post.url!.isNotEmpty)
|
2021-02-24 20:52:18 +01:00
|
|
|
linkPreview(),
|
|
|
|
if (post.post.body != null && fullPost)
|
|
|
|
Padding(
|
2021-04-06 15:48:56 +02:00
|
|
|
padding: const EdgeInsets.all(10),
|
|
|
|
child: MarkdownText(
|
2021-04-09 00:11:44 +02:00
|
|
|
post.post.body!,
|
2021-04-06 15:48:56 +02:00
|
|
|
instanceHost: instanceHost,
|
|
|
|
selectable: true,
|
|
|
|
),
|
|
|
|
),
|
2021-02-24 20:52:18 +01:00
|
|
|
if (post.post.body != null && !fullPost)
|
|
|
|
LayoutBuilder(
|
|
|
|
builder: (context, constraints) {
|
|
|
|
final span = TextSpan(
|
|
|
|
text: post.post.body,
|
|
|
|
);
|
|
|
|
final tp = TextPainter(
|
|
|
|
text: span,
|
|
|
|
maxLines: 10,
|
|
|
|
textDirection: Directionality.of(context),
|
|
|
|
)..layout(maxWidth: constraints.maxWidth - 20);
|
2021-02-06 13:48:54 +01:00
|
|
|
|
2021-02-24 20:52:18 +01:00
|
|
|
if (tp.didExceedMaxLines) {
|
|
|
|
return ConstrainedBox(
|
|
|
|
constraints: BoxConstraints(maxHeight: tp.height),
|
|
|
|
child: Stack(
|
|
|
|
alignment: Alignment.bottomCenter,
|
|
|
|
children: [
|
|
|
|
ClipRect(
|
|
|
|
child: Align(
|
|
|
|
alignment: Alignment.topCenter,
|
|
|
|
heightFactor: 0.8,
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(10),
|
2021-04-09 00:11:44 +02:00
|
|
|
child: MarkdownText(post.post.body!,
|
2021-02-24 20:52:18 +01:00
|
|
|
instanceHost: instanceHost)),
|
|
|
|
),
|
2021-02-06 13:48:54 +01:00
|
|
|
),
|
2021-02-24 20:52:18 +01:00
|
|
|
Container(
|
|
|
|
height: tp.preferredLineHeight * 2.5,
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
gradient: LinearGradient(
|
|
|
|
begin: Alignment.topCenter,
|
|
|
|
end: Alignment.bottomCenter,
|
|
|
|
colors: [
|
|
|
|
theme.cardColor.withAlpha(0),
|
|
|
|
theme.cardColor,
|
|
|
|
],
|
|
|
|
),
|
2021-02-06 13:48:54 +01:00
|
|
|
),
|
|
|
|
),
|
2021-02-24 20:52:18 +01:00
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return Padding(
|
|
|
|
padding: const EdgeInsets.all(10),
|
2021-04-09 00:11:44 +02:00
|
|
|
child: MarkdownText(post.post.body!,
|
2021-02-24 20:52:18 +01:00
|
|
|
instanceHost: instanceHost));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
actions(),
|
|
|
|
],
|
|
|
|
),
|
2020-08-31 21:03:50 +02:00
|
|
|
),
|
2020-08-27 23:27:27 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-09-17 16:46:40 +02:00
|
|
|
|
|
|
|
class _Voting extends HookWidget {
|
|
|
|
final PostView post;
|
|
|
|
|
2020-09-21 12:47:16 +02:00
|
|
|
final bool wasVoted;
|
|
|
|
|
|
|
|
_Voting(this.post)
|
2021-04-09 00:11:44 +02:00
|
|
|
: wasVoted = (post.myVote ?? VoteType.none) != VoteType.none;
|
2020-09-17 16:46:40 +02:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
final theme = Theme.of(context);
|
2020-09-18 22:07:48 +02:00
|
|
|
final myVote = useState(post.myVote ?? VoteType.none);
|
2021-01-03 19:43:39 +01:00
|
|
|
final loading = useDelayedLoading();
|
2020-12-31 14:58:23 +01:00
|
|
|
final loggedInAction = useLoggedInAction(post.instanceHost);
|
2020-09-17 16:46:40 +02:00
|
|
|
|
2020-09-18 21:56:42 +02:00
|
|
|
vote(VoteType vote, Jwt token) async {
|
2021-04-05 20:14:39 +02:00
|
|
|
final api = LemmyApiV3(post.instanceHost);
|
2020-09-17 16:46:40 +02:00
|
|
|
|
|
|
|
loading.start();
|
|
|
|
try {
|
2021-01-24 20:01:55 +01:00
|
|
|
final res = await api.run(
|
|
|
|
CreatePostLike(postId: post.post.id, score: vote, auth: token.raw));
|
2021-01-30 17:38:11 +01:00
|
|
|
myVote.value = res.myVote ?? VoteType.none;
|
2020-09-18 16:16:39 +02:00
|
|
|
// ignore: avoid_catches_without_on_clauses
|
2020-09-17 16:46:40 +02:00
|
|
|
} catch (e) {
|
2021-03-10 08:34:30 +01:00
|
|
|
ScaffoldMessenger.of(context)
|
2021-01-03 19:43:39 +01:00
|
|
|
.showSnackBar(const SnackBar(content: Text('voting failed :(')));
|
2020-09-17 16:46:40 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
loading.cancel();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Row(
|
|
|
|
children: [
|
|
|
|
IconButton(
|
2020-09-18 21:56:42 +02:00
|
|
|
icon: Icon(
|
|
|
|
Icons.arrow_upward,
|
2020-09-18 22:07:48 +02:00
|
|
|
color: myVote.value == VoteType.up ? theme.accentColor : null,
|
2020-09-18 21:56:42 +02:00
|
|
|
),
|
|
|
|
onPressed: loggedInAction(
|
2020-09-18 22:07:48 +02:00
|
|
|
(token) => vote(
|
|
|
|
myVote.value == VoteType.up ? VoteType.none : VoteType.up,
|
|
|
|
token,
|
|
|
|
),
|
2020-09-18 21:56:42 +02:00
|
|
|
)),
|
2020-09-17 16:46:40 +02:00
|
|
|
if (loading.loading)
|
2021-01-03 19:43:39 +01:00
|
|
|
const SizedBox(
|
|
|
|
width: 20, height: 20, child: CircularProgressIndicator())
|
2020-09-17 16:46:40 +02:00
|
|
|
else
|
2020-09-21 12:47:16 +02:00
|
|
|
Text(NumberFormat.compact()
|
2021-01-24 20:01:55 +01:00
|
|
|
.format(post.counts.score + (wasVoted ? 0 : myVote.value.value))),
|
2020-09-17 16:46:40 +02:00
|
|
|
IconButton(
|
2021-01-27 20:53:09 +01:00
|
|
|
icon: Icon(
|
|
|
|
Icons.arrow_downward,
|
|
|
|
color: myVote.value == VoteType.down ? Colors.red : null,
|
|
|
|
),
|
|
|
|
onPressed: loggedInAction(
|
|
|
|
(token) => vote(
|
|
|
|
myVote.value == VoteType.down ? VoteType.none : VoteType.down,
|
|
|
|
token,
|
2020-09-18 21:56:42 +02:00
|
|
|
),
|
2021-01-27 20:53:09 +01:00
|
|
|
),
|
|
|
|
),
|
2020-09-17 16:46:40 +02:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|