feat: surface device and scene load errors
This commit is contained in:
@@ -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);
|
||||
}
|
||||
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,50 +82,48 @@ class _GroupEditScreenState extends ConsumerState<GroupEditScreen> {
|
||||
),
|
||||
],
|
||||
),
|
||||
body: _loading
|
||||
? const Center(
|
||||
child: CircularProgressIndicator(color: Colors.deepOrange),
|
||||
)
|
||||
: 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),
|
||||
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),
|
||||
// Название группы
|
||||
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,
|
||||
),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
// Заголовок списка устройств
|
||||
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(() {
|
||||
if (_selectedMacs.length == devices.length) {
|
||||
_selectedMacs.clear();
|
||||
@@ -132,90 +135,128 @@ class _GroupEditScreenState extends ConsumerState<GroupEditScreen> {
|
||||
}
|
||||
});
|
||||
},
|
||||
child: Text(
|
||||
_selectedMacs.length == devices.length
|
||||
? 'Снять все'
|
||||
: 'Выбрать все',
|
||||
style: const TextStyle(fontSize: 12),
|
||||
),
|
||||
),
|
||||
],
|
||||
child: Text(
|
||||
_selectedMacs.length == devices.length
|
||||
? 'Снять все'
|
||||
: 'Выбрать все',
|
||||
style: const TextStyle(fontSize: 12),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
// Список устройств
|
||||
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);
|
||||
// Список устройств
|
||||
Expanded(child: _buildDevices(devicesState, devices)),
|
||||
|
||||
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: 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,
|
||||
),
|
||||
),
|
||||
|
||||
// Кнопка сохранения
|
||||
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('СОЗДАТЬ ГРУППУ'),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
)
|
||||
: const Text('СОЗДАТЬ ГРУППУ'),
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
});
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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)}')),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user