- 全部功能 gains an 归档 entry: the new page lists every archived session with a count, search over title / id / project, project chips (all plus each project with its count), and a row that opens the chat; each row restores the session or opens a menu to open, restore, copy the id or delete it - the sidebar's 显示已归档 button and archived list are gone; the sidebar now lists only active sessions, while the archive page owns the archived ones (matching OpenChamber's archive page) - version 0.6.0+600 - tests cover the archive list, project chips, search and restore
357 lines
12 KiB
Dart
357 lines
12 KiB
Dart
// 归档: every archived session in one place, grouped by project and searchable.
|
|
// Archived sessions are no longer mixed into the sidebar; this page is the
|
|
// only list of them, mirroring OpenChamber's archive page (title, count,
|
|
// search, project chips, restore and delete).
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
import '../api/models.dart';
|
|
import '../l10n.dart';
|
|
import '../state.dart';
|
|
|
|
class ArchivedSessionsScreen extends StatefulWidget {
|
|
final AppState app;
|
|
|
|
const ArchivedSessionsScreen({super.key, required this.app});
|
|
|
|
@override
|
|
State<ArchivedSessionsScreen> createState() => _ArchivedSessionsScreenState();
|
|
}
|
|
|
|
class _ArchivedSessionsScreenState extends State<ArchivedSessionsScreen> {
|
|
final TextEditingController _searchCtrl = TextEditingController();
|
|
String _query = '';
|
|
|
|
/// The selected project bucket key; null is "all projects".
|
|
String? _bucket;
|
|
|
|
AppState get _app => widget.app;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
// The sidebar no longer fetches the archive; this page owns it.
|
|
if (!_app.archivedLoaded) _app.refreshArchived();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_searchCtrl.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
List<_ArchiveBucket> _buckets() {
|
|
final query = _query.trim().toLowerCase();
|
|
final byKey = <String, _ArchiveBucket>{};
|
|
for (final session in _app.archivedSessions) {
|
|
final label = _app.projectLabelFor(session.directory);
|
|
if (query.isNotEmpty &&
|
|
!session.title.toLowerCase().contains(query) &&
|
|
!session.id.toLowerCase().contains(query) &&
|
|
!label.toLowerCase().contains(query)) {
|
|
continue;
|
|
}
|
|
final key = session.directory.isEmpty
|
|
? AppState.chatTarget
|
|
: slashPath(session.directory);
|
|
final bucket = byKey.putIfAbsent(
|
|
key,
|
|
() => _ArchiveBucket(
|
|
key: key, label: label.isEmpty ? tr('Chat') : label),
|
|
);
|
|
bucket.sessions.add(session);
|
|
}
|
|
final buckets = byKey.values.toList();
|
|
for (final bucket in buckets) {
|
|
bucket.sessions.sort((a, b) => _time(b).compareTo(_time(a)));
|
|
}
|
|
buckets.sort((a, b) =>
|
|
_time(b.sessions.first).compareTo(_time(a.sessions.first)));
|
|
return buckets;
|
|
}
|
|
|
|
static int _time(Session session) =>
|
|
session.archived > 0 ? session.archived : session.updated;
|
|
|
|
List<Session> _shown(List<_ArchiveBucket> buckets, String? selected) {
|
|
if (selected == null) {
|
|
return [
|
|
for (final bucket in buckets) ...bucket.sessions,
|
|
]..sort((a, b) => _time(b).compareTo(_time(a)));
|
|
}
|
|
for (final bucket in buckets) {
|
|
if (bucket.key == selected) return bucket.sessions;
|
|
}
|
|
return const [];
|
|
}
|
|
|
|
Future<void> _refresh() => _app.refreshArchived();
|
|
|
|
Future<void> _unarchive(Session session) async {
|
|
await _app.setArchived(session, false);
|
|
if (!mounted) return;
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: AppText(tr('Session restored'))));
|
|
}
|
|
|
|
void _copy(Session session) {
|
|
Clipboard.setData(ClipboardData(text: session.id));
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
SnackBar(content: AppText(tr('Session ID copied'))));
|
|
}
|
|
|
|
void _confirmDelete(Session session) {
|
|
showDialog(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const AppText('Delete session?'),
|
|
content: AppText(trArgs(
|
|
'“{0}” and its subagent sessions will be deleted permanently.',
|
|
[session.title])),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: const AppText('Cancel')),
|
|
FilledButton(
|
|
onPressed: () {
|
|
Navigator.pop(context);
|
|
_app.client?.deleteSession(session.id);
|
|
_app.sessions.removeWhere((s) => s.id == session.id);
|
|
_app.archivedSessions.removeWhere((s) => s.id == session.id);
|
|
_app.touch();
|
|
},
|
|
child: const AppText('Delete'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
|
|
void _open(Session session) {
|
|
_app.openChat(session);
|
|
// The archive page sits above the all-features page and the shell; the
|
|
// chat opens on the shell.
|
|
Navigator.of(context).popUntil((route) => route.isFirst);
|
|
}
|
|
|
|
String _subtitle(Session session, {required bool showProject}) {
|
|
final parts = <String>[];
|
|
if (showProject) parts.add(_app.projectLabelFor(session.directory));
|
|
if (session.isChild) parts.add(tr('Subagent'));
|
|
final time = _time(session);
|
|
if (time > 0) {
|
|
final d = DateTime.fromMillisecondsSinceEpoch(time);
|
|
parts.add('${d.month.toString().padLeft(2, '0')}-'
|
|
'${d.day.toString().padLeft(2, '0')} '
|
|
'${d.hour.toString().padLeft(2, '0')}:'
|
|
'${d.minute.toString().padLeft(2, '0')}');
|
|
}
|
|
return parts.join(' · ');
|
|
}
|
|
|
|
Widget _row(Session session, {required bool showProject}) {
|
|
return ListTile(
|
|
// Compact like the sidebar's session rows.
|
|
dense: true,
|
|
minTileHeight: 56,
|
|
contentPadding: const EdgeInsets.only(left: 16, right: 4),
|
|
title: Text(
|
|
session.title.isEmpty ? tr('(Untitled session)') : session.title,
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
style: const TextStyle(fontSize: 14),
|
|
),
|
|
subtitle: Text(
|
|
_subtitle(session, showProject: showProject),
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
),
|
|
trailing: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
IconButton(
|
|
icon: const Icon(Icons.unarchive_outlined, size: 20),
|
|
tooltip: tr('Unarchive'),
|
|
onPressed: () => _unarchive(session),
|
|
),
|
|
PopupMenuButton<String>(
|
|
icon: const Icon(Icons.more_vert, size: 18),
|
|
onSelected: (value) {
|
|
switch (value) {
|
|
case 'open':
|
|
_open(session);
|
|
break;
|
|
case 'unarchive':
|
|
_unarchive(session);
|
|
break;
|
|
case 'copy':
|
|
_copy(session);
|
|
break;
|
|
case 'delete':
|
|
_confirmDelete(session);
|
|
break;
|
|
}
|
|
},
|
|
itemBuilder: (_) => [
|
|
const PopupMenuItem(value: 'open', child: AppText('Open')),
|
|
const PopupMenuItem(
|
|
value: 'unarchive', child: AppText('Unarchive')),
|
|
const PopupMenuItem(
|
|
value: 'copy', child: AppText('Copy session ID')),
|
|
const PopupMenuItem(value: 'delete', child: AppText('Delete')),
|
|
],
|
|
),
|
|
],
|
|
),
|
|
onTap: () => _open(session),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final theme = Theme.of(context);
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const AppText('Archive'),
|
|
actions: [
|
|
IconButton(
|
|
icon: const Icon(Icons.refresh),
|
|
tooltip: tr('Refresh'),
|
|
onPressed: _app.archivedLoading ? null : _refresh,
|
|
),
|
|
],
|
|
),
|
|
body: ListenableBuilder(
|
|
listenable: _app,
|
|
builder: (context, _) {
|
|
final buckets = _buckets();
|
|
// A restored session can remove the selected bucket; fall back to
|
|
// "all projects" instead of showing an empty list.
|
|
final selected = buckets.any((bucket) => bucket.key == _bucket)
|
|
? _bucket
|
|
: null;
|
|
final shown = _shown(buckets, selected);
|
|
final all = _app.archivedSessions.length;
|
|
final total =
|
|
buckets.fold<int>(0, (sum, bucket) => sum + bucket.sessions.length);
|
|
final count = shown.length == 1
|
|
? tr('1 archived session')
|
|
: trArgs('{0} archived sessions', [shown.length]);
|
|
return Column(
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.fromLTRB(16, 8, 16, 6),
|
|
child: TextField(
|
|
controller: _searchCtrl,
|
|
onChanged: (value) => setState(() => _query = value),
|
|
decoration: InputDecoration(
|
|
hintText: tr('Search archived sessions'),
|
|
prefixIcon: const Icon(Icons.search),
|
|
isDense: true,
|
|
border: OutlineInputBorder(
|
|
borderRadius: BorderRadius.circular(10)),
|
|
),
|
|
),
|
|
),
|
|
if (buckets.isNotEmpty)
|
|
SizedBox(
|
|
height: 42,
|
|
child: ListView(
|
|
scrollDirection: Axis.horizontal,
|
|
padding: const EdgeInsets.symmetric(horizontal: 12),
|
|
children: [
|
|
_chip(
|
|
label: tr('All'),
|
|
count: total,
|
|
selected: selected == null,
|
|
onTap: () => setState(() => _bucket = null),
|
|
),
|
|
for (final bucket in buckets)
|
|
_chip(
|
|
label: bucket.label,
|
|
count: bucket.sessions.length,
|
|
selected: selected == bucket.key,
|
|
onTap: () => setState(() => _bucket = bucket.key),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.fromLTRB(16, 6, 16, 2),
|
|
child: Align(
|
|
alignment: Alignment.centerLeft,
|
|
child: AppText(
|
|
count,
|
|
style:
|
|
TextStyle(fontSize: 12, color: theme.hintColor),
|
|
),
|
|
),
|
|
),
|
|
Expanded(
|
|
child: _app.archivedLoading && all == 0
|
|
? const Center(child: CircularProgressIndicator())
|
|
: RefreshIndicator(
|
|
onRefresh: _refresh,
|
|
child: shown.isEmpty
|
|
? ListView(
|
|
// Keep pull-to-refresh working on an empty
|
|
// archive.
|
|
physics:
|
|
const AlwaysScrollableScrollPhysics(),
|
|
children: [
|
|
Padding(
|
|
padding: const EdgeInsets.all(32),
|
|
child: AppText(
|
|
all == 0
|
|
? 'No archived sessions'
|
|
: 'No matching archived sessions',
|
|
textAlign: TextAlign.center,
|
|
),
|
|
),
|
|
],
|
|
)
|
|
: ListView(
|
|
physics:
|
|
const AlwaysScrollableScrollPhysics(),
|
|
padding: const EdgeInsets.only(bottom: 24),
|
|
children: [
|
|
for (final session in shown)
|
|
_row(session,
|
|
showProject: selected == null),
|
|
],
|
|
),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _chip({
|
|
required String label,
|
|
required int count,
|
|
required bool selected,
|
|
required VoidCallback onTap,
|
|
}) {
|
|
return Padding(
|
|
padding: const EdgeInsets.only(right: 8),
|
|
child: ChoiceChip(
|
|
selected: selected,
|
|
onSelected: (_) => onTap(),
|
|
label: Text('$label ($count)',
|
|
maxLines: 1, overflow: TextOverflow.ellipsis),
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class _ArchiveBucket {
|
|
final String key;
|
|
final String label;
|
|
final List<Session> sessions = [];
|
|
|
|
_ArchiveBucket({required this.key, required this.label});
|
|
}
|