Solian/lib/widgets/posts/post_action.dart

208 lines
6.9 KiB
Dart
Raw Normal View History

import 'dart:math';
2024-05-19 12:30:50 +00:00
import 'package:flutter/material.dart';
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-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';
import 'package:solian/providers/auth.dart';
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
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
State<PostAction> createState() => _PostActionState();
2024-05-19 12:30:50 +00:00
}
class _PostActionState extends State<PostAction> {
bool _isBusy = true;
bool _canModifyContent = false;
void checkAbleToModifyContent() async {
2024-07-24 17:18:47 +00:00
final AuthProvider auth = Get.find();
if (auth.isAuthorized.isFalse) return;
setState(() => _isBusy = true);
setState(() {
_canModifyContent =
auth.userProfile.value!['id'] == widget.item.author.externalId;
_isBusy = false;
});
}
@override
void initState() {
super.initState();
checkAbleToModifyContent();
}
@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-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 {
final value = await AppRouter.instance.pushNamed(
2024-07-10 02:50:10 +00:00
'postEditor',
2024-07-09 14:39:44 +00:00
extra: PostPublishArguments(reply: widget.item),
);
if (value != null) {
Navigator.pop(context, true);
}
},
),
if (!widget.noReact)
ListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
leading: const FaIcon(FontAwesomeIcons.retweet, size: 20),
title: Text('repost'.tr),
onTap: () async {
final value = await AppRouter.instance.pushNamed(
2024-07-10 02:50:10 +00:00
'postEditor',
2024-07-09 14:39:44 +00:00
extra: PostPublishArguments(repost: widget.item),
);
if (value != null) {
Navigator.pop(context, true);
}
},
),
if (_canModifyContent && !widget.noReact)
const Divider(thickness: 0.3, height: 0.3)
.paddingSymmetric(vertical: 16),
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 {
final client = Get.find<AuthProvider>().configureClient('interactive');
await client.post('/posts/${widget.item.id}/pin', {});
Navigator.pop(context, true);
},
),
if (_canModifyContent)
ListTile(
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
leading: const Icon(Icons.edit),
title: Text('edit'.tr),
onTap: () async {
final value = await AppRouter.instance.pushNamed(
2024-07-10 02:50:10 +00:00
'postEditor',
2024-07-09 13:23:38 +00:00
extra: PostPublishArguments(edit: widget.item),
);
if (value != null) {
Navigator.pop(context, true);
}
},
),
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-19 12:30:50 +00:00
class PostDeletionDialog extends StatefulWidget {
final Post item;
2024-05-19 12:30:50 +00:00
const PostDeletionDialog({super.key, required this.item});
@override
State<PostDeletionDialog> createState() => _PostDeletionDialogState();
}
class _PostDeletionDialogState extends State<PostDeletionDialog> {
bool _isBusy = false;
void performAction() async {
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
setState(() => _isBusy = true);
final resp = await client.delete('/posts/${widget.item.id}');
setState(() => _isBusy = false);
2024-05-19 12:30:50 +00:00
if (resp.statusCode != 200) {
2024-05-19 16:08:20 +00:00
context.showErrorDialog(resp.bodyString);
} else {
Navigator.pop(context, true);
2024-05-19 12:30:50 +00:00
}
}
@override
Widget build(BuildContext context) {
return AlertDialog(
title: Text('postDeletionConfirm'.tr),
content: Text('postDeletionConfirmCaption'.trParams({
'content': widget.item.body['content']
.substring(0, min<int>(widget.item.body['content'].length, 60))
.trim(),
})),
actions: <Widget>[
TextButton(
onPressed: _isBusy ? null : () => Navigator.pop(context),
child: Text('cancel'.tr),
),
TextButton(
onPressed: _isBusy ? null : () => performAction(),
child: Text('confirm'.tr),
),
],
2024-05-19 12:30:50 +00:00
);
}
}