Формы создания/редактирования зон, устройств и правил автоматизации
ZoneFormScreen/DeviceFormScreen/AutomationRuleFormScreen — одна форма на создание и редактирование, показ ошибок валидации Laravel (422) по полям. Списки зон/устройств/правил дополнены FAB "добавить", свайпом/меню удаления и переходом в форму по тапу — всё только для owner (RBAC), viewer видит те же списки в режиме только чтения. Опции дропдаунов (типы устройств, датчики/актуаторы, capabilities) берутся из данных бэкенда, не хардкодятся.
This commit is contained in:
@@ -2,9 +2,11 @@ import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../core/api_service.dart';
|
||||
import '../core/auth_service.dart';
|
||||
import '../models/device.dart';
|
||||
import '../widgets/status_badge.dart';
|
||||
import 'device_detail_screen.dart';
|
||||
import 'device_form_screen.dart';
|
||||
|
||||
class DevicesTab extends StatefulWidget {
|
||||
const DevicesTab({super.key});
|
||||
@@ -30,43 +32,95 @@ class _DevicesTabState extends State<DevicesTab> {
|
||||
await future;
|
||||
}
|
||||
|
||||
Future<void> _openForm({Device? device}) async {
|
||||
final saved = await Navigator.of(context).push<bool>(
|
||||
MaterialPageRoute(builder: (_) => DeviceFormScreen(device: device)),
|
||||
);
|
||||
if (saved == true) _refresh();
|
||||
}
|
||||
|
||||
Future<void> _delete(Device device) async {
|
||||
final confirmed = await showDialog<bool>(
|
||||
context: context,
|
||||
builder: (context) => AlertDialog(
|
||||
title: const Text('Удалить устройство?'),
|
||||
content: Text('«${device.name}» будет удалено без возможности восстановления.'),
|
||||
actions: [
|
||||
TextButton(onPressed: () => Navigator.pop(context, false), child: const Text('Отмена')),
|
||||
TextButton(onPressed: () => Navigator.pop(context, true), child: const Text('Удалить')),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (confirmed != true || !mounted) return;
|
||||
|
||||
try {
|
||||
await context.read<ApiService>().deleteDevice(device.id);
|
||||
_refresh();
|
||||
} catch (e) {
|
||||
if (mounted) ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('$e')));
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return RefreshIndicator(
|
||||
onRefresh: _refresh,
|
||||
child: FutureBuilder<List<Device>>(
|
||||
future: _future,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState != ConnectionState.done) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (snapshot.hasError) {
|
||||
return _ErrorList(message: '${snapshot.error}', onRetry: _refresh);
|
||||
}
|
||||
final isOwner = context.watch<AuthService>().user?.isOwner ?? false;
|
||||
|
||||
final devices = snapshot.data!;
|
||||
if (devices.isEmpty) {
|
||||
return _ErrorList(message: 'Устройств пока нет.', onRetry: _refresh);
|
||||
}
|
||||
return Scaffold(
|
||||
body: RefreshIndicator(
|
||||
onRefresh: _refresh,
|
||||
child: FutureBuilder<List<Device>>(
|
||||
future: _future,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState != ConnectionState.done) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (snapshot.hasError) {
|
||||
return _ErrorList(message: '${snapshot.error}', onRetry: _refresh);
|
||||
}
|
||||
|
||||
return ListView.separated(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
itemCount: devices.length,
|
||||
separatorBuilder: (context, index) => const Divider(height: 1),
|
||||
itemBuilder: (context, index) {
|
||||
final device = devices[index];
|
||||
return ListTile(
|
||||
title: Text(device.name),
|
||||
subtitle: Text('${device.zone?.name ?? '—'} · ${device.deviceType?.code ?? device.protocol}'),
|
||||
trailing: StatusBadge(status: device.status),
|
||||
onTap: () => Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (_) => DeviceDetailScreen(deviceId: device.id)),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
final devices = snapshot.data!;
|
||||
if (devices.isEmpty) {
|
||||
return _ErrorList(message: 'Устройств пока нет.', onRetry: _refresh);
|
||||
}
|
||||
|
||||
return ListView.separated(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
itemCount: devices.length,
|
||||
separatorBuilder: (context, index) => const Divider(height: 1),
|
||||
itemBuilder: (context, index) {
|
||||
final device = devices[index];
|
||||
return ListTile(
|
||||
title: Text(device.name),
|
||||
subtitle: Text('${device.zone?.name ?? '—'} · ${device.deviceType?.code ?? device.protocol}'),
|
||||
trailing: Row(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
StatusBadge(status: device.status),
|
||||
if (isOwner)
|
||||
PopupMenuButton<String>(
|
||||
onSelected: (value) {
|
||||
if (value == 'edit') _openForm(device: device);
|
||||
if (value == 'delete') _delete(device);
|
||||
},
|
||||
itemBuilder: (context) => const [
|
||||
PopupMenuItem(value: 'edit', child: Text('Редактировать')),
|
||||
PopupMenuItem(value: 'delete', child: Text('Удалить')),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
onTap: () => Navigator.of(context).push(
|
||||
MaterialPageRoute(builder: (_) => DeviceDetailScreen(deviceId: device.id)),
|
||||
),
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
floatingActionButton: isOwner
|
||||
? FloatingActionButton(onPressed: () => _openForm(), child: const Icon(Icons.add))
|
||||
: null,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user