Restore mobile Chats workspace on tab retap
This commit is contained in:
@@ -176,6 +176,27 @@ class AppRoute {
|
||||
String toString() => path;
|
||||
}
|
||||
|
||||
/// Chooses what an already-selected Chats tab should show when it is tapped
|
||||
/// again. A detail route is left alone; only the list can use the retained
|
||||
/// workspace route, and an absent/invalid retained route stays at the list.
|
||||
AppRoute? chatRouteForRetap(
|
||||
AppRoute current,
|
||||
AppRoute? retainedWorkspace, {
|
||||
bool Function(String sessionId)? isValidSession,
|
||||
}) {
|
||||
if (current.kind != AppRouteKind.chat || current.chatSessionId != null) {
|
||||
return current.kind == AppRouteKind.chat ? current : null;
|
||||
}
|
||||
final retained = retainedWorkspace;
|
||||
return retained != null &&
|
||||
retained.kind == AppRouteKind.chat &&
|
||||
retained.chatSessionId != null &&
|
||||
retained.chatSessionId!.isNotEmpty &&
|
||||
(isValidSession == null || isValidSession(retained.chatSessionId!))
|
||||
? retained
|
||||
: null;
|
||||
}
|
||||
|
||||
/// `AppRoute.run(3)` reads better than a constructor named after a field.
|
||||
class _RunRouteFactory {
|
||||
const _RunRouteFactory();
|
||||
|
||||
@@ -21,6 +21,8 @@ class ChatDestination extends StatelessWidget {
|
||||
this.initialSessionId,
|
||||
this.onSessionSelected,
|
||||
this.onBack,
|
||||
this.onWorkspaceEmpty,
|
||||
this.onAvailableSessionsChanged,
|
||||
});
|
||||
|
||||
final AppSession session;
|
||||
@@ -28,6 +30,8 @@ class ChatDestination extends StatelessWidget {
|
||||
final String? initialSessionId;
|
||||
final ValueChanged<String>? onSessionSelected;
|
||||
final VoidCallback? onBack;
|
||||
final VoidCallback? onWorkspaceEmpty;
|
||||
final ValueChanged<Set<String>>? onAvailableSessionsChanged;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => AnimatedBuilder(
|
||||
@@ -70,5 +74,7 @@ class ChatDestination extends StatelessWidget {
|
||||
syncToken: session.connectionEpoch,
|
||||
onSessionSelected: onSessionSelected,
|
||||
onBack: onBack,
|
||||
onWorkspaceEmpty: onWorkspaceEmpty,
|
||||
onAvailableSessionsChanged: onAvailableSessionsChanged,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -414,10 +414,23 @@ class _PipelinesAppState extends State<PipelinesApp>
|
||||
/// The last route each tab showed, so switching tabs returns to where the
|
||||
/// operator left it; tapping the open tab again goes to its root.
|
||||
final Map<AppDestination, AppRoute> _tabRoutes = {};
|
||||
AppRoute? _chatWorkspaceRoute;
|
||||
Set<String>? _availableChatSessionIds;
|
||||
|
||||
void _selectDestination(AppDestination destination) {
|
||||
final current = _destinationOf(_delegate.shellRoute.kind);
|
||||
final last = _tabRoutes[destination];
|
||||
if (destination == AppDestination.chat && current == destination) {
|
||||
final reopened = chatRouteForRetap(
|
||||
_delegate.shellRoute,
|
||||
_chatWorkspaceRoute,
|
||||
isValidSession: (id) =>
|
||||
_availableChatSessionIds == null ||
|
||||
_availableChatSessionIds!.contains(id),
|
||||
);
|
||||
_delegate.goTo(reopened ?? _routeOf(destination));
|
||||
return;
|
||||
}
|
||||
_delegate.goTo(
|
||||
destination == current || last == null ? _routeOf(destination) : last,
|
||||
);
|
||||
@@ -426,6 +439,9 @@ class _PipelinesAppState extends State<PipelinesApp>
|
||||
void _routeChanged() {
|
||||
final shown = _delegate.shellRoute;
|
||||
_tabRoutes[_destinationOf(shown.kind)] = shown;
|
||||
if (shown.kind == AppRouteKind.chat && shown.chatSessionId != null) {
|
||||
_chatWorkspaceRoute = shown;
|
||||
}
|
||||
final pending = _pendingRunSearch;
|
||||
if (!mounted || pending == null) return;
|
||||
final route = _delegate.shellRoute;
|
||||
@@ -562,6 +578,14 @@ class _PipelinesAppState extends State<PipelinesApp>
|
||||
initialSessionId: chatRoute.chatSessionId,
|
||||
onSessionSelected: (id) => _delegate.goTo(AppRoute.chatSession(id)),
|
||||
onBack: () => _delegate.goTo(AppRoute.chat),
|
||||
onWorkspaceEmpty: () => _chatWorkspaceRoute = null,
|
||||
onAvailableSessionsChanged: (ids) {
|
||||
_availableChatSessionIds = ids;
|
||||
final retained = _chatWorkspaceRoute?.chatSessionId;
|
||||
if (retained != null && !ids.contains(retained)) {
|
||||
_chatWorkspaceRoute = null;
|
||||
}
|
||||
},
|
||||
),
|
||||
// Search is the fourth tab; a result opens its destination.
|
||||
AppDestination.search: (_) => SearchDestination(
|
||||
|
||||
@@ -59,6 +59,8 @@ class ChatScreen extends StatefulWidget {
|
||||
this.syncToken = 0,
|
||||
this.onSessionSelected,
|
||||
this.onBack,
|
||||
this.onWorkspaceEmpty,
|
||||
this.onAvailableSessionsChanged,
|
||||
this.persistWorkspace = false,
|
||||
});
|
||||
|
||||
@@ -74,6 +76,8 @@ class ChatScreen extends StatefulWidget {
|
||||
|
||||
final ValueChanged<String>? onSessionSelected;
|
||||
final VoidCallback? onBack;
|
||||
final VoidCallback? onWorkspaceEmpty;
|
||||
final ValueChanged<Set<String>>? onAvailableSessionsChanged;
|
||||
|
||||
@override
|
||||
State<ChatScreen> createState() => _ChatScreenState();
|
||||
@@ -157,6 +161,8 @@ class _ChatScreenState extends State<ChatScreen>
|
||||
// Seen from now on, so the press that chooses the first chat counts.
|
||||
LastPointerKind.instance;
|
||||
_sessions.addListener(_nextChanged);
|
||||
_sessions.addListener(_availableSessionsChanged);
|
||||
_availableSessionsChanged();
|
||||
_folds.addListener(_nextChanged);
|
||||
}
|
||||
|
||||
@@ -174,6 +180,15 @@ class _ChatScreenState extends State<ChatScreen>
|
||||
if (next != null) _openBySelection(next);
|
||||
}
|
||||
|
||||
void _availableSessionsChanged() {
|
||||
if (!mounted || _sessions.loading) return;
|
||||
widget.onAvailableSessionsChanged?.call({
|
||||
for (final row in _sessions.sessions)
|
||||
if (row['closed_at'] == null && row['closed'] != true)
|
||||
'${row['id'] ?? ''}',
|
||||
});
|
||||
}
|
||||
|
||||
void _nextChanged() {
|
||||
if (!mounted) return;
|
||||
final id = _selected;
|
||||
@@ -307,6 +322,11 @@ class _ChatScreenState extends State<ChatScreen>
|
||||
/// the list's own build.
|
||||
void _adoptRows(List<JsonMap> rows) {
|
||||
if (!mounted) return;
|
||||
widget.onAvailableSessionsChanged?.call({
|
||||
for (final row in rows)
|
||||
if (row['closed_at'] == null && row['closed'] != true)
|
||||
'${row['id'] ?? ''}',
|
||||
});
|
||||
final selected = _selected;
|
||||
final retained = _workspace.panes.map((pane) => pane.sessionId).toSet();
|
||||
final next = <String, JsonMap>{};
|
||||
@@ -367,6 +387,7 @@ class _ChatScreenState extends State<ChatScreen>
|
||||
_publishedWorkspaceSession = null;
|
||||
_startNotice = null;
|
||||
_composerFocus.cancel();
|
||||
if (_workspace.paneCount == 0) widget.onWorkspaceEmpty?.call();
|
||||
final back = widget.onBack;
|
||||
if (back != null) {
|
||||
back();
|
||||
@@ -408,6 +429,8 @@ class _ChatScreenState extends State<ChatScreen>
|
||||
_workspaceBinding?.dispose();
|
||||
_workspace.removeListener(_workspaceChanged);
|
||||
_workspace.dispose();
|
||||
_sessions.removeListener(_nextChanged);
|
||||
_sessions.removeListener(_availableSessionsChanged);
|
||||
_sessions.dispose();
|
||||
_folds.dispose();
|
||||
_composerFocus.dispose();
|
||||
|
||||
Reference in New Issue
Block a user