41d30a4280
Split the 406-line chat session browser into a controller, body, shared adaptive primitives and separate standard Material and Cupertino lists and dialogs. Preserve the existing server directory/backend catalogs, instructions field, closed/starred grouping, directory headings and empty/no-match states. - keep refresh generations so stale replies are discarded - open a created chat immediately and refresh the list in the background - disable rename Save while the trimmed name is blank - label row actions per session, with the id on duplicate titles - wrap Cupertino empty/no-match/error messages instead of clipping, and give them a Cupertino text style under a bare MaterialApp - allow the Material rename helper to wrap at large text scales - cover both native branches, lifecycle ordering, create/rename/close/star and the Cupertino theme path with widget tests
162 lines
5.0 KiB
Dart
162 lines
5.0 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../../../core/api_gateway.dart';
|
|
import '../../domain/chat_sessions.dart';
|
|
import 'chat_adaptive.dart';
|
|
import 'session_row_style.dart';
|
|
|
|
enum SessionAction { rename, close, reopen }
|
|
|
|
/// The close confirmation's body, matching the web client's explanation.
|
|
String closeChatConfirmation(String title) =>
|
|
'Closes "$title" and marks it closed on every device. '
|
|
'A turn that is running is stopped first. '
|
|
'Nothing is deleted — the chat and its history are kept, '
|
|
'and you can reopen it from the list.';
|
|
|
|
class SessionStarButton extends StatelessWidget {
|
|
const SessionStarButton({
|
|
super.key,
|
|
required this.session,
|
|
required this.onPressed,
|
|
});
|
|
|
|
final JsonMap session;
|
|
final VoidCallback onPressed;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final starred = session['starred'] == true;
|
|
final label = starred ? 'Unstar chat' : 'Star chat';
|
|
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
|
|
return Semantics(
|
|
label: label,
|
|
button: true,
|
|
child: CupertinoButton(
|
|
padding: EdgeInsets.zero,
|
|
onPressed: onPressed,
|
|
child: Icon(starred ? CupertinoIcons.star_fill : CupertinoIcons.star),
|
|
),
|
|
);
|
|
}
|
|
return IconButton(
|
|
tooltip: label,
|
|
onPressed: onPressed,
|
|
icon: Icon(starred ? Icons.star : Icons.star_border),
|
|
);
|
|
}
|
|
}
|
|
|
|
class SessionActionsButton extends StatelessWidget {
|
|
const SessionActionsButton({
|
|
super.key,
|
|
required this.session,
|
|
required this.onSelected,
|
|
this.duplicateTitles = const {},
|
|
});
|
|
|
|
final JsonMap session;
|
|
final ValueChanged<SessionAction> onSelected;
|
|
final Set<String> duplicateTitles;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final label = sessionActionsLabel(session, duplicateTitles);
|
|
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
|
|
return Semantics(
|
|
label: label,
|
|
button: true,
|
|
child: CupertinoButton(
|
|
padding: EdgeInsets.zero,
|
|
onPressed: () => _showSheet(context),
|
|
child: const Icon(CupertinoIcons.ellipsis),
|
|
),
|
|
);
|
|
}
|
|
return PopupMenuButton<SessionAction>(
|
|
tooltip: label,
|
|
onSelected: onSelected,
|
|
itemBuilder: (context) => _menuItems(session),
|
|
);
|
|
}
|
|
|
|
Future<void> _showSheet(BuildContext context) async {
|
|
final action = await showCupertinoModalPopup<SessionAction>(
|
|
context: context,
|
|
builder: (sheetContext) => CupertinoActionSheet(
|
|
title: Text(chatSessionTitle(session)),
|
|
actions: [
|
|
for (final item in _items(session))
|
|
CupertinoActionSheetAction(
|
|
onPressed: () => Navigator.pop(sheetContext, item.action),
|
|
child: Text(item.label),
|
|
),
|
|
],
|
|
cancelButton: CupertinoActionSheetAction(
|
|
onPressed: () => Navigator.pop(sheetContext),
|
|
child: const Text('Cancel'),
|
|
),
|
|
),
|
|
);
|
|
if (action != null && context.mounted) onSelected(action);
|
|
}
|
|
}
|
|
|
|
List<PopupMenuEntry<SessionAction>> _menuItems(JsonMap session) => [
|
|
for (final item in _items(session))
|
|
PopupMenuItem(value: item.action, child: Text(item.label)),
|
|
];
|
|
|
|
List<({SessionAction action, String label})> _items(JsonMap session) {
|
|
final isClosed = session['closed_at'] != null;
|
|
return [
|
|
(action: SessionAction.rename, label: 'Rename…'),
|
|
isClosed
|
|
? (action: SessionAction.reopen, label: 'Reopen')
|
|
: (action: SessionAction.close, label: 'Close chat…'),
|
|
];
|
|
}
|
|
|
|
Future<bool> confirmCloseChat(BuildContext context, JsonMap session) async {
|
|
final title = chatSessionTitle(session);
|
|
final body = closeChatConfirmation(title);
|
|
final confirmed = chatControlsStyleOf(context) == ChatControlsStyle.cupertino
|
|
? await showCupertinoDialog<bool>(
|
|
context: context,
|
|
builder: (context) => CupertinoAlertDialog(
|
|
title: const Text('Close chat?'),
|
|
content: Text(body),
|
|
actions: [
|
|
CupertinoDialogAction(
|
|
onPressed: () => Navigator.pop(context, false),
|
|
child: const Text('Cancel'),
|
|
),
|
|
CupertinoDialogAction(
|
|
isDestructiveAction: true,
|
|
onPressed: () => Navigator.pop(context, true),
|
|
child: const Text('Close chat'),
|
|
),
|
|
],
|
|
),
|
|
)
|
|
: await showDialog<bool>(
|
|
context: context,
|
|
builder: (context) => AlertDialog(
|
|
title: const Text('Close chat?'),
|
|
content: Text(body),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context, false),
|
|
child: const Text('Cancel'),
|
|
),
|
|
FilledButton(
|
|
onPressed: () => Navigator.pop(context, true),
|
|
child: const Text('Close chat'),
|
|
),
|
|
],
|
|
),
|
|
);
|
|
return confirmed == true;
|
|
}
|