feat: move sensor details to sheet, implement placement on floor plan
This commit is contained in:
@@ -15,7 +15,7 @@ class Sensor {
|
||||
final double? x; // room relative
|
||||
final double? y; // room relative
|
||||
|
||||
bool get isPlaced => roomId != null;
|
||||
bool get isPlaced => roomId != null && x != null && y != null;
|
||||
String get displayName => name ?? sensorId;
|
||||
|
||||
factory Sensor.fromJson(Map<String, dynamic> json) => Sensor(
|
||||
|
||||
@@ -6,6 +6,7 @@ import '../../domain/models/floor_plan_mode.dart';
|
||||
import '../../domain/models/sensor.dart';
|
||||
import '../../providers.dart';
|
||||
import '../ble_provision/ble_provision_sheet.dart';
|
||||
import '../sensors/sensor_detail_sheet.dart';
|
||||
import 'widgets/floor_plan_editor.dart';
|
||||
import 'widgets/room_edit_sheet.dart';
|
||||
|
||||
@@ -34,11 +35,39 @@ class _FloorPlanScreenState extends ConsumerState<FloorPlanScreen> {
|
||||
.then((sensors) => _editorKey.currentState?.loadSensors(sensors));
|
||||
}
|
||||
|
||||
Future<void> _confirmPlacement(Sensor sensor) async {
|
||||
final result =
|
||||
await _editorKey.currentState?.getPositionAtCenter();
|
||||
if (!mounted) return;
|
||||
if (result == null || result.roomId == null) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
const SnackBar(
|
||||
content: Text('Move the map so the dot is over a room'),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
await ref.read(sensorRepositoryProvider).placeSensor(
|
||||
sensor.id,
|
||||
roomId: result.roomId!,
|
||||
x: result.x!,
|
||||
y: result.y!,
|
||||
);
|
||||
ref.read(sensorPlacementProvider.notifier).state = null;
|
||||
ref.invalidate(sensorsProvider);
|
||||
ref.invalidate(sensorProvider(sensor.id));
|
||||
}
|
||||
|
||||
void _cancelPlacement() {
|
||||
ref.read(sensorPlacementProvider.notifier).state = null;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final mode = ref.watch(floorPlanModeProvider);
|
||||
final roomsAsync = ref.watch(roomsProvider);
|
||||
final sensorsAsync = ref.watch(sensorsProvider);
|
||||
final placingSensor = ref.watch(sensorPlacementProvider);
|
||||
|
||||
ref.listen(roomsProvider, (_, next) {
|
||||
next.whenData((r) => _editorKey.currentState?.loadFloorPlan(r));
|
||||
@@ -61,104 +90,144 @@ class _FloorPlanScreenState extends ConsumerState<FloorPlanScreen> {
|
||||
.toList() ??
|
||||
[];
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Floor Plan'),
|
||||
actions: [
|
||||
IconButton(
|
||||
tooltip: mode == FloorPlanMode.edit ? 'View mode' : 'Edit mode',
|
||||
icon: Icon(
|
||||
mode == FloorPlanMode.edit ? Icons.visibility : Icons.edit,
|
||||
),
|
||||
onPressed: () {
|
||||
final next = mode == FloorPlanMode.edit
|
||||
? FloorPlanMode.view
|
||||
: FloorPlanMode.edit;
|
||||
ref.read(floorPlanModeProvider.notifier).state = next;
|
||||
_editorKey.currentState?.setMode(next);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Stack(
|
||||
children: [
|
||||
FloorPlanEditor(
|
||||
key: _editorKey,
|
||||
mode: mode,
|
||||
onSensorTapped: (id) {
|
||||
ref.read(selectedSensorIdProvider.notifier).state = id;
|
||||
context.push('/sensors/$id');
|
||||
},
|
||||
onSensorMoved: (sensorId, roomId, x, y) async {
|
||||
await ref.read(sensorRepositoryProvider).placeSensor(
|
||||
int.parse(sensorId),
|
||||
roomId: roomId,
|
||||
x: x,
|
||||
y: y,
|
||||
);
|
||||
ref.invalidate(sensorsProvider);
|
||||
},
|
||||
onRoomsUpdated: (roomUpdates) async {
|
||||
final floor = ref.read(floorProvider).valueOrNull;
|
||||
if (floor == null) return;
|
||||
final repo = ref.read(floorRepositoryProvider);
|
||||
for (final r in roomUpdates) {
|
||||
await repo.updateRoom(floor.id, r.id, x: r.x, y: r.y);
|
||||
}
|
||||
// Do NOT invalidate roomsProvider here: the JS canvas is
|
||||
// already showing the correct positions, and re-fetching
|
||||
// would replace rooms[] with a fresh array, orphaning the
|
||||
// drag-closure references and causing snap-back.
|
||||
},
|
||||
onRoomAdded: (x, y) => _showAddRoomDialog(x, y),
|
||||
onEditRoomTapped: _showRoomEditSheet,
|
||||
onEditSensorTapped: (sensorId) =>
|
||||
context.push('/floorplan/sensors/$sensorId'),
|
||||
onAddSensorTapped: () => showModalBottomSheet<void>(
|
||||
context: context,
|
||||
isScrollControlled: true,
|
||||
useRootNavigator: true,
|
||||
builder: (_) => const BleProvisionSheet(),
|
||||
).then((_) {
|
||||
ref.invalidate(sensorsProvider);
|
||||
}),
|
||||
return PopScope(
|
||||
// While placing, intercept back: cancel placement and go to sensors list.
|
||||
canPop: placingSensor == null,
|
||||
onPopInvokedWithResult: (didPop, _) {
|
||||
if (!didPop && placingSensor != null) {
|
||||
_cancelPlacement();
|
||||
context.go('/sensors');
|
||||
}
|
||||
},
|
||||
child: Scaffold(
|
||||
appBar: AppBar(
|
||||
title: const Text('Floor Plan'),
|
||||
actions: [
|
||||
if (placingSensor == null)
|
||||
IconButton(
|
||||
tooltip:
|
||||
mode == FloorPlanMode.edit ? 'View mode' : 'Edit mode',
|
||||
icon: Icon(
|
||||
mode == FloorPlanMode.edit
|
||||
? Icons.visibility
|
||||
: Icons.edit,
|
||||
),
|
||||
if (roomsAsync.valueOrNull?.isEmpty ?? false)
|
||||
Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.map_outlined,
|
||||
size: 64, color: Colors.grey.shade400),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
'No rooms yet',
|
||||
style: Theme.of(context)
|
||||
.textTheme
|
||||
.titleMedium
|
||||
?.copyWith(color: Colors.grey.shade600),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'Switch to edit mode and tap + to add a room',
|
||||
style: Theme.of(context)
|
||||
.textTheme
|
||||
.bodySmall
|
||||
?.copyWith(color: Colors.grey.shade500),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
onPressed: () {
|
||||
final next = mode == FloorPlanMode.edit
|
||||
? FloorPlanMode.view
|
||||
: FloorPlanMode.edit;
|
||||
ref.read(floorPlanModeProvider.notifier).state = next;
|
||||
_editorKey.currentState?.setMode(next);
|
||||
},
|
||||
),
|
||||
],
|
||||
),
|
||||
body: Column(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Stack(
|
||||
children: [
|
||||
FloorPlanEditor(
|
||||
key: _editorKey,
|
||||
mode: mode,
|
||||
onSensorTapped: (id) {
|
||||
ref.read(selectedSensorIdProvider.notifier).state =
|
||||
id;
|
||||
showSensorDetailSheet(context, int.parse(id));
|
||||
},
|
||||
onSensorMoved: (sensorId, roomId, x, y) async {
|
||||
final id = int.parse(sensorId);
|
||||
await ref
|
||||
.read(sensorRepositoryProvider)
|
||||
.placeSensor(
|
||||
id,
|
||||
roomId: roomId,
|
||||
x: x,
|
||||
y: y,
|
||||
);
|
||||
ref.invalidate(sensorsProvider);
|
||||
ref.invalidate(sensorProvider(id));
|
||||
},
|
||||
onRoomsUpdated: (roomUpdates) async {
|
||||
final floor = ref.read(floorProvider).valueOrNull;
|
||||
if (floor == null) return;
|
||||
final repo = ref.read(floorRepositoryProvider);
|
||||
for (final r in roomUpdates) {
|
||||
await repo.updateRoom(floor.id, r.id,
|
||||
x: r.x, y: r.y);
|
||||
}
|
||||
// Do NOT invalidate roomsProvider here: the JS canvas is
|
||||
// already showing the correct positions, and re-fetching
|
||||
// would replace rooms[] with a fresh array, orphaning the
|
||||
// drag-closure references and causing snap-back.
|
||||
},
|
||||
onRoomAdded: (x, y) => _showAddRoomDialog(x, y),
|
||||
onEditRoomTapped: _showRoomEditSheet,
|
||||
onEditSensorTapped: (id) =>
|
||||
showSensorDetailSheet(context, int.parse(id)),
|
||||
onAddSensorTapped: () => showModalBottomSheet<void>(
|
||||
context: context,
|
||||
isScrollControlled: true,
|
||||
useRootNavigator: true,
|
||||
builder: (_) => const BleProvisionSheet(),
|
||||
).then((_) {
|
||||
ref.invalidate(sensorsProvider);
|
||||
}),
|
||||
),
|
||||
],
|
||||
if (roomsAsync.valueOrNull?.isEmpty ?? false)
|
||||
Center(
|
||||
child: Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Icon(Icons.map_outlined,
|
||||
size: 64, color: Colors.grey.shade400),
|
||||
const SizedBox(height: 12),
|
||||
Text(
|
||||
'No rooms yet',
|
||||
style: Theme.of(context)
|
||||
.textTheme
|
||||
.titleMedium
|
||||
?.copyWith(
|
||||
color: Colors.grey.shade600),
|
||||
),
|
||||
const SizedBox(height: 4),
|
||||
Text(
|
||||
'Switch to edit mode and tap + to add a room',
|
||||
style: Theme.of(context)
|
||||
.textTheme
|
||||
.bodySmall
|
||||
?.copyWith(color: Colors.grey.shade500),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
// Placement mode overlay
|
||||
if (placingSensor != null) ...[
|
||||
const Center(child: _PlacementDot()),
|
||||
Positioned(
|
||||
top: 16,
|
||||
left: 16,
|
||||
right: 16,
|
||||
child: _PlacementCard(
|
||||
sensor: placingSensor,
|
||||
onPlace: () => _confirmPlacement(placingSensor),
|
||||
onCancel: () {
|
||||
_cancelPlacement();
|
||||
context.go('/sensors');
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
if (mode == FloorPlanMode.edit && unplaced.isNotEmpty)
|
||||
_UnplacedPanel(sensors: unplaced),
|
||||
],
|
||||
if (placingSensor == null &&
|
||||
mode == FloorPlanMode.edit &&
|
||||
unplaced.isNotEmpty)
|
||||
_UnplacedPanel(sensors: unplaced),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -234,14 +303,13 @@ class _FloorPlanScreenState extends ConsumerState<FloorPlanScreen> {
|
||||
),
|
||||
);
|
||||
|
||||
final name = nameCtrl.text.trim();
|
||||
final width = double.tryParse(widthCtrl.text) ?? 5.0;
|
||||
final name = nameCtrl.text.trim();
|
||||
final width = double.tryParse(widthCtrl.text) ?? 5.0;
|
||||
final height = double.tryParse(heightCtrl.text) ?? 4.0;
|
||||
|
||||
if (confirmed != true || !mounted) return;
|
||||
if (name.isEmpty) return;
|
||||
|
||||
// Create floor if none exists yet.
|
||||
var floor = ref.read(floorProvider).valueOrNull;
|
||||
if (floor == null) {
|
||||
floor = await ref
|
||||
@@ -262,6 +330,68 @@ class _FloorPlanScreenState extends ConsumerState<FloorPlanScreen> {
|
||||
}
|
||||
}
|
||||
|
||||
class _PlacementDot extends StatelessWidget {
|
||||
const _PlacementDot();
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Container(
|
||||
width: 16,
|
||||
height: 16,
|
||||
decoration: BoxDecoration(
|
||||
shape: BoxShape.circle,
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
border: Border.all(color: Colors.white, width: 2),
|
||||
boxShadow: const [BoxShadow(blurRadius: 4, color: Colors.black26)],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _PlacementCard extends StatelessWidget {
|
||||
const _PlacementCard({
|
||||
required this.sensor,
|
||||
required this.onPlace,
|
||||
required this.onCancel,
|
||||
});
|
||||
|
||||
final Sensor sensor;
|
||||
final VoidCallback onPlace;
|
||||
final VoidCallback onCancel;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
return Card(
|
||||
elevation: 4,
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
|
||||
child: Row(
|
||||
children: [
|
||||
const Icon(Icons.sensors),
|
||||
const SizedBox(width: 10),
|
||||
Expanded(
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Text(sensor.displayName,
|
||||
style: theme.textTheme.labelLarge),
|
||||
Text('Pan to position, then tap Place',
|
||||
style: theme.textTheme.bodySmall),
|
||||
],
|
||||
),
|
||||
),
|
||||
TextButton(onPressed: onCancel, child: const Text('Cancel')),
|
||||
const SizedBox(width: 4),
|
||||
FilledButton(onPressed: onPlace, child: const Text('Place')),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _UnplacedPanel extends StatelessWidget {
|
||||
const _UnplacedPanel({required this.sensors});
|
||||
|
||||
@@ -291,7 +421,7 @@ class _UnplacedPanel extends StatelessWidget {
|
||||
size: 16),
|
||||
label: Text(s.displayName),
|
||||
onPressed: () =>
|
||||
context.push('/sensors/${s.id}'),
|
||||
showSensorDetailSheet(context, s.id),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:webview_flutter/webview_flutter.dart';
|
||||
import 'package:vibration/vibration.dart';
|
||||
|
||||
import '../../../domain/models/floor.dart';
|
||||
import '../../../domain/models/floor_plan_mode.dart';
|
||||
@@ -43,6 +45,7 @@ class FloorPlanEditorState extends State<FloorPlanEditor> {
|
||||
|
||||
int? _selectedRoomId;
|
||||
String? _selectedSensorId;
|
||||
Completer<({int? roomId, double? x, double? y})>? _placementCompleter;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
@@ -82,6 +85,8 @@ class FloorPlanEditorState extends State<FloorPlanEditor> {
|
||||
});
|
||||
} else {
|
||||
widget.onSensorTapped(id);
|
||||
Vibration.hasVibrator()
|
||||
.then((t) { if(t) Vibration.vibrate(duration: 20); });
|
||||
}
|
||||
case 'sensorMoved':
|
||||
widget.onSensorMoved(
|
||||
@@ -110,6 +115,12 @@ class FloorPlanEditorState extends State<FloorPlanEditor> {
|
||||
_selectedRoomId = null;
|
||||
_selectedSensorId = null;
|
||||
});
|
||||
case 'positionAtCenter':
|
||||
final roomId = (data['roomId'] as num?)?.toInt();
|
||||
final x = (data['x'] as num?)?.toDouble();
|
||||
final y = (data['y'] as num?)?.toDouble();
|
||||
_placementCompleter?.complete((roomId: roomId, x: x, y: y));
|
||||
_placementCompleter = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,6 +219,17 @@ class FloorPlanEditorState extends State<FloorPlanEditor> {
|
||||
});
|
||||
}
|
||||
|
||||
/// Asks JS for the room + room-relative coords at the canvas center.
|
||||
/// Returns null roomId if no room is under the center dot.
|
||||
Future<({int? roomId, double? x, double? y})> getPositionAtCenter() {
|
||||
final c = Completer<({int? roomId, double? x, double? y})>();
|
||||
_placementCompleter = c;
|
||||
_run(() async {
|
||||
await _controller.runJavaScript('window.companion.getPositionAtCenter()');
|
||||
});
|
||||
return c.future;
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final inEditMode = widget.mode == FloorPlanMode.edit;
|
||||
|
||||
@@ -0,0 +1,246 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
|
||||
import '../../domain/models/sensor.dart';
|
||||
import '../../providers.dart';
|
||||
|
||||
void showSensorDetailSheet(BuildContext context, int sensorId) {
|
||||
showModalBottomSheet<void>(
|
||||
context: context,
|
||||
useRootNavigator: true,
|
||||
isScrollControlled: true,
|
||||
builder: (_) => SensorDetailSheet(sensorId: sensorId),
|
||||
);
|
||||
}
|
||||
|
||||
class SensorDetailSheet extends ConsumerStatefulWidget {
|
||||
const SensorDetailSheet({super.key, required this.sensorId});
|
||||
|
||||
final int sensorId;
|
||||
|
||||
@override
|
||||
ConsumerState<SensorDetailSheet> createState() => _SensorDetailSheetState();
|
||||
}
|
||||
|
||||
class _SensorDetailSheetState extends ConsumerState<SensorDetailSheet> {
|
||||
bool _editing = false;
|
||||
final _nameCtrl = TextEditingController();
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_nameCtrl.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
Future<void> _saveName(Sensor sensor) async {
|
||||
final name = _nameCtrl.text.trim();
|
||||
setState(() => _editing = false);
|
||||
final unchanged = name == (sensor.name ?? '') ||
|
||||
(name.isEmpty && sensor.name == null);
|
||||
if (unchanged) return;
|
||||
await ref
|
||||
.read(sensorRepositoryProvider)
|
||||
.updateSensor(sensor.id, name: name.isEmpty ? null : name);
|
||||
ref.invalidate(sensorProvider(sensor.id));
|
||||
ref.invalidate(sensorsProvider);
|
||||
}
|
||||
|
||||
Future<void> _delete(BuildContext context, Sensor sensor) 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(ctx).colorScheme.error,
|
||||
),
|
||||
onPressed: () => Navigator.of(ctx).pop(true),
|
||||
child: const Text('Delete'),
|
||||
),
|
||||
],
|
||||
),
|
||||
);
|
||||
if (confirmed == true && context.mounted) {
|
||||
await ref.read(sensorRepositoryProvider).deleteSensor(sensor.id);
|
||||
ref.invalidate(sensorsProvider);
|
||||
if (context.mounted) Navigator.of(context, rootNavigator: true).pop();
|
||||
}
|
||||
}
|
||||
|
||||
void _placeOnFloorPlan(BuildContext context, Sensor sensor) {
|
||||
final router = GoRouter.of(context);
|
||||
ref.read(sensorPlacementProvider.notifier).state = sensor;
|
||||
Navigator.of(context, rootNavigator: true).pop();
|
||||
router.go('/floorplan');
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final sensorAsync = ref.watch(sensorProvider(widget.sensorId));
|
||||
|
||||
return sensorAsync.when(
|
||||
loading: () => const SizedBox(
|
||||
height: 200,
|
||||
child: Center(child: CircularProgressIndicator()),
|
||||
),
|
||||
error: (e, _) => SizedBox(
|
||||
height: 200,
|
||||
child: Center(child: Text(e.toString())),
|
||||
),
|
||||
data: (sensor) {
|
||||
// Keep controller in sync when not actively editing.
|
||||
if (!_editing) _nameCtrl.text = sensor.name ?? '';
|
||||
|
||||
return _SheetBody(
|
||||
sensor: sensor,
|
||||
editing: _editing,
|
||||
nameCtrl: _nameCtrl,
|
||||
onEditToggle: () => setState(() {
|
||||
_editing = true;
|
||||
_nameCtrl.text = sensor.name ?? '';
|
||||
}),
|
||||
onSaveName: () => _saveName(sensor),
|
||||
onPlace: () => _placeOnFloorPlan(context, sensor),
|
||||
onDelete: () => _delete(context, sensor),
|
||||
);
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _SheetBody extends StatelessWidget {
|
||||
const _SheetBody({
|
||||
required this.sensor,
|
||||
required this.editing,
|
||||
required this.nameCtrl,
|
||||
required this.onEditToggle,
|
||||
required this.onSaveName,
|
||||
required this.onPlace,
|
||||
required this.onDelete,
|
||||
});
|
||||
|
||||
final Sensor sensor;
|
||||
final bool editing;
|
||||
final TextEditingController nameCtrl;
|
||||
final VoidCallback onEditToggle;
|
||||
final VoidCallback onSaveName;
|
||||
final VoidCallback onPlace;
|
||||
final VoidCallback onDelete;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
return SafeArea(
|
||||
child: Padding(
|
||||
padding: EdgeInsets.fromLTRB(
|
||||
24,
|
||||
12,
|
||||
24,
|
||||
MediaQuery.of(context).viewInsets.bottom + 24,
|
||||
),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
children: [
|
||||
Center(
|
||||
child: Container(
|
||||
width: 32,
|
||||
height: 4,
|
||||
decoration: BoxDecoration(
|
||||
color: theme.colorScheme.outlineVariant,
|
||||
borderRadius: BorderRadius.circular(2),
|
||||
),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: editing
|
||||
? TextField(
|
||||
controller: nameCtrl,
|
||||
autofocus: true,
|
||||
style: theme.textTheme.titleLarge,
|
||||
decoration: const InputDecoration(
|
||||
isDense: true,
|
||||
border: InputBorder.none,
|
||||
),
|
||||
onSubmitted: (_) => onSaveName(),
|
||||
)
|
||||
: Text(
|
||||
sensor.displayName,
|
||||
style: theme.textTheme.titleLarge,
|
||||
),
|
||||
),
|
||||
IconButton(
|
||||
icon: Icon(editing ? Icons.check : Icons.edit_outlined),
|
||||
tooltip: editing ? 'Save name' : 'Rename',
|
||||
onPressed: editing ? onSaveName : onEditToggle,
|
||||
),
|
||||
],
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
_InfoRow(label: 'Device ID', value: sensor.sensorId),
|
||||
_InfoRow(
|
||||
label: 'Position',
|
||||
value: sensor.isPlaced
|
||||
? '(${sensor.x!.toStringAsFixed(2)}, ${sensor.y!.toStringAsFixed(2)})'
|
||||
: 'Not placed',
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
FilledButton.icon(
|
||||
icon: const Icon(Icons.map_outlined),
|
||||
label: Text(sensor.isPlaced
|
||||
? 'Reposition on floor plan'
|
||||
: 'Place on floor plan'),
|
||||
onPressed: onPlace,
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
OutlinedButton.icon(
|
||||
icon: const Icon(Icons.bluetooth),
|
||||
label: const Text('Reprovision'),
|
||||
onPressed: () {},
|
||||
),
|
||||
const SizedBox(height: 24),
|
||||
TextButton(
|
||||
style: TextButton.styleFrom(
|
||||
foregroundColor: theme.colorScheme.error,
|
||||
),
|
||||
onPressed: onDelete,
|
||||
child: const Text('Delete sensor'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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: 6),
|
||||
child: Row(
|
||||
children: [
|
||||
Expanded(
|
||||
child: Text(label, style: Theme.of(context).textTheme.bodySmall),
|
||||
),
|
||||
Text(value, style: Theme.of(context).textTheme.bodyMedium),
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,10 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
|
||||
import '../../domain/models/sensor.dart';
|
||||
import '../../providers.dart';
|
||||
import '../ble_provision/ble_provision_sheet.dart';
|
||||
import 'sensor_detail_sheet.dart';
|
||||
|
||||
class SensorListScreen extends ConsumerWidget {
|
||||
const SensorListScreen({super.key});
|
||||
@@ -26,7 +28,8 @@ class SensorListScreen extends ConsumerWidget {
|
||||
child: const Text('Dismiss'),
|
||||
),
|
||||
TextButton(
|
||||
onPressed: () => context.push('/sensors/$selectedId'),
|
||||
onPressed: () =>
|
||||
showSensorDetailSheet(context, int.parse(selectedId)),
|
||||
child: const Text('Open'),
|
||||
),
|
||||
],
|
||||
@@ -36,29 +39,37 @@ class SensorListScreen extends ConsumerWidget {
|
||||
loading: () =>
|
||||
const Center(child: CircularProgressIndicator()),
|
||||
error: (e, _) => Center(child: Text(e.toString())),
|
||||
data: (list) => list.isEmpty
|
||||
? const Center(child: Text('No sensors enrolled yet'))
|
||||
: ListView.builder(
|
||||
itemCount: list.length,
|
||||
itemBuilder: (context, i) {
|
||||
final sensor = list[i];
|
||||
final isSelected =
|
||||
selectedId == sensor.id.toString();
|
||||
return ListTile(
|
||||
selected: isSelected,
|
||||
leading: Icon(sensor.isPlaced
|
||||
? Icons.sensors
|
||||
: Icons.sensors_off_outlined),
|
||||
title: Text(sensor.displayName),
|
||||
subtitle: Text(sensor.isPlaced
|
||||
? 'Placed'
|
||||
: 'Not placed on floor plan'),
|
||||
trailing: const Icon(Icons.chevron_right),
|
||||
onTap: () =>
|
||||
context.push('/sensors/${sensor.id}'),
|
||||
);
|
||||
},
|
||||
),
|
||||
data: (list) {
|
||||
if (list.isEmpty) {
|
||||
return const Center(child: Text('No sensors enrolled yet'));
|
||||
}
|
||||
final unplaced =
|
||||
list.where((s) => !s.isPlaced).toList();
|
||||
final placed =
|
||||
list.where((s) => s.isPlaced).toList();
|
||||
return ListView(
|
||||
children: [
|
||||
if (unplaced.isNotEmpty) ...[
|
||||
_SectionHeader(
|
||||
label: 'Unplaced', count: unplaced.length),
|
||||
...unplaced.map((s) => _SensorTile(
|
||||
sensor: s,
|
||||
isSelected:
|
||||
selectedId == s.id.toString(),
|
||||
)),
|
||||
],
|
||||
if (placed.isNotEmpty) ...[
|
||||
_SectionHeader(
|
||||
label: 'Placed', count: placed.length),
|
||||
...placed.map((s) => _SensorTile(
|
||||
sensor: s,
|
||||
isSelected:
|
||||
selectedId == s.id.toString(),
|
||||
)),
|
||||
],
|
||||
],
|
||||
);
|
||||
},
|
||||
),
|
||||
),
|
||||
],
|
||||
@@ -75,3 +86,45 @@ class SensorListScreen extends ConsumerWidget {
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _SectionHeader extends StatelessWidget {
|
||||
const _SectionHeader({required this.label, required this.count});
|
||||
|
||||
final String label;
|
||||
final int count;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Padding(
|
||||
padding: const EdgeInsets.fromLTRB(16, 16, 16, 4),
|
||||
child: Text(
|
||||
'$label ($count)',
|
||||
style: Theme.of(context).textTheme.labelMedium?.copyWith(
|
||||
color: Theme.of(context).colorScheme.primary,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class _SensorTile extends StatelessWidget {
|
||||
const _SensorTile({required this.sensor, required this.isSelected});
|
||||
|
||||
final Sensor sensor;
|
||||
final bool isSelected;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return ListTile(
|
||||
selected: isSelected,
|
||||
leading: Icon(sensor.isPlaced
|
||||
? Icons.sensors
|
||||
: Icons.sensors_off_outlined),
|
||||
title: Text(sensor.displayName),
|
||||
subtitle: Text(
|
||||
sensor.isPlaced ? 'Placed' : 'Not placed on floor plan'),
|
||||
trailing: const Icon(Icons.chevron_right),
|
||||
onTap: () => showSensorDetailSheet(context, sensor.id),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -148,6 +148,10 @@ final sensorProvider =
|
||||
|
||||
final selectedSensorIdProvider = StateProvider<String?>((ref) => null);
|
||||
|
||||
/// Non-null while the user is placing a sensor on the floor plan via the
|
||||
/// center-dot placement flow. Cleared on Place, Cancel, or back navigation.
|
||||
final sensorPlacementProvider = StateProvider<Sensor?>((ref) => null);
|
||||
|
||||
final floorPlanModeProvider =
|
||||
StateProvider<FloorPlanMode>((ref) => FloorPlanMode.view);
|
||||
|
||||
|
||||
@@ -57,28 +57,12 @@ final routerProvider = Provider<GoRouter>((ref) {
|
||||
GoRoute(
|
||||
path: '/floorplan',
|
||||
builder: (context, state) => const FloorPlanScreen(),
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: 'sensors/:id',
|
||||
builder: (context, state) => SensorDetailScreen(
|
||||
sensorId: state.pathParameters['id']!,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
]),
|
||||
StatefulShellBranch(routes: [
|
||||
GoRoute(
|
||||
path: '/sensors',
|
||||
builder: (context, state) => const SensorListScreen(),
|
||||
routes: [
|
||||
GoRoute(
|
||||
path: ':id',
|
||||
builder: (context, state) => SensorDetailScreen(
|
||||
sensorId: state.pathParameters['id']!,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
]),
|
||||
StatefulShellBranch(routes: [
|
||||
|
||||
Reference in New Issue
Block a user