2024-11-11 13:48:50 +00:00
|
|
|
import 'dart:io';
|
|
|
|
import 'dart:ui';
|
|
|
|
|
|
|
|
import 'package:croppy/croppy.dart';
|
|
|
|
import 'package:dismissible_page/dismissible_page.dart';
|
2024-11-10 08:41:11 +00:00
|
|
|
import 'package:easy_localization/easy_localization.dart';
|
2024-11-11 13:48:50 +00:00
|
|
|
import 'package:flutter/foundation.dart';
|
2024-11-10 08:41:11 +00:00
|
|
|
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';
|
2024-11-11 13:57:09 +00:00
|
|
|
import 'package:provider/provider.dart';
|
2024-11-11 13:30:05 +00:00
|
|
|
import 'package:styled_widget/styled_widget.dart';
|
|
|
|
import 'package:surface/controllers/post_write_controller.dart';
|
2024-11-11 13:57:09 +00:00
|
|
|
import 'package:surface/providers/sn_network.dart';
|
2024-11-11 13:48:50 +00:00
|
|
|
import 'package:surface/widgets/attachment/attachment_detail.dart';
|
2024-11-11 13:57:09 +00:00
|
|
|
import 'package:surface/widgets/dialog.dart';
|
2024-11-10 08:41:11 +00:00
|
|
|
|
|
|
|
class PostMediaPendingList extends StatelessWidget {
|
2024-11-17 16:55:39 +00:00
|
|
|
final List<PostWriteMedia> attachments;
|
|
|
|
final bool isBusy;
|
|
|
|
final Future<void> Function(int idx, PostWriteMedia updatedMedia)? onUpdate;
|
|
|
|
final Future<void> Function(int idx)? onRemove;
|
|
|
|
final void Function(bool state)? onUpdateBusy;
|
|
|
|
|
2024-11-23 09:32:48 +00:00
|
|
|
const PostMediaPendingList({
|
2024-11-17 16:55:39 +00:00
|
|
|
super.key,
|
|
|
|
required this.attachments,
|
|
|
|
required this.isBusy,
|
|
|
|
this.onUpdate,
|
|
|
|
this.onRemove,
|
|
|
|
this.onUpdateBusy,
|
|
|
|
});
|
|
|
|
|
|
|
|
Future<void> _cropImage(BuildContext context, int idx) async {
|
|
|
|
final media = attachments[idx];
|
2024-11-11 13:48:50 +00:00
|
|
|
final result = (!kIsWeb && (Platform.isIOS || Platform.isMacOS))
|
|
|
|
? await showCupertinoImageCropper(
|
2024-11-18 14:52:22 +00:00
|
|
|
// ignore: use_build_context_synchronously
|
2024-11-11 13:48:50 +00:00
|
|
|
context,
|
2024-11-18 14:52:22 +00:00
|
|
|
// ignore: use_build_context_synchronously
|
2024-11-11 13:48:50 +00:00
|
|
|
imageProvider: media.getImageProvider(context)!,
|
|
|
|
)
|
|
|
|
: await showMaterialImageCropper(
|
2024-11-18 14:52:22 +00:00
|
|
|
// ignore: use_build_context_synchronously
|
2024-11-11 13:48:50 +00:00
|
|
|
context,
|
2024-11-18 14:52:22 +00:00
|
|
|
// ignore: use_build_context_synchronously
|
2024-11-11 13:48:50 +00:00
|
|
|
imageProvider: media.getImageProvider(context)!,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (result == null) return;
|
2024-11-11 13:57:09 +00:00
|
|
|
|
2024-11-11 13:48:50 +00:00
|
|
|
final rawBytes =
|
|
|
|
(await result.uiImage.toByteData(format: ImageByteFormat.png))!
|
|
|
|
.buffer
|
|
|
|
.asUint8List();
|
2024-11-11 13:57:09 +00:00
|
|
|
|
2024-11-17 16:55:39 +00:00
|
|
|
if (onUpdate != null) {
|
|
|
|
final updatedMedia = PostWriteMedia.fromBytes(
|
|
|
|
rawBytes,
|
|
|
|
media.name,
|
|
|
|
media.type,
|
|
|
|
);
|
|
|
|
await onUpdate!(idx, updatedMedia);
|
|
|
|
}
|
2024-11-11 13:57:09 +00:00
|
|
|
}
|
|
|
|
|
2024-11-17 16:55:39 +00:00
|
|
|
Future<void> _deleteAttachment(BuildContext context, int idx) async {
|
|
|
|
final media = attachments[idx];
|
2024-11-11 13:57:09 +00:00
|
|
|
if (media.attachment == null) return;
|
|
|
|
|
|
|
|
try {
|
2024-11-17 16:55:39 +00:00
|
|
|
onUpdateBusy?.call(true);
|
2024-11-11 13:57:09 +00:00
|
|
|
final sn = context.read<SnNetworkProvider>();
|
|
|
|
await sn.client.delete('/cgi/uc/attachments/${media.attachment!.id}');
|
2024-11-17 16:55:39 +00:00
|
|
|
onRemove!(idx);
|
2024-11-11 13:57:09 +00:00
|
|
|
} catch (err) {
|
|
|
|
if (!context.mounted) return;
|
|
|
|
context.showErrorDialog(err);
|
|
|
|
} finally {
|
2024-11-17 16:55:39 +00:00
|
|
|
onUpdateBusy?.call(false);
|
2024-11-11 13:57:09 +00:00
|
|
|
}
|
2024-11-11 13:48:50 +00:00
|
|
|
}
|
2024-11-10 08:41:11 +00:00
|
|
|
|
2024-11-23 09:32:48 +00:00
|
|
|
ContextMenu _buildContextMenu(
|
|
|
|
BuildContext context, int idx, PostWriteMedia media) {
|
|
|
|
return ContextMenu(
|
|
|
|
entries: [
|
|
|
|
if (media.type == PostWriteMediaType.image && media.attachment != null)
|
|
|
|
MenuItem(
|
|
|
|
label: 'preview'.tr(),
|
|
|
|
icon: Symbols.preview,
|
|
|
|
onSelected: () {
|
|
|
|
context.pushTransparentRoute(
|
2024-11-25 14:41:15 +00:00
|
|
|
AttachmentZoomView(data: [media.attachment!]),
|
2024-11-23 09:32:48 +00:00
|
|
|
rootNavigator: true,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
if (media.type == PostWriteMediaType.image && media.attachment == null)
|
|
|
|
MenuItem(
|
|
|
|
label: 'crop'.tr(),
|
|
|
|
icon: Symbols.crop,
|
|
|
|
onSelected: () => _cropImage(context, idx),
|
|
|
|
),
|
|
|
|
if (media.attachment != null && onRemove != null)
|
|
|
|
MenuItem(
|
|
|
|
label: 'delete'.tr(),
|
|
|
|
icon: Symbols.delete,
|
|
|
|
onSelected: isBusy ? null : () => _deleteAttachment(context, idx),
|
|
|
|
),
|
|
|
|
if (media.attachment == null && onRemove != null)
|
|
|
|
MenuItem(
|
|
|
|
label: 'delete'.tr(),
|
|
|
|
icon: Symbols.delete,
|
|
|
|
onSelected: () {
|
|
|
|
onRemove!(idx);
|
|
|
|
},
|
|
|
|
)
|
|
|
|
else if (onRemove != null)
|
|
|
|
MenuItem(
|
|
|
|
label: 'unlink'.tr(),
|
|
|
|
icon: Symbols.link_off,
|
|
|
|
onSelected: () {
|
|
|
|
onRemove!(idx);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-11-10 08:41:11 +00:00
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-11-11 13:30:05 +00:00
|
|
|
final devicePixelRatio = MediaQuery.of(context).devicePixelRatio;
|
|
|
|
|
2024-11-17 16:55:39 +00:00
|
|
|
return Container(
|
|
|
|
constraints: const BoxConstraints(maxHeight: 120),
|
|
|
|
child: ListView.separated(
|
|
|
|
scrollDirection: Axis.horizontal,
|
|
|
|
padding: const EdgeInsets.symmetric(horizontal: 8),
|
|
|
|
separatorBuilder: (context, index) => const Gap(8),
|
|
|
|
itemCount: attachments.length,
|
|
|
|
itemBuilder: (context, idx) {
|
|
|
|
final media = attachments[idx];
|
|
|
|
return ContextMenuRegion(
|
2024-11-23 09:32:48 +00:00
|
|
|
contextMenu: _buildContextMenu(context, idx, media),
|
2024-11-17 16:55:39 +00:00
|
|
|
child: Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
border: Border.all(
|
|
|
|
color: Theme.of(context).dividerColor,
|
|
|
|
width: 1,
|
2024-11-11 13:48:50 +00:00
|
|
|
),
|
2024-11-17 16:55:39 +00:00
|
|
|
borderRadius: BorderRadius.circular(8),
|
|
|
|
),
|
|
|
|
child: ClipRRect(
|
|
|
|
borderRadius: const BorderRadius.all(Radius.circular(8)),
|
|
|
|
child: AspectRatio(
|
|
|
|
aspectRatio: 1,
|
|
|
|
child: switch (media.type) {
|
|
|
|
PostWriteMediaType.image =>
|
|
|
|
LayoutBuilder(builder: (context, constraints) {
|
|
|
|
return Image(
|
|
|
|
image: media.getImageProvider(
|
|
|
|
context,
|
|
|
|
width: (constraints.maxWidth * devicePixelRatio)
|
|
|
|
.round(),
|
|
|
|
height: (constraints.maxHeight * devicePixelRatio)
|
|
|
|
.round(),
|
|
|
|
)!,
|
|
|
|
fit: BoxFit.cover,
|
|
|
|
);
|
|
|
|
}),
|
|
|
|
_ => Container(
|
|
|
|
color: Theme.of(context).colorScheme.surface,
|
|
|
|
child: const Icon(Symbols.docs).center(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
},
|
|
|
|
),
|
2024-11-10 08:41:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|