feat: polish phase 7 forms and schedules
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../app/error_message.dart';
|
||||
import '../app/load_state.dart';
|
||||
import '../models/api_key_info.dart';
|
||||
import '../providers/providers.dart';
|
||||
import '../widgets/load_error_view.dart';
|
||||
|
||||
/// Экран управления гостевыми API-ключами.
|
||||
/// Доступен только администраторам.
|
||||
class ApiKeysScreen extends ConsumerStatefulWidget {
|
||||
const ApiKeysScreen({super.key});
|
||||
|
||||
@@ -17,6 +16,7 @@ class ApiKeysScreen extends ConsumerStatefulWidget {
|
||||
}
|
||||
|
||||
class _ApiKeysScreenState extends ConsumerState<ApiKeysScreen> {
|
||||
final Set<String> _busyKeys = <String>{};
|
||||
String? _lastCreatedKey;
|
||||
|
||||
@override
|
||||
@@ -39,13 +39,16 @@ class _ApiKeysScreenState extends ConsumerState<ApiKeysScreen> {
|
||||
body: Column(
|
||||
children: [
|
||||
if (_lastCreatedKey != null)
|
||||
_LastCreatedKeyBanner(keyValue: _lastCreatedKey!),
|
||||
_LastCreatedKeyBanner(
|
||||
keyValue: _lastCreatedKey!,
|
||||
onDismiss: () => setState(() => _lastCreatedKey = null),
|
||||
),
|
||||
Expanded(child: _buildContent(keysState, keys)),
|
||||
],
|
||||
),
|
||||
floatingActionButton: FloatingActionButton(
|
||||
backgroundColor: Colors.deepOrange,
|
||||
onPressed: () => _showCreateDialog(context),
|
||||
onPressed: _showCreateDialog,
|
||||
child: const Icon(Icons.add),
|
||||
),
|
||||
);
|
||||
@@ -73,7 +76,7 @@ class _ApiKeysScreenState extends ConsumerState<ApiKeysScreen> {
|
||||
if (keys.isEmpty) {
|
||||
return const Center(
|
||||
child: Text(
|
||||
'Нет гостевых ключей',
|
||||
'Гостевых ключей пока нет',
|
||||
style: TextStyle(color: Colors.white54),
|
||||
),
|
||||
);
|
||||
@@ -105,85 +108,131 @@ class _ApiKeysScreenState extends ConsumerState<ApiKeysScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
final keyIndex = index - statusHeaderCount;
|
||||
final key = keys[keyIndex];
|
||||
final keyData = keys[index - statusHeaderCount];
|
||||
final busy = _busyKeys.contains(keyData.key);
|
||||
return _ApiKeyCard(
|
||||
data: key,
|
||||
onRevoke: () => _revokeKey(key),
|
||||
onActivate: () => _activateKey(key),
|
||||
data: keyData,
|
||||
busy: busy,
|
||||
onRevoke: busy ? null : () => _revokeKey(keyData),
|
||||
onActivate: busy ? null : () => _activateKey(keyData),
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _showCreateDialog(BuildContext context) {
|
||||
Future<void> _showCreateDialog() async {
|
||||
final formKey = GlobalKey<FormState>();
|
||||
final nameCtrl = TextEditingController();
|
||||
bool isAdmin = false;
|
||||
var isAdmin = false;
|
||||
var isCreating = false;
|
||||
|
||||
showDialog(
|
||||
await showDialog<void>(
|
||||
context: context,
|
||||
builder: (ctx) => StatefulBuilder(
|
||||
builder: (ctx, setDialogState) => AlertDialog(
|
||||
title: const Text('Новый API-ключ'),
|
||||
content: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
TextField(
|
||||
controller: nameCtrl,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Имя ключа',
|
||||
hintText: 'Например: "Гость"',
|
||||
content: Form(
|
||||
key: formKey,
|
||||
autovalidateMode: AutovalidateMode.onUserInteraction,
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
TextFormField(
|
||||
controller: nameCtrl,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Имя ключа',
|
||||
hintText: 'Например: Гость',
|
||||
),
|
||||
autofocus: true,
|
||||
validator: (value) {
|
||||
final normalized = value?.trim() ?? '';
|
||||
if (normalized.isEmpty) {
|
||||
return 'Укажите имя ключа';
|
||||
}
|
||||
if (normalized.length < 2) {
|
||||
return 'Слишком короткое имя';
|
||||
}
|
||||
return null;
|
||||
},
|
||||
),
|
||||
autofocus: true,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
SwitchListTile(
|
||||
title: const Text('Администратор'),
|
||||
value: isAdmin,
|
||||
activeThumbColor: Colors.deepOrange,
|
||||
onChanged: (v) => setDialogState(() => isAdmin = v),
|
||||
contentPadding: EdgeInsets.zero,
|
||||
),
|
||||
],
|
||||
const SizedBox(height: 12),
|
||||
SwitchListTile(
|
||||
title: const Text('Дать права администратора'),
|
||||
subtitle: const Text(
|
||||
'Используйте только для доверенных людей',
|
||||
),
|
||||
value: isAdmin,
|
||||
activeThumbColor: Colors.deepOrange,
|
||||
onChanged: isCreating
|
||||
? null
|
||||
: (value) => setDialogState(() => isAdmin = value),
|
||||
contentPadding: EdgeInsets.zero,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(ctx).pop(),
|
||||
onPressed: isCreating ? null : () => Navigator.of(ctx).pop(),
|
||||
child: const Text('Отмена'),
|
||||
),
|
||||
ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.deepOrange,
|
||||
),
|
||||
onPressed: () async {
|
||||
final name = nameCtrl.text.trim();
|
||||
if (name.isEmpty) return;
|
||||
Navigator.of(ctx).pop();
|
||||
final messenger = ScaffoldMessenger.of(context);
|
||||
try {
|
||||
final key = await ref
|
||||
.read(apiKeysProvider.notifier)
|
||||
.create(name, isAdmin: isAdmin);
|
||||
if (!mounted) return;
|
||||
setState(() => _lastCreatedKey = key);
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
messenger.showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
'Ошибка создания ключа: ${describeLoadError(e)}',
|
||||
onPressed: isCreating
|
||||
? null
|
||||
: () async {
|
||||
if (!formKey.currentState!.validate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
setDialogState(() => isCreating = true);
|
||||
try {
|
||||
final key = await ref
|
||||
.read(apiKeysProvider.notifier)
|
||||
.create(nameCtrl.text.trim(), isAdmin: isAdmin);
|
||||
if (!mounted) return;
|
||||
setState(() => _lastCreatedKey = key);
|
||||
if (ctx.mounted) {
|
||||
Navigator.of(ctx).pop();
|
||||
}
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Ключ создан'),
|
||||
duration: Duration(seconds: 1),
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
if (!ctx.mounted) return;
|
||||
setDialogState(() => isCreating = false);
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
'Ошибка создания ключа: ${describeLoadError(e)}',
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
child: isCreating
|
||||
? const SizedBox(
|
||||
width: 18,
|
||||
height: 18,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
color: Colors.white,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
child: const Text('Создать'),
|
||||
)
|
||||
: const Text('Создать'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
nameCtrl.dispose();
|
||||
}
|
||||
|
||||
Future<void> _revokeKey(ApiKeyInfo data) async {
|
||||
@@ -191,7 +240,7 @@ class _ApiKeysScreenState extends ConsumerState<ApiKeysScreen> {
|
||||
context: context,
|
||||
builder: (ctx) => AlertDialog(
|
||||
title: const Text('Отозвать ключ?'),
|
||||
content: Text('Отозвать "${data.name}"?'),
|
||||
content: Text('Ключ "${data.name}" перестанет работать.'),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(ctx).pop(false),
|
||||
@@ -207,31 +256,43 @@ class _ApiKeysScreenState extends ConsumerState<ApiKeysScreen> {
|
||||
],
|
||||
),
|
||||
);
|
||||
if (confirmed == true) {
|
||||
try {
|
||||
await ref.read(apiKeysProvider.notifier).revoke(data.key);
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Ошибка отзыва ключа: ${describeLoadError(e)}'),
|
||||
),
|
||||
);
|
||||
if (confirmed != true) {
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() => _busyKeys.add(data.key));
|
||||
try {
|
||||
await ref.read(apiKeysProvider.notifier).revoke(data.key);
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Ключ отозван'),
|
||||
duration: Duration(seconds: 1),
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Ошибка отзыва ключа: ${describeLoadError(e)}')),
|
||||
);
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() => _busyKeys.remove(data.key));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _activateKey(ApiKeyInfo data) async {
|
||||
setState(() => _busyKeys.add(data.key));
|
||||
try {
|
||||
await ref.read(apiKeysProvider.notifier).activate(data.key);
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Ключ активирован'),
|
||||
duration: Duration(seconds: 1),
|
||||
),
|
||||
);
|
||||
}
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Ключ снова активен'),
|
||||
duration: Duration(seconds: 1),
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
@@ -239,14 +300,22 @@ class _ApiKeysScreenState extends ConsumerState<ApiKeysScreen> {
|
||||
content: Text('Ошибка активации ключа: ${describeLoadError(e)}'),
|
||||
),
|
||||
);
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() => _busyKeys.remove(data.key));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class _LastCreatedKeyBanner extends StatelessWidget {
|
||||
final String keyValue;
|
||||
final VoidCallback onDismiss;
|
||||
|
||||
const _LastCreatedKeyBanner({required this.keyValue});
|
||||
const _LastCreatedKeyBanner({
|
||||
required this.keyValue,
|
||||
required this.onDismiss,
|
||||
});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@@ -261,13 +330,24 @@ class _LastCreatedKeyBanner extends StatelessWidget {
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text(
|
||||
'Новый ключ создан! Скопируйте его сейчас:',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.deepOrange,
|
||||
),
|
||||
Row(
|
||||
children: [
|
||||
const Expanded(
|
||||
child: Text(
|
||||
'Новый ключ создан. Скопируйте его сейчас, потом приложение уже не покажет полный токен.',
|
||||
style: TextStyle(
|
||||
fontSize: 13,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.deepOrange,
|
||||
),
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
onPressed: onDismiss,
|
||||
icon: const Icon(Icons.close, size: 18),
|
||||
tooltip: 'Скрыть',
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
Row(
|
||||
@@ -280,7 +360,7 @@ class _LastCreatedKeyBanner extends StatelessWidget {
|
||||
fontFamily: 'monospace',
|
||||
color: Colors.white70,
|
||||
),
|
||||
maxLines: 2,
|
||||
maxLines: 3,
|
||||
overflow: TextOverflow.ellipsis,
|
||||
),
|
||||
),
|
||||
@@ -304,14 +384,15 @@ class _LastCreatedKeyBanner extends StatelessWidget {
|
||||
}
|
||||
}
|
||||
|
||||
/// Карточка одного API-ключа
|
||||
class _ApiKeyCard extends StatelessWidget {
|
||||
final ApiKeyInfo data;
|
||||
final VoidCallback onRevoke;
|
||||
final VoidCallback onActivate;
|
||||
final bool busy;
|
||||
final VoidCallback? onRevoke;
|
||||
final VoidCallback? onActivate;
|
||||
|
||||
const _ApiKeyCard({
|
||||
required this.data,
|
||||
required this.busy,
|
||||
required this.onRevoke,
|
||||
required this.onActivate,
|
||||
});
|
||||
@@ -329,11 +410,13 @@ class _ApiKeyCard extends StatelessWidget {
|
||||
),
|
||||
title: Row(
|
||||
children: [
|
||||
Text(
|
||||
data.name,
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: data.isActive ? Colors.white : Colors.white38,
|
||||
Expanded(
|
||||
child: Text(
|
||||
data.name,
|
||||
style: TextStyle(
|
||||
fontWeight: FontWeight.bold,
|
||||
color: data.isActive ? Colors.white : Colors.white38,
|
||||
),
|
||||
),
|
||||
),
|
||||
if (data.isAdmin) ...[
|
||||
@@ -372,24 +455,23 @@ class _ApiKeyCard extends StatelessWidget {
|
||||
style: const TextStyle(fontSize: 11, color: Colors.white30),
|
||||
)
|
||||
: null,
|
||||
trailing: data.isActive
|
||||
? IconButton(
|
||||
icon: const Icon(
|
||||
Icons.block,
|
||||
size: 20,
|
||||
color: Colors.redAccent,
|
||||
trailing: busy
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
color: Colors.deepOrange,
|
||||
),
|
||||
tooltip: 'Отозвать',
|
||||
onPressed: onRevoke,
|
||||
)
|
||||
: IconButton(
|
||||
icon: const Icon(
|
||||
Icons.check_circle_outline,
|
||||
icon: Icon(
|
||||
data.isActive ? Icons.block : Icons.check_circle_outline,
|
||||
size: 20,
|
||||
color: Colors.green,
|
||||
color: data.isActive ? Colors.redAccent : Colors.green,
|
||||
),
|
||||
tooltip: 'Активировать',
|
||||
onPressed: onActivate,
|
||||
tooltip: data.isActive ? 'Отозвать' : 'Активировать',
|
||||
onPressed: data.isActive ? onRevoke : onActivate,
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user