e4c196d75b
Transcripts pin to the newest end while following, release on the operator's upward scroll, and offer a native Jump to latest control carrying the count of messages that arrived meanwhile. The chat destination keeps each conversation's saved position so reopening restores it.
149 lines
4.7 KiB
Dart
149 lines
4.7 KiB
Dart
import 'package:flutter/material.dart';
|
|
|
|
import '../../core/api_gateway.dart';
|
|
import 'conversation/chat_models.dart';
|
|
import 'conversation/chat_scroll.dart';
|
|
import 'conversation/chat_thread_view.dart';
|
|
import 'ui/sessions/chat_sessions_screen.dart';
|
|
|
|
/// The Chat destination's list/detail switch.
|
|
///
|
|
/// The route names the open session, so a row tap, a deep link and Back all
|
|
/// navigate through the same value. The session list stays mounted (offstage)
|
|
/// while a conversation is open, so returning to it keeps its rows and query.
|
|
/// The shell owns the app bar; this screen adds no second one.
|
|
class ChatScreen extends StatefulWidget {
|
|
const ChatScreen({
|
|
super.key,
|
|
required this.gateway,
|
|
this.initialSessionId,
|
|
this.syncToken = 0,
|
|
this.onSessionSelected,
|
|
this.onBack,
|
|
});
|
|
|
|
final ApiGateway gateway;
|
|
|
|
/// The chat session the current route names, or null for the list.
|
|
final String? initialSessionId;
|
|
|
|
/// Advances when the shared socket is replaced. The open conversation
|
|
/// re-subscribes and re-reads in place, so its selection survives.
|
|
final int syncToken;
|
|
|
|
final ValueChanged<String>? onSessionSelected;
|
|
final VoidCallback? onBack;
|
|
|
|
@override
|
|
State<ChatScreen> createState() => _ChatScreenState();
|
|
}
|
|
|
|
class _ChatScreenState extends State<ChatScreen> {
|
|
/// Rows this visit has opened, used for the native title and the recorded
|
|
/// model facts. The mounted list also reports its loaded rows, so a routed id
|
|
/// (a deep link) resolves the same row without a second list request.
|
|
final Map<String, JsonMap> _openedSessions = {};
|
|
|
|
/// Model switches the server accepted after a row was opened. The row that
|
|
/// opened the conversation may still carry the pre-switch model, so these win
|
|
/// for the rest of this visit, including when the same chat is reopened.
|
|
final Map<String, ChatModelSelection> _acceptedModels = {};
|
|
|
|
/// Transcript positions for this visit. It lives with the destination, not a
|
|
/// conversation view, so Back and a reopened chat return to the same place.
|
|
final ChatScrollStore _scrollPositions = MemoryChatScrollStore();
|
|
|
|
String? _standaloneSelection;
|
|
|
|
String? get _selected {
|
|
final routed = widget.initialSessionId;
|
|
if (routed != null && routed.isNotEmpty) return routed;
|
|
return _standaloneSelection;
|
|
}
|
|
|
|
void _open(JsonMap session) {
|
|
final id = '${session['id'] ?? ''}';
|
|
if (id.isEmpty) return;
|
|
_openedSessions[id] = session;
|
|
final select = widget.onSessionSelected;
|
|
if (select != null) {
|
|
select(id);
|
|
} else {
|
|
setState(() => _standaloneSelection = id);
|
|
}
|
|
}
|
|
|
|
/// Adopts the list's loaded rows for the routed session: a deep link opens
|
|
/// the conversation before any row has been tapped, and the header's title
|
|
/// and model control come from that row. Called after a frame, never during
|
|
/// the list's own build.
|
|
void _adoptRows(List<JsonMap> rows) {
|
|
final id = _selected;
|
|
if (id == null || _openedSessions.containsKey(id)) return;
|
|
for (final row in rows) {
|
|
if ('${row['id'] ?? ''}' != id) continue;
|
|
setState(() => _openedSessions[id] = row);
|
|
return;
|
|
}
|
|
}
|
|
|
|
void _modelChanged(String id, ChatModelSelection applied) {
|
|
if (!mounted) return;
|
|
setState(() => _acceptedModels[id] = applied);
|
|
}
|
|
|
|
/// The row to open for [id], with an accepted model folded in.
|
|
JsonMap? _rowFor(String id) {
|
|
final row = _openedSessions[id];
|
|
final accepted = _acceptedModels[id];
|
|
if (row == null || accepted == null) return row;
|
|
return {
|
|
...row,
|
|
'model': accepted.model,
|
|
'model_provider': accepted.provider,
|
|
'model_selection': accepted.selection,
|
|
};
|
|
}
|
|
|
|
void _back() {
|
|
final back = widget.onBack;
|
|
if (back != null) {
|
|
back();
|
|
} else {
|
|
setState(() => _standaloneSelection = null);
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final selected = _selected;
|
|
final row = selected == null ? null : _rowFor(selected);
|
|
return Stack(
|
|
children: [
|
|
Offstage(
|
|
offstage: selected != null,
|
|
child: ChatSessionsScreen(
|
|
gateway: widget.gateway,
|
|
embedded: true,
|
|
syncToken: widget.syncToken,
|
|
onOpen: _open,
|
|
onSessions: _adoptRows,
|
|
),
|
|
),
|
|
if (selected != null)
|
|
ChatThreadView(
|
|
key: ValueKey<String>('chat-session-$selected'),
|
|
gateway: widget.gateway,
|
|
sessionId: selected,
|
|
title: '${row?['title'] ?? ''}'.trim(),
|
|
session: row,
|
|
syncToken: widget.syncToken,
|
|
scrollPositions: _scrollPositions,
|
|
onBack: _back,
|
|
onModelChanged: (applied) => _modelChanged(selected, applied),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|