319 lines
10 KiB
Dart
319 lines
10 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../../../core/api_gateway.dart';
|
|
import '../../../../shell/shell_bar.dart';
|
|
import '../../data/chat_repository.dart';
|
|
import 'chat_adaptive.dart';
|
|
import 'chat_session_local.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,
|
|
this.embedded = false,
|
|
this.syncToken = 0,
|
|
this.onSessions,
|
|
this.localStore,
|
|
this.controller,
|
|
this.activeSessionId,
|
|
});
|
|
final ApiGateway gateway;
|
|
final void Function(JsonMap session) onOpen;
|
|
|
|
/// True under the application shell, which owns the single navigation bar.
|
|
final bool embedded;
|
|
|
|
/// Advances when the shared socket is replaced; sessions created, renamed or
|
|
/// closed elsewhere during the gap must not stay invisible until Refresh.
|
|
final int syncToken;
|
|
|
|
/// Reports the loaded rows after a frame. The destination resolves a routed
|
|
/// session id (a deep link) from them, so that conversation gets the same
|
|
/// title and model facts as a tapped row without a second list request.
|
|
final ValueChanged<List<JsonMap>>? onSessions;
|
|
|
|
/// The conversation open above this list, or null when the list is showing.
|
|
/// Pushed activity for it is being read, so it never raises a New mark.
|
|
final String? activeSessionId;
|
|
|
|
/// Overrides the device-local hidden/read store in tests.
|
|
final ChatSessionLocalStore? localStore;
|
|
|
|
/// A list controller the destination owns and keeps across this screen's
|
|
/// life, so a session created from an open conversation can be listed here.
|
|
/// Null makes this screen own its own.
|
|
final ChatSessionsController? controller;
|
|
|
|
@override
|
|
State<ChatSessionsScreen> createState() => _ChatSessionsScreenState();
|
|
}
|
|
|
|
class _ChatSessionsScreenState extends State<ChatSessionsScreen> {
|
|
late final ChatSessionsController _controller =
|
|
(widget.controller ??
|
|
ChatSessionsController(
|
|
ChatRepository(widget.gateway),
|
|
localStore: widget.localStore,
|
|
))
|
|
..activeSessionId = widget.activeSessionId
|
|
..refresh();
|
|
final _search = TextEditingController();
|
|
Timer? _undoTimer;
|
|
String? _markedActiveSessionId;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_controller.addListener(_reportSessions);
|
|
}
|
|
|
|
/// Reports the loaded rows at the end of the frame: the destination learns a
|
|
/// routed row without this screen calling back inside its own build.
|
|
void _reportSessions() {
|
|
_markActiveSessionRead();
|
|
if (widget.onSessions == null) return;
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
if (!mounted) return;
|
|
widget.onSessions?.call(_controller.sessions);
|
|
});
|
|
}
|
|
|
|
void _markActiveSessionRead() {
|
|
final id = widget.activeSessionId;
|
|
if (id == null) {
|
|
_markedActiveSessionId = null;
|
|
return;
|
|
}
|
|
if (id == _markedActiveSessionId) return;
|
|
for (final session in _controller.sessions) {
|
|
if (_sessionId(session) != id) continue;
|
|
// Mark synchronously so row activation and subsequent list notifications
|
|
// cannot write the same read watermark twice.
|
|
_markedActiveSessionId = id;
|
|
unawaited(_controller.markOpened(session));
|
|
return;
|
|
}
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(covariant ChatSessionsScreen oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
_controller.activeSessionId = widget.activeSessionId;
|
|
_markActiveSessionRead();
|
|
if (oldWidget.gateway != widget.gateway) {
|
|
_controller.replaceRepository(ChatRepository(widget.gateway));
|
|
} else if (oldWidget.syncToken != widget.syncToken) {
|
|
unawaited(_controller.refresh());
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_undoTimer?.cancel();
|
|
_controller.removeListener(_reportSessions);
|
|
if (widget.controller == null) _controller.dispose();
|
|
_search.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
String _sessionId(JsonMap session) => '${session['id'] ?? ''}';
|
|
|
|
Future<void> _rename(JsonMap session) async {
|
|
final id = _sessionId(session);
|
|
await showRenameChatDialog(
|
|
context,
|
|
session,
|
|
onSubmit: (title) => _controller.renameOrThrow(id, 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 {
|
|
// One id per create intent: a refusal retried in the dialog reuses it.
|
|
final operationId = 'native-chat-${DateTime.now().microsecondsSinceEpoch}';
|
|
final session = await showChatCreateDialog(
|
|
context,
|
|
repository: _controller.repository,
|
|
operationId: operationId,
|
|
onSubmit: (draft, id) => _controller.createOrThrow(
|
|
title: draft.title,
|
|
directory: draft.directory,
|
|
backend: draft.backend,
|
|
instructions: draft.instructions,
|
|
operationId: id,
|
|
),
|
|
);
|
|
if (session != null && mounted) widget.onOpen(session);
|
|
}
|
|
|
|
void _open(JsonMap session) {
|
|
_markedActiveSessionId = _sessionId(session);
|
|
unawaited(_controller.markOpened(session));
|
|
widget.onOpen(session);
|
|
}
|
|
|
|
Future<void> _hide(JsonMap session) async {
|
|
await _controller.hide(_sessionId(session));
|
|
_undoTimer?.cancel();
|
|
_undoTimer = Timer(const Duration(seconds: 8), () {
|
|
if (mounted) _controller.dismissUndo();
|
|
});
|
|
}
|
|
|
|
Future<void> _undoHide() async {
|
|
_undoTimer?.cancel();
|
|
await _controller.undoHide();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return ListenableBuilder(
|
|
listenable: _controller,
|
|
builder: (context, _) {
|
|
final controls = chatControlsStyleOf(context);
|
|
final body = Column(
|
|
children: [
|
|
if (_controller.error != null)
|
|
ChatErrorNotice(
|
|
message: _controller.error!,
|
|
onRetry: _controller.refresh,
|
|
),
|
|
Expanded(
|
|
child: Stack(
|
|
children: [
|
|
ChatSessionsBody(
|
|
sessions: _controller.sessions,
|
|
query: _search.text,
|
|
loading: _controller.loading,
|
|
error: _controller.error,
|
|
onOpen: _open,
|
|
onRename: _rename,
|
|
onClose: _close,
|
|
onReopen: (session) =>
|
|
_controller.reopen(_sessionId(session)),
|
|
onStar: (session) => _controller.star(
|
|
_sessionId(session),
|
|
session['starred'] != true,
|
|
),
|
|
onHide: _hide,
|
|
onClearSearch: () {
|
|
_search.clear();
|
|
setState(() {});
|
|
},
|
|
isUnseen: _controller.isUnseen,
|
|
errorFor: _controller.errorFor,
|
|
onDismissError: _controller.dismissError,
|
|
),
|
|
// The web's hide report is a fixed overlay: replacing or
|
|
// growing it must never move the list or overflow the screen.
|
|
// A standalone Material screen keeps it clear of the FAB.
|
|
if (_controller.lastHidden != null)
|
|
Positioned(
|
|
left: 8,
|
|
right: 8,
|
|
bottom:
|
|
!widget.embedded &&
|
|
controls == ChatControlsStyle.material
|
|
? 88
|
|
: 8,
|
|
child: ChatUndoBar(
|
|
key: const Key('chat-undo-hide'),
|
|
message:
|
|
'Hidden on this device; the chat stays on the server.',
|
|
onUndo: _undoHide,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
),
|
|
],
|
|
);
|
|
|
|
// Under the shell, New chat and Refresh are its bar's actions; the
|
|
// list keeps the space without a search field.
|
|
if (widget.embedded) {
|
|
if (ShellBar.available(context)) {
|
|
return ShellBar(
|
|
active: widget.activeSessionId == null,
|
|
actions: _actions(),
|
|
// The Chat tab counts chats with messages not yet seen.
|
|
child: ShellBadge(
|
|
kind: ShellBadgeKind.unread,
|
|
count: _controller.sessions.where(_controller.isUnseen).length,
|
|
child: body,
|
|
),
|
|
);
|
|
}
|
|
return Column(
|
|
children: [
|
|
Align(
|
|
alignment: AlignmentDirectional.centerEnd,
|
|
child: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: _actions(),
|
|
),
|
|
),
|
|
Expanded(child: body),
|
|
],
|
|
);
|
|
}
|
|
|
|
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
|
|
return CupertinoPageScaffold(
|
|
navigationBar: CupertinoNavigationBar(
|
|
middle: const Text('Chats'),
|
|
trailing: Row(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: _actions(),
|
|
),
|
|
),
|
|
child: SafeArea(child: body),
|
|
);
|
|
}
|
|
return Scaffold(
|
|
appBar: AppBar(title: const Text('Chats'), actions: _actions()),
|
|
body: body,
|
|
floatingActionButton: FloatingActionButton.extended(
|
|
key: const Key('chat-new-chat-fab'),
|
|
onPressed: _newChat,
|
|
icon: const Icon(Icons.add),
|
|
label: const Text('New chat'),
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
List<Widget> _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,
|
|
),
|
|
];
|
|
}
|