Build chat panes through one content builder
Phase 2.2 of docs/plans/floating-windows.md: buildChatPaneContent wraps a conversation in the scopes it needs and keys it by the runtime, so the tiling and (next) a floating window build the same subtree. The tiling uses it; behaviour is unchanged.
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/widgets.dart';
|
||||
|
||||
import '../../core/api_gateway.dart';
|
||||
import '../../core/servers/chat/chat_identity.dart';
|
||||
@@ -20,9 +22,42 @@ import 'ui/sessions/chat_sessions_controller.dart';
|
||||
///
|
||||
/// It notifies when a row a conversation is drawn from changes.
|
||||
class ChatRuntime extends ChangeNotifier {
|
||||
ChatRuntime({required ApiGateway gateway})
|
||||
ChatRuntime({required ApiGateway gateway, this.serverKey = ''})
|
||||
: _gateway = gateway,
|
||||
sessions = ChatSessionsController(ChatRepository(gateway));
|
||||
sessions = ChatSessionsController(ChatRepository(gateway)) {
|
||||
sessions.onSessionReplaced = (previousId, session) {
|
||||
for (final listener in _replaced.toList()) {
|
||||
listener(previousId, session);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// The server (or set of servers) these chats belong to; windows carry it.
|
||||
final String serverKey;
|
||||
|
||||
/// Advances when the socket is replaced, for chats shown outside the Chat
|
||||
/// destination.
|
||||
final ValueNotifier<int> syncToken = ValueNotifier(0);
|
||||
|
||||
final List<void Function(String previousId, JsonMap session)> _replaced = [];
|
||||
|
||||
/// Calls [listener] when another device moved a chat to a new session.
|
||||
void addSessionReplacedListener(
|
||||
void Function(String previousId, JsonMap session) listener,
|
||||
) => _replaced.add(listener);
|
||||
|
||||
void removeSessionReplacedListener(
|
||||
void Function(String previousId, JsonMap session) listener,
|
||||
) => _replaced.remove(listener);
|
||||
|
||||
final Map<String, GlobalKey> _viewKeys = {};
|
||||
|
||||
/// The key of [sessionId]'s live view, so it can move between the tiling
|
||||
/// and a window without being rebuilt.
|
||||
GlobalKey viewKey(String sessionId) => _viewKeys.putIfAbsent(
|
||||
sessionId,
|
||||
() => GlobalKey(debugLabel: 'chat view $sessionId'),
|
||||
);
|
||||
|
||||
ApiGateway _gateway;
|
||||
|
||||
@@ -128,6 +163,24 @@ class ChatRuntime extends ChangeNotifier {
|
||||
return true;
|
||||
}
|
||||
|
||||
/// An accepted mode switch also re-reads the list, so a listing already
|
||||
/// in flight from before the switch is superseded rather than adopted over
|
||||
/// the accepted mode.
|
||||
void acceptMode(String id, String mode, String name) {
|
||||
if (modeChanged(id, mode, name)) unawaited(sessions.refresh());
|
||||
}
|
||||
|
||||
void acceptEffort(String id, ChatEffortState state) {
|
||||
if (effortChanged(id, state)) unawaited(sessions.refresh());
|
||||
}
|
||||
|
||||
/// `/rename` was accepted: the opened row follows the shared title and the
|
||||
/// list re-reads it.
|
||||
void acceptRename(String id, String title) {
|
||||
renamed(id, title);
|
||||
unawaited(sessions.refresh());
|
||||
}
|
||||
|
||||
/// `/rename` was accepted for [id]: the opened row follows the title.
|
||||
void renamed(String id, String title) {
|
||||
final row = opened[id];
|
||||
@@ -183,6 +236,8 @@ class ChatRuntime extends ChangeNotifier {
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_replaced.clear();
|
||||
syncToken.dispose();
|
||||
sessions.dispose();
|
||||
folds.dispose();
|
||||
composerFocus.dispose();
|
||||
|
||||
Reference in New Issue
Block a user