2020-08-27 23:27:27 +02:00
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
|
|
|
import 'package:esys_flutter_share/esys_flutter_share.dart';
|
|
|
|
import 'package:flutter/gestures.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
2020-08-29 21:01:01 +02:00
|
|
|
import 'package:intl/intl.dart';
|
2020-08-28 13:47:52 +02:00
|
|
|
import 'package:lemmy_api_client/lemmy_api_client.dart';
|
2020-08-27 23:27:27 +02:00
|
|
|
import 'package:timeago/timeago.dart' as timeago;
|
|
|
|
|
2020-08-31 12:22:29 +02:00
|
|
|
import 'markdown_text.dart';
|
2020-08-30 16:49:59 +02:00
|
|
|
|
2020-08-29 21:01:01 +02:00
|
|
|
enum MediaType {
|
|
|
|
image,
|
|
|
|
gallery,
|
|
|
|
video,
|
|
|
|
other,
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaType whatType(String url) {
|
|
|
|
if (url == null) return MediaType.other;
|
|
|
|
|
2020-08-30 19:29:12 +02:00
|
|
|
// TODO: make detection more nuanced
|
2020-08-29 21:01:01 +02:00
|
|
|
if (url.endsWith('.jpg') || url.endsWith('.png') || url.endsWith('.gif')) {
|
|
|
|
return MediaType.image;
|
|
|
|
}
|
|
|
|
return MediaType.other;
|
|
|
|
}
|
|
|
|
|
2020-08-31 19:56:48 +02:00
|
|
|
class Post extends StatelessWidget {
|
2020-08-27 23:27:27 +02:00
|
|
|
final PostView post;
|
2020-08-31 21:03:50 +02:00
|
|
|
final String instanceUrl;
|
2020-08-27 23:27:27 +02:00
|
|
|
|
|
|
|
/// nullable
|
2020-08-31 21:03:50 +02:00
|
|
|
final String postUrlDomain;
|
2020-08-28 13:30:42 +02:00
|
|
|
|
2020-08-31 19:56:48 +02:00
|
|
|
Post(this.post)
|
2020-08-31 21:03:50 +02:00
|
|
|
: instanceUrl = post.communityActorId.split('/')[2],
|
|
|
|
postUrlDomain = post.url != null ? post.url.split('/')[2] : null;
|
2020-08-27 23:27:27 +02:00
|
|
|
|
2020-08-28 13:30:42 +02:00
|
|
|
// == ACTIONS ==
|
|
|
|
|
|
|
|
void _openLink() {
|
|
|
|
print('OPEN LINK');
|
|
|
|
}
|
|
|
|
|
|
|
|
void _goToUser() {
|
|
|
|
print('GO TO USER');
|
|
|
|
}
|
|
|
|
|
2020-08-31 21:03:50 +02:00
|
|
|
void _goToPost(BuildContext context) {
|
|
|
|
print('GO TO POST');
|
2020-08-28 13:30:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void _goToCommunity() {
|
|
|
|
print('GO TO COMMUNITY');
|
|
|
|
}
|
|
|
|
|
|
|
|
void _goToInstance() {
|
|
|
|
print('GO TO INSTANCE');
|
|
|
|
}
|
|
|
|
|
2020-08-29 21:01:01 +02:00
|
|
|
void _openFullImage() {
|
|
|
|
print('OPEN FULL IMAGE');
|
|
|
|
}
|
|
|
|
|
2020-08-28 13:30:42 +02:00
|
|
|
// POST ACTIONS
|
|
|
|
|
|
|
|
void _savePost() {
|
|
|
|
print('SAVE POST');
|
|
|
|
}
|
|
|
|
|
|
|
|
void _upvotePost() {
|
|
|
|
print('UPVOTE POST');
|
|
|
|
}
|
|
|
|
|
|
|
|
void _downvotePost() {
|
|
|
|
print('DOWNVOTE POST');
|
|
|
|
}
|
|
|
|
|
|
|
|
void _showMoreMenu() {
|
|
|
|
print('SHOW MORE MENU');
|
|
|
|
}
|
|
|
|
|
|
|
|
// == 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);
|
|
|
|
|
2020-08-31 21:35:54 +02:00
|
|
|
// TODO: add NSFW, locked, removed, deleted, stickied
|
2020-08-31 21:03:50 +02:00
|
|
|
/// assemble info section
|
2020-08-31 21:35:54 +02:00
|
|
|
Widget info() => Column(children: [
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(10),
|
|
|
|
child: Row(children: [
|
|
|
|
Column(
|
|
|
|
mainAxisSize: MainAxisSize.min,
|
|
|
|
children: [
|
|
|
|
if (post.communityIcon != null)
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.only(right: 10),
|
|
|
|
child: InkWell(
|
|
|
|
onTap: _goToCommunity,
|
|
|
|
child: SizedBox(
|
|
|
|
height: 40,
|
|
|
|
width: 40,
|
|
|
|
child: CachedNetworkImage(
|
|
|
|
imageBuilder: (context, imageProvider) => Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
shape: BoxShape.circle,
|
|
|
|
image: DecorationImage(
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
image: imageProvider,
|
|
|
|
),
|
2020-08-31 21:03:50 +02:00
|
|
|
),
|
2020-08-27 23:27:27 +02:00
|
|
|
),
|
2020-08-31 21:35:54 +02:00
|
|
|
imageUrl: post.communityIcon,
|
|
|
|
errorWidget: (context, url, error) =>
|
|
|
|
Text(error.toString()),
|
2020-08-27 23:27:27 +02:00
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
2020-08-31 21:35:54 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
Column(
|
|
|
|
children: [
|
|
|
|
Row(children: [
|
|
|
|
RichText(
|
|
|
|
overflow: TextOverflow.ellipsis, // TODO: fix overflowing
|
2020-08-31 21:03:50 +02:00
|
|
|
text: TextSpan(
|
|
|
|
style: TextStyle(
|
2020-08-31 21:35:54 +02:00
|
|
|
fontSize: 15,
|
2020-08-31 21:03:50 +02:00
|
|
|
color: theme.textTheme.bodyText1.color),
|
|
|
|
children: [
|
|
|
|
TextSpan(
|
2020-08-31 21:35:54 +02:00
|
|
|
text: '!',
|
2020-08-31 21:03:50 +02:00
|
|
|
style: TextStyle(fontWeight: FontWeight.w300)),
|
|
|
|
TextSpan(
|
2020-08-31 21:35:54 +02:00
|
|
|
text: post.communityName,
|
|
|
|
style: TextStyle(fontWeight: FontWeight.w600),
|
|
|
|
recognizer: TapGestureRecognizer()
|
|
|
|
..onTap = _goToCommunity),
|
2020-08-31 21:03:50 +02:00
|
|
|
TextSpan(
|
2020-08-31 21:35:54 +02:00
|
|
|
text: '@',
|
|
|
|
style: TextStyle(fontWeight: FontWeight.w300)),
|
|
|
|
TextSpan(
|
|
|
|
text: instanceUrl,
|
|
|
|
style: TextStyle(fontWeight: FontWeight.w600),
|
|
|
|
recognizer: TapGestureRecognizer()
|
|
|
|
..onTap = _goToInstance),
|
2020-08-31 21:03:50 +02:00
|
|
|
],
|
2020-08-31 21:35:54 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
]),
|
|
|
|
Row(children: [
|
|
|
|
RichText(
|
|
|
|
overflow: TextOverflow.ellipsis,
|
|
|
|
text: TextSpan(
|
|
|
|
style: TextStyle(
|
|
|
|
fontSize: 13,
|
|
|
|
color: theme.textTheme.bodyText1.color),
|
|
|
|
children: [
|
|
|
|
TextSpan(
|
|
|
|
text: 'by',
|
|
|
|
style: TextStyle(fontWeight: FontWeight.w300)),
|
|
|
|
TextSpan(
|
|
|
|
text:
|
|
|
|
''' ${post.creatorPreferredUsername ?? post.creatorName}''',
|
|
|
|
style: TextStyle(fontWeight: FontWeight.w600),
|
|
|
|
recognizer: TapGestureRecognizer()
|
|
|
|
..onTap = _goToUser,
|
|
|
|
),
|
|
|
|
TextSpan(
|
|
|
|
text:
|
|
|
|
''' · ${timeago.format(post.published, locale: 'en_short')}'''),
|
|
|
|
if (postUrlDomain != null)
|
|
|
|
TextSpan(text: ' · $postUrlDomain'),
|
|
|
|
if (post.locked) TextSpan(text: ' · 🔒'),
|
|
|
|
],
|
|
|
|
))
|
|
|
|
]),
|
|
|
|
],
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
2020-08-31 21:03:50 +02:00
|
|
|
),
|
|
|
|
Spacer(),
|
2020-08-31 21:35:54 +02:00
|
|
|
Column(
|
|
|
|
children: [
|
|
|
|
IconButton(
|
|
|
|
onPressed: _showMoreMenu,
|
|
|
|
icon: Icon(Icons.more_vert),
|
2020-08-31 21:03:50 +02:00
|
|
|
)
|
2020-08-31 21:35:54 +02:00
|
|
|
],
|
2020-08-31 21:03:50 +02: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: [
|
|
|
|
Flexible(
|
|
|
|
child: Text(
|
|
|
|
'${post.name}',
|
|
|
|
textAlign: TextAlign.left,
|
|
|
|
softWrap: true,
|
|
|
|
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
if (post.url != null &&
|
|
|
|
whatType(post.url) == MediaType.other &&
|
|
|
|
post.thumbnailUrl != null)
|
|
|
|
Spacer(),
|
|
|
|
if (post.url != null &&
|
|
|
|
whatType(post.url) == MediaType.other &&
|
|
|
|
post.thumbnailUrl != null)
|
|
|
|
InkWell(
|
|
|
|
onTap: _openLink,
|
|
|
|
child: Stack(children: [
|
|
|
|
ClipRRect(
|
|
|
|
borderRadius: BorderRadius.circular(20),
|
|
|
|
child: CachedNetworkImage(
|
|
|
|
imageUrl: post.thumbnailUrl,
|
|
|
|
width: 70,
|
|
|
|
height: 70,
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
errorWidget: (context, url, error) =>
|
|
|
|
Text(error.toString()),
|
|
|
|
)),
|
|
|
|
Positioned(
|
|
|
|
top: 8,
|
|
|
|
right: 8,
|
|
|
|
child: Icon(
|
|
|
|
Icons.launch,
|
|
|
|
size: 20,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
]),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
2020-08-27 23:27:27 +02:00
|
|
|
|
2020-08-31 21:03:50 +02:00
|
|
|
/// assemble link preview
|
|
|
|
Widget linkPreview() {
|
|
|
|
assert(post.url != null);
|
2020-08-27 23:27:27 +02:00
|
|
|
|
2020-08-31 21:03:50 +02:00
|
|
|
var url = post.url.split('/')[2];
|
|
|
|
if (url.startsWith('www.')) {
|
|
|
|
url = url.substring(4);
|
|
|
|
}
|
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(
|
|
|
|
onTap: _openLink,
|
|
|
|
child: Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
border: Border.all(width: 1),
|
|
|
|
borderRadius: BorderRadius.circular(5)),
|
|
|
|
child: Padding(
|
|
|
|
padding: const EdgeInsets.all(10),
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Row(children: [
|
|
|
|
Spacer(),
|
|
|
|
Text('$url ',
|
|
|
|
style: theme.textTheme.caption
|
|
|
|
.apply(fontStyle: FontStyle.italic)),
|
|
|
|
Icon(Icons.launch, size: 12),
|
|
|
|
]),
|
|
|
|
Row(children: [
|
|
|
|
Flexible(
|
|
|
|
child: Text(post.embedTitle,
|
|
|
|
style: theme.textTheme.subtitle1
|
|
|
|
.apply(fontWeightDelta: 2)))
|
|
|
|
]),
|
|
|
|
Row(children: [Flexible(child: Text(post.embedDescription))]),
|
|
|
|
],
|
|
|
|
),
|
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() {
|
|
|
|
assert(post.url != null);
|
|
|
|
|
|
|
|
return InkWell(
|
|
|
|
onTap: _openFullImage,
|
|
|
|
child: CachedNetworkImage(
|
|
|
|
imageUrl: post.url,
|
|
|
|
progressIndicatorBuilder: (context, url, progress) =>
|
|
|
|
CircularProgressIndicator(value: progress.progress),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
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: [
|
|
|
|
Icon(Icons.comment),
|
|
|
|
Expanded(
|
|
|
|
flex: 999,
|
|
|
|
child: Text(
|
|
|
|
''' ${NumberFormat.compact().format(post.numberOfComments)} comment${post.numberOfComments == 1 ? '' : 's'}''',
|
|
|
|
overflow: TextOverflow.fade,
|
|
|
|
softWrap: false,
|
|
|
|
),
|
2020-08-31 21:03:50 +02:00
|
|
|
),
|
2020-08-31 21:35:54 +02:00
|
|
|
Spacer(),
|
|
|
|
IconButton(
|
|
|
|
icon: Icon(Icons.share),
|
|
|
|
onPressed: () => Share.text('Share post url', post.apId,
|
|
|
|
'text/plain')), // TODO: find a way to mark it as url
|
|
|
|
IconButton(
|
|
|
|
icon: post.saved == true
|
|
|
|
? Icon(Icons.bookmark)
|
|
|
|
: Icon(Icons.bookmark_border),
|
|
|
|
onPressed: _savePost),
|
|
|
|
IconButton(
|
|
|
|
icon: Icon(Icons.arrow_upward), onPressed: _upvotePost),
|
|
|
|
Text(NumberFormat.compact().format(post.score)),
|
|
|
|
IconButton(
|
|
|
|
icon: Icon(Icons.arrow_downward), onPressed: _downvotePost),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
2020-08-31 21:03:50 +02:00
|
|
|
|
|
|
|
return Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
boxShadow: [BoxShadow(blurRadius: 10, color: Colors.black54)],
|
|
|
|
color: theme.colorScheme.surface,
|
|
|
|
borderRadius: BorderRadius.all(Radius.circular(20)),
|
|
|
|
),
|
|
|
|
child: InkWell(
|
|
|
|
onTap: () => _goToPost(context),
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
info(),
|
|
|
|
title(),
|
|
|
|
if (whatType(post.url) != MediaType.other)
|
|
|
|
postImage()
|
|
|
|
else if (post.url != null)
|
|
|
|
linkPreview(),
|
|
|
|
if (post.body != null)
|
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(10),
|
|
|
|
child: MarkdownText(post.body)),
|
|
|
|
actions(),
|
|
|
|
],
|
|
|
|
),
|
2020-08-27 23:27:27 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|