feat: polish phase 7 forms and schedules
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../app/error_message.dart';
|
||||
import '../app/load_state.dart';
|
||||
import '../features/groups/group_form_logic.dart';
|
||||
import '../models/ignis_device.dart';
|
||||
import '../models/ignis_group.dart';
|
||||
import '../providers/providers.dart';
|
||||
import '../widgets/load_error_view.dart';
|
||||
|
||||
/// Экран создания новой группы ламп.
|
||||
/// Загружает список устройств, позволяет выбрать нужные.
|
||||
class GroupEditScreen extends ConsumerStatefulWidget {
|
||||
const GroupEditScreen({super.key});
|
||||
|
||||
@@ -16,47 +17,114 @@ class GroupEditScreen extends ConsumerStatefulWidget {
|
||||
}
|
||||
|
||||
class _GroupEditScreenState extends ConsumerState<GroupEditScreen> {
|
||||
final _formKey = GlobalKey<FormState>();
|
||||
final _idCtrl = TextEditingController();
|
||||
final _nameCtrl = TextEditingController();
|
||||
final Set<String> _selectedMacs = {};
|
||||
final Set<String> _selectedMacs = <String>{};
|
||||
|
||||
bool _saving = false;
|
||||
bool _rescanning = false;
|
||||
bool _deviceSelectionTouched = false;
|
||||
bool _idEditedManually = false;
|
||||
bool _syncingIdFromName = false;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_loadDevices();
|
||||
_nameCtrl.addListener(_handleNameChanged);
|
||||
_idCtrl.addListener(_handleIdChanged);
|
||||
Future<void>.microtask(() async {
|
||||
await _loadDevices();
|
||||
try {
|
||||
await ref.read(groupsProvider.notifier).refresh();
|
||||
} catch (_) {
|
||||
// Покажем текущее состояние через экран, без лишнего шума.
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_nameCtrl.removeListener(_handleNameChanged);
|
||||
_idCtrl.removeListener(_handleIdChanged);
|
||||
_idCtrl.dispose();
|
||||
_nameCtrl.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
void _handleNameChanged() {
|
||||
if (_idEditedManually) {
|
||||
return;
|
||||
}
|
||||
final slug = slugifyGroupId(_nameCtrl.text);
|
||||
_syncingIdFromName = true;
|
||||
_idCtrl.value = _idCtrl.value.copyWith(
|
||||
text: slug,
|
||||
selection: TextSelection.collapsed(offset: slug.length),
|
||||
composing: TextRange.empty,
|
||||
);
|
||||
_syncingIdFromName = false;
|
||||
}
|
||||
|
||||
void _handleIdChanged() {
|
||||
if (_syncingIdFromName) {
|
||||
return;
|
||||
}
|
||||
final suggested = slugifyGroupId(_nameCtrl.text);
|
||||
final current = _idCtrl.text.trim();
|
||||
_idEditedManually = current.isNotEmpty && current != suggested;
|
||||
}
|
||||
|
||||
Future<void> _loadDevices() async {
|
||||
await ref.read(devicesProvider.notifier).load();
|
||||
}
|
||||
|
||||
/// Пересканировать сеть и перезагрузить устройства
|
||||
Future<void> _rescan() async {
|
||||
final beforeIds = ref
|
||||
.read(devicesProvider)
|
||||
.data
|
||||
.map((device) => device.groupMemberId)
|
||||
.toSet();
|
||||
|
||||
setState(() => _rescanning = true);
|
||||
try {
|
||||
await ref.read(apiProvider).rescanNetwork();
|
||||
// Подождать немного -- сканирование асинхронное
|
||||
await Future.delayed(const Duration(seconds: 3));
|
||||
await ref.read(devicesProvider.notifier).load();
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Ошибка сканирования: ${describeLoadError(e)}'),
|
||||
),
|
||||
);
|
||||
|
||||
var changed = false;
|
||||
for (var attempt = 0; attempt < 6; attempt++) {
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
await ref.read(devicesProvider.notifier).load();
|
||||
final currentIds = ref
|
||||
.read(devicesProvider)
|
||||
.data
|
||||
.map((device) => device.groupMemberId)
|
||||
.toSet();
|
||||
if (!_sameSet(beforeIds, currentIds)) {
|
||||
changed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text(
|
||||
changed
|
||||
? 'Список устройств обновился'
|
||||
: 'Сканирование завершилось, но новых устройств пока не видно',
|
||||
),
|
||||
duration: const Duration(seconds: 2),
|
||||
),
|
||||
);
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Ошибка сканирования: ${describeLoadError(e)}')),
|
||||
);
|
||||
} finally {
|
||||
if (mounted) setState(() => _rescanning = false);
|
||||
if (mounted) {
|
||||
setState(() => _rescanning = false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,12 +132,13 @@ class _GroupEditScreenState extends ConsumerState<GroupEditScreen> {
|
||||
Widget build(BuildContext context) {
|
||||
final devicesState = ref.watch(devicesProvider);
|
||||
final devices = devicesState.data;
|
||||
final groups = ref.watch(groupsProvider);
|
||||
final conflicts = findGroupMembershipConflicts(_selectedMacs, groups);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('НОВАЯ ГРУППА'),
|
||||
actions: [
|
||||
// Кнопка ресканирования сети
|
||||
IconButton(
|
||||
icon: _rescanning
|
||||
? const SizedBox(
|
||||
@@ -83,99 +152,129 @@ class _GroupEditScreenState extends ConsumerState<GroupEditScreen> {
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
// ID группы
|
||||
TextField(
|
||||
controller: _idCtrl,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'ID группы (например "bedroom")',
|
||||
prefixIcon: Icon(Icons.tag),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
|
||||
// Название группы
|
||||
TextField(
|
||||
controller: _nameCtrl,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Название (например "Спальня")',
|
||||
prefixIcon: Icon(Icons.label),
|
||||
),
|
||||
textCapitalization: TextCapitalization.sentences,
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
|
||||
// Заголовок списка устройств
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
'Устройства (${_selectedMacs.length} выбрано)',
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.white70,
|
||||
),
|
||||
body: Form(
|
||||
key: _formKey,
|
||||
autovalidateMode: AutovalidateMode.onUserInteraction,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
TextFormField(
|
||||
controller: _nameCtrl,
|
||||
decoration: const InputDecoration(
|
||||
labelText: 'Название группы',
|
||||
hintText: 'Например: Спальня',
|
||||
prefixIcon: Icon(Icons.label),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: devices.isEmpty
|
||||
? null
|
||||
: () {
|
||||
setState(() {
|
||||
if (_selectedMacs.length == devices.length) {
|
||||
_selectedMacs.clear();
|
||||
} else {
|
||||
for (final d in devices) {
|
||||
_selectedMacs.add(d.groupMemberId);
|
||||
textCapitalization: TextCapitalization.sentences,
|
||||
validator: (value) => validateGroupName(value ?? ''),
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
TextFormField(
|
||||
controller: _idCtrl,
|
||||
decoration: InputDecoration(
|
||||
labelText: 'ID группы',
|
||||
hintText: 'Например: bedroom',
|
||||
helperText: _idEditedManually
|
||||
? 'Используется в API и расписаниях'
|
||||
: 'Подставляется автоматически из названия',
|
||||
prefixIcon: const Icon(Icons.tag),
|
||||
),
|
||||
autocorrect: false,
|
||||
enableSuggestions: false,
|
||||
validator: (value) => validateGroupId(value ?? '', groups),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
const Text(
|
||||
'Выберите устройства, которые будут управляться как одна группа.',
|
||||
style: TextStyle(color: Colors.white54),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
||||
children: [
|
||||
Text(
|
||||
'Устройства (${_selectedMacs.length} выбрано)',
|
||||
style: const TextStyle(
|
||||
fontSize: 14,
|
||||
fontWeight: FontWeight.bold,
|
||||
color: Colors.white70,
|
||||
),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: devices.isEmpty
|
||||
? null
|
||||
: () {
|
||||
setState(() {
|
||||
_deviceSelectionTouched = true;
|
||||
if (_selectedMacs.length == devices.length) {
|
||||
_selectedMacs.clear();
|
||||
} else {
|
||||
_selectedMacs
|
||||
..clear()
|
||||
..addAll(
|
||||
devices.map(
|
||||
(device) => device.groupMemberId,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
});
|
||||
},
|
||||
child: Text(
|
||||
_selectedMacs.length == devices.length
|
||||
? 'Снять все'
|
||||
: 'Выбрать все',
|
||||
style: const TextStyle(fontSize: 12),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
if (_deviceSelectionTouched && _selectedMacs.isEmpty)
|
||||
const Padding(
|
||||
padding: EdgeInsets.only(top: 4, bottom: 8),
|
||||
child: Text(
|
||||
_selectedMacs.length == devices.length
|
||||
? 'Снять все'
|
||||
: 'Выбрать все',
|
||||
style: const TextStyle(fontSize: 12),
|
||||
'Выберите хотя бы одно устройство',
|
||||
style: TextStyle(color: Colors.redAccent, fontSize: 12),
|
||||
),
|
||||
)
|
||||
else
|
||||
const SizedBox(height: 8),
|
||||
if (conflicts.isNotEmpty)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(bottom: 12),
|
||||
child: _ConflictBanner(conflicts: conflicts),
|
||||
),
|
||||
Expanded(child: _buildDevices(devicesState, devices)),
|
||||
Padding(
|
||||
padding: EdgeInsets.only(
|
||||
top: 12,
|
||||
bottom: MediaQuery.of(context).padding.bottom + 8,
|
||||
),
|
||||
child: SizedBox(
|
||||
width: double.infinity,
|
||||
height: 48,
|
||||
child: ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.deepOrange,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
onPressed: _saving ? null : _save,
|
||||
child: _saving
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
color: Colors.white,
|
||||
),
|
||||
)
|
||||
: const Text('СОЗДАТЬ ГРУППУ'),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
// Список устройств
|
||||
Expanded(child: _buildDevices(devicesState, devices)),
|
||||
|
||||
// Кнопка сохранения
|
||||
Padding(
|
||||
padding: EdgeInsets.only(
|
||||
bottom: MediaQuery.of(context).padding.bottom + 8,
|
||||
),
|
||||
child: SizedBox(
|
||||
width: double.infinity,
|
||||
height: 48,
|
||||
child: ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(
|
||||
backgroundColor: Colors.deepOrange,
|
||||
foregroundColor: Colors.white,
|
||||
),
|
||||
onPressed: _saving ? null : _save,
|
||||
child: _saving
|
||||
? const SizedBox(
|
||||
width: 20,
|
||||
height: 20,
|
||||
child: CircularProgressIndicator(
|
||||
strokeWidth: 2,
|
||||
color: Colors.white,
|
||||
),
|
||||
)
|
||||
: const Text('СОЗДАТЬ ГРУППУ'),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
@@ -203,7 +302,7 @@ class _GroupEditScreenState extends ConsumerState<GroupEditScreen> {
|
||||
if (devices.isEmpty) {
|
||||
return const Center(
|
||||
child: Text(
|
||||
'Устройства не найдены.\nПопробуйте пересканировать сеть.',
|
||||
'Устройства не найдены.\nПересканируйте сеть и попробуйте снова.',
|
||||
textAlign: TextAlign.center,
|
||||
style: TextStyle(color: Colors.white38),
|
||||
),
|
||||
@@ -231,26 +330,26 @@ class _GroupEditScreenState extends ConsumerState<GroupEditScreen> {
|
||||
);
|
||||
}
|
||||
|
||||
final deviceIndex = index - statusHeaderCount;
|
||||
final d = devices[deviceIndex];
|
||||
final selected = _selectedMacs.contains(d.groupMemberId);
|
||||
final device = devices[index - statusHeaderCount];
|
||||
final selected = _selectedMacs.contains(device.groupMemberId);
|
||||
|
||||
return CheckboxListTile(
|
||||
value: selected,
|
||||
activeColor: Colors.deepOrange,
|
||||
title: Text(d.name),
|
||||
subtitle: d.subtitle == null
|
||||
title: Text(device.name),
|
||||
subtitle: device.subtitle == null
|
||||
? null
|
||||
: Text(
|
||||
d.subtitle!,
|
||||
device.subtitle!,
|
||||
style: const TextStyle(fontSize: 11, color: Colors.white38),
|
||||
),
|
||||
onChanged: (v) {
|
||||
onChanged: (value) {
|
||||
setState(() {
|
||||
if (v == true) {
|
||||
_selectedMacs.add(d.groupMemberId);
|
||||
_deviceSelectionTouched = true;
|
||||
if (value == true) {
|
||||
_selectedMacs.add(device.groupMemberId);
|
||||
} else {
|
||||
_selectedMacs.remove(d.groupMemberId);
|
||||
_selectedMacs.remove(device.groupMemberId);
|
||||
}
|
||||
});
|
||||
},
|
||||
@@ -260,38 +359,106 @@ class _GroupEditScreenState extends ConsumerState<GroupEditScreen> {
|
||||
}
|
||||
|
||||
Future<void> _save() async {
|
||||
final id = _idCtrl.text.trim();
|
||||
final name = _nameCtrl.text.trim();
|
||||
FocusScope.of(context).unfocus();
|
||||
setState(() => _deviceSelectionTouched = true);
|
||||
|
||||
if (id.isEmpty || name.isEmpty) {
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(const SnackBar(content: Text('Укажите ID и название')));
|
||||
if (!_formKey.currentState!.validate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (_selectedMacs.isEmpty) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(content: Text('Выберите хотя бы одно устройство')),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() => _saving = true);
|
||||
|
||||
try {
|
||||
await ref.read(apiProvider).createGroup(id, name, _selectedMacs.toList());
|
||||
// Обновить список групп
|
||||
await ref.read(groupsProvider.notifier).refresh();
|
||||
if (mounted) Navigator.of(context).pop();
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Ошибка создания: ${describeLoadError(e)}')),
|
||||
);
|
||||
final groups = ref.read(groupsProvider);
|
||||
final conflicts = findGroupMembershipConflicts(_selectedMacs, groups);
|
||||
if (conflicts.isNotEmpty) {
|
||||
final shouldContinue = await _confirmConflicts(conflicts);
|
||||
if (shouldContinue != true) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (mounted) setState(() => _saving = false);
|
||||
setState(() => _saving = true);
|
||||
try {
|
||||
final id = _idCtrl.text.trim().toLowerCase();
|
||||
final name = _nameCtrl.text.trim();
|
||||
await ref.read(apiProvider).createGroup(id, name, _selectedMacs.toList());
|
||||
await ref.read(groupsProvider.notifier).refresh();
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Группа создана'),
|
||||
duration: Duration(seconds: 1),
|
||||
),
|
||||
);
|
||||
Navigator.of(context).pop();
|
||||
} catch (e) {
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Ошибка создания: ${describeLoadError(e)}')),
|
||||
);
|
||||
} finally {
|
||||
if (mounted) {
|
||||
setState(() => _saving = false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool?> _confirmConflicts(List<IgnisGroup> conflicts) {
|
||||
return showDialog<bool>(
|
||||
context: context,
|
||||
builder: (ctx) => AlertDialog(
|
||||
title: const Text('Устройства уже состоят в других группах'),
|
||||
content: Text(
|
||||
'Выбранные устройства уже входят в: ${conflicts.map((group) => group.name).join(', ')}.\n\nПродолжить создание новой группы?',
|
||||
),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () => Navigator.of(ctx).pop(false),
|
||||
child: const Text('Отмена'),
|
||||
),
|
||||
ElevatedButton(
|
||||
style: ElevatedButton.styleFrom(backgroundColor: Colors.deepOrange),
|
||||
onPressed: () => Navigator.of(ctx).pop(true),
|
||||
child: const Text('Продолжить'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
bool _sameSet(Set<String> left, Set<String> right) {
|
||||
if (left.length != right.length) {
|
||||
return false;
|
||||
}
|
||||
for (final item in left) {
|
||||
if (!right.contains(item)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
class _ConflictBanner extends StatelessWidget {
|
||||
final List<IgnisGroup> conflicts;
|
||||
|
||||
const _ConflictBanner({required this.conflicts});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
width: double.infinity,
|
||||
padding: const EdgeInsets.all(12),
|
||||
decoration: BoxDecoration(
|
||||
color: Colors.amber.withValues(alpha: 0.12),
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
border: Border.all(color: Colors.amber.withValues(alpha: 0.3)),
|
||||
),
|
||||
child: Text(
|
||||
'Внимание: часть выбранных устройств уже состоит в группах ${conflicts.map((group) => group.name).join(', ')}.',
|
||||
style: const TextStyle(color: Colors.amber),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user