Files
pipelines/app/lib/features/chat/ui/sessions/chat_sessions_controller.dart
T
alan 41d30a4280 feat(app): split chat sessions into native Material and Cupertino views
Split the 406-line chat session browser into a controller, body, shared
adaptive primitives and separate standard Material and Cupertino lists and
dialogs. Preserve the existing server directory/backend catalogs,
instructions field, closed/starred grouping, directory headings and
empty/no-match states.

- keep refresh generations so stale replies are discarded
- open a created chat immediately and refresh the list in the background
- disable rename Save while the trimmed name is blank
- label row actions per session, with the id on duplicate titles
- wrap Cupertino empty/no-match/error messages instead of clipping, and
  give them a Cupertino text style under a bare MaterialApp
- allow the Material rename helper to wrap at large text scales
- cover both native branches, lifecycle ordering, create/rename/close/star
  and the Cupertino theme path with widget tests
2026-10-21 17:42:39 +01:00

112 lines
2.9 KiB
Dart

import 'dart:async';
import 'package:flutter/foundation.dart';
import '../../../../core/api_gateway.dart';
import '../../data/chat_repository.dart';
/// Owns the session list, its refresh generation and the row actions.
///
/// A refresh that finishes after a newer one (or after the screen is gone) is
/// discarded, so a slow reply can never replace fresher state.
class ChatSessionsController extends ChangeNotifier {
ChatSessionsController(this.repository);
ChatRepository repository;
List<JsonMap> _sessions = const [];
String? _error;
bool _loading = true;
bool _disposed = false;
int _generation = 0;
List<JsonMap> get sessions => _sessions;
String? get error => _error;
bool get loading => _loading;
void replaceRepository(ChatRepository next) {
if (identical(next.gateway, repository.gateway)) return;
repository = next;
refresh();
}
Future<void> refresh() async {
if (_disposed) return;
final generation = ++_generation;
_loading = true;
_error = null;
_notify();
try {
final sessions = await repository.sessions();
if (generation != _generation) return;
_sessions = sessions;
_loading = false;
_notify();
} catch (error) {
if (generation != _generation) return;
_error = '$error';
_loading = false;
_notify();
}
}
Future<void> star(String sessionId, bool starred) =>
_mutate(() => repository.star(sessionId, starred));
Future<void> close(String sessionId) =>
_mutate(() => repository.close(sessionId));
Future<void> reopen(String sessionId) =>
_mutate(() => repository.reopen(sessionId));
Future<void> rename(String sessionId, String title) =>
_mutate(() => repository.rename(sessionId, title));
Future<JsonMap?> create({
required String title,
required String directory,
required String backend,
required String instructions,
required String operationId,
}) async {
try {
final reply = await repository.create(
title: title,
directory: directory,
backend: backend,
operationId: operationId,
instructions: instructions,
);
// Opening the created chat must not wait for the list refresh: the dialog
// has already closed, so a slow listing would look like a failed create.
unawaited(refresh());
final session = reply['chat_session'];
return session is Map ? Map<String, dynamic>.from(session) : null;
} catch (error) {
_error = '$error';
_notify();
return null;
}
}
Future<void> _mutate(Future<JsonMap> Function() action) async {
try {
await action();
await refresh();
} catch (error) {
_error = '$error';
_notify();
}
}
void _notify() {
if (!_disposed) notifyListeners();
}
@override
void dispose() {
_disposed = true;
_generation++;
super.dispose();
}
}