126 lines
3.9 KiB
Dart
126 lines
3.9 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';
|
|
import 'session_row_style.dart';
|
|
import '../../../../ui/layout/app_overflow_menu.dart';
|
|
|
|
enum SessionAction {
|
|
openBeside,
|
|
openBelow,
|
|
star,
|
|
color,
|
|
rename,
|
|
close,
|
|
reopen,
|
|
hide,
|
|
}
|
|
|
|
/// 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 SessionActionsButton extends StatelessWidget {
|
|
const SessionActionsButton({
|
|
super.key,
|
|
required this.session,
|
|
required this.onSelected,
|
|
this.duplicateTitles = const {},
|
|
this.allowOpenBeside = false,
|
|
this.allowOpenBelow = false,
|
|
});
|
|
|
|
final JsonMap session;
|
|
final ValueChanged<SessionAction> onSelected;
|
|
final Set<String> duplicateTitles;
|
|
final bool allowOpenBeside;
|
|
final bool allowOpenBelow;
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return AppOverflowMenu<SessionAction>(
|
|
cupertino: chatControlsStyleOf(context) == ChatControlsStyle.cupertino,
|
|
label: sessionActionsLabel(session, duplicateTitles),
|
|
sheetTitle: chatSessionTitle(session),
|
|
actions: [
|
|
if (allowOpenBeside)
|
|
const AppMenuAction(
|
|
value: SessionAction.openBeside,
|
|
label: 'Open to the right',
|
|
),
|
|
if (allowOpenBelow)
|
|
const AppMenuAction(
|
|
value: SessionAction.openBelow,
|
|
label: 'Open below',
|
|
),
|
|
for (final item in _items(session))
|
|
AppMenuAction(value: item.action, label: item.label),
|
|
],
|
|
onSelected: (_, action) => onSelected(action),
|
|
);
|
|
}
|
|
}
|
|
|
|
List<({SessionAction action, String label})> _items(JsonMap session) {
|
|
final isClosed = session['closed_at'] != null;
|
|
return [
|
|
(
|
|
action: SessionAction.star,
|
|
label: session['starred'] == true ? 'Unstar' : 'Star',
|
|
),
|
|
(action: SessionAction.color, label: 'Colour…'),
|
|
(action: SessionAction.rename, label: 'Rename…'),
|
|
isClosed
|
|
? (action: SessionAction.reopen, label: 'Reopen')
|
|
: (action: SessionAction.close, label: 'Close chat…'),
|
|
(action: SessionAction.hide, label: 'Hide on this device'),
|
|
];
|
|
}
|
|
|
|
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;
|
|
}
|