✨ Post repost, reply and even more optimization!
This commit is contained in:
@ -1,115 +1,192 @@
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_animate/flutter_animate.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:solian/exts.dart';
|
||||
import 'package:solian/models/post.dart';
|
||||
import 'package:solian/models/reaction.dart';
|
||||
import 'package:solian/providers/auth.dart';
|
||||
import 'package:solian/router.dart';
|
||||
import 'package:solian/screens/posts/publish.dart';
|
||||
import 'package:solian/services.dart';
|
||||
import 'package:solian/widgets/posts/post_reaction.dart';
|
||||
|
||||
class PostQuickAction extends StatefulWidget {
|
||||
class PostAction extends StatefulWidget {
|
||||
final Post item;
|
||||
final void Function(String symbol, int num) onReact;
|
||||
|
||||
const PostQuickAction({super.key, required this.item, required this.onReact});
|
||||
const PostAction({super.key, required this.item});
|
||||
|
||||
@override
|
||||
State<PostQuickAction> createState() => _PostQuickActionState();
|
||||
State<PostAction> createState() => _PostActionState();
|
||||
}
|
||||
|
||||
class _PostQuickActionState extends State<PostQuickAction> {
|
||||
bool _isSubmitting = false;
|
||||
class _PostActionState extends State<PostAction> {
|
||||
bool _isBusy = true;
|
||||
bool _canModifyContent = false;
|
||||
|
||||
void showReactMenu() {
|
||||
showModalBottomSheet(
|
||||
useRootNavigator: true,
|
||||
isScrollControlled: true,
|
||||
context: context,
|
||||
builder: (context) => PostReactionPopup(
|
||||
item: widget.item,
|
||||
onReact: (key, value) {
|
||||
doWidgetReact(key, value.attitude);
|
||||
},
|
||||
void checkAbleToModifyContent() async {
|
||||
final AuthProvider provider = Get.find();
|
||||
if (!await provider.isAuthorized) return;
|
||||
|
||||
setState(() => _isBusy = true);
|
||||
|
||||
final prof = await provider.getProfile();
|
||||
setState(() {
|
||||
_canModifyContent = prof.body?['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: [
|
||||
ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
|
||||
leading: const Icon(Icons.reply),
|
||||
title: Text('reply'.tr),
|
||||
onTap: () async {
|
||||
final value = await AppRouter.instance.pushNamed(
|
||||
'postPublishing',
|
||||
extra: PostPublishingArguments(reply: widget.item),
|
||||
);
|
||||
if (value != null) {
|
||||
Navigator.pop(context, true);
|
||||
}
|
||||
},
|
||||
),
|
||||
ListTile(
|
||||
contentPadding: const EdgeInsets.symmetric(horizontal: 24),
|
||||
leading: const Icon(Icons.redo),
|
||||
title: Text('repost'.tr),
|
||||
onTap: () async {
|
||||
final value = await AppRouter.instance.pushNamed(
|
||||
'postPublishing',
|
||||
extra: PostPublishingArguments(repost: widget.item),
|
||||
);
|
||||
if (value != null) {
|
||||
Navigator.pop(context, true);
|
||||
}
|
||||
},
|
||||
),
|
||||
if (_canModifyContent)
|
||||
const Divider(thickness: 0.3, height: 0.3)
|
||||
.paddingSymmetric(vertical: 16),
|
||||
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(
|
||||
'postPublishing',
|
||||
extra: PostPublishingArguments(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);
|
||||
}
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> doWidgetReact(String symbol, int attitude) async {
|
||||
class PostDeletionDialog extends StatefulWidget {
|
||||
final Post item;
|
||||
|
||||
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();
|
||||
|
||||
if (_isSubmitting) return;
|
||||
if (!await auth.isAuthorized) return;
|
||||
|
||||
final client = GetConnect();
|
||||
client.httpClient.baseUrl = ServiceFinder.services['interactive'];
|
||||
client.httpClient.addAuthenticator(auth.reqAuthenticator);
|
||||
|
||||
setState(() => _isSubmitting = true);
|
||||
setState(() => _isBusy = true);
|
||||
final resp = await client.delete('/api/posts/${widget.item.id}');
|
||||
setState(() => _isBusy = false);
|
||||
|
||||
final resp = await client.post('/api/posts/${widget.item.alias}/react', {
|
||||
'symbol': symbol,
|
||||
'attitude': attitude,
|
||||
});
|
||||
if (resp.statusCode == 201) {
|
||||
widget.onReact(symbol, 1);
|
||||
context.showSnackbar('reactCompleted'.tr);
|
||||
} else if (resp.statusCode == 204) {
|
||||
widget.onReact(symbol, -1);
|
||||
context.showSnackbar('reactUncompleted'.tr);
|
||||
} else {
|
||||
if (resp.statusCode != 200) {
|
||||
context.showErrorDialog(resp.bodyString);
|
||||
} else {
|
||||
Navigator.pop(context, true);
|
||||
}
|
||||
|
||||
setState(() => _isSubmitting = false);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
const density = VisualDensity(horizontal: -4, vertical: -3);
|
||||
|
||||
return SizedBox(
|
||||
height: 32,
|
||||
width: double.infinity,
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
ActionChip(
|
||||
avatar: const Icon(Icons.comment),
|
||||
label: Text(widget.item.replyCount.toString()),
|
||||
visualDensity: density,
|
||||
onPressed: () {},
|
||||
),
|
||||
const VerticalDivider(thickness: 0.3, width: 0.3, indent: 8, endIndent: 8).paddingOnly(left: 8),
|
||||
Expanded(
|
||||
child: ListView(
|
||||
shrinkWrap: true,
|
||||
scrollDirection: Axis.horizontal,
|
||||
children: [
|
||||
...widget.item.reactionList.entries.map((x) {
|
||||
final info = reactions[x.key];
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(right: 8),
|
||||
child: ActionChip(
|
||||
avatar: Text(info!.icon),
|
||||
label: Text(x.value.toString()),
|
||||
tooltip: ':${x.key}:',
|
||||
visualDensity: density,
|
||||
onPressed: _isSubmitting ? null : () => doWidgetReact(x.key, info.attitude),
|
||||
),
|
||||
);
|
||||
}),
|
||||
ActionChip(
|
||||
avatar: const Icon(Icons.add_reaction, color: Colors.teal),
|
||||
label: Text('reactAdd'.tr),
|
||||
visualDensity: density,
|
||||
onPressed: () => showReactMenu(),
|
||||
),
|
||||
],
|
||||
).paddingOnly(left: 8),
|
||||
)
|
||||
],
|
||||
),
|
||||
return AlertDialog(
|
||||
title: Text('postDeletionConfirm'.tr),
|
||||
content: Text('postDeletionConfirmCaption'.trParams({
|
||||
'content': widget.item.content
|
||||
.substring(0, min(widget.item.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),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -5,13 +5,18 @@ import 'package:get/get_utils/get_utils.dart';
|
||||
import 'package:solian/models/post.dart';
|
||||
import 'package:solian/widgets/account/account_avatar.dart';
|
||||
import 'package:solian/widgets/attachments/attachment_list.dart';
|
||||
import 'package:solian/widgets/posts/post_action.dart';
|
||||
import 'package:solian/widgets/posts/post_quick_action.dart';
|
||||
import 'package:timeago/timeago.dart' show format;
|
||||
|
||||
class PostItem extends StatefulWidget {
|
||||
final Post item;
|
||||
final bool isReactable;
|
||||
|
||||
const PostItem({super.key, required this.item});
|
||||
const PostItem({
|
||||
super.key,
|
||||
required this.item,
|
||||
this.isReactable = true,
|
||||
});
|
||||
|
||||
@override
|
||||
State<PostItem> createState() => _PostItemState();
|
||||
@ -45,7 +50,8 @@ class _PostItemState extends State<PostItem> {
|
||||
item.author.nick,
|
||||
style: const TextStyle(fontWeight: FontWeight.bold),
|
||||
).paddingOnly(left: 12),
|
||||
Text(format(item.createdAt, locale: 'en_short')).paddingOnly(left: 4),
|
||||
Text(format(item.createdAt, locale: 'en_short'))
|
||||
.paddingOnly(left: 4),
|
||||
],
|
||||
),
|
||||
Markdown(
|
||||
@ -59,17 +65,19 @@ class _PostItemState extends State<PostItem> {
|
||||
)
|
||||
],
|
||||
).paddingOnly(
|
||||
top: 18,
|
||||
top: 10,
|
||||
bottom: hasAttachment ? 10 : 0,
|
||||
right: 16,
|
||||
left: 16,
|
||||
),
|
||||
AttachmentList(attachmentsId: item.attachments ?? List.empty()),
|
||||
PostQuickAction(
|
||||
isReactable: widget.isReactable,
|
||||
item: widget.item,
|
||||
onReact: (symbol, changes) {
|
||||
setState(() {
|
||||
item.reactionList[symbol] = (item.reactionList[symbol] ?? 0) + changes;
|
||||
item.reactionList[symbol] =
|
||||
(item.reactionList[symbol] ?? 0) + changes;
|
||||
});
|
||||
},
|
||||
).paddingOnly(
|
||||
|
141
lib/widgets/posts/post_quick_action.dart
Normal file
141
lib/widgets/posts/post_quick_action.dart
Normal file
@ -0,0 +1,141 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:get/get.dart';
|
||||
import 'package:solian/exts.dart';
|
||||
import 'package:solian/models/post.dart';
|
||||
import 'package:solian/models/reaction.dart';
|
||||
import 'package:solian/providers/auth.dart';
|
||||
import 'package:solian/services.dart';
|
||||
import 'package:solian/widgets/posts/post_reaction.dart';
|
||||
|
||||
class PostQuickAction extends StatefulWidget {
|
||||
final Post item;
|
||||
final bool isReactable;
|
||||
final void Function(String symbol, int num) onReact;
|
||||
|
||||
const PostQuickAction({
|
||||
super.key,
|
||||
required this.item,
|
||||
this.isReactable = true,
|
||||
required this.onReact,
|
||||
});
|
||||
|
||||
@override
|
||||
State<PostQuickAction> createState() => _PostQuickActionState();
|
||||
}
|
||||
|
||||
class _PostQuickActionState extends State<PostQuickAction> {
|
||||
bool _isSubmitting = false;
|
||||
|
||||
void showReactMenu() {
|
||||
showModalBottomSheet(
|
||||
useRootNavigator: true,
|
||||
isScrollControlled: true,
|
||||
context: context,
|
||||
builder: (context) => PostReactionPopup(
|
||||
item: widget.item,
|
||||
onReact: (key, value) {
|
||||
doWidgetReact(key, value.attitude);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> doWidgetReact(String symbol, int attitude) async {
|
||||
if (!widget.isReactable) return;
|
||||
|
||||
final AuthProvider auth = Get.find();
|
||||
|
||||
if (_isSubmitting) return;
|
||||
if (!await auth.isAuthorized) return;
|
||||
|
||||
final client = GetConnect();
|
||||
client.httpClient.baseUrl = ServiceFinder.services['interactive'];
|
||||
client.httpClient.addAuthenticator(auth.reqAuthenticator);
|
||||
|
||||
setState(() => _isSubmitting = true);
|
||||
|
||||
final resp = await client.post('/api/posts/${widget.item.alias}/react', {
|
||||
'symbol': symbol,
|
||||
'attitude': attitude,
|
||||
});
|
||||
if (resp.statusCode == 201) {
|
||||
widget.onReact(symbol, 1);
|
||||
context.showSnackbar('reactCompleted'.tr);
|
||||
} else if (resp.statusCode == 204) {
|
||||
widget.onReact(symbol, -1);
|
||||
context.showSnackbar('reactUncompleted'.tr);
|
||||
} else {
|
||||
context.showErrorDialog(resp.bodyString);
|
||||
}
|
||||
|
||||
setState(() => _isSubmitting = false);
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
if (!widget.isReactable && widget.item.reactionList.isEmpty) {
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) {
|
||||
widget.onReact('thumb_up', 0);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
const density = VisualDensity(horizontal: -4, vertical: -3);
|
||||
|
||||
return SizedBox(
|
||||
height: 32,
|
||||
width: double.infinity,
|
||||
child: Row(
|
||||
mainAxisAlignment: MainAxisAlignment.start,
|
||||
crossAxisAlignment: CrossAxisAlignment.center,
|
||||
children: [
|
||||
if (widget.isReactable)
|
||||
ActionChip(
|
||||
avatar: const Icon(Icons.comment),
|
||||
label: Text(widget.item.replyCount.toString()),
|
||||
visualDensity: density,
|
||||
onPressed: () {},
|
||||
),
|
||||
if (widget.isReactable)
|
||||
const VerticalDivider(
|
||||
thickness: 0.3, width: 0.3, indent: 8, endIndent: 8)
|
||||
.paddingOnly(left: 8),
|
||||
Expanded(
|
||||
child: ListView(
|
||||
shrinkWrap: true,
|
||||
scrollDirection: Axis.horizontal,
|
||||
children: [
|
||||
...widget.item.reactionList.entries.map((x) {
|
||||
final info = reactions[x.key];
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(right: 8),
|
||||
child: ActionChip(
|
||||
avatar: Text(info!.icon),
|
||||
label: Text(x.value.toString()),
|
||||
tooltip: ':${x.key}:',
|
||||
visualDensity: density,
|
||||
onPressed: _isSubmitting
|
||||
? null
|
||||
: () => doWidgetReact(x.key, info.attitude),
|
||||
),
|
||||
);
|
||||
}),
|
||||
if (widget.isReactable)
|
||||
ActionChip(
|
||||
avatar: const Icon(Icons.add_reaction, color: Colors.teal),
|
||||
label: Text('reactAdd'.tr),
|
||||
visualDensity: density,
|
||||
onPressed: () => showReactMenu(),
|
||||
),
|
||||
],
|
||||
).paddingOnly(left: 8),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user