import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../../core/api_gateway.dart'; import '../domain/chat_cycle.dart'; import '../ui/sessions/chat_adaptive.dart'; import '../ui/sessions/chat_sessions_body.dart'; import '../ui/sessions/chat_sessions_controller.dart'; /// Pick from exactly the sidebar's visible rows, using its native list rows. /// Local search only narrows this set; it cannot reveal a hidden/folded chat. Future showAddChatPanePicker( BuildContext context, { required ChatSessionsController controller, required ChatSectionFolds sidebarFolds, required Set excludedSessionIds, required ValueChanged onSelected, }) async { final cupertino = chatControlsStyleOf(context) == ChatControlsStyle.cupertino; Widget content(BuildContext context) => FractionallySizedBox( heightFactor: 0.85, child: _AddChatPanePicker( controller: controller, sidebarFolds: sidebarFolds, excludedSessionIds: excludedSessionIds, cupertino: cupertino, ), ); final selected = cupertino ? await showCupertinoModalPopup( context: context, builder: content, ) : await showModalBottomSheet( context: context, isScrollControlled: true, useSafeArea: true, showDragHandle: true, constraints: const BoxConstraints(maxWidth: 720), builder: content, ); if (selected == null || !context.mounted) return; final id = '${selected['id'] ?? ''}'; final current = chatSessionsShown( controller.sessions, query: '', isOpen: sidebarFolds.isOpen, ) .where( (row) => '${row['id'] ?? ''}' == id && !excludedSessionIds.contains(id), ) .firstOrNull; if (current != null) onSelected(current); } class _AddChatPanePicker extends StatefulWidget { const _AddChatPanePicker({ required this.controller, required this.sidebarFolds, required this.excludedSessionIds, required this.cupertino, }); final ChatSessionsController controller; final ChatSectionFolds sidebarFolds; final Set excludedSessionIds; final bool cupertino; @override State<_AddChatPanePicker> createState() => _AddChatPanePickerState(); } class _AddChatPanePickerState extends State<_AddChatPanePicker> { final _search = TextEditingController(); final _pickerFolds = ChatSectionFolds()..set('Closed', true); @override void dispose() { _search.dispose(); _pickerFolds.dispose(); super.dispose(); } void _ignore(JsonMap _) {} void _clear() { _search.clear(); setState(() {}); } @override Widget build(BuildContext context) { final list = Column( children: [ if (!widget.cupertino) Padding( padding: const EdgeInsetsDirectional.fromSTEB(24, 8, 8, 0), child: Row( children: [ Expanded( child: Text( 'Add chat pane', style: Theme.of(context).textTheme.titleLarge, ), ), TextButton( onPressed: () => Navigator.pop(context), child: const Text('Cancel'), ), ], ), ), ChatSearchField(controller: _search, onChanged: (_) => setState(() {})), Expanded( child: ListenableBuilder( listenable: Listenable.merge([ widget.controller, widget.sidebarFolds, ]), builder: (context, _) { final controller = widget.controller; final available = chatSessionsShown( controller.sessions, query: '', isOpen: widget.sidebarFolds.isOpen, ) .where( (row) => !widget.excludedSessionIds.contains( '${row['id'] ?? ''}', ), ) .toList(); return Column( children: [ if (controller.error != null) ChatErrorNotice( message: controller.error!, onRetry: controller.refresh, ), Expanded( child: ChatSessionsBody( sessions: available, query: _search.text, loading: controller.loading, error: controller.error, emptyMessageOverride: available.isEmpty && controller.error == null ? 'No other visible chats to add.' : null, folds: _pickerFolds, selectionOnly: true, onOpen: (row) => Navigator.pop(context, row), onRename: _ignore, onClose: _ignore, onReopen: _ignore, onStar: _ignore, onHide: _ignore, onClearSearch: _clear, isUnseen: controller.isUnseen, errorFor: (_) => null, ), ), ], ); }, ), ), ], ); return widget.cupertino ? CupertinoPageScaffold( navigationBar: CupertinoNavigationBar( automaticallyImplyLeading: false, middle: const Text('Add chat pane'), trailing: CupertinoButton( padding: EdgeInsets.zero, onPressed: () => Navigator.pop(context), child: const Text('Cancel'), ), ), child: SafeArea(child: list), ) : Material( child: Padding( padding: EdgeInsets.only( bottom: MediaQuery.viewInsetsOf(context).bottom, ), child: SafeArea(top: false, child: list), ), ); } }