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
177 lines
5.5 KiB
Dart
177 lines
5.5 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../../../core/api_gateway.dart';
|
|
import '../../data/chat_repository.dart';
|
|
import 'chat_adaptive.dart';
|
|
import 'chat_sessions_body.dart';
|
|
import 'chat_sessions_controller.dart';
|
|
import 'create_chat_dialog.dart';
|
|
import 'rename_chat_dialog.dart';
|
|
import 'session_actions.dart';
|
|
|
|
/// Browses server-owned sessions. Opening a row is read-only at this layer.
|
|
class ChatSessionsScreen extends StatefulWidget {
|
|
const ChatSessionsScreen({
|
|
super.key,
|
|
required this.gateway,
|
|
required this.onOpen,
|
|
});
|
|
final ApiGateway gateway;
|
|
final void Function(JsonMap session) onOpen;
|
|
|
|
@override
|
|
State<ChatSessionsScreen> createState() => _ChatSessionsScreenState();
|
|
}
|
|
|
|
class _ChatSessionsScreenState extends State<ChatSessionsScreen> {
|
|
late final ChatSessionsController _controller = ChatSessionsController(
|
|
ChatRepository(widget.gateway),
|
|
)..refresh();
|
|
final _search = TextEditingController();
|
|
|
|
@override
|
|
void didUpdateWidget(covariant ChatSessionsScreen oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (oldWidget.gateway != widget.gateway) {
|
|
_controller.replaceRepository(ChatRepository(widget.gateway));
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_controller.dispose();
|
|
_search.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
String _sessionId(JsonMap session) => '${session['id'] ?? ''}';
|
|
|
|
Future<void> _rename(JsonMap session) async {
|
|
final title = await showRenameChatDialog(context, session);
|
|
if (title == null || !mounted) return;
|
|
await _controller.rename(_sessionId(session), title);
|
|
}
|
|
|
|
Future<void> _close(JsonMap session) async {
|
|
if (!await confirmCloseChat(context, session)) return;
|
|
if (!mounted) return;
|
|
await _controller.close(_sessionId(session));
|
|
}
|
|
|
|
Future<void> _newChat() async {
|
|
final operationId = 'native-chat-${DateTime.now().microsecondsSinceEpoch}';
|
|
final draft = await showChatCreateDialog(
|
|
context,
|
|
repository: _controller.repository,
|
|
);
|
|
if (draft == null || !mounted) return;
|
|
final session = await _controller.create(
|
|
title: draft.title,
|
|
directory: draft.directory,
|
|
backend: draft.backend,
|
|
instructions: draft.instructions,
|
|
operationId: operationId,
|
|
);
|
|
if (session != null && mounted) widget.onOpen(session);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return ListenableBuilder(
|
|
listenable: _controller,
|
|
builder: (context, _) {
|
|
final body = Column(
|
|
children: [
|
|
ChatSearchField(
|
|
controller: _search,
|
|
onChanged: (_) => setState(() {}),
|
|
),
|
|
if (_controller.error != null)
|
|
ChatErrorNotice(
|
|
message: _controller.error!,
|
|
onRetry: _controller.refresh,
|
|
),
|
|
Expanded(
|
|
child: ChatSessionsBody(
|
|
sessions: _controller.sessions,
|
|
query: _search.text,
|
|
loading: _controller.loading,
|
|
error: _controller.error,
|
|
onOpen: widget.onOpen,
|
|
onRename: _rename,
|
|
onClose: _close,
|
|
onReopen: (session) => _controller.reopen(_sessionId(session)),
|
|
onStar: (session) => _controller.star(
|
|
_sessionId(session),
|
|
session['starred'] != true,
|
|
),
|
|
onClearSearch: () {
|
|
_search.clear();
|
|
setState(() {});
|
|
},
|
|
),
|
|
),
|
|
],
|
|
);
|
|
|
|
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
|
|
return CupertinoPageScaffold(
|
|
navigationBar: CupertinoNavigationBar(
|
|
middle: const Text('Chats'),
|
|
trailing: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
ChatBarAction(
|
|
key: const Key('chat-new-chat'),
|
|
label: 'New chat',
|
|
onPressed: _newChat,
|
|
materialIcon: Icons.add,
|
|
cupertinoIcon: CupertinoIcons.add,
|
|
),
|
|
ChatBarAction(
|
|
key: const Key('chat-refresh'),
|
|
label: 'Refresh',
|
|
onPressed: _controller.refresh,
|
|
materialIcon: Icons.refresh,
|
|
cupertinoIcon: CupertinoIcons.refresh,
|
|
),
|
|
],
|
|
),
|
|
),
|
|
child: SafeArea(child: body),
|
|
);
|
|
}
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: const Text('Chats'),
|
|
actions: [
|
|
ChatBarAction(
|
|
key: const Key('chat-new-chat'),
|
|
label: 'New chat',
|
|
onPressed: _newChat,
|
|
materialIcon: Icons.add,
|
|
cupertinoIcon: CupertinoIcons.add,
|
|
),
|
|
ChatBarAction(
|
|
key: const Key('chat-refresh'),
|
|
label: 'Refresh',
|
|
onPressed: _controller.refresh,
|
|
materialIcon: Icons.refresh,
|
|
cupertinoIcon: CupertinoIcons.refresh,
|
|
),
|
|
],
|
|
),
|
|
body: body,
|
|
floatingActionButton: FloatingActionButton.extended(
|
|
key: const Key('chat-new-chat-fab'),
|
|
onPressed: _newChat,
|
|
icon: const Icon(Icons.add),
|
|
label: const Text('New chat'),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|