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';
|
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';
|
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-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';
|
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';
|
2020-09-19 00:40:47 +02:00
|
|
|
import '../util/extensions/api.dart';
|
2020-09-09 17:41:54 +02:00
|
|
|
import '../util/goto.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-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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2020-09-17 15:25:47 +02:00
|
|
|
class Post extends HookWidget {
|
2020-08-27 23:27:27 +02:00
|
|
|
final PostView post;
|
2020-08-31 21:03:50 +02:00
|
|
|
final String instanceUrl;
|
2020-09-02 00:05:42 +02:00
|
|
|
final bool fullPost;
|
2020-08-27 23:27:27 +02:00
|
|
|
|
2020-09-09 20:52:31 +02:00
|
|
|
Post(this.post, {this.fullPost = false}) : instanceUrl = post.instanceUrl;
|
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) {
|
|
|
|
showModalBottomSheet(
|
|
|
|
backgroundColor: Colors.transparent,
|
|
|
|
context: context,
|
|
|
|
builder: (context) => BottomModal(
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
ListTile(
|
|
|
|
leading: Icon(Icons.open_in_browser),
|
|
|
|
title: Text('Open in browser'),
|
|
|
|
onTap: () async => await ul.canLaunch(post.apId)
|
|
|
|
? ul.launch(post.apId)
|
|
|
|
: Scaffold.of(context).showSnackBar(
|
|
|
|
SnackBar(content: Text("can't open in browser"))),
|
|
|
|
),
|
|
|
|
ListTile(
|
|
|
|
leading: Icon(Icons.info_outline),
|
|
|
|
title: Text('Nerd stuff'),
|
|
|
|
onTap: () {
|
|
|
|
showDialog(
|
|
|
|
context: context,
|
|
|
|
child: SimpleDialog(
|
|
|
|
contentPadding: const EdgeInsets.symmetric(
|
|
|
|
horizontal: 20,
|
|
|
|
vertical: 15,
|
|
|
|
),
|
|
|
|
children: [
|
|
|
|
Table(
|
|
|
|
children: [
|
|
|
|
TableRow(children: [
|
|
|
|
Text('upvotes:'),
|
|
|
|
Text(post.upvotes.toString()),
|
|
|
|
]),
|
|
|
|
TableRow(children: [
|
|
|
|
Text('downvotes:'),
|
|
|
|
Text(post.downvotes.toString()),
|
|
|
|
]),
|
|
|
|
TableRow(children: [
|
|
|
|
Text('score:'),
|
|
|
|
Text(post.score.toString()),
|
|
|
|
]),
|
|
|
|
TableRow(children: [
|
|
|
|
Text('% of upvotes:'),
|
|
|
|
Text(
|
|
|
|
'''${(100 * (post.upvotes / (post.upvotes + post.downvotes))).toInt()}%'''),
|
|
|
|
]),
|
|
|
|
TableRow(children: [
|
|
|
|
Text('hotrank:'),
|
|
|
|
Text(post.hotRank.toString()),
|
|
|
|
]),
|
|
|
|
TableRow(children: [
|
|
|
|
Text('hotrank active:'),
|
|
|
|
Text(post.hotRankActive.toString()),
|
|
|
|
]),
|
|
|
|
TableRow(children: [
|
|
|
|
Text('published:'),
|
|
|
|
Text(
|
|
|
|
'''${DateFormat.yMMMd().format(post.published)}'''
|
|
|
|
''' ${DateFormat.Hms().format(post.published)}'''),
|
|
|
|
]),
|
|
|
|
TableRow(children: [
|
|
|
|
Text('updated:'),
|
|
|
|
Text(post.updated != null
|
|
|
|
? '''${DateFormat.yMMMd().format(post.updated)}'''
|
|
|
|
''' ${DateFormat.Hms().format(post.updated)}'''
|
|
|
|
: 'never'),
|
|
|
|
]),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
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);
|
2020-09-12 09:30:22 +02:00
|
|
|
void _openLink() =>
|
2020-09-12 17:05:52 +02:00
|
|
|
linkLauncher(context: context, url: post.url, instanceUrl: instanceUrl);
|
2020-08-31 21:03:50 +02:00
|
|
|
|
2020-09-09 20:52:31 +02:00
|
|
|
final urlDomain = () {
|
2020-09-28 23:28:41 +02:00
|
|
|
if (whatType(post.url) == MediaType.none) return null;
|
2020-09-09 20:52:31 +02:00
|
|
|
|
2020-09-16 23:22:04 +02:00
|
|
|
final url = post.url.split('/')[2];
|
2020-09-09 20:52:31 +02:00
|
|
|
if (url.startsWith('www.')) return url.substring(4);
|
|
|
|
return url;
|
|
|
|
}();
|
|
|
|
|
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(
|
2020-09-09 17:41:54 +02:00
|
|
|
onTap: () => goToCommunity.byId(
|
|
|
|
context, instanceUrl, post.communityId),
|
2020-08-31 21:35:54 +02:00
|
|
|
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()
|
2020-09-09 17:41:54 +02:00
|
|
|
..onTap = () => goToCommunity.byId(
|
|
|
|
context, instanceUrl, post.communityId)),
|
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()
|
2020-09-09 17:41:54 +02:00
|
|
|
..onTap =
|
|
|
|
() => goToInstance(context, instanceUrl)),
|
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()
|
2020-09-09 17:41:54 +02:00
|
|
|
..onTap = () => goToUser.byId(
|
|
|
|
context, post.instanceUrl, post.creatorId),
|
2020-08-31 21:35:54 +02:00
|
|
|
),
|
|
|
|
TextSpan(
|
|
|
|
text:
|
|
|
|
''' · ${timeago.format(post.published, locale: 'en_short')}'''),
|
2020-09-03 00:35:08 +02:00
|
|
|
if (post.locked) TextSpan(text: ' · 🔒'),
|
|
|
|
if (post.stickied) TextSpan(text: ' · 📌'),
|
|
|
|
if (post.nsfw) TextSpan(text: ' · '),
|
|
|
|
if (post.nsfw)
|
|
|
|
TextSpan(
|
|
|
|
text: 'NSFW',
|
|
|
|
style: TextStyle(color: Colors.red)),
|
2020-09-09 20:52:31 +02:00
|
|
|
if (urlDomain != null)
|
|
|
|
TextSpan(text: ' · $urlDomain'),
|
2020-09-03 00:35:08 +02:00
|
|
|
if (post.removed) TextSpan(text: ' · REMOVED'),
|
|
|
|
if (post.deleted) TextSpan(text: ' · DELETED'),
|
2020-08-31 21:35:54 +02:00
|
|
|
],
|
|
|
|
))
|
|
|
|
]),
|
|
|
|
],
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
2020-08-31 21:03:50 +02:00
|
|
|
),
|
|
|
|
Spacer(),
|
2020-09-02 00:05:42 +02:00
|
|
|
if (!fullPost)
|
|
|
|
Column(
|
|
|
|
children: [
|
|
|
|
IconButton(
|
2020-09-09 21:23:48 +02:00
|
|
|
onPressed: () => showMoreMenu(context, post),
|
2020-09-02 00:05:42 +02:00
|
|
|
icon: Icon(Icons.more_vert),
|
2020-09-28 23:28:41 +02:00
|
|
|
iconSize: 24,
|
|
|
|
padding: EdgeInsets.all(0),
|
|
|
|
visualDensity: VisualDensity.compact,
|
2020-09-02 00:05:42 +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: [
|
2020-09-09 20:52:31 +02:00
|
|
|
Expanded(
|
|
|
|
flex: 100,
|
2020-08-31 21:35:54 +02:00
|
|
|
child: Text(
|
|
|
|
'${post.name}',
|
|
|
|
textAlign: TextAlign.left,
|
|
|
|
softWrap: true,
|
|
|
|
style: TextStyle(fontSize: 18, fontWeight: FontWeight.w600),
|
|
|
|
),
|
|
|
|
),
|
2020-09-28 23:28:41 +02:00
|
|
|
if (whatType(post.url) == MediaType.other &&
|
2020-09-09 20:52:31 +02:00
|
|
|
post.thumbnailUrl != null) ...[
|
2020-08-31 21:35:54 +02:00
|
|
|
Spacer(),
|
|
|
|
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-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() {
|
|
|
|
assert(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(
|
|
|
|
onTap: _openLink,
|
|
|
|
child: Container(
|
|
|
|
decoration: BoxDecoration(
|
2020-09-03 00:07:07 +02:00
|
|
|
border: Border.all(
|
|
|
|
width: 1,
|
|
|
|
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: [
|
|
|
|
Row(children: [
|
|
|
|
Spacer(),
|
2020-09-09 20:52:31 +02:00
|
|
|
Text('$urlDomain ',
|
2020-08-31 21:03:50 +02:00
|
|
|
style: theme.textTheme.caption
|
|
|
|
.apply(fontStyle: FontStyle.italic)),
|
|
|
|
Icon(Icons.launch, size: 12),
|
|
|
|
]),
|
|
|
|
Row(children: [
|
|
|
|
Flexible(
|
2020-09-28 23:28:41 +02:00
|
|
|
child: Text('${post.embedTitle ?? ''}',
|
2020-08-31 21:03:50 +02:00
|
|
|
style: theme.textTheme.subtitle1
|
|
|
|
.apply(fontWeightDelta: 2)))
|
|
|
|
]),
|
2020-09-28 23:28:41 +02:00
|
|
|
if (post.embedDescription != null &&
|
|
|
|
post.embedDescription.isNotEmpty)
|
2020-09-09 17:41:54 +02:00
|
|
|
Row(children: [
|
|
|
|
Flexible(child: Text(post.embedDescription))
|
|
|
|
]),
|
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() {
|
|
|
|
assert(post.url != null);
|
|
|
|
|
2020-09-11 20:38:50 +02:00
|
|
|
return FullscreenableImage(
|
|
|
|
url: post.url,
|
|
|
|
child: CachedNetworkImage(
|
|
|
|
imageUrl: post.url,
|
2020-09-13 16:05:17 +02:00
|
|
|
errorWidget: (_, __, ___) => 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: [
|
|
|
|
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(),
|
2020-09-02 00:05:42 +02:00
|
|
|
if (!fullPost)
|
|
|
|
IconButton(
|
|
|
|
icon: Icon(Icons.share),
|
|
|
|
onPressed: () => Share.text('Share post url', post.apId,
|
|
|
|
'text/plain')), // TODO: find a way to mark it as url
|
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(
|
|
|
|
boxShadow: [BoxShadow(blurRadius: 10, color: Colors.black54)],
|
2020-09-10 13:11:16 +02:00
|
|
|
color: theme.cardColor,
|
2020-08-31 21:03:50 +02:00
|
|
|
borderRadius: BorderRadius.all(Radius.circular(20)),
|
|
|
|
),
|
|
|
|
child: InkWell(
|
2020-09-10 13:11:16 +02:00
|
|
|
onTap: fullPost
|
|
|
|
? null
|
2020-09-16 22:53:04 +02:00
|
|
|
: () => goTo(
|
|
|
|
context,
|
|
|
|
(context) =>
|
|
|
|
FullPostPage.fromPostView(post)), //, instanceUrl, post.id),
|
2020-08-31 21:03:50 +02:00
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
info(),
|
|
|
|
title(),
|
2020-09-28 23:28:41 +02:00
|
|
|
if (whatType(post.url) != MediaType.other &&
|
|
|
|
whatType(post.url) != MediaType.none)
|
2020-08-31 21:03:50 +02:00
|
|
|
postImage()
|
2020-09-28 23:28:41 +02:00
|
|
|
else if (post.url != null && post.url.isNotEmpty)
|
2020-08-31 21:03:50 +02:00
|
|
|
linkPreview(),
|
|
|
|
if (post.body != null)
|
2020-09-30 19:05:00 +02:00
|
|
|
// TODO: trim content
|
2020-08-31 21:03:50 +02:00
|
|
|
Padding(
|
|
|
|
padding: const EdgeInsets.all(10),
|
2020-09-12 09:30:22 +02:00
|
|
|
child: MarkdownText(post.body, instanceUrl: instanceUrl)),
|
2020-08-31 21:03:50 +02:00
|
|
|
actions(),
|
|
|
|
],
|
|
|
|
),
|
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)
|
|
|
|
: 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);
|
2020-09-17 16:46:40 +02:00
|
|
|
final loading = useDelayedLoading(Duration(milliseconds: 500));
|
2020-09-18 21:56:42 +02:00
|
|
|
final loggedInAction = useLoggedInAction(post.instanceUrl);
|
2020-09-17 16:46:40 +02:00
|
|
|
|
2020-09-18 21:56:42 +02:00
|
|
|
vote(VoteType vote, Jwt token) async {
|
2020-09-17 16:46:40 +02:00
|
|
|
final api = LemmyApi(post.instanceUrl).v1;
|
|
|
|
|
|
|
|
loading.start();
|
|
|
|
try {
|
|
|
|
final res = await api.createPostLike(
|
|
|
|
postId: post.id, score: vote, auth: token.raw);
|
|
|
|
myVote.value = res.myVote;
|
2020-09-18 16:16:39 +02:00
|
|
|
// ignore: avoid_catches_without_on_clauses
|
2020-09-17 16:46:40 +02:00
|
|
|
} catch (e) {
|
|
|
|
Scaffold.of(context)
|
|
|
|
.showSnackBar(SnackBar(content: Text('voting failed :(')));
|
|
|
|
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)
|
|
|
|
SizedBox(child: CircularProgressIndicator(), width: 20, height: 20)
|
|
|
|
else
|
2020-09-21 12:47:16 +02:00
|
|
|
Text(NumberFormat.compact()
|
|
|
|
.format(post.score + (wasVoted ? 0 : myVote.value.value))),
|
2020-09-17 16:46:40 +02:00
|
|
|
IconButton(
|
2020-09-18 21:56:42 +02:00
|
|
|
icon: Icon(
|
|
|
|
Icons.arrow_downward,
|
2020-09-18 22:07:48 +02:00
|
|
|
color: myVote.value == VoteType.down ? Colors.red : null,
|
2020-09-18 21:56:42 +02:00
|
|
|
),
|
|
|
|
onPressed: loggedInAction(
|
|
|
|
(token) => vote(
|
2020-09-18 22:07:48 +02:00
|
|
|
myVote.value == VoteType.down ? VoteType.none : VoteType.down,
|
|
|
|
token,
|
|
|
|
),
|
2020-09-18 21:56:42 +02:00
|
|
|
)),
|
2020-09-17 16:46:40 +02:00
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|