feat: surface device and scene load errors
This commit is contained in:
@@ -503,7 +503,8 @@ class GroupsNotifier extends Notifier<List<dynamic>> {
|
|||||||
await _api.controlGroup(id, {'state': on});
|
await _api.controlGroup(id, {'state': on});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
_lockUntil.remove(id);
|
_lockUntil.remove(id);
|
||||||
refresh();
|
await refresh();
|
||||||
|
rethrow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -540,7 +541,8 @@ class GroupsNotifier extends Notifier<List<dynamic>> {
|
|||||||
await _api.controlGroup(id, {'scene': scene});
|
await _api.controlGroup(id, {'scene': scene});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
_lockUntil.remove(id);
|
_lockUntil.remove(id);
|
||||||
refresh();
|
await refresh();
|
||||||
|
rethrow;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -558,64 +560,92 @@ class GroupsNotifier extends Notifier<List<dynamic>> {
|
|||||||
|
|
||||||
// ─── Устройства (для создания групп) ─────────────────────────
|
// ─── Устройства (для создания групп) ─────────────────────────
|
||||||
|
|
||||||
final devicesProvider = NotifierProvider<DevicesNotifier, List<dynamic>>(
|
final devicesProvider =
|
||||||
|
NotifierProvider<DevicesNotifier, LoadState<List<dynamic>>>(
|
||||||
() => DevicesNotifier(),
|
() => DevicesNotifier(),
|
||||||
);
|
);
|
||||||
|
|
||||||
class DevicesNotifier extends Notifier<List<dynamic>> {
|
class DevicesNotifier extends Notifier<LoadState<List<dynamic>>> {
|
||||||
@override
|
@override
|
||||||
List<dynamic> build() => [];
|
LoadState<List<dynamic>> build() => const LoadState.idle(<dynamic>[]);
|
||||||
|
|
||||||
/// Загрузить список устройств из текущего дома
|
/// Загрузить список устройств из текущего дома
|
||||||
Future<void> load() async {
|
Future<void> load() async {
|
||||||
|
state = LoadState.loading(state.data);
|
||||||
try {
|
try {
|
||||||
final api = ref.read(apiProvider);
|
final api = ref.read(apiProvider);
|
||||||
final res = await api.getDevices();
|
final res = await api.getDevices();
|
||||||
if (res.data is List) {
|
final data = res.data;
|
||||||
state = res.data;
|
late final List<dynamic> devices;
|
||||||
} else if (res.data is Map) {
|
if (data is List) {
|
||||||
state =
|
devices = List<dynamic>.from(data);
|
||||||
res.data['data'] ?? res.data['devices'] ?? res.data.values.toList();
|
} else if (data is Map) {
|
||||||
|
final value = data['data'] ?? data['devices'] ?? data.values.toList();
|
||||||
|
if (value is! List) {
|
||||||
|
throw FormatException('devices должен быть списком устройств');
|
||||||
}
|
}
|
||||||
|
devices = List<dynamic>.from(value);
|
||||||
|
} else {
|
||||||
|
throw FormatException('devices должен быть списком устройств');
|
||||||
|
}
|
||||||
|
|
||||||
|
state = devices.isEmpty
|
||||||
|
? LoadState.empty(devices)
|
||||||
|
: LoadState.data(devices);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debugPrint("Ошибка загрузки устройств: $e");
|
state = LoadState.error(state.data, describeLoadError(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ─── Сцены ───────────────────────────────────────────────────
|
// ─── Сцены ───────────────────────────────────────────────────
|
||||||
|
|
||||||
final scenesProvider = NotifierProvider<ScenesNotifier, List<dynamic>>(
|
final scenesProvider =
|
||||||
|
NotifierProvider<ScenesNotifier, LoadState<List<dynamic>>>(
|
||||||
() => ScenesNotifier(),
|
() => ScenesNotifier(),
|
||||||
);
|
);
|
||||||
|
|
||||||
class ScenesNotifier extends Notifier<List<dynamic>> {
|
class ScenesNotifier extends Notifier<LoadState<List<dynamic>>> {
|
||||||
@override
|
@override
|
||||||
List<dynamic> build() => [];
|
LoadState<List<dynamic>> build() => const LoadState.idle(<dynamic>[]);
|
||||||
|
|
||||||
Future<void> load() async {
|
Future<void> load() async {
|
||||||
|
state = LoadState.loading(state.data);
|
||||||
try {
|
try {
|
||||||
final api = ref.read(apiProvider);
|
final api = ref.read(apiProvider);
|
||||||
final res = await api.getScenes();
|
final res = await api.getScenes();
|
||||||
final data = res.data;
|
final data = res.data;
|
||||||
|
late final List<dynamic> scenes;
|
||||||
if (data is List) {
|
if (data is List) {
|
||||||
state = data;
|
scenes = List<dynamic>.from(data);
|
||||||
} else if (data is Map) {
|
} else if (data is Map) {
|
||||||
// Бэкенд может вернуть {scene_id: "Scene Name", ...}
|
// Бэкенд может вернуть {scene_id: "Scene Name", ...}
|
||||||
// или {data: [...]} или {scenes: [...]}
|
// или {data: [...]} или {scenes: [...]}
|
||||||
if (data.containsKey('data') && data['data'] is List) {
|
if (data.containsKey('data')) {
|
||||||
state = data['data'];
|
final value = data['data'];
|
||||||
} else if (data.containsKey('scenes') && data['scenes'] is List) {
|
if (value is! List) {
|
||||||
state = data['scenes'];
|
throw FormatException('scenes.data должен быть списком сцен');
|
||||||
|
}
|
||||||
|
scenes = List<dynamic>.from(value);
|
||||||
|
} else if (data.containsKey('scenes')) {
|
||||||
|
final value = data['scenes'];
|
||||||
|
if (value is! List) {
|
||||||
|
throw FormatException('scenes должен быть списком сцен');
|
||||||
|
}
|
||||||
|
scenes = List<dynamic>.from(value);
|
||||||
} else {
|
} else {
|
||||||
// Map вида {id: name} -- преобразуем в список
|
// Map вида {id: name} -- преобразуем в список
|
||||||
state = data.entries
|
scenes = data.entries
|
||||||
.map((e) => {'id': e.key.toString(), 'name': e.value.toString()})
|
.map((e) => {'id': e.key.toString(), 'name': e.value.toString()})
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
throw FormatException('scenes должен быть списком сцен');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
state = scenes.isEmpty ? LoadState.empty(scenes) : LoadState.data(scenes);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
debugPrint("Ошибка загрузки сцен: $e");
|
state = LoadState.error(state.data, describeLoadError(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
import '../app/error_message.dart';
|
||||||
|
import '../app/load_state.dart';
|
||||||
import '../providers/providers.dart';
|
import '../providers/providers.dart';
|
||||||
|
import '../widgets/load_error_view.dart';
|
||||||
|
|
||||||
/// Экран создания новой группы ламп.
|
/// Экран создания новой группы ламп.
|
||||||
/// Загружает список устройств, позволяет выбрать нужные.
|
/// Загружает список устройств, позволяет выбрать нужные.
|
||||||
@@ -15,7 +18,6 @@ class _GroupEditScreenState extends ConsumerState<GroupEditScreen> {
|
|||||||
final _idCtrl = TextEditingController();
|
final _idCtrl = TextEditingController();
|
||||||
final _nameCtrl = TextEditingController();
|
final _nameCtrl = TextEditingController();
|
||||||
final Set<String> _selectedMacs = {};
|
final Set<String> _selectedMacs = {};
|
||||||
bool _loading = true;
|
|
||||||
bool _saving = false;
|
bool _saving = false;
|
||||||
bool _rescanning = false;
|
bool _rescanning = false;
|
||||||
|
|
||||||
@@ -34,7 +36,6 @@ class _GroupEditScreenState extends ConsumerState<GroupEditScreen> {
|
|||||||
|
|
||||||
Future<void> _loadDevices() async {
|
Future<void> _loadDevices() async {
|
||||||
await ref.read(devicesProvider.notifier).load();
|
await ref.read(devicesProvider.notifier).load();
|
||||||
if (mounted) setState(() => _loading = false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Пересканировать сеть и перезагрузить устройства
|
/// Пересканировать сеть и перезагрузить устройства
|
||||||
@@ -47,17 +48,21 @@ class _GroupEditScreenState extends ConsumerState<GroupEditScreen> {
|
|||||||
await ref.read(devicesProvider.notifier).load();
|
await ref.read(devicesProvider.notifier).load();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
ScaffoldMessenger.of(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
context,
|
SnackBar(
|
||||||
).showSnackBar(SnackBar(content: Text('Ошибка сканирования: $e')));
|
content: Text('Ошибка сканирования: ${describeLoadError(e)}'),
|
||||||
}
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
if (mounted) setState(() => _rescanning = false);
|
if (mounted) setState(() => _rescanning = false);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final devices = ref.watch(devicesProvider);
|
final devicesState = ref.watch(devicesProvider);
|
||||||
|
final devices = devicesState.data;
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
@@ -77,11 +82,7 @@ class _GroupEditScreenState extends ConsumerState<GroupEditScreen> {
|
|||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
body: _loading
|
body: Padding(
|
||||||
? const Center(
|
|
||||||
child: CircularProgressIndicator(color: Colors.deepOrange),
|
|
||||||
)
|
|
||||||
: Padding(
|
|
||||||
padding: const EdgeInsets.all(16),
|
padding: const EdgeInsets.all(16),
|
||||||
child: Column(
|
child: Column(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
@@ -120,7 +121,9 @@ class _GroupEditScreenState extends ConsumerState<GroupEditScreen> {
|
|||||||
),
|
),
|
||||||
),
|
),
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () {
|
onPressed: devices.isEmpty
|
||||||
|
? null
|
||||||
|
: () {
|
||||||
setState(() {
|
setState(() {
|
||||||
if (_selectedMacs.length == devices.length) {
|
if (_selectedMacs.length == devices.length) {
|
||||||
_selectedMacs.clear();
|
_selectedMacs.clear();
|
||||||
@@ -143,48 +146,7 @@ class _GroupEditScreenState extends ConsumerState<GroupEditScreen> {
|
|||||||
),
|
),
|
||||||
|
|
||||||
// Список устройств
|
// Список устройств
|
||||||
Expanded(
|
Expanded(child: _buildDevices(devicesState, devices)),
|
||||||
child: devices.isEmpty
|
|
||||||
? const Center(
|
|
||||||
child: Text(
|
|
||||||
'Устройства не найдены.\nПопробуйте пересканировать сеть.',
|
|
||||||
textAlign: TextAlign.center,
|
|
||||||
style: TextStyle(color: Colors.white38),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
: ListView.builder(
|
|
||||||
itemCount: devices.length,
|
|
||||||
itemBuilder: (context, index) {
|
|
||||||
final d = devices[index];
|
|
||||||
final mac = _extractMac(d) ?? '';
|
|
||||||
final name = _extractName(d);
|
|
||||||
final ip = _extractIp(d);
|
|
||||||
final selected = _selectedMacs.contains(mac);
|
|
||||||
|
|
||||||
return CheckboxListTile(
|
|
||||||
value: selected,
|
|
||||||
activeColor: Colors.deepOrange,
|
|
||||||
title: Text(name),
|
|
||||||
subtitle: Text(
|
|
||||||
'$mac${ip != null ? ' - $ip' : ''}',
|
|
||||||
style: const TextStyle(
|
|
||||||
fontSize: 11,
|
|
||||||
color: Colors.white38,
|
|
||||||
),
|
|
||||||
),
|
|
||||||
onChanged: (v) {
|
|
||||||
setState(() {
|
|
||||||
if (v == true) {
|
|
||||||
_selectedMacs.add(mac);
|
|
||||||
} else {
|
|
||||||
_selectedMacs.remove(mac);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
);
|
|
||||||
},
|
|
||||||
),
|
|
||||||
),
|
|
||||||
|
|
||||||
// Кнопка сохранения
|
// Кнопка сохранения
|
||||||
Padding(
|
Padding(
|
||||||
@@ -219,6 +181,85 @@ class _GroupEditScreenState extends ConsumerState<GroupEditScreen> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Widget _buildDevices(
|
||||||
|
LoadState<List<dynamic>> devicesState,
|
||||||
|
List<dynamic> devices,
|
||||||
|
) {
|
||||||
|
if ((devicesState.isIdle || devicesState.isLoading) && devices.isEmpty) {
|
||||||
|
return const Center(
|
||||||
|
child: CircularProgressIndicator(color: Colors.deepOrange),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (devicesState.hasError && devices.isEmpty) {
|
||||||
|
return LoadErrorView(
|
||||||
|
title: 'Не удалось загрузить устройства',
|
||||||
|
message: devicesState.errorMessage,
|
||||||
|
icon: Icons.lightbulb_outline,
|
||||||
|
onRetry: _loadDevices,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (devices.isEmpty) {
|
||||||
|
return const Center(
|
||||||
|
child: Text(
|
||||||
|
'Устройства не найдены.\nПопробуйте пересканировать сеть.',
|
||||||
|
textAlign: TextAlign.center,
|
||||||
|
style: TextStyle(color: Colors.white38),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final hasStatusHeader = devicesState.isLoading || devicesState.hasError;
|
||||||
|
final statusHeaderCount = hasStatusHeader ? 1 : 0;
|
||||||
|
|
||||||
|
return ListView.builder(
|
||||||
|
itemCount: devices.length + statusHeaderCount,
|
||||||
|
itemBuilder: (context, index) {
|
||||||
|
if (hasStatusHeader && index == 0) {
|
||||||
|
if (devicesState.isLoading) {
|
||||||
|
return const Padding(
|
||||||
|
padding: EdgeInsets.only(bottom: 12),
|
||||||
|
child: LinearProgressIndicator(color: Colors.deepOrange),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return LoadErrorBanner(
|
||||||
|
title: 'Не удалось обновить устройства',
|
||||||
|
message: devicesState.errorMessage,
|
||||||
|
onRetry: _loadDevices,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
final deviceIndex = index - statusHeaderCount;
|
||||||
|
final d = devices[deviceIndex];
|
||||||
|
final mac = _extractMac(d) ?? '';
|
||||||
|
final name = _extractName(d);
|
||||||
|
final ip = _extractIp(d);
|
||||||
|
final selected = _selectedMacs.contains(mac);
|
||||||
|
|
||||||
|
return CheckboxListTile(
|
||||||
|
value: selected,
|
||||||
|
activeColor: Colors.deepOrange,
|
||||||
|
title: Text(name),
|
||||||
|
subtitle: Text(
|
||||||
|
'$mac${ip != null ? ' - $ip' : ''}',
|
||||||
|
style: const TextStyle(fontSize: 11, color: Colors.white38),
|
||||||
|
),
|
||||||
|
onChanged: (v) {
|
||||||
|
setState(() {
|
||||||
|
if (v == true) {
|
||||||
|
_selectedMacs.add(mac);
|
||||||
|
} else {
|
||||||
|
_selectedMacs.remove(mac);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/// Извлечь MAC-адрес из объекта устройства
|
/// Извлечь MAC-адрес из объекта устройства
|
||||||
String? _extractMac(dynamic device) {
|
String? _extractMac(dynamic device) {
|
||||||
if (device is Map) {
|
if (device is Map) {
|
||||||
@@ -272,9 +313,9 @@ class _GroupEditScreenState extends ConsumerState<GroupEditScreen> {
|
|||||||
if (mounted) Navigator.of(context).pop();
|
if (mounted) Navigator.of(context).pop();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (mounted) {
|
if (mounted) {
|
||||||
ScaffoldMessenger.of(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
context,
|
SnackBar(content: Text('Ошибка создания: ${describeLoadError(e)}')),
|
||||||
).showSnackBar(SnackBar(content: Text('Ошибка создания: $e')));
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||||
|
import '../app/error_message.dart';
|
||||||
import '../providers/providers.dart';
|
import '../providers/providers.dart';
|
||||||
import 'color_picker.dart';
|
import 'color_picker.dart';
|
||||||
|
|
||||||
@@ -90,14 +91,12 @@ class _GroupCardState extends ConsumerState<GroupCard> {
|
|||||||
color: Colors.white38,
|
color: Colors.white38,
|
||||||
),
|
),
|
||||||
tooltip: 'Включить на 4 часа',
|
tooltip: 'Включить на 4 часа',
|
||||||
onPressed: () =>
|
onPressed: () => _setTimer4h(id),
|
||||||
ref.read(groupsProvider.notifier).setTimer4h(id),
|
|
||||||
),
|
),
|
||||||
Switch(
|
Switch(
|
||||||
value: isOn,
|
value: isOn,
|
||||||
activeThumbColor: Colors.deepOrange,
|
activeThumbColor: Colors.deepOrange,
|
||||||
onChanged: (v) =>
|
onChanged: (v) => _toggleGroup(id, v),
|
||||||
ref.read(groupsProvider.notifier).toggleGroup(id, v),
|
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
@@ -204,6 +203,32 @@ class _GroupCardState extends ConsumerState<GroupCard> {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _toggleGroup(String id, bool value) async {
|
||||||
|
try {
|
||||||
|
await ref.read(groupsProvider.notifier).toggleGroup(id, value);
|
||||||
|
} catch (e) {
|
||||||
|
if (!mounted) return;
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text('Ошибка управления группой: ${describeLoadError(e)}'),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _setTimer4h(String id) async {
|
||||||
|
try {
|
||||||
|
await ref.read(groupsProvider.notifier).setTimer4h(id);
|
||||||
|
} catch (e) {
|
||||||
|
if (!mounted) return;
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text('Ошибка создания таймера: ${describeLoadError(e)}'),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Слайдер с иконкой и подписью
|
/// Слайдер с иконкой и подписью
|
||||||
@@ -324,16 +349,18 @@ class _SceneSelector extends ConsumerStatefulWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class _SceneSelectorState extends ConsumerState<_SceneSelector> {
|
class _SceneSelectorState extends ConsumerState<_SceneSelector> {
|
||||||
bool _loadStarted = false;
|
@override
|
||||||
|
void initState() {
|
||||||
|
super.initState();
|
||||||
|
Future.microtask(() => ref.read(scenesProvider.notifier).load());
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
final scenes = ref.watch(scenesProvider);
|
final scenesState = ref.watch(scenesProvider);
|
||||||
|
final scenes = scenesState.data;
|
||||||
|
|
||||||
if (scenes.isEmpty && !_loadStarted) {
|
if ((scenesState.isIdle || scenesState.isLoading) && scenes.isEmpty) {
|
||||||
// Загрузить сцены при первом показе
|
|
||||||
_loadStarted = true;
|
|
||||||
Future.microtask(() => ref.read(scenesProvider.notifier).load());
|
|
||||||
return const Padding(
|
return const Padding(
|
||||||
padding: EdgeInsets.all(8.0),
|
padding: EdgeInsets.all(8.0),
|
||||||
child: Center(
|
child: Center(
|
||||||
@@ -346,6 +373,34 @@ class _SceneSelectorState extends ConsumerState<_SceneSelector> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (scenesState.hasError && scenes.isEmpty) {
|
||||||
|
return Padding(
|
||||||
|
padding: const EdgeInsets.all(8.0),
|
||||||
|
child: Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
Text(
|
||||||
|
'Не удалось загрузить сцены',
|
||||||
|
style: const TextStyle(color: Colors.white54, fontSize: 12),
|
||||||
|
),
|
||||||
|
if (scenesState.errorMessage != null)
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.only(top: 4),
|
||||||
|
child: Text(
|
||||||
|
scenesState.errorMessage!,
|
||||||
|
style: const TextStyle(color: Colors.white30, fontSize: 11),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
TextButton.icon(
|
||||||
|
onPressed: _loadScenes,
|
||||||
|
icon: const Icon(Icons.refresh, size: 16),
|
||||||
|
label: const Text('Повторить'),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
if (scenes.isEmpty) {
|
if (scenes.isEmpty) {
|
||||||
return const Padding(
|
return const Padding(
|
||||||
padding: EdgeInsets.all(8.0),
|
padding: EdgeInsets.all(8.0),
|
||||||
@@ -356,7 +411,35 @@ class _SceneSelectorState extends ConsumerState<_SceneSelector> {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return Wrap(
|
return Column(
|
||||||
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
if (scenesState.hasError)
|
||||||
|
Padding(
|
||||||
|
padding: const EdgeInsets.only(bottom: 8),
|
||||||
|
child: Row(
|
||||||
|
children: [
|
||||||
|
const Icon(
|
||||||
|
Icons.warning_amber_rounded,
|
||||||
|
size: 16,
|
||||||
|
color: Colors.deepOrange,
|
||||||
|
),
|
||||||
|
const SizedBox(width: 6),
|
||||||
|
const Expanded(
|
||||||
|
child: Text(
|
||||||
|
'Сцены не обновились',
|
||||||
|
style: TextStyle(color: Colors.white38, fontSize: 12),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
IconButton(
|
||||||
|
tooltip: 'Повторить',
|
||||||
|
onPressed: _loadScenes,
|
||||||
|
icon: const Icon(Icons.refresh, size: 16),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
Wrap(
|
||||||
spacing: 8,
|
spacing: 8,
|
||||||
runSpacing: 4,
|
runSpacing: 4,
|
||||||
children: scenes.map((scene) {
|
children: scenes.map((scene) {
|
||||||
@@ -379,11 +462,24 @@ class _SceneSelectorState extends ConsumerState<_SceneSelector> {
|
|||||||
return ActionChip(
|
return ActionChip(
|
||||||
label: Text(sceneName, style: const TextStyle(fontSize: 12)),
|
label: Text(sceneName, style: const TextStyle(fontSize: 12)),
|
||||||
backgroundColor: Colors.white10,
|
backgroundColor: Colors.white10,
|
||||||
onPressed: () => ref
|
onPressed: () => _setScene(sceneId),
|
||||||
.read(groupsProvider.notifier)
|
|
||||||
.setScene(widget.groupId, sceneId),
|
|
||||||
);
|
);
|
||||||
}).toList(),
|
}).toList(),
|
||||||
|
),
|
||||||
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _loadScenes() => ref.read(scenesProvider.notifier).load();
|
||||||
|
|
||||||
|
Future<void> _setScene(String sceneId) async {
|
||||||
|
try {
|
||||||
|
await ref.read(groupsProvider.notifier).setScene(widget.groupId, sceneId);
|
||||||
|
} catch (e) {
|
||||||
|
if (!mounted) return;
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(content: Text('Ошибка сцены: ${describeLoadError(e)}')),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,28 +6,77 @@ import 'package:ignis_app/providers/providers.dart';
|
|||||||
import 'package:ignis_app/services/api_client.dart';
|
import 'package:ignis_app/services/api_client.dart';
|
||||||
|
|
||||||
class FakeIgnisApi extends IgnisApi {
|
class FakeIgnisApi extends IgnisApi {
|
||||||
|
Object? devicesData;
|
||||||
|
Object? scenesData;
|
||||||
Object? tasksData;
|
Object? tasksData;
|
||||||
Object? statsData;
|
Object? statsData;
|
||||||
Object? eventLogData;
|
Object? eventLogData;
|
||||||
Object? apiKeysData;
|
Object? apiKeysData;
|
||||||
|
Object? devicesError;
|
||||||
|
Object? scenesError;
|
||||||
Object? tasksError;
|
Object? tasksError;
|
||||||
Object? statsError;
|
Object? statsError;
|
||||||
Object? eventLogError;
|
Object? eventLogError;
|
||||||
Object? apiKeysError;
|
Object? apiKeysError;
|
||||||
|
Object? controlGroupError;
|
||||||
Object? cancelTaskError;
|
Object? cancelTaskError;
|
||||||
Object? revokeApiKeyError;
|
Object? revokeApiKeyError;
|
||||||
|
String? controlledGroupId;
|
||||||
|
Map<String, dynamic>? controlGroupParams;
|
||||||
int? requestedDays;
|
int? requestedDays;
|
||||||
int? requestedLimit;
|
int? requestedLimit;
|
||||||
String? cancelledJobId;
|
String? cancelledJobId;
|
||||||
String? revokedApiKey;
|
String? revokedApiKey;
|
||||||
|
|
||||||
FakeIgnisApi({
|
FakeIgnisApi({
|
||||||
|
this.devicesData,
|
||||||
|
this.scenesData,
|
||||||
this.tasksData,
|
this.tasksData,
|
||||||
this.statsData,
|
this.statsData,
|
||||||
this.eventLogData,
|
this.eventLogData,
|
||||||
this.apiKeysData,
|
this.apiKeysData,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Response> getDevices() async {
|
||||||
|
final error = devicesError;
|
||||||
|
if (error != null) throw error;
|
||||||
|
return Response(
|
||||||
|
requestOptions: RequestOptions(path: '/devices'),
|
||||||
|
data: devicesData,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Response> getScenes() async {
|
||||||
|
final error = scenesError;
|
||||||
|
if (error != null) throw error;
|
||||||
|
return Response(
|
||||||
|
requestOptions: RequestOptions(path: '/devices/scenes'),
|
||||||
|
data: scenesData,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Response> getGroups() async {
|
||||||
|
return Response(
|
||||||
|
requestOptions: RequestOptions(path: '/devices/groups'),
|
||||||
|
data: <Object>[],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Future<Response> controlGroup(String id, Map<String, dynamic> params) async {
|
||||||
|
controlledGroupId = id;
|
||||||
|
controlGroupParams = params;
|
||||||
|
final error = controlGroupError;
|
||||||
|
if (error != null) throw error;
|
||||||
|
return Response(
|
||||||
|
requestOptions: RequestOptions(path: '/control/group/$id'),
|
||||||
|
data: <String, dynamic>{'ok': true},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<Response> getTasks() async {
|
Future<Response> getTasks() async {
|
||||||
final error = tasksError;
|
final error = tasksError;
|
||||||
@@ -120,6 +169,57 @@ void main() {
|
|||||||
expect(api.requestedDays, 14);
|
expect(api.requestedDays, 14);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('devices load exposes data state', () async {
|
||||||
|
final api = FakeIgnisApi(
|
||||||
|
devicesData: {
|
||||||
|
'devices': [
|
||||||
|
{'mac': 'AA:BB', 'name': 'Kitchen bulb'},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
);
|
||||||
|
final container = containerWith(api);
|
||||||
|
|
||||||
|
await container.read(devicesProvider.notifier).load();
|
||||||
|
|
||||||
|
final state = container.read(devicesProvider);
|
||||||
|
expect(state.status, LoadStatus.data);
|
||||||
|
expect(state.data, hasLength(1));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('devices load exposes empty state', () async {
|
||||||
|
final api = FakeIgnisApi(devicesData: {'devices': <Object>[]});
|
||||||
|
final container = containerWith(api);
|
||||||
|
|
||||||
|
await container.read(devicesProvider.notifier).load();
|
||||||
|
|
||||||
|
final state = container.read(devicesProvider);
|
||||||
|
expect(state.status, LoadStatus.empty);
|
||||||
|
expect(state.data, isEmpty);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('devices load error exposes message', () async {
|
||||||
|
final api = FakeIgnisApi(
|
||||||
|
devicesData: [
|
||||||
|
{'mac': 'AA:BB'},
|
||||||
|
],
|
||||||
|
);
|
||||||
|
final container = containerWith(api);
|
||||||
|
|
||||||
|
await container.read(devicesProvider.notifier).load();
|
||||||
|
api.devicesError = DioException(
|
||||||
|
requestOptions: RequestOptions(path: '/devices'),
|
||||||
|
type: DioExceptionType.connectionError,
|
||||||
|
message: 'No route to host',
|
||||||
|
);
|
||||||
|
|
||||||
|
await container.read(devicesProvider.notifier).load();
|
||||||
|
|
||||||
|
final state = container.read(devicesProvider);
|
||||||
|
expect(state.status, LoadStatus.error);
|
||||||
|
expect(state.data, hasLength(1));
|
||||||
|
expect(state.errorMessage, contains('Backend недоступен'));
|
||||||
|
});
|
||||||
|
|
||||||
test('tasks load exposes data state', () async {
|
test('tasks load exposes data state', () async {
|
||||||
final api = FakeIgnisApi(
|
final api = FakeIgnisApi(
|
||||||
tasksData: {
|
tasksData: {
|
||||||
@@ -200,6 +300,48 @@ void main() {
|
|||||||
expect(api.requestedLimit, 50);
|
expect(api.requestedLimit, 50);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('scenes load accepts id-name map and exposes data state', () async {
|
||||||
|
final api = FakeIgnisApi(scenesData: {'party': 'Party', 'relax': 'Relax'});
|
||||||
|
final container = containerWith(api);
|
||||||
|
|
||||||
|
await container.read(scenesProvider.notifier).load();
|
||||||
|
|
||||||
|
final state = container.read(scenesProvider);
|
||||||
|
expect(state.status, LoadStatus.data);
|
||||||
|
expect(state.data, hasLength(2));
|
||||||
|
expect(state.data.first, containsPair('id', 'party'));
|
||||||
|
});
|
||||||
|
|
||||||
|
test('scenes load exposes empty state', () async {
|
||||||
|
final api = FakeIgnisApi(scenesData: {'scenes': <Object>[]});
|
||||||
|
final container = containerWith(api);
|
||||||
|
|
||||||
|
await container.read(scenesProvider.notifier).load();
|
||||||
|
|
||||||
|
final state = container.read(scenesProvider);
|
||||||
|
expect(state.status, LoadStatus.empty);
|
||||||
|
expect(state.data, isEmpty);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('scenes load error exposes message', () async {
|
||||||
|
final api = FakeIgnisApi(scenesData: ['Party']);
|
||||||
|
final container = containerWith(api);
|
||||||
|
|
||||||
|
await container.read(scenesProvider.notifier).load();
|
||||||
|
api.scenesError = DioException(
|
||||||
|
requestOptions: RequestOptions(path: '/devices/scenes'),
|
||||||
|
type: DioExceptionType.connectionError,
|
||||||
|
message: 'No route to host',
|
||||||
|
);
|
||||||
|
|
||||||
|
await container.read(scenesProvider.notifier).load();
|
||||||
|
|
||||||
|
final state = container.read(scenesProvider);
|
||||||
|
expect(state.status, LoadStatus.error);
|
||||||
|
expect(state.data, hasLength(1));
|
||||||
|
expect(state.errorMessage, contains('Backend недоступен'));
|
||||||
|
});
|
||||||
|
|
||||||
test('api keys load exposes data state', () async {
|
test('api keys load exposes data state', () async {
|
||||||
final api = FakeIgnisApi(
|
final api = FakeIgnisApi(
|
||||||
apiKeysData: {
|
apiKeysData: {
|
||||||
@@ -315,4 +457,25 @@ void main() {
|
|||||||
);
|
);
|
||||||
expect(api.revokedApiKey, 'secret');
|
expect(api.revokedApiKey, 'secret');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('set scene error is not swallowed', () async {
|
||||||
|
final api = FakeIgnisApi();
|
||||||
|
final container = containerWith(api);
|
||||||
|
final error = DioException(
|
||||||
|
requestOptions: RequestOptions(path: '/control/group/kitchen'),
|
||||||
|
type: DioExceptionType.badResponse,
|
||||||
|
response: Response(
|
||||||
|
requestOptions: RequestOptions(path: '/control/group/kitchen'),
|
||||||
|
statusCode: 500,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
api.controlGroupError = error;
|
||||||
|
|
||||||
|
await expectLater(
|
||||||
|
container.read(groupsProvider.notifier).setScene('kitchen', 'party'),
|
||||||
|
throwsA(same(error)),
|
||||||
|
);
|
||||||
|
expect(api.controlledGroupId, 'kitchen');
|
||||||
|
expect(api.controlGroupParams, containsPair('scene', 'party'));
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user