Surface/lib/widgets/post/post_media_pending_list.dart

266 lines
9.4 KiB
Dart
Raw Permalink Normal View History

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';
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';
2024-12-07 15:40:26 +00:00
import 'package:surface/widgets/attachment/attachment_zoom.dart';
import 'package:surface/widgets/dialog.dart';
2024-11-10 08:41:11 +00:00
class PostMediaPendingList extends StatelessWidget {
2024-12-07 09:43:44 +00:00
final PostWriteMedia? thumbnail;
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;
2024-12-07 09:43:44 +00:00
final Future<void> Function(int idx)? onUpload;
final void Function(int? idx)? onPostSetThumbnail;
final void Function(int idx)? onInsertLink;
2024-11-17 16:55:39 +00:00
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,
2024-12-07 09:43:44 +00:00
this.thumbnail,
2024-11-17 16:55:39 +00:00
required this.attachments,
required this.isBusy,
this.onUpdate,
this.onRemove,
2024-12-07 09:43:44 +00:00
this.onUpload,
this.onPostSetThumbnail,
this.onInsertLink,
2024-11-17 16:55:39 +00:00
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-12-07 09:43:44 +00:00
final rawBytes = (await result.uiImage.toByteData(format: ImageByteFormat.png))!.buffer.asUint8List();
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-17 16:55:39 +00:00
Future<void> _deleteAttachment(BuildContext context, int idx) async {
2024-12-07 09:43:44 +00:00
final media = idx == -1 ? thumbnail! : attachments[idx];
if (media.attachment == null) return;
try {
2024-11-17 16:55:39 +00:00
onUpdateBusy?.call(true);
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);
} 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:48:50 +00:00
}
2024-11-10 08:41:11 +00:00
2024-12-07 09:43:44 +00:00
ContextMenu _buildContextMenu(BuildContext context, int idx, PostWriteMedia media) {
2024-11-23 09:32:48 +00:00
return ContextMenu(
entries: [
2024-12-07 09:43:44 +00:00
if (media.attachment == null && onUpload != null)
MenuItem(
label: 'attachmentUpload'.tr(),
icon: Symbols.upload,
onSelected: () {
onUpload!(idx);
}),
if (media.attachment != null && onPostSetThumbnail != null && idx != -1)
MenuItem(
label: 'attachmentSetAsPostThumbnail'.tr(),
icon: Symbols.gallery_thumbnail,
onSelected: () {
onPostSetThumbnail!(idx);
},
)
else if (media.attachment != null && onPostSetThumbnail != null)
MenuItem(
label: 'attachmentUnsetAsPostThumbnail'.tr(),
icon: Symbols.cancel,
onSelected: () {
onPostSetThumbnail!(null);
},
),
if (media.attachment != null && onInsertLink != null)
MenuItem(
label: 'attachmentInsertLink'.tr(),
icon: Symbols.add_link,
onSelected: () {
onInsertLink!(idx);
},
),
2024-11-23 09:32:48 +00:00
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) {
final devicePixelRatio = MediaQuery.of(context).devicePixelRatio;
2024-11-17 16:55:39 +00:00
return Container(
constraints: const BoxConstraints(maxHeight: 120),
2024-12-07 09:43:44 +00:00
child: Row(
children: [
const Gap(8),
if (thumbnail != null)
ContextMenuRegion(
contextMenu: _buildContextMenu(context, -1, thumbnail!),
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Theme.of(context).dividerColor,
width: 1,
),
borderRadius: BorderRadius.circular(8),
2024-11-11 13:48:50 +00:00
),
2024-12-07 09:43:44 +00:00
child: ClipRRect(
borderRadius: const BorderRadius.all(Radius.circular(8)),
child: AspectRatio(
aspectRatio: 1,
child: switch (thumbnail!.type) {
PostWriteMediaType.image => LayoutBuilder(builder: (context, constraints) {
return Image(
image: thumbnail!.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-17 16:55:39 +00:00
),
),
),
if (thumbnail != null)
const VerticalDivider(width: 1, thickness: 1).padding(
horizontal: 12,
vertical: 16,
),
2024-12-07 09:43:44 +00:00
Expanded(
child: ListView.separated(
scrollDirection: Axis.horizontal,
padding: const EdgeInsets.only(right: 8),
separatorBuilder: (context, index) => const Gap(8),
itemCount: attachments.length,
itemBuilder: (context, idx) {
final media = attachments[idx];
return ContextMenuRegion(
contextMenu: _buildContextMenu(context, idx, media),
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Theme.of(context).dividerColor,
width: 1,
),
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-17 16:55:39 +00:00
),
2024-11-10 08:41:11 +00:00
);
}
}