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
This commit is contained in:
@@ -0,0 +1,200 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Which standard control family this screen renders.
|
||||
enum ChatControlsStyle { material, cupertino }
|
||||
|
||||
/// Cupertino under a Cupertino app or an iOS/macOS Material theme, Material
|
||||
/// everywhere else. The shell decides the app class, so a bare Cupertino app
|
||||
/// never has to grow a Material ancestor just for this screen.
|
||||
ChatControlsStyle chatControlsStyleOf(BuildContext context) {
|
||||
if (context.findAncestorWidgetOfExactType<MaterialApp>() == null) {
|
||||
return ChatControlsStyle.cupertino;
|
||||
}
|
||||
final platform = Theme.of(context).platform;
|
||||
return platform == TargetPlatform.iOS || platform == TargetPlatform.macOS
|
||||
? ChatControlsStyle.cupertino
|
||||
: ChatControlsStyle.material;
|
||||
}
|
||||
|
||||
class ChatBarAction extends StatelessWidget {
|
||||
const ChatBarAction({
|
||||
super.key,
|
||||
required this.label,
|
||||
required this.onPressed,
|
||||
required this.materialIcon,
|
||||
required this.cupertinoIcon,
|
||||
});
|
||||
|
||||
final String label;
|
||||
final VoidCallback onPressed;
|
||||
final IconData materialIcon;
|
||||
final IconData cupertinoIcon;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
|
||||
return Semantics(
|
||||
label: label,
|
||||
button: true,
|
||||
child: CupertinoButton(
|
||||
padding: EdgeInsets.zero,
|
||||
onPressed: onPressed,
|
||||
child: Icon(cupertinoIcon),
|
||||
),
|
||||
);
|
||||
}
|
||||
return IconButton(
|
||||
tooltip: label,
|
||||
onPressed: onPressed,
|
||||
icon: Icon(materialIcon),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ChatSearchField extends StatelessWidget {
|
||||
const ChatSearchField({
|
||||
super.key,
|
||||
required this.controller,
|
||||
required this.onChanged,
|
||||
});
|
||||
|
||||
final TextEditingController controller;
|
||||
final ValueChanged<String> onChanged;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: CupertinoSearchTextField(
|
||||
controller: controller,
|
||||
placeholder: 'Search chats',
|
||||
onChanged: onChanged,
|
||||
),
|
||||
);
|
||||
}
|
||||
return Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: TextField(
|
||||
controller: controller,
|
||||
onChanged: onChanged,
|
||||
decoration: const InputDecoration(
|
||||
prefixIcon: Icon(Icons.search),
|
||||
labelText: 'Search chats',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// A grouped Cupertino card for a message that may wrap onto several lines.
|
||||
///
|
||||
/// [CupertinoListTile] forces its title to one ellipsised line, which cut long
|
||||
/// no-match queries and server reasons. This layout keeps the native card but
|
||||
/// lets the message wrap, with its action on a separate button below.
|
||||
class ChatCupertinoMessage extends StatelessWidget {
|
||||
const ChatCupertinoMessage({
|
||||
super.key,
|
||||
required this.message,
|
||||
this.icon,
|
||||
this.actionLabel,
|
||||
this.onAction,
|
||||
});
|
||||
|
||||
final String message;
|
||||
final IconData? icon;
|
||||
final String? actionLabel;
|
||||
final VoidCallback? onAction;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Semantics(
|
||||
container: true,
|
||||
explicitChildNodes: true,
|
||||
child: CupertinoListSection.insetGrouped(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Row(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
if (icon != null) ...[
|
||||
Icon(
|
||||
icon,
|
||||
color: CupertinoColors.secondaryLabel.resolveFrom(context),
|
||||
),
|
||||
const SizedBox(width: 12),
|
||||
],
|
||||
Expanded(
|
||||
// Cupertino widgets normally supply their own text style.
|
||||
// This card is also mounted under a bare MaterialApp (the
|
||||
// iOS/macOS theme path), where Text would otherwise inherit
|
||||
// MaterialApp's "missing Material" fallback style.
|
||||
child: DefaultTextStyle.merge(
|
||||
style: CupertinoTheme.of(context).textTheme.textStyle,
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(message),
|
||||
if (actionLabel != null && onAction != null)
|
||||
Align(
|
||||
alignment: AlignmentDirectional.centerStart,
|
||||
child: CupertinoButton(
|
||||
padding: const EdgeInsets.only(top: 8),
|
||||
onPressed: onAction,
|
||||
child: Text(actionLabel!),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ChatErrorNotice extends StatelessWidget {
|
||||
const ChatErrorNotice({
|
||||
super.key,
|
||||
required this.message,
|
||||
required this.onRetry,
|
||||
});
|
||||
|
||||
final String message;
|
||||
final VoidCallback onRetry;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
|
||||
return ChatCupertinoMessage(
|
||||
message: message,
|
||||
icon: CupertinoIcons.exclamationmark_circle,
|
||||
actionLabel: 'Retry',
|
||||
onAction: onRetry,
|
||||
);
|
||||
}
|
||||
return MaterialBanner(
|
||||
content: Text(message),
|
||||
actions: [TextButton(onPressed: onRetry, child: const Text('Retry'))],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class ChatLoadingIndicator extends StatelessWidget {
|
||||
const ChatLoadingIndicator({super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Center(
|
||||
child: chatControlsStyleOf(context) == ChatControlsStyle.cupertino
|
||||
? const CupertinoActivityIndicator()
|
||||
: const CircularProgressIndicator(),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'chat_adaptive.dart';
|
||||
|
||||
/// The create dialog's server catalog area: loading, error with retry, the
|
||||
/// empty-catalog message, or the two server-backed selectors.
|
||||
class ChatCatalogFields extends StatelessWidget {
|
||||
const ChatCatalogFields({
|
||||
super.key,
|
||||
required this.loading,
|
||||
required this.error,
|
||||
required this.directories,
|
||||
required this.backends,
|
||||
required this.directory,
|
||||
required this.backend,
|
||||
required this.onDirectory,
|
||||
required this.onBackend,
|
||||
required this.onRetry,
|
||||
});
|
||||
|
||||
final bool loading;
|
||||
final String? error;
|
||||
final List<String> directories;
|
||||
final List<String> backends;
|
||||
final String? directory;
|
||||
final String? backend;
|
||||
final ValueChanged<String> onDirectory;
|
||||
final ValueChanged<String> onBackend;
|
||||
final VoidCallback onRetry;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final cupertino =
|
||||
chatControlsStyleOf(context) == ChatControlsStyle.cupertino;
|
||||
if (loading) return const Text('Loading directories…');
|
||||
if (error != null) {
|
||||
return _message(cupertino, 'Unable to load chat options: $error');
|
||||
}
|
||||
if (directories.isEmpty || backends.isEmpty) {
|
||||
return _message(
|
||||
cupertino,
|
||||
'The server has no chat directories or backends available.',
|
||||
);
|
||||
}
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
if (cupertino)
|
||||
CupertinoPickerField(
|
||||
label: 'Server directory',
|
||||
value: directory,
|
||||
values: directories,
|
||||
onSelected: onDirectory,
|
||||
)
|
||||
else
|
||||
MaterialPickerField(
|
||||
label: 'Server directory',
|
||||
value: directory,
|
||||
values: directories,
|
||||
onSelected: onDirectory,
|
||||
),
|
||||
if (cupertino)
|
||||
CupertinoPickerField(
|
||||
label: 'Backend',
|
||||
value: backend,
|
||||
values: backends,
|
||||
onSelected: onBackend,
|
||||
)
|
||||
else
|
||||
MaterialPickerField(
|
||||
label: 'Backend',
|
||||
value: backend,
|
||||
values: backends,
|
||||
onSelected: onBackend,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _message(bool cupertino, String text) {
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
Text(text),
|
||||
Align(
|
||||
alignment: AlignmentDirectional.centerStart,
|
||||
child: cupertino
|
||||
? CupertinoButton(
|
||||
padding: EdgeInsets.zero,
|
||||
onPressed: onRetry,
|
||||
child: const Text('Retry'),
|
||||
)
|
||||
: TextButton(onPressed: onRetry, child: const Text('Retry')),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class MaterialPickerField extends StatelessWidget {
|
||||
const MaterialPickerField({
|
||||
super.key,
|
||||
required this.label,
|
||||
required this.value,
|
||||
required this.values,
|
||||
required this.onSelected,
|
||||
});
|
||||
|
||||
final String label;
|
||||
final String? value;
|
||||
final List<String> values;
|
||||
final ValueChanged<String> onSelected;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return DropdownButtonFormField<String>(
|
||||
key: Key('chat-create-${label.toLowerCase().replaceAll(' ', '-')}'),
|
||||
initialValue: value,
|
||||
decoration: InputDecoration(labelText: label),
|
||||
items: [
|
||||
for (final item in values)
|
||||
DropdownMenuItem(value: item, child: Text(item)),
|
||||
],
|
||||
onChanged: (item) {
|
||||
if (item != null) onSelected(item);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class CupertinoPickerField extends StatelessWidget {
|
||||
const CupertinoPickerField({
|
||||
super.key,
|
||||
required this.label,
|
||||
required this.value,
|
||||
required this.values,
|
||||
required this.onSelected,
|
||||
});
|
||||
|
||||
final String label;
|
||||
final String? value;
|
||||
final List<String> values;
|
||||
final ValueChanged<String> onSelected;
|
||||
|
||||
Future<void> _choose(BuildContext context) async {
|
||||
final choice = await showCupertinoModalPopup<String>(
|
||||
context: context,
|
||||
builder: (sheetContext) => CupertinoActionSheet(
|
||||
title: Text(label),
|
||||
actions: [
|
||||
for (final item in values)
|
||||
CupertinoActionSheetAction(
|
||||
onPressed: () => Navigator.pop(sheetContext, item),
|
||||
child: Text(item),
|
||||
),
|
||||
],
|
||||
cancelButton: CupertinoActionSheetAction(
|
||||
onPressed: () => Navigator.pop(sheetContext),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
),
|
||||
);
|
||||
if (choice != null) onSelected(choice);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Semantics(
|
||||
label: '$label: ${value ?? ''}',
|
||||
button: true,
|
||||
child: CupertinoButton(
|
||||
key: Key('chat-create-${label.toLowerCase().replaceAll(' ', '-')}'),
|
||||
padding: const EdgeInsets.symmetric(vertical: 4),
|
||||
onPressed: () => _choose(context),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(child: Text('$label: ${value ?? ''}')),
|
||||
const Icon(CupertinoIcons.chevron_down),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../../core/api_gateway.dart';
|
||||
import '../../domain/chat_sessions.dart';
|
||||
import 'chat_adaptive.dart';
|
||||
import 'session_list_cupertino.dart';
|
||||
import 'session_list_material.dart';
|
||||
import 'session_row_style.dart';
|
||||
|
||||
typedef ChatSessionSection = ({String title, List<JsonMap> sessions});
|
||||
|
||||
/// Groups the visible sessions the way the server reports them.
|
||||
class ChatSessionsBody extends StatelessWidget {
|
||||
const ChatSessionsBody({
|
||||
super.key,
|
||||
required this.sessions,
|
||||
required this.query,
|
||||
required this.loading,
|
||||
required this.error,
|
||||
required this.onOpen,
|
||||
required this.onRename,
|
||||
required this.onClose,
|
||||
required this.onReopen,
|
||||
required this.onStar,
|
||||
required this.onClearSearch,
|
||||
});
|
||||
|
||||
final List<JsonMap> sessions;
|
||||
final String query;
|
||||
final bool loading;
|
||||
final String? error;
|
||||
final ValueChanged<JsonMap> onOpen;
|
||||
final ValueChanged<JsonMap> onRename;
|
||||
final ValueChanged<JsonMap> onClose;
|
||||
final ValueChanged<JsonMap> onReopen;
|
||||
final ValueChanged<JsonMap> onStar;
|
||||
final VoidCallback onClearSearch;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final visible = filterChatSessions(sessions, query);
|
||||
final sections = <ChatSessionSection>[
|
||||
if (visible.any((s) => s['starred'] == true && s['closed_at'] == null))
|
||||
(
|
||||
title: 'Starred',
|
||||
sessions: visible
|
||||
.where((s) => s['starred'] == true && s['closed_at'] == null)
|
||||
.toList(),
|
||||
),
|
||||
if (visible.any((s) => s['starred'] != true && s['closed_at'] == null))
|
||||
(
|
||||
title: 'Chats',
|
||||
sessions: visible
|
||||
.where((s) => s['starred'] != true && s['closed_at'] == null)
|
||||
.toList(),
|
||||
),
|
||||
if (visible.any((s) => s['closed_at'] != null))
|
||||
(
|
||||
title: 'Closed',
|
||||
sessions: visible.where((s) => s['closed_at'] != null).toList(),
|
||||
),
|
||||
];
|
||||
final emptyMessage = sessions.isEmpty
|
||||
? error == null
|
||||
? 'No chat sessions on this server yet.'
|
||||
: null
|
||||
: 'No chats match "${query.trim()}".';
|
||||
|
||||
if (loading && sessions.isEmpty) return const ChatLoadingIndicator();
|
||||
final clearSearch = query.trim().isEmpty ? null : onClearSearch;
|
||||
final duplicateTitles = duplicateChatTitles(visible);
|
||||
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
|
||||
return CupertinoSessionList(
|
||||
sections: sections,
|
||||
emptyMessage: visible.isEmpty ? emptyMessage : null,
|
||||
duplicateTitles: duplicateTitles,
|
||||
onOpen: onOpen,
|
||||
onRename: onRename,
|
||||
onClose: onClose,
|
||||
onReopen: onReopen,
|
||||
onStar: onStar,
|
||||
onClearSearch: clearSearch,
|
||||
);
|
||||
}
|
||||
return MaterialSessionList(
|
||||
sections: sections,
|
||||
emptyMessage: visible.isEmpty ? emptyMessage : null,
|
||||
duplicateTitles: duplicateTitles,
|
||||
onOpen: onOpen,
|
||||
onRename: onRename,
|
||||
onClose: onClose,
|
||||
onReopen: onReopen,
|
||||
onStar: onStar,
|
||||
onClearSearch: clearSearch,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,14 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../../core/api_gateway.dart';
|
||||
import '../../data/chat_repository.dart';
|
||||
import '../../domain/chat_sessions.dart';
|
||||
import 'chat_adaptive.dart';
|
||||
import 'chat_sessions_body.dart';
|
||||
import 'chat_sessions_controller.dart';
|
||||
import 'create_chat_dialog.dart';
|
||||
import 'rename_chat_dialog.dart';
|
||||
import 'session_actions.dart';
|
||||
|
||||
/// Browses server-owned sessions. Opening a row is read-only at this layer.
|
||||
class ChatSessionsScreen extends StatefulWidget {
|
||||
@@ -19,388 +25,152 @@ class ChatSessionsScreen extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _ChatSessionsScreenState extends State<ChatSessionsScreen> {
|
||||
late ChatRepository _repository;
|
||||
late final ChatSessionsController _controller = ChatSessionsController(
|
||||
ChatRepository(widget.gateway),
|
||||
)..refresh();
|
||||
final _search = TextEditingController();
|
||||
List<JsonMap> _sessions = [];
|
||||
String? _error;
|
||||
bool _loading = true;
|
||||
int _generation = 0;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_repository = ChatRepository(widget.gateway);
|
||||
_refresh();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant ChatSessionsScreen oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.gateway != widget.gateway) {
|
||||
_repository = ChatRepository(widget.gateway);
|
||||
_refresh();
|
||||
_controller.replaceRepository(ChatRepository(widget.gateway));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_generation++;
|
||||
_controller.dispose();
|
||||
_search.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _refresh() async {
|
||||
final generation = ++_generation;
|
||||
setState(() {
|
||||
_loading = true;
|
||||
_error = null;
|
||||
});
|
||||
try {
|
||||
final sessions = await _repository.sessions();
|
||||
if (!mounted || generation != _generation) return;
|
||||
setState(() {
|
||||
_sessions = sessions;
|
||||
_loading = false;
|
||||
});
|
||||
} catch (error) {
|
||||
if (!mounted || generation != _generation) return;
|
||||
setState(() {
|
||||
_error = '$error';
|
||||
_loading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _mutate(
|
||||
Future<JsonMap> Function(String) action,
|
||||
String id,
|
||||
) async {
|
||||
try {
|
||||
await action(id);
|
||||
await _refresh();
|
||||
} catch (error) {
|
||||
if (mounted) setState(() => _error = '$error');
|
||||
}
|
||||
}
|
||||
String _sessionId(JsonMap session) => '${session['id'] ?? ''}';
|
||||
|
||||
Future<void> _rename(JsonMap session) async {
|
||||
final controller = TextEditingController(text: chatSessionTitle(session));
|
||||
final title = await showDialog<String>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Rename chat'),
|
||||
content: TextField(
|
||||
controller: controller,
|
||||
autofocus: true,
|
||||
decoration: const InputDecoration(labelText: 'Chat name'),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () => Navigator.pop(context, controller.text.trim()),
|
||||
child: const Text('Save'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
controller.dispose();
|
||||
if (title == null || title.trim().isEmpty) return;
|
||||
try {
|
||||
await _repository.rename('${session['id']}', title);
|
||||
await _refresh();
|
||||
} catch (error) {
|
||||
if (mounted) setState(() => _error = '$error');
|
||||
}
|
||||
final title = await showRenameChatDialog(context, session);
|
||||
if (title == null || !mounted) return;
|
||||
await _controller.rename(_sessionId(session), title);
|
||||
}
|
||||
|
||||
Future<void> _close(JsonMap session) async {
|
||||
if (!await confirmCloseChat(context, session)) return;
|
||||
if (!mounted) return;
|
||||
await _controller.close(_sessionId(session));
|
||||
}
|
||||
|
||||
Future<void> _newChat() async {
|
||||
List<String> directories;
|
||||
List<String> backends;
|
||||
try {
|
||||
final catalog = await Future.wait([
|
||||
_repository.directories(),
|
||||
_repository.backends(),
|
||||
]);
|
||||
directories = catalog[0];
|
||||
backends = catalog[1];
|
||||
} catch (error) {
|
||||
if (mounted) {
|
||||
setState(() => _error = 'Unable to load chat options: $error');
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!mounted) return;
|
||||
if (directories.isEmpty || backends.isEmpty) {
|
||||
setState(
|
||||
() => _error =
|
||||
'The server has no chat directories or backends available.',
|
||||
);
|
||||
return;
|
||||
}
|
||||
final dirs = ValueNotifier<String>(directories.first);
|
||||
final backend = ValueNotifier<String>(backends.first);
|
||||
final title = TextEditingController(text: 'New chat');
|
||||
final instructions = TextEditingController();
|
||||
final result =
|
||||
await showDialog<
|
||||
({
|
||||
String title,
|
||||
String directory,
|
||||
String backend,
|
||||
String instructions,
|
||||
})
|
||||
>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('New chat'),
|
||||
content: SingleChildScrollView(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
TextField(
|
||||
controller: title,
|
||||
decoration: const InputDecoration(labelText: 'Name'),
|
||||
),
|
||||
ValueListenableBuilder<String>(
|
||||
valueListenable: dirs,
|
||||
builder: (context, value, _) =>
|
||||
DropdownButtonFormField<String>(
|
||||
initialValue: value,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Server directory',
|
||||
),
|
||||
items: [
|
||||
for (final item in directories)
|
||||
DropdownMenuItem(value: item, child: Text(item)),
|
||||
],
|
||||
onChanged: (item) {
|
||||
if (item != null) dirs.value = item;
|
||||
},
|
||||
),
|
||||
),
|
||||
ValueListenableBuilder<String>(
|
||||
valueListenable: backend,
|
||||
builder: (context, value, _) =>
|
||||
DropdownButtonFormField<String>(
|
||||
initialValue: value,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Backend',
|
||||
),
|
||||
items: [
|
||||
for (final item in backends)
|
||||
DropdownMenuItem(value: item, child: Text(item)),
|
||||
],
|
||||
onChanged: (item) {
|
||||
if (item != null) backend.value = item;
|
||||
},
|
||||
),
|
||||
),
|
||||
const Text('New chats use the server default model.'),
|
||||
TextField(
|
||||
controller: instructions,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Instructions',
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () => Navigator.pop(context, (
|
||||
title: title.text.trim(),
|
||||
directory: dirs.value,
|
||||
backend: backend.value,
|
||||
instructions: instructions.text,
|
||||
)),
|
||||
child: const Text('Create'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
title.dispose();
|
||||
dirs.dispose();
|
||||
backend.dispose();
|
||||
instructions.dispose();
|
||||
if (result == null) return;
|
||||
if (result.title.isEmpty || result.directory.isEmpty) {
|
||||
setState(() => _error = 'Enter a name and server directory.');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
final reply = await _repository.create(
|
||||
title: result.title,
|
||||
directory: result.directory,
|
||||
backend: result.backend,
|
||||
operationId: 'native-chat-${DateTime.now().microsecondsSinceEpoch}',
|
||||
instructions: result.instructions,
|
||||
);
|
||||
final session = reply['chat_session'];
|
||||
if (session is Map) widget.onOpen(Map<String, dynamic>.from(session));
|
||||
await _refresh();
|
||||
} catch (error) {
|
||||
if (mounted) setState(() => _error = '$error');
|
||||
}
|
||||
final operationId = 'native-chat-${DateTime.now().microsecondsSinceEpoch}';
|
||||
final draft = await showChatCreateDialog(
|
||||
context,
|
||||
repository: _controller.repository,
|
||||
);
|
||||
if (draft == null || !mounted) return;
|
||||
final session = await _controller.create(
|
||||
title: draft.title,
|
||||
directory: draft.directory,
|
||||
backend: draft.backend,
|
||||
instructions: draft.instructions,
|
||||
operationId: operationId,
|
||||
);
|
||||
if (session != null && mounted) widget.onOpen(session);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final visible = filterChatSessions(_sessions, _search.text);
|
||||
final starred = visible
|
||||
.where((s) => s['starred'] == true && s['closed_at'] == null)
|
||||
.toList();
|
||||
final open = visible
|
||||
.where((s) => s['starred'] != true && s['closed_at'] == null)
|
||||
.toList();
|
||||
final closed = visible.where((s) => s['closed_at'] != null).toList();
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Chats'),
|
||||
actions: [
|
||||
IconButton(
|
||||
onPressed: _newChat,
|
||||
tooltip: 'New chat',
|
||||
icon: const Icon(Icons.add),
|
||||
),
|
||||
IconButton(
|
||||
onPressed: _refresh,
|
||||
tooltip: 'Refresh',
|
||||
icon: const Icon(Icons.refresh),
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: TextField(
|
||||
return ListenableBuilder(
|
||||
listenable: _controller,
|
||||
builder: (context, _) {
|
||||
final body = Column(
|
||||
children: [
|
||||
ChatSearchField(
|
||||
controller: _search,
|
||||
onChanged: (_) => setState(() {}),
|
||||
decoration: const InputDecoration(
|
||||
prefixIcon: Icon(Icons.search),
|
||||
labelText: 'Search chats',
|
||||
),
|
||||
if (_controller.error != null)
|
||||
ChatErrorNotice(
|
||||
message: _controller.error!,
|
||||
onRetry: _controller.refresh,
|
||||
),
|
||||
Expanded(
|
||||
child: ChatSessionsBody(
|
||||
sessions: _controller.sessions,
|
||||
query: _search.text,
|
||||
loading: _controller.loading,
|
||||
error: _controller.error,
|
||||
onOpen: widget.onOpen,
|
||||
onRename: _rename,
|
||||
onClose: _close,
|
||||
onReopen: (session) => _controller.reopen(_sessionId(session)),
|
||||
onStar: (session) => _controller.star(
|
||||
_sessionId(session),
|
||||
session['starred'] != true,
|
||||
),
|
||||
onClearSearch: () {
|
||||
_search.clear();
|
||||
setState(() {});
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
if (_error != null)
|
||||
MaterialBanner(
|
||||
content: Text(_error!),
|
||||
actions: [
|
||||
TextButton(onPressed: _refresh, child: const Text('Retry')),
|
||||
],
|
||||
),
|
||||
Expanded(
|
||||
child: _loading && _sessions.isEmpty
|
||||
? const Center(child: CircularProgressIndicator())
|
||||
: ListView(
|
||||
children: [
|
||||
if (starred.isNotEmpty) _section('Starred', starred),
|
||||
if (open.isNotEmpty) _section('Chats', open),
|
||||
if (closed.isNotEmpty) _section('Closed', closed),
|
||||
if (visible.isEmpty && !_loading)
|
||||
ListTile(
|
||||
title: Text(
|
||||
_sessions.isEmpty
|
||||
? 'No chats yet.'
|
||||
: 'No chats match "${_search.text.trim()}".',
|
||||
),
|
||||
trailing: _search.text.isEmpty
|
||||
? null
|
||||
: TextButton(
|
||||
onPressed: () {
|
||||
_search.clear();
|
||||
setState(() {});
|
||||
},
|
||||
child: const Text('Clear search'),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
);
|
||||
|
||||
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
|
||||
return CupertinoPageScaffold(
|
||||
navigationBar: CupertinoNavigationBar(
|
||||
middle: const Text('Chats'),
|
||||
trailing: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
ChatBarAction(
|
||||
key: const Key('chat-new-chat'),
|
||||
label: 'New chat',
|
||||
onPressed: _newChat,
|
||||
materialIcon: Icons.add,
|
||||
cupertinoIcon: CupertinoIcons.add,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
floatingActionButton: FloatingActionButton.extended(
|
||||
onPressed: _newChat,
|
||||
icon: const Icon(Icons.add),
|
||||
label: const Text('New chat'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _section(String title, List<JsonMap> sessions) => ExpansionTile(
|
||||
title: Text(title),
|
||||
initiallyExpanded: true,
|
||||
children: [
|
||||
for (final group in groupChatSessions(sessions)) ...[
|
||||
if (groupChatSessions(sessions).length > 1)
|
||||
ListTile(
|
||||
dense: true,
|
||||
leading: const Icon(Icons.folder_outlined),
|
||||
title: Text(
|
||||
group.directory.isEmpty ? 'No directory' : group.directory,
|
||||
ChatBarAction(
|
||||
key: const Key('chat-refresh'),
|
||||
label: 'Refresh',
|
||||
onPressed: _controller.refresh,
|
||||
materialIcon: Icons.refresh,
|
||||
cupertinoIcon: CupertinoIcons.refresh,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
child: SafeArea(child: body),
|
||||
);
|
||||
}
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Chats'),
|
||||
actions: [
|
||||
ChatBarAction(
|
||||
key: const Key('chat-new-chat'),
|
||||
label: 'New chat',
|
||||
onPressed: _newChat,
|
||||
materialIcon: Icons.add,
|
||||
cupertinoIcon: CupertinoIcons.add,
|
||||
),
|
||||
ChatBarAction(
|
||||
key: const Key('chat-refresh'),
|
||||
label: 'Refresh',
|
||||
onPressed: _controller.refresh,
|
||||
materialIcon: Icons.refresh,
|
||||
cupertinoIcon: CupertinoIcons.refresh,
|
||||
),
|
||||
],
|
||||
),
|
||||
for (final session in group.sessions) _row(session),
|
||||
],
|
||||
],
|
||||
);
|
||||
|
||||
Widget _row(JsonMap session) {
|
||||
final id = '${session['id'] ?? ''}';
|
||||
final isClosed = session['closed_at'] != null;
|
||||
final starred = session['starred'] == true;
|
||||
return ListTile(
|
||||
leading: Icon(
|
||||
session['running'] == true
|
||||
? Icons.sync
|
||||
: starred
|
||||
? Icons.star
|
||||
: Icons.chat_bubble_outline,
|
||||
),
|
||||
title: Text(chatSessionTitle(session)),
|
||||
subtitle: Text(
|
||||
'${session['directory'] ?? ''}${session['running'] == true
|
||||
? ' · Streaming'
|
||||
: session['unseen'] == true
|
||||
? ' · New'
|
||||
: ''}',
|
||||
),
|
||||
onTap: () => widget.onOpen(session),
|
||||
trailing: PopupMenuButton<String>(
|
||||
tooltip: 'Chat actions',
|
||||
onSelected: (value) {
|
||||
if (value == 'rename') {
|
||||
_rename(session);
|
||||
}
|
||||
if (value == 'star') {
|
||||
_mutate((id) => _repository.star(id, !starred), id);
|
||||
}
|
||||
if (value == 'close') {
|
||||
_mutate(_repository.close, id);
|
||||
}
|
||||
if (value == 'reopen') {
|
||||
_mutate(_repository.reopen, id);
|
||||
}
|
||||
},
|
||||
itemBuilder: (_) => [
|
||||
const PopupMenuItem(value: 'rename', child: Text('Rename')),
|
||||
PopupMenuItem(
|
||||
value: 'star',
|
||||
child: Text(starred ? 'Remove from Starred' : 'Star'),
|
||||
body: body,
|
||||
floatingActionButton: FloatingActionButton.extended(
|
||||
key: const Key('chat-new-chat-fab'),
|
||||
onPressed: _newChat,
|
||||
icon: const Icon(Icons.add),
|
||||
label: const Text('New chat'),
|
||||
),
|
||||
if (isClosed)
|
||||
const PopupMenuItem(value: 'reopen', child: Text('Reopen'))
|
||||
else
|
||||
const PopupMenuItem(value: 'close', child: Text('Close')),
|
||||
],
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,222 @@
|
||||
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'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
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. Save is disabled while the trimmed name is blank,
|
||||
/// matching the web dialog.
|
||||
Future<String?> showRenameChatDialog(BuildContext context, JsonMap session) {
|
||||
final dialog = _RenameChatDialog(initialTitle: chatSessionTitle(session));
|
||||
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});
|
||||
|
||||
final String initialTitle;
|
||||
|
||||
@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,
|
||||
);
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_title.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _save() {
|
||||
final title = _title.text.trim();
|
||||
if (title.isEmpty) return;
|
||||
Navigator.pop(context, title);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ValueListenableBuilder<TextEditingValue>(
|
||||
valueListenable: _title,
|
||||
builder: (context, value, _) {
|
||||
final canSave = value.text.trim().isNotEmpty;
|
||||
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.'),
|
||||
],
|
||||
),
|
||||
actions: [
|
||||
CupertinoDialogAction(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
CupertinoDialogAction(
|
||||
isDefaultAction: true,
|
||||
onPressed: canSave ? _save : null,
|
||||
child: const Text('Save'),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
return AlertDialog(
|
||||
title: const Text('Rename chat'),
|
||||
content: 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,
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: canSave ? _save : null,
|
||||
child: const Text('Save'),
|
||||
),
|
||||
],
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../../core/api_gateway.dart';
|
||||
import '../../domain/chat_sessions.dart';
|
||||
import 'chat_adaptive.dart';
|
||||
import 'session_row_style.dart';
|
||||
|
||||
enum SessionAction { rename, close, reopen }
|
||||
|
||||
/// The close confirmation's body, matching the web client's explanation.
|
||||
String closeChatConfirmation(String title) =>
|
||||
'Closes "$title" and marks it closed on every device. '
|
||||
'A turn that is running is stopped first. '
|
||||
'Nothing is deleted — the chat and its history are kept, '
|
||||
'and you can reopen it from the list.';
|
||||
|
||||
class SessionStarButton extends StatelessWidget {
|
||||
const SessionStarButton({
|
||||
super.key,
|
||||
required this.session,
|
||||
required this.onPressed,
|
||||
});
|
||||
|
||||
final JsonMap session;
|
||||
final VoidCallback onPressed;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final starred = session['starred'] == true;
|
||||
final label = starred ? 'Unstar chat' : 'Star chat';
|
||||
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
|
||||
return Semantics(
|
||||
label: label,
|
||||
button: true,
|
||||
child: CupertinoButton(
|
||||
padding: EdgeInsets.zero,
|
||||
onPressed: onPressed,
|
||||
child: Icon(starred ? CupertinoIcons.star_fill : CupertinoIcons.star),
|
||||
),
|
||||
);
|
||||
}
|
||||
return IconButton(
|
||||
tooltip: label,
|
||||
onPressed: onPressed,
|
||||
icon: Icon(starred ? Icons.star : Icons.star_border),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class SessionActionsButton extends StatelessWidget {
|
||||
const SessionActionsButton({
|
||||
super.key,
|
||||
required this.session,
|
||||
required this.onSelected,
|
||||
this.duplicateTitles = const {},
|
||||
});
|
||||
|
||||
final JsonMap session;
|
||||
final ValueChanged<SessionAction> onSelected;
|
||||
final Set<String> duplicateTitles;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final label = sessionActionsLabel(session, duplicateTitles);
|
||||
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
|
||||
return Semantics(
|
||||
label: label,
|
||||
button: true,
|
||||
child: CupertinoButton(
|
||||
padding: EdgeInsets.zero,
|
||||
onPressed: () => _showSheet(context),
|
||||
child: const Icon(CupertinoIcons.ellipsis),
|
||||
),
|
||||
);
|
||||
}
|
||||
return PopupMenuButton<SessionAction>(
|
||||
tooltip: label,
|
||||
onSelected: onSelected,
|
||||
itemBuilder: (context) => _menuItems(session),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _showSheet(BuildContext context) async {
|
||||
final action = await showCupertinoModalPopup<SessionAction>(
|
||||
context: context,
|
||||
builder: (sheetContext) => CupertinoActionSheet(
|
||||
title: Text(chatSessionTitle(session)),
|
||||
actions: [
|
||||
for (final item in _items(session))
|
||||
CupertinoActionSheetAction(
|
||||
onPressed: () => Navigator.pop(sheetContext, item.action),
|
||||
child: Text(item.label),
|
||||
),
|
||||
],
|
||||
cancelButton: CupertinoActionSheetAction(
|
||||
onPressed: () => Navigator.pop(sheetContext),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
),
|
||||
);
|
||||
if (action != null && context.mounted) onSelected(action);
|
||||
}
|
||||
}
|
||||
|
||||
List<PopupMenuEntry<SessionAction>> _menuItems(JsonMap session) => [
|
||||
for (final item in _items(session))
|
||||
PopupMenuItem(value: item.action, child: Text(item.label)),
|
||||
];
|
||||
|
||||
List<({SessionAction action, String label})> _items(JsonMap session) {
|
||||
final isClosed = session['closed_at'] != null;
|
||||
return [
|
||||
(action: SessionAction.rename, label: 'Rename…'),
|
||||
isClosed
|
||||
? (action: SessionAction.reopen, label: 'Reopen')
|
||||
: (action: SessionAction.close, label: 'Close chat…'),
|
||||
];
|
||||
}
|
||||
|
||||
Future<bool> confirmCloseChat(BuildContext context, JsonMap session) async {
|
||||
final title = chatSessionTitle(session);
|
||||
final body = closeChatConfirmation(title);
|
||||
final confirmed = chatControlsStyleOf(context) == ChatControlsStyle.cupertino
|
||||
? await showCupertinoDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => CupertinoAlertDialog(
|
||||
title: const Text('Close chat?'),
|
||||
content: Text(body),
|
||||
actions: [
|
||||
CupertinoDialogAction(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
CupertinoDialogAction(
|
||||
isDestructiveAction: true,
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: const Text('Close chat'),
|
||||
),
|
||||
],
|
||||
),
|
||||
)
|
||||
: await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Close chat?'),
|
||||
content: Text(body),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.pop(context, false),
|
||||
child: const Text('Cancel'),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: () => Navigator.pop(context, true),
|
||||
child: const Text('Close chat'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
return confirmed == true;
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
|
||||
import '../../../../core/api_gateway.dart';
|
||||
import '../../domain/chat_sessions.dart';
|
||||
import 'chat_adaptive.dart';
|
||||
import 'chat_sessions_body.dart';
|
||||
import 'session_actions.dart';
|
||||
import 'session_row_style.dart';
|
||||
|
||||
class CupertinoSessionList extends StatelessWidget {
|
||||
const CupertinoSessionList({
|
||||
super.key,
|
||||
required this.sections,
|
||||
required this.emptyMessage,
|
||||
required this.duplicateTitles,
|
||||
required this.onOpen,
|
||||
required this.onRename,
|
||||
required this.onClose,
|
||||
required this.onReopen,
|
||||
required this.onStar,
|
||||
required this.onClearSearch,
|
||||
});
|
||||
|
||||
final List<ChatSessionSection> sections;
|
||||
final String? emptyMessage;
|
||||
final Set<String> duplicateTitles;
|
||||
final ValueChanged<JsonMap> onOpen;
|
||||
final ValueChanged<JsonMap> onRename;
|
||||
final ValueChanged<JsonMap> onClose;
|
||||
final ValueChanged<JsonMap> onReopen;
|
||||
final ValueChanged<JsonMap> onStar;
|
||||
final VoidCallback? onClearSearch;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListView(
|
||||
padding: const EdgeInsets.only(bottom: 24),
|
||||
children: [
|
||||
for (final section in sections) ..._section(section),
|
||||
if (emptyMessage != null)
|
||||
ChatCupertinoMessage(
|
||||
message: emptyMessage!,
|
||||
actionLabel: onClearSearch == null ? null : 'Clear search',
|
||||
onAction: onClearSearch,
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
List<CupertinoListSection> _section(ChatSessionSection section) {
|
||||
final groups = groupChatSessions(section.sessions);
|
||||
return [
|
||||
for (final group in groups)
|
||||
CupertinoListSection.insetGrouped(
|
||||
header: Text(
|
||||
groups.length > 1
|
||||
? '${section.title} · ${group.directory.isEmpty ? 'No directory' : group.directory}'
|
||||
: section.title,
|
||||
),
|
||||
children: [
|
||||
for (final session in group.sessions)
|
||||
_CupertinoSessionRow(
|
||||
session: session,
|
||||
duplicateTitles: duplicateTitles,
|
||||
onOpen: onOpen,
|
||||
onRename: onRename,
|
||||
onClose: onClose,
|
||||
onReopen: onReopen,
|
||||
onStar: onStar,
|
||||
),
|
||||
],
|
||||
),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
class _CupertinoSessionRow extends StatelessWidget {
|
||||
const _CupertinoSessionRow({
|
||||
required this.session,
|
||||
required this.duplicateTitles,
|
||||
required this.onOpen,
|
||||
required this.onRename,
|
||||
required this.onClose,
|
||||
required this.onReopen,
|
||||
required this.onStar,
|
||||
});
|
||||
|
||||
final JsonMap session;
|
||||
final Set<String> duplicateTitles;
|
||||
final ValueChanged<JsonMap> onOpen;
|
||||
final ValueChanged<JsonMap> onRename;
|
||||
final ValueChanged<JsonMap> onClose;
|
||||
final ValueChanged<JsonMap> onReopen;
|
||||
final ValueChanged<JsonMap> onStar;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final id = '${session['id'] ?? ''}';
|
||||
return CupertinoListTile(
|
||||
key: Key('chat-row-$id'),
|
||||
leading: Icon(sessionCupertinoIcon(session)),
|
||||
title: Text(chatSessionTitle(session)),
|
||||
subtitle: Text(sessionSubtitle(session)),
|
||||
onTap: () => onOpen(session),
|
||||
trailing: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
SessionStarButton(
|
||||
key: Key('chat-star-$id'),
|
||||
session: session,
|
||||
onPressed: () => onStar(session),
|
||||
),
|
||||
SessionActionsButton(
|
||||
key: Key('chat-actions-$id'),
|
||||
session: session,
|
||||
duplicateTitles: duplicateTitles,
|
||||
onSelected: _select,
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _select(SessionAction action) {
|
||||
switch (action) {
|
||||
case SessionAction.rename:
|
||||
onRename(session);
|
||||
case SessionAction.close:
|
||||
onClose(session);
|
||||
case SessionAction.reopen:
|
||||
onReopen(session);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../../core/api_gateway.dart';
|
||||
import '../../domain/chat_sessions.dart';
|
||||
import 'chat_sessions_body.dart';
|
||||
import 'session_actions.dart';
|
||||
import 'session_row_style.dart';
|
||||
|
||||
class MaterialSessionList extends StatelessWidget {
|
||||
const MaterialSessionList({
|
||||
super.key,
|
||||
required this.sections,
|
||||
required this.emptyMessage,
|
||||
required this.duplicateTitles,
|
||||
required this.onOpen,
|
||||
required this.onRename,
|
||||
required this.onClose,
|
||||
required this.onReopen,
|
||||
required this.onStar,
|
||||
required this.onClearSearch,
|
||||
});
|
||||
|
||||
final List<ChatSessionSection> sections;
|
||||
final String? emptyMessage;
|
||||
final Set<String> duplicateTitles;
|
||||
final ValueChanged<JsonMap> onOpen;
|
||||
final ValueChanged<JsonMap> onRename;
|
||||
final ValueChanged<JsonMap> onClose;
|
||||
final ValueChanged<JsonMap> onReopen;
|
||||
final ValueChanged<JsonMap> onStar;
|
||||
final VoidCallback? onClearSearch;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListView(
|
||||
children: [
|
||||
for (final section in sections) _section(section),
|
||||
if (emptyMessage != null)
|
||||
ListTile(
|
||||
title: Text(emptyMessage!),
|
||||
trailing: onClearSearch == null
|
||||
? null
|
||||
: TextButton(
|
||||
onPressed: onClearSearch,
|
||||
child: const Text('Clear search'),
|
||||
),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
Widget _section(ChatSessionSection section) {
|
||||
final groups = groupChatSessions(section.sessions);
|
||||
return ExpansionTile(
|
||||
title: Text(section.title),
|
||||
initiallyExpanded: true,
|
||||
children: [
|
||||
for (final group in groups) ...[
|
||||
if (groups.length > 1)
|
||||
ListTile(
|
||||
dense: true,
|
||||
leading: const Icon(Icons.folder_outlined),
|
||||
title: Text(
|
||||
group.directory.isEmpty ? 'No directory' : group.directory,
|
||||
),
|
||||
),
|
||||
for (final session in group.sessions)
|
||||
_MaterialSessionRow(
|
||||
session: session,
|
||||
duplicateTitles: duplicateTitles,
|
||||
onOpen: onOpen,
|
||||
onRename: onRename,
|
||||
onClose: onClose,
|
||||
onReopen: onReopen,
|
||||
onStar: onStar,
|
||||
),
|
||||
],
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _MaterialSessionRow extends StatelessWidget {
|
||||
const _MaterialSessionRow({
|
||||
required this.session,
|
||||
required this.duplicateTitles,
|
||||
required this.onOpen,
|
||||
required this.onRename,
|
||||
required this.onClose,
|
||||
required this.onReopen,
|
||||
required this.onStar,
|
||||
});
|
||||
|
||||
final JsonMap session;
|
||||
final Set<String> duplicateTitles;
|
||||
final ValueChanged<JsonMap> onOpen;
|
||||
final ValueChanged<JsonMap> onRename;
|
||||
final ValueChanged<JsonMap> onClose;
|
||||
final ValueChanged<JsonMap> onReopen;
|
||||
final ValueChanged<JsonMap> onStar;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final id = '${session['id'] ?? ''}';
|
||||
return ListTile(
|
||||
key: Key('chat-row-$id'),
|
||||
leading: Icon(sessionMaterialIcon(session)),
|
||||
title: Text(chatSessionTitle(session)),
|
||||
subtitle: Text(sessionSubtitle(session)),
|
||||
onTap: () => onOpen(session),
|
||||
trailing: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
SessionStarButton(
|
||||
key: Key('chat-star-$id'),
|
||||
session: session,
|
||||
onPressed: () => onStar(session),
|
||||
),
|
||||
SessionActionsButton(
|
||||
key: Key('chat-actions-$id'),
|
||||
session: session,
|
||||
duplicateTitles: duplicateTitles,
|
||||
onSelected: (action) => _select(action),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _select(SessionAction action) {
|
||||
switch (action) {
|
||||
case SessionAction.rename:
|
||||
onRename(session);
|
||||
case SessionAction.close:
|
||||
onClose(session);
|
||||
case SessionAction.reopen:
|
||||
onReopen(session);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../../../core/api_gateway.dart';
|
||||
import '../../domain/chat_sessions.dart';
|
||||
|
||||
/// Titles shared by more than one visible session, so row action labels can
|
||||
/// add the session id the way the web client does.
|
||||
Set<String> duplicateChatTitles(Iterable<JsonMap> sessions) {
|
||||
final seen = <String>{};
|
||||
final duplicates = <String>{};
|
||||
for (final session in sessions) {
|
||||
final title = chatSessionTitle(session);
|
||||
if (!seen.add(title)) duplicates.add(title);
|
||||
}
|
||||
return duplicates;
|
||||
}
|
||||
|
||||
String sessionActionsLabel(JsonMap session, Set<String> duplicateTitles) {
|
||||
final title = chatSessionTitle(session);
|
||||
return duplicateTitles.contains(title)
|
||||
? 'Chat actions for $title (${session['id'] ?? ''})'
|
||||
: 'Chat actions for $title';
|
||||
}
|
||||
|
||||
String sessionSubtitle(JsonMap session) {
|
||||
final directory = '${session['directory'] ?? ''}';
|
||||
switch (chatSessionActivity(
|
||||
running: session['running'] == true,
|
||||
unseen: session['unseen'] == true,
|
||||
)) {
|
||||
case 'streaming':
|
||||
return '$directory · Streaming';
|
||||
case 'new':
|
||||
return '$directory · New';
|
||||
default:
|
||||
return directory;
|
||||
}
|
||||
}
|
||||
|
||||
IconData sessionMaterialIcon(JsonMap session) {
|
||||
if (session['running'] == true) return Icons.sync;
|
||||
if (session['starred'] == true) return Icons.star;
|
||||
return Icons.chat_bubble_outline;
|
||||
}
|
||||
|
||||
IconData sessionCupertinoIcon(JsonMap session) {
|
||||
if (session['running'] == true) return CupertinoIcons.arrow_2_circlepath;
|
||||
if (session['starred'] == true) return CupertinoIcons.star_fill;
|
||||
return CupertinoIcons.chat_bubble;
|
||||
}
|
||||
Reference in New Issue
Block a user