Files
openchamber-android-native/lib/widgets/composer_attachment.dart
T
blumia fff6562cbc feat: a new-session draft and a sidebar grouped by project
- Starting a session opens NewSessionScreen instead of a title sheet: a
  project chip (GET /api/project, one row per canonical), a model/agent
  chip, attachments and the composer. Nothing is created on the server
  until the first send, which posts the session with its directory, model
  and agent and then sends that message.
- The draft lives in AppState (memory only): leaving it keeps it; the
  sidebar shows a draft row with its first line and a discard button; a
  failed create stays on the draft with its text.
- The session list groups by project (chat folders under 聊天), dates the
  rows (今天/昨天/本周早些时候/更早), pins through
  session.metadata.openchamber.pinned, archives on demand, pages with
  "载入更多", and swipes to pin/archive/delete (flutter_slidable).
- Child sessions are hidden; a parent's row shows its children's activity.
- The chat app bar follows the live session list, so the title the server
  generates from the first message appears at once.
- Attachments and the model picker move to shared widgets, used by the
  chat and the draft.
- 0.3.13 (313).
2026-11-21 14:29:52 +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';
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: Text('附件不能超过 10MB')));
}
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;
}
}