import 'package:flutter/material.dart'; import '../../core/api_gateway.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? onSessionSelected; final VoidCallback? onBack; @override State createState() => _ChatScreenState(); } class _ChatScreenState extends State { /// Rows opened in this visit, used only for the native title. A deep link has /// no title until the list loads, and this screen waits for no list request. final Map _titles = {}; 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; final title = '${session['title'] ?? ''}'.trim(); if (title.isNotEmpty) _titles[id] = title; final select = widget.onSessionSelected; if (select != null) { select(id); } else { setState(() => _standaloneSelection = id); } } void _back() { final back = widget.onBack; if (back != null) { back(); } else { setState(() => _standaloneSelection = null); } } @override Widget build(BuildContext context) { final selected = _selected; return Stack( children: [ Offstage( offstage: selected != null, child: ChatSessionsScreen( gateway: widget.gateway, embedded: true, syncToken: widget.syncToken, onOpen: _open, ), ), if (selected != null) ChatThreadView( key: ValueKey('chat-session-$selected'), gateway: widget.gateway, sessionId: selected, title: _titles[selected] ?? '', syncToken: widget.syncToken, onBack: _back, ), ], ); } }