2024-05-21 16:05:03 +00:00
|
|
|
import 'dart:math';
|
|
|
|
|
2024-08-16 15:08:05 +00:00
|
|
|
import 'package:firebase_analytics/firebase_analytics.dart';
|
2024-05-19 12:30:50 +00:00
|
|
|
import 'package:flutter/material.dart';
|
2024-05-21 16:05:03 +00:00
|
|
|
import 'package:flutter_animate/flutter_animate.dart';
|
2024-05-23 12:00:26 +00:00
|
|
|
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
|
2024-05-19 12:30:50 +00:00
|
|
|
import 'package:get/get.dart';
|
2024-07-26 18:11:59 +00:00
|
|
|
import 'package:share_plus/share_plus.dart';
|
2024-05-19 16:08:20 +00:00
|
|
|
import 'package:solian/exts.dart';
|
2024-05-19 12:30:50 +00:00
|
|
|
import 'package:solian/models/post.dart';
|
2024-08-04 10:32:16 +00:00
|
|
|
import 'package:solian/platform.dart';
|
2024-05-19 12:30:50 +00:00
|
|
|
import 'package:solian/providers/auth.dart';
|
2024-05-21 16:05:03 +00:00
|
|
|
import 'package:solian/router.dart';
|
2024-07-10 02:50:10 +00:00
|
|
|
import 'package:solian/screens/posts/post_editor.dart';
|
2024-05-19 12:30:50 +00:00
|
|
|
|
2024-05-21 16:05:03 +00:00
|
|
|
class PostAction extends StatefulWidget {
|
2024-05-19 12:30:50 +00:00
|
|
|
final Post item;
|
2024-07-09 14:39:44 +00:00
|
|
|
final bool noReact;
|
2024-05-19 12:30:50 +00:00
|
|
|
|
2024-07-09 14:39:44 +00:00
|
|
|
const PostAction({super.key, required this.item, this.noReact = false});
|
2024-05-19 12:30:50 +00:00
|
|
|
|
|
|
|
@override
|
2024-05-21 16:05:03 +00:00
|
|
|
State<PostAction> createState() => _PostActionState();
|
2024-05-19 12:30:50 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 16:05:03 +00:00
|
|
|
class _PostActionState extends State<PostAction> {
|
|
|
|
bool _isBusy = true;
|
|
|
|
bool _canModifyContent = false;
|
|
|
|
|
2024-07-27 06:32:31 +00:00
|
|
|
void _checkAbleToModifyContent() async {
|
2024-07-24 17:18:47 +00:00
|
|
|
final AuthProvider auth = Get.find();
|
|
|
|
if (auth.isAuthorized.isFalse) return;
|
2024-05-21 16:05:03 +00:00
|
|
|
|
|
|
|
setState(() => _isBusy = true);
|
|
|
|
|
|
|
|
setState(() {
|
2024-07-26 10:23:51 +00:00
|
|
|
_canModifyContent =
|
|
|
|
auth.userProfile.value!['id'] == widget.item.author.externalId;
|
2024-05-21 16:05:03 +00:00
|
|
|
_isBusy = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-04 10:32:16 +00:00
|
|
|
Future<void> _doShare({bool noUri = false}) async {
|
2024-08-16 15:08:05 +00:00
|
|
|
ShareResult result;
|
2024-07-27 06:32:31 +00:00
|
|
|
final box = context.findRenderObject() as RenderBox?;
|
2024-08-04 10:32:16 +00:00
|
|
|
if ((PlatformInfo.isAndroid || PlatformInfo.isIOS) && !noUri) {
|
2024-08-16 15:08:05 +00:00
|
|
|
result = await Share.shareUri(
|
2024-08-10 10:24:47 +00:00
|
|
|
Uri.parse('https://solsynth.dev/posts/${widget.item.id}'),
|
2024-08-04 10:32:16 +00:00
|
|
|
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
final extraContent = [
|
|
|
|
widget.item.body['title'],
|
|
|
|
widget.item.body['description'],
|
|
|
|
];
|
|
|
|
final isExtraNotEmpty = extraContent.any((x) => x != null);
|
2024-08-16 15:08:05 +00:00
|
|
|
result = await Share.share(
|
2024-08-04 10:32:16 +00:00
|
|
|
'postShareContent'.trParams({
|
|
|
|
'username': widget.item.author.nick,
|
|
|
|
'content':
|
|
|
|
'${extraContent.join('\n')}${isExtraNotEmpty ? '\n\n' : ''}${widget.item.body['content'] ?? 'no content'}',
|
2024-08-10 10:24:47 +00:00
|
|
|
'link': 'https://solsynth.dev/posts/${widget.item.id}',
|
2024-08-04 10:32:16 +00:00
|
|
|
}),
|
|
|
|
subject: 'postShareSubject'.trParams({
|
|
|
|
'username': widget.item.author.nick,
|
|
|
|
}),
|
|
|
|
sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size,
|
|
|
|
);
|
|
|
|
}
|
2024-08-16 15:08:05 +00:00
|
|
|
|
|
|
|
if (result.status != ShareResultStatus.dismissed) {
|
|
|
|
await FirebaseAnalytics.instance.logShare(
|
|
|
|
contentType: 'Post',
|
|
|
|
itemId: widget.item.id.toString(),
|
|
|
|
method: result.raw,
|
|
|
|
);
|
|
|
|
}
|
2024-07-27 06:32:31 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 16:05:03 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
super.initState();
|
2024-07-27 06:32:31 +00:00
|
|
|
_checkAbleToModifyContent();
|
2024-05-21 16:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
return SafeArea(
|
|
|
|
child: Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Column(
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
children: [
|
|
|
|
Text(
|
|
|
|
'postActionList'.tr,
|
|
|
|
style: Theme.of(context).textTheme.headlineSmall,
|
|
|
|
),
|
|
|
|
Text(
|
|
|
|
'#${widget.item.id.toString().padLeft(8, '0')}',
|
|
|
|
style: Theme.of(context).textTheme.bodySmall,
|
|
|
|
),
|
|
|
|
],
|
|
|
|
).paddingOnly(left: 24, right: 24, top: 32, bottom: 16),
|
|
|
|
if (_isBusy) const LinearProgressIndicator().animate().scaleX(),
|
|
|
|
Expanded(
|
|
|
|
child: ListView(
|
|
|
|
children: [
|
2024-07-26 18:11:59 +00:00
|
|
|
ListTile(
|
|
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
|
|
|
|
leading: const Icon(Icons.share),
|
|
|
|
title: Text('share'.tr),
|
2024-08-04 10:32:16 +00:00
|
|
|
trailing: PlatformInfo.isIOS || PlatformInfo.isAndroid
|
|
|
|
? IconButton(
|
|
|
|
icon: const Icon(Icons.link_off),
|
|
|
|
tooltip: 'shareNoUri'.tr,
|
|
|
|
onPressed: () async {
|
|
|
|
await _doShare(noUri: true);
|
|
|
|
Navigator.pop(context);
|
|
|
|
},
|
|
|
|
)
|
|
|
|
: null,
|
2024-07-26 18:11:59 +00:00
|
|
|
onTap: () async {
|
2024-07-27 06:32:31 +00:00
|
|
|
await _doShare();
|
2024-07-26 18:11:59 +00:00
|
|
|
Navigator.pop(context);
|
|
|
|
},
|
|
|
|
),
|
2024-07-09 14:39:44 +00:00
|
|
|
if (!widget.noReact)
|
|
|
|
ListTile(
|
|
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
|
|
|
|
leading: const FaIcon(FontAwesomeIcons.reply, size: 20),
|
|
|
|
title: Text('reply'.tr),
|
|
|
|
onTap: () async {
|
2024-08-04 10:13:59 +00:00
|
|
|
Navigator.pop(
|
|
|
|
context,
|
|
|
|
AppRouter.instance.pushNamed(
|
|
|
|
'postEditor',
|
|
|
|
extra: PostPublishArguments(reply: widget.item),
|
|
|
|
),
|
2024-07-09 14:39:44 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
if (!widget.noReact)
|
|
|
|
ListTile(
|
|
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
|
|
|
|
leading: const FaIcon(FontAwesomeIcons.retweet, size: 20),
|
|
|
|
title: Text('repost'.tr),
|
|
|
|
onTap: () async {
|
2024-08-04 10:13:59 +00:00
|
|
|
Navigator.pop(
|
|
|
|
context,
|
|
|
|
AppRouter.instance.pushNamed(
|
|
|
|
'postEditor',
|
|
|
|
extra: PostPublishArguments(repost: widget.item),
|
|
|
|
),
|
2024-07-09 14:39:44 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
if (_canModifyContent && !widget.noReact)
|
2024-05-21 16:05:03 +00:00
|
|
|
const Divider(thickness: 0.3, height: 0.3)
|
|
|
|
.paddingSymmetric(vertical: 16),
|
2024-07-26 10:23:51 +00:00
|
|
|
if (_canModifyContent)
|
|
|
|
ListTile(
|
|
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
|
|
|
|
leading: const Icon(Icons.push_pin),
|
|
|
|
title: Text(
|
|
|
|
widget.item.pinnedAt == null
|
|
|
|
? 'pinPost'.tr
|
|
|
|
: 'unpinPost'.tr,
|
|
|
|
),
|
|
|
|
onTap: () async {
|
2024-07-26 18:11:59 +00:00
|
|
|
final client = Get.find<AuthProvider>()
|
|
|
|
.configureClient('interactive');
|
2024-07-26 10:23:51 +00:00
|
|
|
await client.post('/posts/${widget.item.id}/pin', {});
|
|
|
|
Navigator.pop(context, true);
|
|
|
|
},
|
|
|
|
),
|
2024-05-21 16:05:03 +00:00
|
|
|
if (_canModifyContent)
|
|
|
|
ListTile(
|
|
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
|
|
|
|
leading: const Icon(Icons.edit),
|
|
|
|
title: Text('edit'.tr),
|
|
|
|
onTap: () async {
|
2024-08-04 10:13:59 +00:00
|
|
|
Navigator.pop(
|
|
|
|
context,
|
|
|
|
AppRouter.instance.pushNamed(
|
|
|
|
'postEditor',
|
|
|
|
extra: PostPublishArguments(edit: widget.item),
|
|
|
|
),
|
2024-05-21 16:05:03 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
if (_canModifyContent)
|
|
|
|
ListTile(
|
|
|
|
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
|
|
|
|
leading: const Icon(Icons.delete),
|
|
|
|
title: Text('delete'.tr),
|
|
|
|
onTap: () async {
|
|
|
|
final value = await showDialog(
|
|
|
|
context: context,
|
|
|
|
builder: (context) =>
|
|
|
|
PostDeletionDialog(item: widget.item),
|
|
|
|
);
|
|
|
|
if (value != null) {
|
|
|
|
Navigator.pop(context, true);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
2024-05-19 12:30:50 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
2024-05-21 16:05:03 +00:00
|
|
|
}
|
2024-05-19 12:30:50 +00:00
|
|
|
|
2024-05-21 16:05:03 +00:00
|
|
|
class PostDeletionDialog extends StatefulWidget {
|
|
|
|
final Post item;
|
2024-05-19 12:30:50 +00:00
|
|
|
|
2024-05-21 16:05:03 +00:00
|
|
|
const PostDeletionDialog({super.key, required this.item});
|
|
|
|
|
|
|
|
@override
|
|
|
|
State<PostDeletionDialog> createState() => _PostDeletionDialogState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _PostDeletionDialogState extends State<PostDeletionDialog> {
|
|
|
|
bool _isBusy = false;
|
|
|
|
|
2024-07-27 06:32:31 +00:00
|
|
|
void _performAction() async {
|
2024-05-21 16:05:03 +00:00
|
|
|
final AuthProvider auth = Get.find();
|
2024-07-24 17:18:47 +00:00
|
|
|
if (auth.isAuthorized.isFalse) return;
|
2024-05-19 12:30:50 +00:00
|
|
|
|
2024-06-22 14:39:32 +00:00
|
|
|
final client = auth.configureClient('interactive');
|
2024-05-19 12:30:50 +00:00
|
|
|
|
2024-05-21 16:05:03 +00:00
|
|
|
setState(() => _isBusy = true);
|
2024-07-16 11:46:53 +00:00
|
|
|
final resp = await client.delete('/posts/${widget.item.id}');
|
2024-05-21 16:05:03 +00:00
|
|
|
setState(() => _isBusy = false);
|
2024-05-19 12:30:50 +00:00
|
|
|
|
2024-05-21 16:05:03 +00:00
|
|
|
if (resp.statusCode != 200) {
|
2024-05-19 16:08:20 +00:00
|
|
|
context.showErrorDialog(resp.bodyString);
|
2024-05-21 16:05:03 +00:00
|
|
|
} else {
|
|
|
|
Navigator.pop(context, true);
|
2024-05-19 12:30:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-05-21 16:05:03 +00:00
|
|
|
return AlertDialog(
|
|
|
|
title: Text('postDeletionConfirm'.tr),
|
|
|
|
content: Text('postDeletionConfirmCaption'.trParams({
|
2024-07-23 10:09:41 +00:00
|
|
|
'content': widget.item.body['content']
|
|
|
|
.substring(0, min<int>(widget.item.body['content'].length, 60))
|
2024-05-21 16:05:03 +00:00
|
|
|
.trim(),
|
|
|
|
})),
|
|
|
|
actions: <Widget>[
|
|
|
|
TextButton(
|
|
|
|
onPressed: _isBusy ? null : () => Navigator.pop(context),
|
|
|
|
child: Text('cancel'.tr),
|
|
|
|
),
|
|
|
|
TextButton(
|
2024-07-27 06:32:31 +00:00
|
|
|
onPressed: _isBusy ? null : () => _performAction(),
|
2024-05-21 16:05:03 +00:00
|
|
|
child: Text('confirm'.tr),
|
|
|
|
),
|
|
|
|
],
|
2024-05-19 12:30:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|