Files
openchamber-android-native/lib/widgets/composer_attachment.dart
T
blumia 511c3b006b feat: write the UI in English, select only conversation content
- source strings are English again; Chinese comes from lib/l10n/zh.dart
  through tr()/trArgs() and AppText, and a language without a table
  (Spanish, ...) reads English instead of Chinese
- server and user data (titles, messages, file names, notes, task names)
  is drawn with plain Text, so it is never looked up in the table
- SelectionArea moved from the whole message list to the messages, the
  answer text, an opened tool call's details and the turn footer; status
  lines and folded tool rows no longer start a selection
- a message the transcript does not know (its start event was lost while
  the stream was down) fetches the tail once, so a turn continued from
  another client comes in with its prompt
- bump to 0.5.3
2026-11-26 11:48:16 +08:00

136 lines
3.9 KiB
Dart

// Attachments shared by the chat composer and the new-session draft: what a
// picked file becomes (a data URL the server stores) and the chips that show
// it above the input.
import 'dart:convert';
import 'dart:io';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import '../l10n.dart';
class ComposerAttachment {
final String name;
final String mime;
final String dataUrl;
final String? path;
const ComposerAttachment({
required this.name,
required this.mime,
required this.dataUrl,
this.path,
});
bool get isImage => mime.startsWith('image/');
}
/// Opens the system picker and turns each choice into an attachment. Files
/// over 10 MB are refused with a message instead of being dropped silently.
Future<List<ComposerAttachment>> pickComposerAttachments(
BuildContext context) async {
final picked = await FilePicker.pickFiles();
if (picked.isEmpty) return const [];
final out = <ComposerAttachment>[];
for (final file in picked) {
final bytes = await file.readAsBytes();
if (bytes.length > 10 * 1024 * 1024) {
if (context.mounted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: AppText('Attachments cannot be larger than 10 MB')));
}
continue;
}
final mime = _mimeOf(file.name);
String? keptPath;
if (mime.startsWith('image/') && file.path != null) {
keptPath = await _keepImage(file.path!, file.name);
}
out.add(ComposerAttachment(
name: file.name,
mime: mime,
dataUrl: 'data:$mime;base64,${base64Encode(bytes)}',
path: keptPath,
));
}
return out;
}
/// Chips with a thumbnail, unfolded above the input bar.
class AttachmentTray extends StatelessWidget {
final List<ComposerAttachment> items;
final ValueChanged<int> onRemove;
const AttachmentTray({
super.key,
required this.items,
required this.onRemove,
});
@override
Widget build(BuildContext context) {
return SizedBox(
height: 46,
child: ListView(
scrollDirection: Axis.horizontal,
children: [
for (var i = 0; i < items.length; i++)
Padding(
padding: const EdgeInsets.only(right: 6),
child: InputChip(
avatar: items[i].path != null
? CircleAvatar(
backgroundImage: FileImage(File(items[i].path!)),
)
: const Icon(Icons.insert_drive_file, size: 16),
label: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 160),
child: Text(items[i].name,
maxLines: 1, overflow: TextOverflow.ellipsis),
),
onDeleted: () => onRemove(i),
),
),
],
),
);
}
}
String _mimeOf(String name) {
final ext = name.split('.').last.toLowerCase();
return switch (ext) {
'png' => 'image/png',
'jpg' || 'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'webp' => 'image/webp',
'bmp' => 'image/bmp',
'avif' => 'image/avif',
'ico' => 'image/x-icon',
'pdf' => 'application/pdf',
'json' => 'application/json',
'md' || 'markdown' => 'text/markdown',
'txt' => 'text/plain',
'csv' => 'text/csv',
'html' => 'text/html',
'xml' => 'text/xml',
'zip' => 'application/zip',
_ => 'application/octet-stream',
};
}
Future<String?> _keepImage(String src, String name) async {
try {
// No plugin needed: a cache copy is enough for the message being sent.
final dir = Directory(
'${Directory.systemTemp.path}/openchamber-attachments');
await dir.create(recursive: true);
final safe =
name.replaceAll(RegExp(r'[/\\:*?"<>|]'), '_');
final dst =
'${dir.path}/${DateTime.now().microsecondsSinceEpoch}-$safe';
await File(src).copy(dst);
return dst;
} catch (_) {
return null;
}
}