118 lines
3.1 KiB
Dart
118 lines
3.1 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
import 'package:sentry_flutter/sentry_flutter.dart';
|
|
|
|
import '../../l10n/app_localizations.dart';
|
|
import '../../models/bar_tab.dart';
|
|
import '../../viewmodels/bar_screen_view_model.dart';
|
|
|
|
Future<void> showEditTabDialog(BuildContext context, BarTab tab) async {
|
|
final l10n = AppLocalizations.of(context);
|
|
|
|
final name = await showDialog<String>(
|
|
context: context,
|
|
builder: (_) => _EditTabDialog(initialName: tab.customerName),
|
|
);
|
|
|
|
if (name == null || name.trim().isEmpty || !context.mounted) return;
|
|
|
|
try {
|
|
await context.read<BarScreenViewModel>().renameTab(tab.id, name.trim());
|
|
} catch (e, stack) {
|
|
Sentry.captureException(e, stackTrace: stack);
|
|
if (!context.mounted) return;
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text(l10n.couldNotRenameTab)));
|
|
}
|
|
}
|
|
|
|
class _EditTabDialog extends StatefulWidget {
|
|
final String initialName;
|
|
|
|
const _EditTabDialog({required this.initialName});
|
|
|
|
@override
|
|
State<_EditTabDialog> createState() => _EditTabDialogState();
|
|
}
|
|
|
|
class _EditTabDialogState extends State<_EditTabDialog> {
|
|
late final TextEditingController _controller;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
_controller = TextEditingController(text: widget.initialName);
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_controller.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final l10n = AppLocalizations.of(context);
|
|
|
|
return AlertDialog(
|
|
title: Text(l10n.editTab),
|
|
content: TextField(
|
|
controller: _controller,
|
|
autofocus: true,
|
|
textInputAction: TextInputAction.done,
|
|
decoration: InputDecoration(labelText: l10n.customerGroupName),
|
|
onSubmitted: (value) {
|
|
Navigator.of(context).pop(value);
|
|
},
|
|
),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(context).pop(),
|
|
child: Text(l10n.cancel),
|
|
),
|
|
FilledButton(
|
|
onPressed: () => Navigator.of(context).pop(_controller.text),
|
|
child: Text(l10n.confirm),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> confirmDeleteTab(BuildContext context, BarTab tab) async {
|
|
final l10n = AppLocalizations.of(context);
|
|
|
|
final confirmed = await showDialog<bool>(
|
|
context: context,
|
|
builder: (dialogContext) {
|
|
return AlertDialog(
|
|
title: Text(l10n.deleteTab),
|
|
content: Text(l10n.deleteTabDescription),
|
|
actions: [
|
|
TextButton(
|
|
onPressed: () => Navigator.of(dialogContext).pop(false),
|
|
child: Text(l10n.cancel),
|
|
),
|
|
FilledButton(
|
|
onPressed: () => Navigator.of(dialogContext).pop(true),
|
|
child: Text(l10n.delete),
|
|
),
|
|
],
|
|
);
|
|
},
|
|
);
|
|
|
|
if (confirmed != true || !context.mounted) return;
|
|
|
|
try {
|
|
await context.read<BarScreenViewModel>().deleteTab(tab.id);
|
|
} catch (e, stack) {
|
|
Sentry.captureException(e, stackTrace: stack);
|
|
if (!context.mounted) return;
|
|
ScaffoldMessenger.of(
|
|
context,
|
|
).showSnackBar(SnackBar(content: Text(l10n.couldNotDeleteTab)));
|
|
}
|
|
}
|