160 lines
5.0 KiB
Dart
160 lines
5.0 KiB
Dart
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../../../core/api_gateway.dart';
|
|
import '../../domain/chat_sessions.dart';
|
|
import 'chat_adaptive.dart';
|
|
|
|
/// Renames one session through the shared server field. Save is disabled while
|
|
/// the trimmed name is blank, matching the web dialog. A refusal keeps the
|
|
/// dialog open with the server's own words.
|
|
Future<String?> showRenameChatDialog(
|
|
BuildContext context,
|
|
JsonMap session, {
|
|
required Future<void> Function(String title) onSubmit,
|
|
}) {
|
|
final dialog = _RenameChatDialog(
|
|
initialTitle: chatSessionTitle(session),
|
|
onSubmit: onSubmit,
|
|
);
|
|
return chatControlsStyleOf(context) == ChatControlsStyle.cupertino
|
|
? showCupertinoDialog<String>(context: context, builder: (_) => dialog)
|
|
: showDialog<String>(context: context, builder: (_) => dialog);
|
|
}
|
|
|
|
class _RenameChatDialog extends StatefulWidget {
|
|
const _RenameChatDialog({required this.initialTitle, required this.onSubmit});
|
|
|
|
final String initialTitle;
|
|
final Future<void> Function(String title) onSubmit;
|
|
|
|
@override
|
|
State<_RenameChatDialog> createState() => _RenameChatDialogState();
|
|
}
|
|
|
|
class _RenameChatDialogState extends State<_RenameChatDialog> {
|
|
late final TextEditingController _title =
|
|
TextEditingController(text: widget.initialTitle)
|
|
..selection = TextSelection(
|
|
baseOffset: 0,
|
|
extentOffset: widget.initialTitle.length,
|
|
);
|
|
String? _error;
|
|
bool _saving = false;
|
|
|
|
@override
|
|
void dispose() {
|
|
_title.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
Future<void> _save() async {
|
|
if (_saving) return;
|
|
final title = _title.text.trim();
|
|
if (title.isEmpty) return;
|
|
setState(() {
|
|
_saving = true;
|
|
_error = null;
|
|
});
|
|
try {
|
|
await widget.onSubmit(title);
|
|
if (!mounted) return;
|
|
Navigator.pop(context, title);
|
|
} catch (error) {
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_saving = false;
|
|
_error = '$error';
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return ValueListenableBuilder<TextEditingValue>(
|
|
valueListenable: _title,
|
|
builder: (context, value, _) {
|
|
final canSave = !_saving && value.text.trim().isNotEmpty;
|
|
final error = _error;
|
|
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
|
|
return CupertinoAlertDialog(
|
|
title: const Text('Rename chat'),
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
children: [
|
|
CupertinoTextField(
|
|
key: const Key('chat-rename-field'),
|
|
controller: _title,
|
|
autofocus: true,
|
|
placeholder: 'Name',
|
|
),
|
|
const SizedBox(height: 8),
|
|
const Text('This name is shared with every device.'),
|
|
if (error != null) ...[
|
|
const SizedBox(height: 8),
|
|
Text(
|
|
error,
|
|
style: TextStyle(
|
|
color: CupertinoColors.systemRed.resolveFrom(context),
|
|
),
|
|
),
|
|
],
|
|
],
|
|
),
|
|
actions: [
|
|
CupertinoDialogAction(
|
|
onPressed: _saving ? null : () => Navigator.pop(context),
|
|
child: const Text('Cancel'),
|
|
),
|
|
CupertinoDialogAction(
|
|
isDefaultAction: true,
|
|
onPressed: canSave ? _save : null,
|
|
child: const Text('Save'),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
return AlertDialog(
|
|
// A long server refusal plus a 2x helper text can exceed the dialog
|
|
// height at 320px; scroll the content instead of clipping it.
|
|
scrollable: true,
|
|
title: const Text('Rename chat'),
|
|
content: Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
TextField(
|
|
key: const Key('chat-rename-field'),
|
|
controller: _title,
|
|
autofocus: true,
|
|
decoration: const InputDecoration(
|
|
labelText: 'Name',
|
|
helperText: 'This name is shared with every device.',
|
|
helperMaxLines: 2,
|
|
),
|
|
),
|
|
if (error != null) ...[
|
|
const SizedBox(height: 8),
|
|
Text(
|
|
error,
|
|
style: TextStyle(color: Theme.of(context).colorScheme.error),
|
|
),
|
|
],
|
|
],
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: _saving ? null : () => Navigator.pop(context),
|
|
child: const Text('Cancel'),
|
|
),
|
|
FilledButton(
|
|
onPressed: canSave ? _save : null,
|
|
child: const Text('Save'),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|