41d30a4280
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
223 lines
6.0 KiB
Dart
223 lines
6.0 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../data/chat_repository.dart';
|
|
import 'chat_adaptive.dart';
|
|
import 'chat_create_fields.dart';
|
|
|
|
class ChatDraft {
|
|
const ChatDraft({
|
|
required this.title,
|
|
required this.directory,
|
|
required this.backend,
|
|
required this.instructions,
|
|
});
|
|
|
|
final String title;
|
|
final String directory;
|
|
final String backend;
|
|
final String instructions;
|
|
}
|
|
|
|
Future<ChatDraft?> showChatCreateDialog(
|
|
BuildContext context, {
|
|
required ChatRepository repository,
|
|
}) {
|
|
final dialog = CreateChatDialog(repository: repository);
|
|
return chatControlsStyleOf(context) == ChatControlsStyle.cupertino
|
|
? showCupertinoDialog<ChatDraft>(context: context, builder: (_) => dialog)
|
|
: showDialog<ChatDraft>(context: context, builder: (_) => dialog);
|
|
}
|
|
|
|
class CreateChatDialog extends StatefulWidget {
|
|
const CreateChatDialog({super.key, required this.repository});
|
|
|
|
final ChatRepository repository;
|
|
|
|
@override
|
|
State<CreateChatDialog> createState() => _CreateChatDialogState();
|
|
}
|
|
|
|
class _CreateChatDialogState extends State<CreateChatDialog> {
|
|
final _name = TextEditingController(text: 'New chat');
|
|
final _instructions = TextEditingController();
|
|
List<String> _directories = const [];
|
|
List<String> _backends = const [];
|
|
String? _directory;
|
|
String? _backend;
|
|
String? _catalogError;
|
|
String? _formError;
|
|
bool _loading = true;
|
|
int _attempt = 0;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_loadCatalog();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_attempt++;
|
|
_name.dispose();
|
|
_instructions.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _loadCatalog() async {
|
|
final attempt = ++_attempt;
|
|
setState(() {
|
|
_loading = true;
|
|
_catalogError = null;
|
|
});
|
|
try {
|
|
final values = await Future.wait([
|
|
widget.repository.directories(),
|
|
widget.repository.backends(),
|
|
]);
|
|
if (!mounted || attempt != _attempt) return;
|
|
setState(() {
|
|
_directories = values[0];
|
|
_backends = values[1];
|
|
_directory = _directories.contains(_directory)
|
|
? _directory
|
|
: _directories.isEmpty
|
|
? null
|
|
: _directories.first;
|
|
_backend = _backends.contains(_backend)
|
|
? _backend
|
|
: _backends.isEmpty
|
|
? null
|
|
: _backends.first;
|
|
_loading = false;
|
|
});
|
|
} catch (error) {
|
|
if (!mounted || attempt != _attempt) return;
|
|
setState(() {
|
|
_catalogError = '$error';
|
|
_loading = false;
|
|
});
|
|
}
|
|
}
|
|
|
|
bool get _catalogReady =>
|
|
!_loading &&
|
|
_catalogError == null &&
|
|
_directories.isNotEmpty &&
|
|
_backends.isNotEmpty;
|
|
|
|
void _submit() {
|
|
if (!_catalogReady) return;
|
|
final title = _name.text.trim();
|
|
final directory = _directory ?? '';
|
|
if (title.isEmpty || directory.isEmpty) {
|
|
setState(() => _formError = 'Enter a name and server directory.');
|
|
return;
|
|
}
|
|
Navigator.pop(
|
|
context,
|
|
ChatDraft(
|
|
title: title,
|
|
directory: directory,
|
|
backend: _backend ?? '',
|
|
instructions: _instructions.text,
|
|
),
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final cupertino =
|
|
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
|
final content = SingleChildScrollView(
|
|
child: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
if (cupertino)
|
|
CupertinoTextField(
|
|
key: const Key('chat-create-name'),
|
|
controller: _name,
|
|
placeholder: 'Name',
|
|
)
|
|
else
|
|
TextField(
|
|
key: const Key('chat-create-name'),
|
|
controller: _name,
|
|
decoration: const InputDecoration(labelText: 'Name'),
|
|
),
|
|
const SizedBox(height: 12),
|
|
ChatCatalogFields(
|
|
loading: _loading,
|
|
error: _catalogError,
|
|
directories: _directories,
|
|
backends: _backends,
|
|
directory: _directory,
|
|
backend: _backend,
|
|
onDirectory: (value) => setState(() => _directory = value),
|
|
onBackend: (value) => setState(() => _backend = value),
|
|
onRetry: _loadCatalog,
|
|
),
|
|
if (_catalogReady) ...[
|
|
const SizedBox(height: 8),
|
|
const Text('New chats use the server default model.'),
|
|
],
|
|
const SizedBox(height: 12),
|
|
if (cupertino)
|
|
CupertinoTextField(
|
|
key: const Key('chat-create-instructions'),
|
|
controller: _instructions,
|
|
placeholder: 'Instructions',
|
|
minLines: 1,
|
|
maxLines: 3,
|
|
)
|
|
else
|
|
TextField(
|
|
key: const Key('chat-create-instructions'),
|
|
controller: _instructions,
|
|
decoration: const InputDecoration(labelText: 'Instructions'),
|
|
minLines: 1,
|
|
maxLines: 3,
|
|
),
|
|
if (_formError != null) ...[
|
|
const SizedBox(height: 8),
|
|
Text(_formError!),
|
|
],
|
|
],
|
|
),
|
|
);
|
|
|
|
if (cupertino) {
|
|
return CupertinoAlertDialog(
|
|
title: const Text('New chat'),
|
|
content: content,
|
|
actions: [
|
|
CupertinoDialogAction(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: const Text('Cancel'),
|
|
),
|
|
CupertinoDialogAction(
|
|
isDefaultAction: true,
|
|
onPressed: _catalogReady ? _submit : null,
|
|
child: const Text('Create'),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
return AlertDialog(
|
|
title: const Text('New chat'),
|
|
content: content,
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.pop(context),
|
|
child: const Text('Cancel'),
|
|
),
|
|
FilledButton(
|
|
onPressed: _catalogReady ? _submit : null,
|
|
child: const Text('Create'),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|