feat: implement Sensor detail screen

This commit is contained in:
2026-05-12 16:41:32 +02:00
parent ddb8bfb81e
commit f0a77a9708
+163 -43
View File
@@ -2,58 +2,178 @@ import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart'; import 'package:go_router/go_router.dart';
import '../../domain/models/sensor.dart';
import '../../providers.dart'; import '../../providers.dart';
class SensorDetailScreen extends ConsumerWidget { class SensorDetailScreen extends ConsumerStatefulWidget {
const SensorDetailScreen({super.key, required this.sensorId}); const SensorDetailScreen({super.key, required this.sensorId});
final String sensorId; final String sensorId;
@override @override
Widget build(BuildContext context, WidgetRef ref) { ConsumerState<SensorDetailScreen> createState() => _SensorDetailScreenState();
// TODO: fetch sensor via sensorRepositoryProvider.getSensor(sensorId). }
class _SensorDetailScreenState extends ConsumerState<SensorDetailScreen> {
late final int _id = int.parse(widget.sensorId);
@override
Widget build(BuildContext context) {
final sensor = ref.watch(sensorProvider(_id));
return Scaffold( return Scaffold(
appBar: AppBar(title: Text('Sensor $sensorId')), appBar: AppBar(
body: Padding( title: Text(sensor.valueOrNull?.displayName ?? 'Sensor'),
padding: const EdgeInsets.all(24), ),
child: Column( body: sensor.when(
crossAxisAlignment: CrossAxisAlignment.stretch, loading: () => const Center(child: CircularProgressIndicator()),
children: [ error: (e, _) => Center(child: Text(e.toString())),
// TODO: display sensor fields (name, status, position, last seen). data: (s) => _Body(sensor: s, id: _id, sensorId: widget.sensorId),
const Placeholder(fallbackHeight: 200), ),
const SizedBox(height: 24), );
OutlinedButton.icon( }
icon: const Icon(Icons.map_outlined), }
label: const Text('Locate on floor plan'),
onPressed: () { class _Body extends ConsumerWidget {
ref.read(selectedSensorIdProvider.notifier).state = sensorId; const _Body({required this.sensor, required this.id, required this.sensorId});
context.go('/floorplan');
}, final Sensor sensor;
), final int id;
const SizedBox(height: 12), final String sensorId;
// TODO: re-provision button → show BleProvisionSheet pre-filled.
OutlinedButton.icon( @override
icon: const Icon(Icons.bluetooth), Widget build(BuildContext context, WidgetRef ref) {
label: const Text('Re-provision WiFi'), return Padding(
onPressed: () {}, // TODO padding: const EdgeInsets.all(24),
), child: Column(
const SizedBox(height: 12), crossAxisAlignment: CrossAxisAlignment.stretch,
OutlinedButton.icon( children: [
icon: const Icon(Icons.edit_outlined), _InfoRow(label: 'Device ID', value: sensor.sensorId),
label: const Text('Rename'), if (sensor.rssiRef != null)
onPressed: () {}, // TODO _InfoRow(
), label: 'RSSI reference',
const Spacer(), value: '${sensor.rssiRef!.toStringAsFixed(1)} dBm',
TextButton( ),
style: TextButton.styleFrom( _InfoRow(
foregroundColor: Theme.of(context).colorScheme.error, label: 'Floor position',
), value: sensor.isPlaced
onPressed: () {}, // TODO: confirm dialog then delete ? '(${sensor.x!.toStringAsFixed(2)}, ${sensor.y!.toStringAsFixed(2)})'
child: const Text('Delete sensor'), : 'Not placed',
), ),
], const SizedBox(height: 24),
), OutlinedButton.icon(
icon: const Icon(Icons.map_outlined),
label: const Text('Locate on floor plan'),
onPressed: () {
ref.read(selectedSensorIdProvider.notifier).state = sensorId;
context.go('/floorplan');
},
),
const SizedBox(height: 12),
OutlinedButton.icon(
icon: const Icon(Icons.edit_outlined),
label: const Text('Rename'),
onPressed: () => _rename(context, ref),
),
const SizedBox(height: 12),
OutlinedButton.icon(
icon: const Icon(Icons.bluetooth),
label: const Text('Re-provision WiFi'),
onPressed: () {}, // TODO: show BleProvisionSheet pre-filled
),
const Spacer(),
TextButton(
style: TextButton.styleFrom(
foregroundColor: Theme.of(context).colorScheme.error,
),
onPressed: () => _delete(context, ref),
child: const Text('Delete sensor'),
),
],
),
);
}
Future<void> _rename(BuildContext context, WidgetRef ref) async {
final controller = TextEditingController(text: sensor.name);
final confirmed = await showDialog<bool>(
context: context,
builder: (ctx) => AlertDialog(
title: const Text('Rename sensor'),
content: TextField(
controller: controller,
decoration: const InputDecoration(labelText: 'Name'),
autofocus: true,
onSubmitted: (_) => Navigator.of(ctx).pop(true),
),
actions: [
TextButton(
onPressed: () => Navigator.of(ctx).pop(false),
child: const Text('Cancel'),
),
FilledButton(
onPressed: () => Navigator.of(ctx).pop(true),
child: const Text('Save'),
),
],
),
);
final name = controller.text.trim();
controller.dispose();
if (confirmed == true && name.isNotEmpty && context.mounted) {
await ref.read(sensorRepositoryProvider).updateSensor(id, name: name);
ref.invalidate(sensorProvider(id));
ref.invalidate(sensorsProvider);
}
}
Future<void> _delete(BuildContext context, WidgetRef ref) async {
final confirmed = await showDialog<bool>(
context: context,
builder: (ctx) => AlertDialog(
title: const Text('Delete sensor?'),
content: const Text('This will unenrol the sensor from the system.'),
actions: [
TextButton(
onPressed: () => Navigator.of(ctx).pop(false),
child: const Text('Cancel'),
),
FilledButton(
style: FilledButton.styleFrom(
backgroundColor: Theme.of(context).colorScheme.error,
),
onPressed: () => Navigator.of(ctx).pop(true),
child: const Text('Delete'),
),
],
),
);
if (confirmed == true && context.mounted) {
await ref.read(sensorRepositoryProvider).deleteSensor(id);
ref.invalidate(sensorsProvider);
if (context.mounted) context.pop();
}
}
}
class _InfoRow extends StatelessWidget {
const _InfoRow({required this.label, required this.value});
final String label;
final String value;
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Row(
children: [
Expanded(
child: Text(label,
style: Theme.of(context).textTheme.bodySmall),
),
Text(value, style: Theme.of(context).textTheme.bodyMedium),
],
), ),
); );
} }