💄 Optimized attachment edit action

This commit is contained in:
LittleSheep 2024-08-01 17:19:55 +08:00
parent 1e4b44a78b
commit adb415700a

View File

@ -18,7 +18,6 @@ import 'package:solian/platform.dart';
import 'package:solian/providers/auth.dart'; import 'package:solian/providers/auth.dart';
import 'package:solian/providers/content/attachment.dart'; import 'package:solian/providers/content/attachment.dart';
import 'package:solian/widgets/attachments/attachment_fullscreen.dart'; import 'package:solian/widgets/attachments/attachment_fullscreen.dart';
import 'package:solian/widgets/attachments/attachment_item.dart';
class AttachmentEditorPopup extends StatefulWidget { class AttachmentEditorPopup extends StatefulWidget {
final String usage; final String usage;
@ -233,10 +232,6 @@ class _AttachmentEditorPopupState extends State<AttachmentEditorPopup> {
builder: (context) { builder: (context) {
return AttachmentEditorDialog( return AttachmentEditorDialog(
item: element, item: element,
onDelete: () {
setState(() => _attachments.removeAt(index));
widget.onUpdate(_attachments.map((e) => e!.id).toList());
},
onUpdate: (item) { onUpdate: (item) {
setState(() => _attachments[index] = item); setState(() => _attachments[index] = item);
widget.onUpdate(_attachments.map((e) => e!.id).toList()); widget.onUpdate(_attachments.map((e) => e!.id).toList());
@ -246,6 +241,18 @@ class _AttachmentEditorPopupState extends State<AttachmentEditorPopup> {
); );
} }
Future<void> _deleteAttachment(Attachment element) async {
setState(() => _isBusy = true);
try {
final AttachmentProvider provider = Get.find();
await provider.deleteAttachment(element.id);
} catch (e) {
context.showErrorDialog(e);
} finally {
setState(() => _isBusy = false);
}
}
Widget _buildListEntry(Attachment element, int index) { Widget _buildListEntry(Attachment element, int index) {
var fileType = element.mimetype.split('/').firstOrNull; var fileType = element.mimetype.split('/').firstOrNull;
fileType ??= 'unknown'; fileType ??= 'unknown';
@ -289,11 +296,40 @@ class _AttachmentEditorPopupState extends State<AttachmentEditorPopup> {
? () => _showAttachmentPreview(element) ? () => _showAttachmentPreview(element)
: null, : null,
), ),
IconButton( PopupMenuButton(
color: Theme.of(context).colorScheme.primary,
visualDensity: const VisualDensity(horizontal: -4),
icon: const Icon(Icons.more_horiz), icon: const Icon(Icons.more_horiz),
onPressed: () => _showEdit(element, index), iconColor: Theme.of(context).colorScheme.primary,
style: const ButtonStyle(
visualDensity: VisualDensity(horizontal: -4),
),
itemBuilder: (BuildContext context) => [
PopupMenuItem(
child: ListTile(
title: Text('edit'.tr),
leading: const Icon(Icons.edit),
contentPadding: const EdgeInsets.symmetric(
horizontal: 8,
),
),
onTap: () => _showEdit(element, index),
),
PopupMenuItem(
child: ListTile(
title: Text('delete'.tr),
leading: const Icon(Icons.delete),
contentPadding: const EdgeInsets.symmetric(
horizontal: 8,
),
),
onTap: () {
_deleteAttachment(element).then((_) {
setState(() => _attachments.removeAt(index));
widget.onUpdate(
_attachments.map((e) => e!.id).toList(),
);
});
}),
],
), ),
], ],
).paddingSymmetric(vertical: 8, horizontal: 16), ).paddingSymmetric(vertical: 8, horizontal: 16),
@ -412,14 +448,13 @@ class _AttachmentEditorPopupState extends State<AttachmentEditorPopup> {
class AttachmentEditorDialog extends StatefulWidget { class AttachmentEditorDialog extends StatefulWidget {
final Attachment item; final Attachment item;
final Function onDelete;
final Function(Attachment item) onUpdate; final Function(Attachment item) onUpdate;
const AttachmentEditorDialog( const AttachmentEditorDialog({
{super.key, super.key,
required this.item, required this.item,
required this.onDelete, required this.onUpdate,
required this.onUpdate}); });
@override @override
State<AttachmentEditorDialog> createState() => _AttachmentEditorDialogState(); State<AttachmentEditorDialog> createState() => _AttachmentEditorDialogState();
@ -431,7 +466,7 @@ class _AttachmentEditorDialogState extends State<AttachmentEditorDialog> {
bool _isBusy = false; bool _isBusy = false;
bool _isMature = false; bool _isMature = false;
Future<Attachment?> updateAttachment() async { Future<Attachment?> _updateAttachment() async {
final AttachmentProvider provider = Get.find(); final AttachmentProvider provider = Get.find();
setState(() => _isBusy = true); setState(() => _isBusy = true);
@ -455,19 +490,6 @@ class _AttachmentEditorDialogState extends State<AttachmentEditorDialog> {
} }
} }
Future<void> deleteAttachment() async {
setState(() => _isBusy = true);
try {
final AttachmentProvider provider = Get.find();
await provider.deleteAttachment(widget.item.id);
widget.onDelete();
} catch (e) {
context.showErrorDialog(e);
} finally {
setState(() => _isBusy = false);
}
}
void syncWidget() { void syncWidget() {
_isMature = widget.item.isMature; _isMature = widget.item.isMature;
_altController.text = widget.item.alt; _altController.text = widget.item.alt;
@ -505,8 +527,9 @@ class _AttachmentEditorDialogState extends State<AttachmentEditorDialog> {
onTapOutside: (_) => onTapOutside: (_) =>
FocusManager.instance.primaryFocus?.unfocus(), FocusManager.instance.primaryFocus?.unfocus(),
), ),
Card( const SizedBox(height: 8),
child: CheckboxListTile( CheckboxListTile(
contentPadding: const EdgeInsets.only(left: 4, right: 18),
shape: const RoundedRectangleBorder( shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10))), borderRadius: BorderRadius.all(Radius.circular(10))),
title: Text('matureContent'.tr), title: Text('matureContent'.tr),
@ -517,22 +540,11 @@ class _AttachmentEditorDialogState extends State<AttachmentEditorDialog> {
}, },
controlAffinity: ListTileControlAffinity.leading, controlAffinity: ListTileControlAffinity.leading,
), ),
),
], ],
), ),
), ),
actionsAlignment: MainAxisAlignment.spaceBetween, actionsAlignment: MainAxisAlignment.spaceBetween,
actions: <Widget>[ actions: <Widget>[
TextButton(
style: TextButton.styleFrom(
foregroundColor: Theme.of(context).colorScheme.error),
onPressed: () {
deleteAttachment().then((_) {
Navigator.pop(context);
});
},
child: Text('delete'.tr),
),
Row( Row(
mainAxisSize: MainAxisSize.min, mainAxisSize: MainAxisSize.min,
children: [ children: [
@ -546,7 +558,7 @@ class _AttachmentEditorDialogState extends State<AttachmentEditorDialog> {
TextButton( TextButton(
child: Text('apply'.tr), child: Text('apply'.tr),
onPressed: () { onPressed: () {
updateAttachment().then((value) { _updateAttachment().then((value) {
if (value != null) { if (value != null) {
widget.onUpdate(value); widget.onUpdate(value);
Navigator.pop(context); Navigator.pop(context);