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});
|
||||
} catch (e) {
|
||||
_lockUntil.remove(id);
|
||||
refresh();
|
||||
await refresh();
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -540,7 +541,8 @@ class GroupsNotifier extends Notifier<List<dynamic>> {
|
||||
await _api.controlGroup(id, {'scene': scene});
|
||||
} catch (e) {
|
||||
_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(),
|
||||
);
|
||||
|
||||
class DevicesNotifier extends Notifier<List<dynamic>> {
|
||||
class DevicesNotifier extends Notifier<LoadState<List<dynamic>>> {
|
||||
@override
|
||||
List<dynamic> build() => [];
|
||||
LoadState<List<dynamic>> build() => const LoadState.idle(<dynamic>[]);
|
||||
|
||||
/// Загрузить список устройств из текущего дома
|
||||
Future<void> load() async {
|
||||
state = LoadState.loading(state.data);
|
||||
try {
|
||||
final api = ref.read(apiProvider);
|
||||
final res = await api.getDevices();
|
||||
if (res.data is List) {
|
||||
state = res.data;
|
||||
} else if (res.data is Map) {
|
||||
state =
|
||||
res.data['data'] ?? res.data['devices'] ?? res.data.values.toList();
|
||||
final data = res.data;
|
||||
late final List<dynamic> devices;
|
||||
if (data is List) {
|
||||
devices = List<dynamic>.from(data);
|
||||
} 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) {
|
||||
debugPrint("Ошибка загрузки устройств: $e");
|
||||
state = LoadState.error(state.data, describeLoadError(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Сцены ───────────────────────────────────────────────────
|
||||
|
||||
final scenesProvider = NotifierProvider<ScenesNotifier, List<dynamic>>(
|
||||
final scenesProvider =
|
||||
NotifierProvider<ScenesNotifier, LoadState<List<dynamic>>>(
|
||||
() => ScenesNotifier(),
|
||||
);
|
||||
|
||||
class ScenesNotifier extends Notifier<List<dynamic>> {
|
||||
class ScenesNotifier extends Notifier<LoadState<List<dynamic>>> {
|
||||
@override
|
||||
List<dynamic> build() => [];
|
||||
LoadState<List<dynamic>> build() => const LoadState.idle(<dynamic>[]);
|
||||
|
||||
Future<void> load() async {
|
||||
state = LoadState.loading(state.data);
|
||||
try {
|
||||
final api = ref.read(apiProvider);
|
||||
final res = await api.getScenes();
|
||||
final data = res.data;
|
||||
late final List<dynamic> scenes;
|
||||
if (data is List) {
|
||||
state = data;
|
||||
scenes = List<dynamic>.from(data);
|
||||
} else if (data is Map) {
|
||||
// Бэкенд может вернуть {scene_id: "Scene Name", ...}
|
||||
// или {data: [...]} или {scenes: [...]}
|
||||
if (data.containsKey('data') && data['data'] is List) {
|
||||
state = data['data'];
|
||||
} else if (data.containsKey('scenes') && data['scenes'] is List) {
|
||||
state = data['scenes'];
|
||||
if (data.containsKey('data')) {
|
||||
final value = data['data'];
|
||||
if (value is! List) {
|
||||
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 {
|
||||
// Map вида {id: name} -- преобразуем в список
|
||||
state = data.entries
|
||||
scenes = data.entries
|
||||
.map((e) => {'id': e.key.toString(), 'name': e.value.toString()})
|
||||
.toList();
|
||||
}
|
||||
} else {
|
||||
throw FormatException('scenes должен быть списком сцен');
|
||||
}
|
||||
|
||||
state = scenes.isEmpty ? LoadState.empty(scenes) : LoadState.data(scenes);
|
||||
} catch (e) {
|
||||
debugPrint("Ошибка загрузки сцен: $e");
|
||||
state = LoadState.error(state.data, describeLoadError(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../app/error_message.dart';
|
||||
import '../app/load_state.dart';
|
||||
import '../providers/providers.dart';
|
||||
import '../widgets/load_error_view.dart';
|
||||
|
||||
/// Экран создания новой группы ламп.
|
||||
/// Загружает список устройств, позволяет выбрать нужные.
|
||||
@@ -15,7 +18,6 @@ class _GroupEditScreenState extends ConsumerState<GroupEditScreen> {
|
||||
final _idCtrl = TextEditingController();
|
||||
final _nameCtrl = TextEditingController();
|
||||
final Set<String> _selectedMacs = {};
|
||||
bool _loading = true;
|
||||
bool _saving = false;
|
||||
bool _rescanning = false;
|
||||
|
||||
@@ -34,7 +36,6 @@ class _GroupEditScreenState extends ConsumerState<GroupEditScreen> {
|
||||
|
||||
Future<void> _loadDevices() async {
|
||||
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();
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(SnackBar(content: Text('Ошибка сканирования: $e')));
|
||||
}
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('Ошибка сканирования: ${describeLoadError(e)}'),
|
||||
),
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
if (mounted) setState(() => _rescanning = false);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final devices = ref.watch(devicesProvider);
|
||||
final devicesState = ref.watch(devicesProvider);
|
||||
final devices = devicesState.data;
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
@@ -77,11 +82,7 @@ class _GroupEditScreenState extends ConsumerState<GroupEditScreen> {
|
||||
),
|
||||
],
|
||||
),
|
||||
body: _loading
|
||||
? const Center(
|
||||
child: CircularProgressIndicator(color: Colors.deepOrange),
|
||||
)
|
||||
: Padding(
|
||||
body: Padding(
|
||||
padding: const EdgeInsets.all(16),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
@@ -120,7 +121,9 @@ class _GroupEditScreenState extends ConsumerState<GroupEditScreen> {
|
||||
),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
onPressed: devices.isEmpty
|
||||
? null
|
||||
: () {
|
||||
setState(() {
|
||||
if (_selectedMacs.length == devices.length) {
|
||||
_selectedMacs.clear();
|
||||
@@ -143,48 +146,7 @@ class _GroupEditScreenState extends ConsumerState<GroupEditScreen> {
|
||||
),
|
||||
|
||||
// Список устройств
|
||||
Expanded(
|
||||
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);
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
Expanded(child: _buildDevices(devicesState, devices)),
|
||||
|
||||
// Кнопка сохранения
|
||||
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-адрес из объекта устройства
|
||||
String? _extractMac(dynamic device) {
|
||||
if (device is Map) {
|
||||
@@ -272,9 +313,9 @@ class _GroupEditScreenState extends ConsumerState<GroupEditScreen> {
|
||||
if (mounted) Navigator.of(context).pop();
|
||||
} catch (e) {
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(SnackBar(content: Text('Ошибка создания: $e')));
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text('Ошибка создания: ${describeLoadError(e)}')),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import '../app/error_message.dart';
|
||||
import '../providers/providers.dart';
|
||||
import 'color_picker.dart';
|
||||
|
||||
@@ -90,14 +91,12 @@ class _GroupCardState extends ConsumerState<GroupCard> {
|
||||
color: Colors.white38,
|
||||
),
|
||||
tooltip: 'Включить на 4 часа',
|
||||
onPressed: () =>
|
||||
ref.read(groupsProvider.notifier).setTimer4h(id),
|
||||
onPressed: () => _setTimer4h(id),
|
||||
),
|
||||
Switch(
|
||||
value: isOn,
|
||||
activeThumbColor: Colors.deepOrange,
|
||||
onChanged: (v) =>
|
||||
ref.read(groupsProvider.notifier).toggleGroup(id, v),
|
||||
onChanged: (v) => _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> {
|
||||
bool _loadStarted = false;
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
Future.microtask(() => ref.read(scenesProvider.notifier).load());
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final scenes = ref.watch(scenesProvider);
|
||||
final scenesState = ref.watch(scenesProvider);
|
||||
final scenes = scenesState.data;
|
||||
|
||||
if (scenes.isEmpty && !_loadStarted) {
|
||||
// Загрузить сцены при первом показе
|
||||
_loadStarted = true;
|
||||
Future.microtask(() => ref.read(scenesProvider.notifier).load());
|
||||
if ((scenesState.isIdle || scenesState.isLoading) && scenes.isEmpty) {
|
||||
return const Padding(
|
||||
padding: EdgeInsets.all(8.0),
|
||||
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) {
|
||||
return const Padding(
|
||||
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,
|
||||
runSpacing: 4,
|
||||
children: scenes.map((scene) {
|
||||
@@ -379,11 +462,24 @@ class _SceneSelectorState extends ConsumerState<_SceneSelector> {
|
||||
return ActionChip(
|
||||
label: Text(sceneName, style: const TextStyle(fontSize: 12)),
|
||||
backgroundColor: Colors.white10,
|
||||
onPressed: () => ref
|
||||
.read(groupsProvider.notifier)
|
||||
.setScene(widget.groupId, sceneId),
|
||||
onPressed: () => _setScene(sceneId),
|
||||
);
|
||||
}).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';
|
||||
|
||||
class FakeIgnisApi extends IgnisApi {
|
||||
Object? devicesData;
|
||||
Object? scenesData;
|
||||
Object? tasksData;
|
||||
Object? statsData;
|
||||
Object? eventLogData;
|
||||
Object? apiKeysData;
|
||||
Object? devicesError;
|
||||
Object? scenesError;
|
||||
Object? tasksError;
|
||||
Object? statsError;
|
||||
Object? eventLogError;
|
||||
Object? apiKeysError;
|
||||
Object? controlGroupError;
|
||||
Object? cancelTaskError;
|
||||
Object? revokeApiKeyError;
|
||||
String? controlledGroupId;
|
||||
Map<String, dynamic>? controlGroupParams;
|
||||
int? requestedDays;
|
||||
int? requestedLimit;
|
||||
String? cancelledJobId;
|
||||
String? revokedApiKey;
|
||||
|
||||
FakeIgnisApi({
|
||||
this.devicesData,
|
||||
this.scenesData,
|
||||
this.tasksData,
|
||||
this.statsData,
|
||||
this.eventLogData,
|
||||
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
|
||||
Future<Response> getTasks() async {
|
||||
final error = tasksError;
|
||||
@@ -120,6 +169,57 @@ void main() {
|
||||
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 {
|
||||
final api = FakeIgnisApi(
|
||||
tasksData: {
|
||||
@@ -200,6 +300,48 @@ void main() {
|
||||
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 {
|
||||
final api = FakeIgnisApi(
|
||||
apiKeysData: {
|
||||
@@ -315,4 +457,25 @@ void main() {
|
||||
);
|
||||
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