feat(app): pick chat attachments from the host file input
This commit is contained in:
@@ -0,0 +1,87 @@
|
|||||||
|
import 'dart:typed_data';
|
||||||
|
|
||||||
|
import 'chat_attachment_draft.dart';
|
||||||
|
import 'chat_attachment_picker_stub.dart'
|
||||||
|
if (dart.library.js_interop) 'chat_attachment_picker_web.dart'
|
||||||
|
as impl;
|
||||||
|
|
||||||
|
/// One file the operator chose in the host's file picker, with the bytes the
|
||||||
|
/// host handed back.
|
||||||
|
///
|
||||||
|
/// The seam records only what the picker supplied: the file's own name, its
|
||||||
|
/// MIME type and its bytes. It applies no name fallback and no size or count
|
||||||
|
/// policy; [ChatAttachmentDraft] owns those decisions.
|
||||||
|
class ChatPickedFile {
|
||||||
|
const ChatPickedFile({
|
||||||
|
required this.name,
|
||||||
|
required this.mime,
|
||||||
|
required this.bytes,
|
||||||
|
});
|
||||||
|
|
||||||
|
/// The file name exactly as the host recorded it, blank when the host
|
||||||
|
/// supplied none. [ChatAttachmentDraft.add] refuses a blank name rather than
|
||||||
|
/// replacing it.
|
||||||
|
final String name;
|
||||||
|
|
||||||
|
/// The MIME type the host recorded, empty when the host supplied none.
|
||||||
|
final String mime;
|
||||||
|
|
||||||
|
/// The file's complete contents as read when it was chosen.
|
||||||
|
final Uint8List bytes;
|
||||||
|
|
||||||
|
/// The recorded size, taken from the bytes that were actually read.
|
||||||
|
int get sizeBytes => bytes.length;
|
||||||
|
|
||||||
|
/// The draft row for this file. The draft still owns the blank-name and
|
||||||
|
/// total-size decisions.
|
||||||
|
ChatAttachment get attachment =>
|
||||||
|
ChatAttachment(name: name, mime: mime, sizeBytes: sizeBytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Opens the host's file selection and returns the files the operator chose.
|
||||||
|
///
|
||||||
|
/// A null result means the operator dismissed the picker without choosing
|
||||||
|
/// anything, which is different from an empty list. Only one selection is
|
||||||
|
/// returned per call.
|
||||||
|
typedef ChatFileSelection = Future<List<ChatPickedFile>?> Function();
|
||||||
|
|
||||||
|
/// The file picker seam for one host.
|
||||||
|
///
|
||||||
|
/// [pick] is the operator's explicit pick action: on web it opens the host's
|
||||||
|
/// native file input, and the chosen files are read before the future
|
||||||
|
/// completes. A host without a picker constructs
|
||||||
|
/// [ChatAttachmentPicker.unsupported] so the missing capability is reported
|
||||||
|
/// plainly instead of silently returning nothing. Tests inject a fake
|
||||||
|
/// [select] to exercise the seam without a browser.
|
||||||
|
class ChatAttachmentPicker {
|
||||||
|
const ChatAttachmentPicker({required ChatFileSelection this.select})
|
||||||
|
: unsupportedMessage = null;
|
||||||
|
|
||||||
|
const ChatAttachmentPicker.unsupported(String message)
|
||||||
|
: select = null,
|
||||||
|
unsupportedMessage = message;
|
||||||
|
|
||||||
|
/// The host selection, or null when this host cannot pick files.
|
||||||
|
final ChatFileSelection? select;
|
||||||
|
|
||||||
|
/// Why this host cannot pick files, when [select] is null.
|
||||||
|
final String? unsupportedMessage;
|
||||||
|
|
||||||
|
/// Whether this host can open a file selection at all.
|
||||||
|
bool get isSupported => select != null;
|
||||||
|
|
||||||
|
/// Opens the host's file selection and resolves with the chosen files, or
|
||||||
|
/// null when the operator cancelled.
|
||||||
|
///
|
||||||
|
/// Throws [UnsupportedError] with [unsupportedMessage] on a host that cannot
|
||||||
|
/// pick files.
|
||||||
|
Future<List<ChatPickedFile>?> pick() async {
|
||||||
|
final select = this.select;
|
||||||
|
if (select == null) throw UnsupportedError(unsupportedMessage!);
|
||||||
|
return select();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The picker for the running host.
|
||||||
|
ChatAttachmentPicker get hostChatAttachmentPicker =>
|
||||||
|
impl.hostChatAttachmentPicker;
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import 'chat_attachment_picker.dart';
|
||||||
|
|
||||||
|
/// Native fallback for the chat attachment picker.
|
||||||
|
///
|
||||||
|
/// No native file-picker plugin is a dependency of this slice, so a non-web
|
||||||
|
/// host reports the missing capability instead of pretending the operator
|
||||||
|
/// cancelled. The attach control can disable itself from [isSupported] and
|
||||||
|
/// show this exact explanation.
|
||||||
|
const String chatAttachmentUnsupportedMessage =
|
||||||
|
'Attaching files is not supported on this platform yet.';
|
||||||
|
|
||||||
|
ChatAttachmentPicker get hostChatAttachmentPicker =>
|
||||||
|
const ChatAttachmentPicker.unsupported(chatAttachmentUnsupportedMessage);
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
import 'dart:async';
|
||||||
|
import 'dart:js_interop';
|
||||||
|
|
||||||
|
import 'package:web/web.dart' as web;
|
||||||
|
|
||||||
|
import 'chat_attachment_picker.dart';
|
||||||
|
|
||||||
|
/// The web file picker: one explicit pick action creates the host's native
|
||||||
|
/// file input, opens it and reads the files the operator chose.
|
||||||
|
///
|
||||||
|
/// The input is `multiple` and has no `accept` filter, matching the web
|
||||||
|
/// composer's attach control. Only a selection or a cancel completes the
|
||||||
|
/// future; a document without a body has nothing to open and yields null.
|
||||||
|
ChatAttachmentPicker get hostChatAttachmentPicker =>
|
||||||
|
const ChatAttachmentPicker(select: _selectFiles);
|
||||||
|
|
||||||
|
Future<List<ChatPickedFile>?> _selectFiles() {
|
||||||
|
final input = web.HTMLInputElement()
|
||||||
|
..type = 'file'
|
||||||
|
..multiple = true;
|
||||||
|
input.style.display = 'none';
|
||||||
|
|
||||||
|
final completer = Completer<List<ChatPickedFile>?>();
|
||||||
|
|
||||||
|
void finish(List<ChatPickedFile>? files) {
|
||||||
|
if (completer.isCompleted) return;
|
||||||
|
input.remove();
|
||||||
|
completer.complete(files);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> readSelection() async {
|
||||||
|
final selected = input.files;
|
||||||
|
if (selected == null || selected.length == 0) {
|
||||||
|
finish(null);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
final files = <ChatPickedFile>[];
|
||||||
|
for (var index = 0; index < selected.length; index += 1) {
|
||||||
|
final file = selected.item(index);
|
||||||
|
if (file == null) continue;
|
||||||
|
final buffer = await file.arrayBuffer().toDart;
|
||||||
|
files.add(
|
||||||
|
ChatPickedFile(
|
||||||
|
name: file.name,
|
||||||
|
mime: file.type,
|
||||||
|
bytes: buffer.toDart.asUint8List(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
finish(files);
|
||||||
|
} catch (error, stackTrace) {
|
||||||
|
if (completer.isCompleted) return;
|
||||||
|
input.remove();
|
||||||
|
completer.completeError(error, stackTrace);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
input.addEventListener('change', ((web.Event _) {
|
||||||
|
unawaited(readSelection());
|
||||||
|
}).toJS);
|
||||||
|
input.addEventListener('cancel', ((web.Event _) {
|
||||||
|
finish(null);
|
||||||
|
}).toJS);
|
||||||
|
|
||||||
|
final body = web.document.body;
|
||||||
|
if (body == null) return Future.value(null);
|
||||||
|
body.appendChild(input);
|
||||||
|
input.click();
|
||||||
|
return completer.future;
|
||||||
|
}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
import 'dart:typed_data';
|
||||||
|
|
||||||
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
|
import 'package:pipelines_app/features/chat/domain/chat_attachment_draft.dart';
|
||||||
|
import 'package:pipelines_app/features/chat/domain/chat_attachment_picker.dart';
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
test('passes the chosen names, MIME types and bytes through', () async {
|
||||||
|
final bytes = Uint8List.fromList([0, 1, 2, 250]);
|
||||||
|
final picked = ChatPickedFile(
|
||||||
|
name: 'screen.png',
|
||||||
|
mime: 'image/png',
|
||||||
|
bytes: bytes,
|
||||||
|
);
|
||||||
|
final picker = ChatAttachmentPicker(select: () async => [picked]);
|
||||||
|
|
||||||
|
expect(picker.isSupported, isTrue);
|
||||||
|
final files = await picker.pick();
|
||||||
|
|
||||||
|
expect(files, hasLength(1));
|
||||||
|
expect(files!.single.name, 'screen.png');
|
||||||
|
expect(files.single.mime, 'image/png');
|
||||||
|
expect(files.single.bytes, bytes);
|
||||||
|
expect(files.single.sizeBytes, 4);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('feeds the picked file to the attachment draft', () async {
|
||||||
|
final picker = ChatAttachmentPicker(
|
||||||
|
select: () async => [
|
||||||
|
ChatPickedFile(
|
||||||
|
name: 'notes.txt',
|
||||||
|
mime: 'text/plain',
|
||||||
|
bytes: Uint8List.fromList([1, 2, 3]),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
);
|
||||||
|
final draft = ChatAttachmentDraft();
|
||||||
|
|
||||||
|
final files = await picker.pick();
|
||||||
|
|
||||||
|
expect(draft.add(files!.single.attachment), isNull);
|
||||||
|
expect(
|
||||||
|
draft.attachments.single,
|
||||||
|
const ChatAttachment(
|
||||||
|
name: 'notes.txt',
|
||||||
|
mime: 'text/plain',
|
||||||
|
sizeBytes: 3,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('returns nothing when the operator dismisses the picker', () async {
|
||||||
|
var opened = 0;
|
||||||
|
final picker = ChatAttachmentPicker(
|
||||||
|
select: () async {
|
||||||
|
opened += 1;
|
||||||
|
return null;
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(await picker.pick(), isNull);
|
||||||
|
expect(opened, 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('the host picker reports its unsupported capability honestly', () async {
|
||||||
|
final picker = hostChatAttachmentPicker;
|
||||||
|
|
||||||
|
expect(picker.isSupported, isFalse);
|
||||||
|
await expectLater(
|
||||||
|
picker.pick(),
|
||||||
|
throwsA(
|
||||||
|
isA<UnsupportedError>().having(
|
||||||
|
(error) => error.message,
|
||||||
|
'message',
|
||||||
|
'Attaching files is not supported on this platform yet.',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user