Files
buzz/mobile/lib/features/channels/compose_bar/attachments.dart
klopez4212 d647c9fd98 Refine iOS composer and transcribe recordings into drafts (#9574)
Refine the iOS composer with 40-point glass controls, native attachment
menus, persistent emoji access, and selected-text formatting. The mic
now transcribes recordings on device into an editable, unsent draft;
Dictation has no five-minute recording cap or timer label, and its
waveform fills the space between the controls. Voice note remains
available in attachments with its five-minute cap. Unsupported devices
fail before recording, and audio is retained until the draft is durably
saved. Stopped dictation remains recoverable across editor teardown;
failed retention and cleanup can be retried. Recovered transcripts are
inserted only once across navigation or restart, preserving live exact
mention bindings.

Preserve selection handles over glass with animated avatars. Reduce the
conversation fade to 100 points above the keyboard and transition
continuously to 180 as the keyboard closes, without a first-frame jump
when opening; Home is unchanged.

Validation: 5,117 mobile tests and 27 crypto tests pass; analysis
passes. Prior native suite on iPhone 16/iOS 26.5: 119 pass, one
pre-existing expected failure, zero unexpected failures. Physical Air
preview approved; simulator selection/dragging and attachment flows
exercised. Fixed the native navigation test's timing race and a
community-cache cleanup test that checked a parent directory before its
asynchronous removal completed. Repository-wide `just ci` previously
stopped in unchanged `buzz-test-client` load-generator tests; new-head
hosted checks are pending.

Snapshots: production composer in an iPhone Air simulator fixture with
fictional conversation text; Photos snapshot retained from the
attachment preview.

| Composer | Attachments | Photos |
| --- | --- | --- |
| ![Keyboard-up
composer](https://github.com/user-attachments/assets/b7c8f0a4-25f8-4ca2-b8ae-11e18655e64a)
| ![Native attachment
menu](https://github.com/user-attachments/assets/5eaf5259-8e04-4da7-93f5-1b2eccab3c3c)
|
![Photos](https://github.com/user-attachments/assets/a6216b7d-5eaf-482c-b587-6052825b88fc)
|

---------

Signed-off-by: Terp <5856f6900e8e0a4e0ea1f4f8d2ee33e4753d2119bc122f94ecafc660b46c0511@buzz.block.builderlab.xyz>
Signed-off-by: Tom Brow <tomb@squareup.com>
Signed-off-by: Princess Donut <b238ea756dee4d98afa5883fc7f1de61eeabe65bf700e3a5a5a80db5e42e2c2b@buzz.block.builderlab.xyz>
Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: Terp <5856f6900e8e0a4e0ea1f4f8d2ee33e4753d2119bc122f94ecafc660b46c0511@buzz.block.builderlab.xyz>
Co-authored-by: Tom Brow <tomb@block.xyz>
Co-authored-by: Tom Brow <tomb@squareup.com>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
Co-authored-by: Princess Donut <b238ea756dee4d98afa5883fc7f1de61eeabe65bf700e3a5a5a80db5e42e2c2b@buzz.block.builderlab.xyz>
2026-11-20 16:35:20 +00:00

945 lines
30 KiB
Dart

part of '../compose_bar.dart';
bool _rejectsNonVoiceAttachment(
_ComposerVoiceNote voiceNote,
List<_PendingAttachment> attachments,
ValueNotifier<String?> uploadError,
) {
if (!voiceNote.isPreparing &&
voiceNote.recorder == null &&
!attachments.any(
(attachment) => attachment.kind == _PendingAttachmentKind.voiceNote,
)) {
return false;
}
uploadError.value = voiceNote.recorder != null || voiceNote.isPreparing
? 'Finish or discard the voice note before attaching a file.'
: 'A voice note must be the only attachment.';
return true;
}
bool _queueComposerAttachment(
XFile file,
_PendingAttachmentKind kind,
ObjectRef<_ComposerVoiceNote> voiceNote,
ValueNotifier<List<_PendingAttachment>> attachments,
ValueNotifier<String?> uploadError,
ObjectRef<int> draftRevision, {
bool deleteAfterUse = false,
}) {
if (kind != _PendingAttachmentKind.voiceNote &&
_rejectsNonVoiceAttachment(
voiceNote.value,
attachments.value,
uploadError,
)) {
return false;
}
draftRevision.value += 1;
uploadError.value = null;
attachments.value = [
...attachments.value,
_PendingAttachment(file: file, kind: kind, deleteAfterUse: deleteAfterUse),
];
return true;
}
final _pickedVideoExtension = RegExp(
r'\.(mp4|mov|m4v|webm|3gp|3gpp|3g2|avi|mkv)$',
caseSensitive: false,
);
bool _isPickedVideo(XFile file) {
final mime = file.mimeType;
if (mime != null && mime != 'application/octet-stream') {
return mime.startsWith('video/');
}
return _pickedVideoExtension.hasMatch(file.name) ||
_pickedVideoExtension.hasMatch(file.path);
}
bool _queueComposerImages(
List<XFile> images,
_ComposerVoiceNote voiceNote,
ValueNotifier<List<_PendingAttachment>> attachments,
ValueNotifier<String?> uploadError,
ObjectRef<int> draftRevision,
bool deleteAfterUse,
) {
if (images.isEmpty ||
_rejectsNonVoiceAttachment(voiceNote, attachments.value, uploadError)) {
return false;
}
draftRevision.value += 1;
uploadError.value = null;
attachments.value = [
...attachments.value,
for (final image in images)
_PendingAttachment(
file: image,
kind: _isPickedVideo(image)
? _PendingAttachmentKind.video
: _PendingAttachmentKind.image,
deleteAfterUse: deleteAfterUse,
),
];
return true;
}
enum _AttachmentSurface { closed, menu, camera, photos }
void _toggleComposerAttachments(
ValueNotifier<_AttachmentSurface> surface,
ValueNotifier<_InputTool?> tool,
) {
surface.value = switch (surface.value) {
_AttachmentSurface.closed => _AttachmentSurface.menu,
_AttachmentSurface.menu => _AttachmentSurface.closed,
_AttachmentSurface.camera ||
_AttachmentSurface.photos => _AttachmentSurface.menu,
};
if (defaultTargetPlatform == TargetPlatform.iOS) {
tool.value = surface.value == _AttachmentSurface.closed
? null
: _InputTool.attachments;
}
}
bool get _hasCombinedMediaPicker =>
defaultTargetPlatform == TargetPlatform.iOS ||
defaultTargetPlatform == TargetPlatform.android;
const _attachmentMenuWidth = 216.0;
const _attachmentMenuPadding = Grid.xs;
const _attachmentMenuItemHeight = 52.0;
const _attachmentMenuItemSpacing = Grid.xxs;
const _attachmentMenuIconSize = 24.0;
const _attachmentMenuIconSlotWidth = 28.0;
const _attachmentExpandedHeight = 372.0;
@immutable
class _AttachmentMenuLayout {
final double itemHeight;
final double contentHeight;
final double height;
const _AttachmentMenuLayout({
required this.itemHeight,
required this.contentHeight,
required this.height,
});
factory _AttachmentMenuLayout.from(BuildContext context) {
final textPainter = TextPainter(
text: TextSpan(text: 'Camera', style: context.textTheme.titleMedium),
textDirection: Directionality.of(context),
textScaler: MediaQuery.textScalerOf(context),
maxLines: 1,
)..layout();
final itemHeight = math.max(
_attachmentMenuItemHeight,
textPainter.height + (Grid.xxs * 2),
);
textPainter.dispose();
final itemCount = (_hasCombinedMediaPicker ? 4 : 5);
final contentHeight =
(_attachmentMenuPadding * 2) +
(itemHeight * itemCount) +
(_attachmentMenuItemSpacing * (itemCount - 1));
return _AttachmentMenuLayout(
itemHeight: itemHeight,
contentHeight: contentHeight,
height: math.min(contentHeight, _attachmentExpandedHeight),
);
}
bool get isScrollable => contentHeight > height;
}
class _AttachmentSurfacePanel extends HookWidget {
final _AttachmentSurface surface;
final Widget suggestionPanel;
final VoidCallback onBack;
final VoidCallback onCamera;
final VoidCallback onPhotos;
final VoidCallback onVideo;
final VoidCallback onVoiceNote;
final VoidCallback onFiles;
final Future<void> Function(XFile image) onCapture;
final Future<List<XFile>> Function() onPickAllPhotos;
final Future<void> Function(List<XFile> photos) onChoosePhotos;
final Future<void> Function(List<XFile> photos) onChooseAllPhotos;
final Future<void> Function(Future<void> Function()) preparePhotos;
const _AttachmentSurfacePanel({
super.key,
required this.surface,
required this.suggestionPanel,
required this.onBack,
required this.onCamera,
required this.onPhotos,
required this.onVideo,
required this.onVoiceNote,
required this.onFiles,
required this.onCapture,
required this.onPickAllPhotos,
required this.onChoosePhotos,
required this.onChooseAllPhotos,
required this.preparePhotos,
});
@override
Widget build(BuildContext context) {
if (surface == _AttachmentSurface.closed) return suggestionPanel;
final menuLayout = _AttachmentMenuLayout.from(context);
final reducedMotion = MediaQuery.disableAnimationsOf(context);
final isExpanded =
surface == _AttachmentSurface.camera ||
surface == _AttachmentSurface.photos;
final morphController = useAnimationController(
initialValue: isExpanded ? 1 : 0,
duration: reducedMotion
? Duration.zero
: const Duration(milliseconds: 320),
reverseDuration: reducedMotion
? Duration.zero
: const Duration(milliseconds: 250),
);
final rawProgress = useAnimation(morphController);
final renderedExpandedSurface = useState<_AttachmentSurface?>(
isExpanded ? surface : null,
);
final latestSurface = useRef(surface);
latestSurface.value = surface;
useEffect(() {
void disposeCollapsedContent(AnimationStatus status) {
if (status == AnimationStatus.dismissed &&
latestSurface.value == _AttachmentSurface.menu) {
renderedExpandedSurface.value = null;
}
}
morphController.addStatusListener(disposeCollapsedContent);
return () =>
morphController.removeStatusListener(disposeCollapsedContent);
}, [morphController]);
useEffect(() {
if (isExpanded) {
renderedExpandedSurface.value = surface;
morphController.forward();
} else {
morphController.reverse();
}
return null;
}, [isExpanded, morphController, surface]);
final visibleExpandedSurface =
renderedExpandedSurface.value ?? (isExpanded ? surface : null);
final cameraInitializationReady =
reducedMotion ||
(surface == _AttachmentSurface.camera && rawProgress >= 1);
final expandedContent = switch (visibleExpandedSurface) {
_AttachmentSurface.camera => KeyedSubtree(
key: const ValueKey('camera-preview'),
child: KeyedSubtree(
key: ValueKey(
cameraInitializationReady
? 'camera-initialization-ready'
: 'camera-initialization-deferred',
),
child: _InlineCameraPreview(
initializeCamera: cameraInitializationReady,
onClose: onBack,
onCapture: onCapture,
),
),
),
_AttachmentSurface.photos => KeyedSubtree(
key: const ValueKey('photo-gallery'),
child: _PhotoGalleryPicker(
onBack: onBack,
onPickAllPhotos: onPickAllPhotos,
onChoosePhotos: onChoosePhotos,
onChooseAllPhotos: onChooseAllPhotos,
preparePhotos: preparePhotos,
),
),
_AttachmentSurface.closed ||
_AttachmentSurface.menu ||
null => const SizedBox.shrink(),
};
double interval(double value, double begin, double end) {
return ((value - begin) / (end - begin)).clamp(0.0, 1.0);
}
final menuOpacity = 1 - interval(rawProgress, 0.12, 0.38);
final expandedOpacity = interval(rawProgress, 0.28, 0.65);
final sizeProgress = morphController.status == AnimationStatus.reverse
? const Cubic(0.22, 1, 0.36, 1).transform(rawProgress)
: Curves.easeInOutCubic.transform(rawProgress);
if (defaultTargetPlatform == TargetPlatform.iOS) {
if (surface != _AttachmentSurface.menu) {
return SizedBox.expand(child: expandedContent);
}
return _AttachmentMenu(
tiled: !_ComposerGlassScope.of(context),
layout: menuLayout,
onCamera: onCamera,
onPhotos: onPhotos,
onVideo: onVideo,
onVoiceNote: onVoiceNote,
onFiles: onFiles,
);
}
return LayoutBuilder(
builder: (context, constraints) {
final expandedWidth = constraints.maxWidth;
const expandedHeight = _attachmentExpandedHeight;
final width =
_attachmentMenuWidth +
((expandedWidth - _attachmentMenuWidth) * sizeProgress);
final height =
menuLayout.height +
((expandedHeight - menuLayout.height) * sizeProgress);
final baseColor = appPopoverColor(context);
final expandedColor =
visibleExpandedSurface == _AttachmentSurface.camera
? Colors.black
: baseColor;
return Align(
alignment: Alignment.topLeft,
heightFactor: 1,
child: SizedBox(
width: width,
height: height,
child: Material(
key: const ValueKey('attachment-surface-popover'),
type: MaterialType.card,
color: Color.lerp(baseColor, expandedColor, sizeProgress),
surfaceTintColor: Colors.transparent,
elevation: appPopoverElevation,
shadowColor: appPopoverShadowColor(context),
shape: appPopoverShape(context),
clipBehavior: Clip.antiAlias,
child: Stack(
clipBehavior: Clip.hardEdge,
children: [
Positioned(
left: 0,
top: 0,
width: _attachmentMenuWidth,
height: menuLayout.height,
child: IgnorePointer(
ignoring: surface != _AttachmentSurface.menu,
child: Opacity(
opacity: menuOpacity,
child: _AttachmentMenu(
layout: menuLayout,
onCamera: onCamera,
onPhotos: onPhotos,
onVideo: onVideo,
onVoiceNote: onVoiceNote,
onFiles: onFiles,
),
),
),
),
Positioned(
left: 0,
top: 0,
width: expandedWidth,
height: expandedHeight,
child: IgnorePointer(
ignoring: !isExpanded,
child: Opacity(
opacity: expandedOpacity,
child: expandedContent,
),
),
),
],
),
),
),
);
},
);
}
}
@immutable
class _ComposeDraftPayload {
final String content;
final List<List<String>> mediaTags;
const _ComposeDraftPayload({required this.content, required this.mediaTags});
factory _ComposeDraftPayload.fromDraft({
required String text,
required List<BlobDescriptor> attachments,
required List<CustomEmoji> customEmoji,
}) {
var content = text;
final mediaTags = <List<String>>[];
for (final attachment in attachments) {
mediaTags.add(attachment.toImetaTag());
content += '\n${attachment.toMarkdownImage()}';
}
mediaTags.addAll(buildCustomEmojiTags(content, customEmoji));
return _ComposeDraftPayload(content: content, mediaTags: mediaTags);
}
}
enum _PendingAttachmentKind { image, video, voiceNote, file }
@immutable
class _PendingAttachment {
static var _nextId = 0;
final int id;
final XFile file;
final _PendingAttachmentKind kind;
final bool deleteAfterUse;
final Duration? duration;
final List<double> waveform;
_PendingAttachment({
required this.file,
required this.kind,
this.deleteAfterUse = false,
this.duration,
this.waveform = const [],
}) : id = _nextId++;
}
Future<void> _deleteXFile(XFile file) async {
final path = file.path;
if (path.isEmpty) return;
try {
await File(path).delete();
} on FileSystemException {
// Temporary files may already have been removed by the operating system.
}
}
Future<void> _deleteOwnedAttachments(
Iterable<_PendingAttachment> attachments,
) async {
for (final attachment in attachments) {
if (attachment.deleteAfterUse) await _deleteXFile(attachment.file);
}
}
void _useOwnedAttachmentCleanup(
ValueNotifier<List<_PendingAttachment>> attachments,
) {
useEffect(
() =>
() => unawaited(_deleteOwnedAttachments(attachments.value)),
[attachments],
);
}
void _removePendingAttachment(
ValueNotifier<List<_PendingAttachment>> attachments,
ObjectRef<int> draftRevision,
int id,
) {
draftRevision.value += 1;
final removed = attachments.value.where((attachment) => attachment.id == id);
attachments.value = _withoutAttachment(attachments.value, id);
unawaited(_deleteOwnedAttachments(removed));
}
Future<void> _retainAndQueueImages(
BuildContext context,
List<XFile> images,
bool Function(List<XFile>, {bool deleteAfterUse}) queueImages,
) async {
final retained = await retainTemporaryImages(images);
if (!context.mounted || !queueImages(retained, deleteAfterUse: true)) {
for (final image in retained) {
await _deleteXFile(image);
}
}
}
Future<BlobDescriptor> _uploadPendingAttachment(
MediaUploadService service,
_PendingAttachment attachment, {
ValueChanged<double>? onProgress,
UploadCancellationToken? cancellationToken,
}) => switch (attachment.kind) {
_PendingAttachmentKind.image => service.uploadImage(
attachment.file,
onProgress: onProgress,
cancellationToken: cancellationToken,
),
_PendingAttachmentKind.video => service.uploadVideo(
attachment.file,
onProgress: onProgress,
cancellationToken: cancellationToken,
),
_PendingAttachmentKind.voiceNote => service.uploadVoiceNote(
attachment.file,
duration: attachment.duration ?? Duration.zero,
onProgress: onProgress,
cancellationToken: cancellationToken,
),
_PendingAttachmentKind.file => service.uploadFile(
attachment.file,
onProgress: onProgress,
cancellationToken: cancellationToken,
),
};
class _AttachmentTrigger extends StatelessWidget {
final _AttachmentSurface surface;
final bool formattingOpen;
final double size;
final bool enabled;
final ValueChanged<BuildContext> onTap;
const _AttachmentTrigger({
super.key,
this.size = 36,
this.enabled = true,
required this.surface,
required this.formattingOpen,
required this.onTap,
});
@override
Widget build(BuildContext context) {
if (defaultTargetPlatform == TargetPlatform.iOS) {
return IosGlassNavigationButton(
icon: switch (surface) {
_AttachmentSurface.closed =>
formattingOpen
? IosGlassNavigationIcon.close
: IosGlassNavigationIcon.plus,
_AttachmentSurface.menu => IosGlassNavigationIcon.close,
_AttachmentSurface.camera ||
_AttachmentSurface.photos => IosGlassNavigationIcon.back,
},
semanticLabel: switch (surface) {
_AttachmentSurface.closed =>
formattingOpen ? 'Close formatting' : 'Add attachment',
_AttachmentSurface.menu => 'Close attachments',
_AttachmentSurface.camera ||
_AttachmentSurface.photos => 'Back to attachment options',
},
width: size,
height: size,
controlSize: size,
iconSize: 20,
foregroundColor: context.colors.onSurfaceVariant,
onPressed: enabled
? () => _runComposerAction(() => onTap(context))
: null,
);
}
final duration = MediaQuery.disableAnimationsOf(context)
? Duration.zero
: const Duration(milliseconds: 240);
return SizedBox.square(
dimension: size,
child: DecoratedBox(
decoration: BoxDecoration(
color: context.colors.surface,
shape: BoxShape.circle,
border: Border.all(
color: Colors.black.withValues(alpha: 0.04),
width: 1,
),
),
child: IconButton(
tooltip: switch (surface) {
_AttachmentSurface.closed =>
formattingOpen ? 'Close formatting' : 'Add attachment',
_AttachmentSurface.menu => 'Close attachments',
_AttachmentSurface.camera ||
_AttachmentSurface.photos => 'Back to attachment options',
},
onPressed: enabled
? () => _runComposerAction(() => onTap(context))
: null,
padding: EdgeInsets.zero,
visualDensity: VisualDensity.compact,
icon: AnimatedRotation(
duration: duration,
curve: Curves.easeOutBack,
turns: surface == _AttachmentSurface.menu || formattingOpen
? 0.125
: 0,
child: AnimatedSwitcher(
duration: duration,
switchInCurve: Curves.easeOutBack,
switchOutCurve: Curves.easeInOutCubic,
transitionBuilder: (child, animation) => FadeTransition(
opacity: animation,
child: ScaleTransition(
scale: Tween<double>(begin: 0.92, end: 1).animate(animation),
child: child,
),
),
child: Icon(
switch (surface) {
_AttachmentSurface.camera => BuzzIcons.camera,
_AttachmentSurface.photos => BuzzIcons.images,
_AttachmentSurface.closed ||
_AttachmentSurface.menu => BuzzIcons.plus,
},
key: ValueKey('attachment-trigger-${surface.name}'),
size: 20,
color: context.colors.onSurfaceVariant,
),
),
),
),
),
);
}
}
class _AttachmentMenu extends StatelessWidget {
final bool tiled;
final _AttachmentMenuLayout layout;
final VoidCallback onCamera;
final VoidCallback onPhotos;
final VoidCallback onVideo;
final VoidCallback onVoiceNote;
final VoidCallback onFiles;
const _AttachmentMenu({
this.tiled = false,
required this.layout,
required this.onCamera,
required this.onPhotos,
required this.onVideo,
required this.onVoiceNote,
required this.onFiles,
});
@override
Widget build(BuildContext context) {
final items = <(IconData, String, VoidCallback)>[
(BuzzIcons.camera, 'Camera', onCamera),
(BuzzIcons.images, 'Photos', onPhotos),
if (!_hasCombinedMediaPicker) (BuzzIcons.video, 'Video', onVideo),
(BuzzIcons.mic, 'Voice note', onVoiceNote),
(BuzzIcons.file, 'Files', onFiles),
];
if (tiled) {
return Padding(
key: const ValueKey('attachment-menu'),
padding: const EdgeInsets.all(Grid.md),
child: LayoutBuilder(
builder: (context, constraints) => Wrap(
spacing: Grid.xs,
runSpacing: Grid.xs,
children: [
for (final (icon, label, onTap) in items)
SizedBox(
width: (constraints.maxWidth - Grid.xs) / 2,
child: FilledButton.icon(
key: ValueKey(
'attachment-menu-item-${label.toLowerCase()}',
),
style: FilledButton.styleFrom(
minimumSize: const Size(0, 80),
padding: const EdgeInsets.all(Grid.md),
backgroundColor: context.colors.surfaceContainerHighest,
foregroundColor: context.colors.onSurface,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(Radii.dialog),
),
),
onPressed: () => _runComposerAction(onTap),
icon: Icon(icon, size: _attachmentMenuIconSize),
label: Text(label, textAlign: TextAlign.center),
),
),
],
),
),
);
}
return SizedBox(
key: const ValueKey('attachment-menu'),
width: _attachmentMenuWidth,
height: layout.height,
child: ListView.separated(
key: const ValueKey('attachment-menu-scroll'),
padding: const EdgeInsets.all(_attachmentMenuPadding),
physics: layout.isScrollable || _ComposerGlassScope.of(context)
? null
: const NeverScrollableScrollPhysics(),
itemCount: items.length,
separatorBuilder: (_, _) =>
const SizedBox(height: _attachmentMenuItemSpacing),
itemBuilder: (context, index) {
final (icon, label, onTap) = items[index];
return _AttachmentMenuItem(
height: layout.itemHeight,
icon: icon,
label: label,
onTap: onTap,
);
},
),
);
}
}
class _AttachmentMenuItem extends StatelessWidget {
final double height;
final IconData icon;
final String label;
final VoidCallback onTap;
const _AttachmentMenuItem({
required this.height,
required this.icon,
required this.label,
required this.onTap,
});
@override
Widget build(BuildContext context) {
return SizedBox(
key: ValueKey('attachment-menu-item-${label.toLowerCase()}'),
height: height,
child: Tooltip(
message: label,
child: InkWell(
onTap: () => _runComposerAction(onTap),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: Grid.xxs),
child: Row(
children: [
SizedBox(
key: ValueKey('attachment-menu-icon-${label.toLowerCase()}'),
width: _attachmentMenuIconSlotWidth,
child: Center(
child: Icon(
icon,
size: _attachmentMenuIconSize,
color: context.colors.onSurfaceVariant,
),
),
),
const SizedBox(width: Grid.xxs),
Expanded(
child: Text(
label,
key: ValueKey(
'attachment-menu-label-${label.toLowerCase()}',
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: context.textTheme.titleMedium?.copyWith(
color: context.colors.onSurface,
fontWeight: FontWeight.w400,
),
),
),
],
),
),
),
),
);
}
}
List<_PendingAttachment> _withoutAttachment(
List<_PendingAttachment> attachments,
int id,
) {
return [
for (final attachment in attachments)
if (attachment.id != id) attachment,
];
}
class _AttachmentStrip extends StatelessWidget {
final List<_PendingAttachment> attachments;
final ValueChanged<int> onRemove;
const _AttachmentStrip({required this.attachments, required this.onRemove});
@override
Widget build(BuildContext context) {
final thumbWidth = 72.0;
final thumbHeight = 72.0;
return SizedBox(
height: thumbHeight,
child: LayoutBuilder(
builder: (context, constraints) => ListView.separated(
scrollDirection: Axis.horizontal,
itemCount: attachments.length,
separatorBuilder: (_, _) => const SizedBox(width: Grid.half),
itemBuilder: (context, index) {
final attachment = attachments[index];
if (attachment.kind == _PendingAttachmentKind.voiceNote) {
return SizedBox(
width: constraints.maxWidth,
child: VoiceNoteAttachment.local(
path: attachment.file.path,
duration: attachment.duration ?? Duration.zero,
waveform: attachment.waveform,
onRemove: () =>
_runComposerAction(() => onRemove(attachment.id)),
),
);
}
return Container(
key: ValueKey('compose-attachment:${attachment.id}'),
width: thumbWidth,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(Radii.dialog),
border: Border.all(color: context.colors.outlineVariant),
),
child: Stack(
fit: StackFit.expand,
children: [
ClipRRect(
borderRadius: BorderRadius.circular(Radii.dialog),
child: attachment.kind == _PendingAttachmentKind.video
? _PendingVideoThumbnail(file: attachment.file)
: attachment.kind == _PendingAttachmentKind.image &&
attachment.file.path.isNotEmpty
? Image.file(
File(attachment.file.path),
fit: BoxFit.cover,
errorBuilder: (_, _, _) => ColoredBox(
color: context.colors.surface,
child: Icon(
BuzzIcons.image,
color: context.colors.onSurfaceVariant,
),
),
)
: attachment.kind == _PendingAttachmentKind.image
? _MemoryAttachmentImage(file: attachment.file)
: ColoredBox(
color: context.colors.surface,
child: Padding(
padding: const EdgeInsets.all(Grid.xxs),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(
BuzzIcons.file,
color: context.colors.onSurfaceVariant,
),
const SizedBox(height: Grid.quarter),
Text(
attachment.file.name.isEmpty
? 'File'
: attachment.file.name,
maxLines: 2,
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
style: context.textTheme.labelSmall
?.copyWith(
color:
context.colors.onSurfaceVariant,
),
),
],
),
),
),
),
Positioned(
top: Grid.quarter,
right: Grid.quarter,
child: SizedBox(
width: 24,
height: 24,
child: IconButton(
onPressed: () =>
_runComposerAction(() => onRemove(attachment.id)),
tooltip: 'Remove attachment',
visualDensity: VisualDensity.compact,
style: IconButton.styleFrom(
backgroundColor: context.colors.surface.withValues(
alpha: 0.92,
),
minimumSize: const Size(24, 24),
maximumSize: const Size(24, 24),
padding: EdgeInsets.zero,
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
),
icon: Icon(
BuzzIcons.x,
size: 14,
color: context.colors.onSurface,
),
),
),
),
],
),
);
},
),
),
);
}
}
class _MemoryAttachmentImage extends HookConsumerWidget {
final XFile file;
const _MemoryAttachmentImage({required this.file});
@override
Widget build(BuildContext context, WidgetRef ref) {
final bytes = useFuture(useMemoized(() => file.readAsBytes(), [file]));
final data = bytes.data;
if (data == null || data.isEmpty) {
return ColoredBox(
color: context.colors.surface,
child: Icon(BuzzIcons.image, color: context.colors.onSurfaceVariant),
);
}
return Image.memory(
data,
fit: BoxFit.cover,
errorBuilder: (_, _, _) => ColoredBox(
color: context.colors.surface,
child: Icon(BuzzIcons.image, color: context.colors.onSurfaceVariant),
),
);
}
}
// Keep a new-chat draft mounted until a selected attachment has been retained.
Future<T> _prepareComposerAttachment<T>(
BuildContext context,
ValueNotifier<int> pending,
Future<T> Function() action,
) async {
pending.value++;
try {
return await action();
} finally {
if (context.mounted) pending.value--;
}
}