lemmur-app-android/lib/widgets/post.dart

553 lines
20 KiB
Dart
Raw Normal View History

2020-08-27 23:27:27 +02:00
import 'package:cached_network_image/cached_network_image.dart';
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';
import '../hooks/logged_in_action.dart';
2021-03-01 14:21:45 +01:00
import '../l10n/l10n.dart';
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';
import '../util/extensions/datetime.dart';
import '../util/goto.dart';
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';
import 'fullscreenable_image.dart';
2020-10-26 18:47:49 +01:00
import 'info_table_popup.dart';
import 'markdown_text.dart';
2020-09-17 22:50:18 +02:00
import 'save_post_button.dart';
2020-08-29 21:01:01 +02:00
enum MediaType {
image,
gallery,
video,
other,
none,
2020-08-29 21:01:01 +02:00
}
MediaType whatType(String? url) {
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;
final String instanceHost;
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
// == 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
),
);
}
// == UI ==
2020-08-27 23:27:27 +02:00
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
void _openLink(String url) =>
linkLauncher(context: context, url: url, instanceHost: instanceHost);
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
return urlHost(post.post.url!);
2020-09-09 20:52:31 +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(
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,
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(
children: [
2021-01-27 20:53:09 +01:00
RichText(
overflow: TextOverflow.ellipsis,
text: TextSpan(
style: TextStyle(
fontSize: 13,
color: theme.textTheme.bodyText1?.color),
2021-01-27 20:53:09 +01:00
children: [
2021-03-01 14:21:45 +01:00
TextSpan(
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:
' · ${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(
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'),
],
),
)
],
),
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,
)
],
)
],
),
2021-01-27 20:53:09 +01: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,
child: Text(
2021-01-24 20:01:55 +01:00
post.post.name,
textAlign: TextAlign.left,
softWrap: true,
2021-01-03 19:43:39 +01:00
style: const TextStyle(
fontSize: 18, fontWeight: FontWeight.w600),
),
),
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(),
InkWell(
2021-02-24 20:52:18 +01:00
borderRadius: BorderRadius.circular(20),
onTap: () => _openLink(post.post.url!),
2021-01-27 20:53:09 +01:00
child: Stack(
children: [
ClipRRect(
borderRadius: BorderRadius.circular(20),
child: CachedNetworkImage(
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()),
),
),
2021-01-27 20:53:09 +01:00
const Positioned(
top: 8,
right: 8,
child: Icon(
Icons.launch,
size: 20,
),
)
],
),
)
2020-09-09 20:52:31 +02:00
]
],
),
);
2020-08-27 23:27:27 +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
return Padding(
padding: const EdgeInsets.all(10),
child: InkWell(
onTap: () => _openLink(post.post.url!),
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Theme.of(context).iconTheme.color!.withAlpha(170)),
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
?.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
?.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 &&
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(
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-27 23:27:27 +02:00
),
),
),
);
}
2020-08-27 23:27:27 +02:00
/// assemble image
Widget postImage() {
2021-01-24 20:01:55 +01:00
assert(post.post.url != null);
return FullscreenableImage(
url: post.post.url!,
child: CachedNetworkImage(
imageUrl: post.post.url!,
2021-01-03 19:43:39 +01:00
errorWidget: (_, __, ___) => const Icon(Icons.warning),
progressIndicatorBuilder: (context, url, progress) =>
CircularProgressIndicator(value: progress.progress),
),
);
}
2020-08-29 21:01:01 +02:00
/// assemble actions section
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),
Expanded(
flex: 999,
child: Text(
L10n.of(context)!.number_of_comments(post.counts.comments),
overflow: TextOverflow.fade,
softWrap: false,
),
),
2021-01-03 19:43:39 +01:00
const Spacer(),
if (!fullPost)
IconButton(
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),
],
),
);
return Container(
decoration: BoxDecoration(
2021-02-24 20:52:18 +01:00
boxShadow: const [BoxShadow(blurRadius: 10, color: Colors.black45)],
color: theme.cardColor,
2021-01-03 19:43:39 +01:00
borderRadius: const BorderRadius.all(Radius.circular(20)),
),
2021-02-24 20:52:18 +01:00
child: GestureDetector(
onTap: fullPost
? null
: () => 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()
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(
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-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),
child: MarkdownText(post.post.body!,
2021-02-24 20:52:18 +01:00
instanceHost: instanceHost)),
),
),
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-24 20:52:18 +01:00
],
),
);
} else {
return Padding(
padding: const EdgeInsets.all(10),
child: MarkdownText(post.post.body!,
2021-02-24 20:52:18 +01:00
instanceHost: instanceHost));
}
},
),
actions(),
],
),
),
2020-08-27 23:27:27 +02:00
),
);
}
}
2020-09-17 16:46:40 +02:00
class _Voting extends HookWidget {
final PostView post;
final bool wasVoted;
_Voting(this.post)
: 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();
final loggedInAction = useLoggedInAction(post.instanceHost);
2020-09-17 16:46:40 +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));
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(
icon: Icon(
Icons.arrow_upward,
2020-09-18 22:07:48 +02:00
color: myVote.value == VoteType.up ? theme.accentColor : null,
),
onPressed: loggedInAction(
2020-09-18 22:07:48 +02:00
(token) => vote(
myVote.value == VoteType.up ? VoteType.none : VoteType.up,
token,
),
)),
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
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,
),
2021-01-27 20:53:09 +01:00
),
),
2020-09-17 16:46:40 +02:00
],
);
}
}