From 5b198412f699116a36030709b99faf230e15ea3c Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Mon, 11 Nov 2024 21:57:09 +0800 Subject: [PATCH] :sparkles: Allow user delete uploaded attachment --- lib/controllers/post_write_controller.dart | 5 +++ lib/widgets/post/post_media_pending_list.dart | 33 +++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/lib/controllers/post_write_controller.dart b/lib/controllers/post_write_controller.dart index 0877a07..b881eb1 100644 --- a/lib/controllers/post_write_controller.dart +++ b/lib/controllers/post_write_controller.dart @@ -363,6 +363,11 @@ class PostWriteController extends ChangeNotifier { notifyListeners(); } + void setIsBusy(bool value) { + isBusy = value; + notifyListeners(); + } + @override void dispose() { contentController.dispose(); diff --git a/lib/widgets/post/post_media_pending_list.dart b/lib/widgets/post/post_media_pending_list.dart index f46ccb6..976f094 100644 --- a/lib/widgets/post/post_media_pending_list.dart +++ b/lib/widgets/post/post_media_pending_list.dart @@ -9,9 +9,12 @@ import 'package:flutter/material.dart'; import 'package:flutter_context_menu/flutter_context_menu.dart'; import 'package:gap/gap.dart'; import 'package:material_symbols_icons/symbols.dart'; +import 'package:provider/provider.dart'; import 'package:styled_widget/styled_widget.dart'; import 'package:surface/controllers/post_write_controller.dart'; +import 'package:surface/providers/sn_network.dart'; import 'package:surface/widgets/attachment/attachment_detail.dart'; +import 'package:surface/widgets/dialog.dart'; class PostMediaPendingList extends StatelessWidget { final PostWriteController controller; @@ -36,6 +39,8 @@ class PostMediaPendingList extends StatelessWidget { if (result == null) return; if (!context.mounted) return; + controller.setIsBusy(true); + final rawBytes = (await result.uiImage.toByteData(format: ImageByteFormat.png))! .buffer @@ -44,6 +49,26 @@ class PostMediaPendingList extends StatelessWidget { idx, PostWriteMedia.fromBytes(rawBytes, media.name, media.type), ); + + controller.setIsBusy(false); + } + + void _deleteAttachment(BuildContext context, int idx) async { + final media = controller.attachments[idx]; + if (media.attachment == null) return; + + controller.setIsBusy(true); + + try { + final sn = context.read(); + await sn.client.delete('/cgi/uc/attachments/${media.attachment!.id}'); + controller.removeAttachmentAt(idx); + } catch (err) { + if (!context.mounted) return; + context.showErrorDialog(err); + } finally { + controller.setIsBusy(false); + } } @override @@ -84,6 +109,14 @@ class PostMediaPendingList extends StatelessWidget { icon: Symbols.crop, onSelected: () => _cropImage(context, idx), ), + if (media.attachment != null) + MenuItem( + label: 'delete'.tr(), + icon: Symbols.delete, + onSelected: controller.isBusy + ? null + : () => _deleteAttachment(context, idx), + ), if (media.attachment == null) MenuItem( label: 'delete'.tr(),