Allow user delete uploaded attachment

This commit is contained in:
LittleSheep 2024-11-11 21:57:09 +08:00
parent 7087c41f07
commit 5b198412f6
2 changed files with 38 additions and 0 deletions

View File

@ -363,6 +363,11 @@ class PostWriteController extends ChangeNotifier {
notifyListeners(); notifyListeners();
} }
void setIsBusy(bool value) {
isBusy = value;
notifyListeners();
}
@override @override
void dispose() { void dispose() {
contentController.dispose(); contentController.dispose();

View File

@ -9,9 +9,12 @@ import 'package:flutter/material.dart';
import 'package:flutter_context_menu/flutter_context_menu.dart'; import 'package:flutter_context_menu/flutter_context_menu.dart';
import 'package:gap/gap.dart'; import 'package:gap/gap.dart';
import 'package:material_symbols_icons/symbols.dart'; import 'package:material_symbols_icons/symbols.dart';
import 'package:provider/provider.dart';
import 'package:styled_widget/styled_widget.dart'; import 'package:styled_widget/styled_widget.dart';
import 'package:surface/controllers/post_write_controller.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/attachment/attachment_detail.dart';
import 'package:surface/widgets/dialog.dart';
class PostMediaPendingList extends StatelessWidget { class PostMediaPendingList extends StatelessWidget {
final PostWriteController controller; final PostWriteController controller;
@ -36,6 +39,8 @@ class PostMediaPendingList extends StatelessWidget {
if (result == null) return; if (result == null) return;
if (!context.mounted) return; if (!context.mounted) return;
controller.setIsBusy(true);
final rawBytes = final rawBytes =
(await result.uiImage.toByteData(format: ImageByteFormat.png))! (await result.uiImage.toByteData(format: ImageByteFormat.png))!
.buffer .buffer
@ -44,6 +49,26 @@ class PostMediaPendingList extends StatelessWidget {
idx, idx,
PostWriteMedia.fromBytes(rawBytes, media.name, media.type), 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<SnNetworkProvider>();
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 @override
@ -84,6 +109,14 @@ class PostMediaPendingList extends StatelessWidget {
icon: Symbols.crop, icon: Symbols.crop,
onSelected: () => _cropImage(context, idx), 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) if (media.attachment == null)
MenuItem( MenuItem(
label: 'delete'.tr(), label: 'delete'.tr(),