import '../../../core/api_gateway.dart'; import 'chat_sessions.dart'; /// The rows the chat list draws, top to bottom: the [query]'s matches in /// Starred, then Chats by directory, then Closed, leaving out every section /// [isOpen] reports folded. Ctrl+1…Ctrl+9 and Next chat both count these. List chatSessionsShown( List sessions, { required String query, required bool Function(String title) isOpen, }) => chatSessionsInListOrder( chatSessionSections(filterChatSessions(sessions, query)), isOpen: isOpen, ); /// The row after [currentId] in [shown], wrapping from the last row to the /// first. A chat not among [shown] (its section is folded) moves to the /// first row. Null when no other row is shown, so there is nothing to move to. JsonMap? chatSessionAfter(List shown, String currentId) { final index = shown.indexWhere((row) => '${row['id'] ?? ''}' == currentId); final others = index < 0 ? shown.length : shown.length - 1; if (others < 1) return null; return shown[(index + 1) % shown.length]; } /// The chats Ctrl+Tab steps through, in the order the list draws them: the /// starred chats when any open chat is starred, otherwise every open chat /// (Chats by directory). Closed chats never take part, and a folded section /// does not hide its rows from the cycle. List chatTabCycle(List sessions) { final open = sessions .where((row) => row['closed_at'] == null && row['closed'] != true) .toList(); final sections = chatSessionSections(open); final starred = sections.where((section) => section.title == 'Starred'); return chatSessionsInListOrder( starred.isNotEmpty ? starred.toList() : sections, isOpen: (_) => true, ); } /// The chat [step] (1 forward, -1 back) from [currentId] in [cycle], /// wrapping. A chat outside the cycle (unstarred while starred chats exist) /// moves to the first chat, or the last going back. Null when there is no /// other chat to move to. JsonMap? chatTabTarget(List cycle, String currentId, int step) { if (cycle.isEmpty) return null; final index = cycle.indexWhere((row) => '${row['id'] ?? ''}' == currentId); if (index < 0) return step < 0 ? cycle.last : cycle.first; if (cycle.length < 2) return null; return cycle[(index + step) % cycle.length]; }