feat(chat): attach files on Android and iOS with the native picker
This commit is contained in:
@@ -3,6 +3,7 @@ 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'
|
||||
if (dart.library.io) 'chat_attachment_picker_native.dart'
|
||||
as impl;
|
||||
|
||||
/// One file the operator chose in the host's file picker, with the bytes the
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import 'package:file_picker/file_picker.dart';
|
||||
|
||||
import 'chat_attachment_picker.dart';
|
||||
|
||||
/// The native (Android/iOS) file picker.
|
||||
///
|
||||
/// It uses the platform's own document picker through `file_picker`, reads the
|
||||
/// bytes with the selection so the draft owns one copy, and records the file's
|
||||
/// name and a MIME type derived from its extension. A cancelled selection
|
||||
/// returns null, exactly like the web picker.
|
||||
ChatAttachmentPicker get hostChatAttachmentPicker =>
|
||||
const ChatAttachmentPicker(select: _selectFiles);
|
||||
|
||||
const Map<String, String> _mimeByExtension = {
|
||||
'png': 'image/png',
|
||||
'jpg': 'image/jpeg',
|
||||
'jpeg': 'image/jpeg',
|
||||
'gif': 'image/gif',
|
||||
'webp': 'image/webp',
|
||||
'heic': 'image/heic',
|
||||
'pdf': 'application/pdf',
|
||||
'txt': 'text/plain',
|
||||
'md': 'text/markdown',
|
||||
'json': 'application/json',
|
||||
'csv': 'text/csv',
|
||||
'html': 'text/html',
|
||||
'zip': 'application/zip',
|
||||
};
|
||||
|
||||
String _mimeFor(String name) {
|
||||
final dot = name.lastIndexOf('.');
|
||||
if (dot < 0 || dot == name.length - 1) return '';
|
||||
return _mimeByExtension[name.substring(dot + 1).toLowerCase()] ?? '';
|
||||
}
|
||||
|
||||
Future<List<ChatPickedFile>?> _selectFiles() async {
|
||||
final selected = await FilePicker.pickFiles(type: FileType.any);
|
||||
if (selected.isEmpty) return null;
|
||||
final files = <ChatPickedFile>[];
|
||||
for (final file in selected) {
|
||||
final bytes = await file.xFile.readAsBytes();
|
||||
files.add(
|
||||
ChatPickedFile(name: file.name, mime: _mimeFor(file.name), bytes: bytes),
|
||||
);
|
||||
}
|
||||
return files;
|
||||
}
|
||||
Reference in New Issue
Block a user