Каркас приложения: авторизация, зоны/устройства/правила, управление устройствами
HTTP-клиент к /api/v1 с Sanctum-токеном (хранится в secure storage), модели зеркалят Laravel API Resources. Экраны: логин, список устройств с живым статусом, детальная страница устройства (история телеметрии, turn-on/turn-off/set-level с проверкой capability), списки зон и правил автоматизации (пока только чтение — формы создания/редактирования впереди).
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:provider/provider.dart';
|
||||
|
||||
import '../core/api_service.dart';
|
||||
import '../models/automation_rule.dart';
|
||||
|
||||
class AutomationRulesTab extends StatefulWidget {
|
||||
const AutomationRulesTab({super.key});
|
||||
|
||||
@override
|
||||
State<AutomationRulesTab> createState() => _AutomationRulesTabState();
|
||||
}
|
||||
|
||||
class _AutomationRulesTabState extends State<AutomationRulesTab> {
|
||||
late Future<List<AutomationRule>> _future;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_future = _load();
|
||||
}
|
||||
|
||||
Future<List<AutomationRule>> _load() => context.read<ApiService>().automationRules();
|
||||
|
||||
Future<void> _refresh() async {
|
||||
final future = _load();
|
||||
setState(() => _future = future);
|
||||
await future;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return RefreshIndicator(
|
||||
onRefresh: _refresh,
|
||||
child: FutureBuilder<List<AutomationRule>>(
|
||||
future: _future,
|
||||
builder: (context, snapshot) {
|
||||
if (snapshot.connectionState != ConnectionState.done) {
|
||||
return const Center(child: CircularProgressIndicator());
|
||||
}
|
||||
if (snapshot.hasError) {
|
||||
return ListView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
children: [
|
||||
Padding(
|
||||
padding: const EdgeInsets.all(24),
|
||||
child: Text('${snapshot.error}', textAlign: TextAlign.center),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
final rules = snapshot.data!;
|
||||
if (rules.isEmpty) {
|
||||
return ListView(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
children: const [
|
||||
Padding(
|
||||
padding: EdgeInsets.all(24),
|
||||
child: Text('Правил пока нет.', textAlign: TextAlign.center),
|
||||
),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
return ListView.separated(
|
||||
physics: const AlwaysScrollableScrollPhysics(),
|
||||
itemCount: rules.length,
|
||||
separatorBuilder: (context, index) => const Divider(height: 1),
|
||||
itemBuilder: (context, index) {
|
||||
final rule = rules[index];
|
||||
final condition =
|
||||
'${rule.conditionSourceDevice?.name ?? '?'}: ${rule.conditionSensorType} ${rule.conditionOperator} ${rule.conditionValue}';
|
||||
final action = rule.level != null
|
||||
? '${rule.actionType} (${rule.level}) → ${rule.targetDevice?.name ?? '?'}'
|
||||
: '${rule.actionType} → ${rule.targetDevice?.name ?? '?'}';
|
||||
|
||||
return ListTile(
|
||||
leading: Icon(rule.isActive ? Icons.bolt : Icons.bolt_outlined),
|
||||
title: Text('ЕСЛИ $condition'),
|
||||
subtitle: Text('ТО $action'),
|
||||
isThreeLine: true,
|
||||
);
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user