feat: add sensor move mode to floor plan

This commit is contained in:
2026-05-15 15:46:13 +02:00
parent 2c3e60d2b1
commit 077585bd73
8 changed files with 99 additions and 94 deletions
+51 -24
View File
@@ -32,7 +32,26 @@ class _FloorPlanScreenState extends ConsumerState<FloorPlanScreen> {
.then((rooms) => _editorKey.currentState?.loadFloorPlan(rooms));
ref
.read(sensorsProvider.future)
.then((sensors) => _editorKey.currentState?.loadSensors(sensors));
.then((sensors) {
_editorKey.currentState?.loadSensors(sensors);
_applyPlacementState();
});
}
void _applyPlacementState() {
final placing = ref.read(sensorPlacementProvider);
if (placing == null) {
_editorKey.currentState?.highlightSensor(null);
return;
}
final currentMode = ref.read(floorPlanModeProvider);
if (currentMode != FloorPlanMode.view) {
ref.read(floorPlanModeProvider.notifier).state = FloorPlanMode.view;
_editorKey.currentState?.setMode(FloorPlanMode.view);
}
if (placing.isPlaced) {
_editorKey.currentState?.highlightSensor(placing.id.toString());
}
}
Future<void> _confirmPlacement(Sensor sensor) async {
@@ -81,9 +100,7 @@ class _FloorPlanScreenState extends ConsumerState<FloorPlanScreen> {
ref.listen(particleCloudProvider, (_, next) {
next.whenData((p) => _editorKey.currentState?.updateParticleCloud(p));
});
ref.listen(selectedSensorIdProvider, (_, id) {
_editorKey.currentState?.highlightSensor(id);
});
ref.listen(sensorPlacementProvider, (prev, next) => _applyPlacementState());
final unplaced = sensorsAsync.valueOrNull
?.where((s) => !s.isPlaced)
@@ -103,23 +120,37 @@ class _FloorPlanScreenState extends ConsumerState<FloorPlanScreen> {
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 (placingSensor == null) ...[
if (mode == FloorPlanMode.view) ...[
IconButton(
tooltip: 'Edit mode',
icon: const Icon(Icons.edit),
onPressed: () {
ref.read(floorPlanModeProvider.notifier).state =
FloorPlanMode.edit;
_editorKey.currentState?.setMode(FloorPlanMode.edit);
},
),
onPressed: () {
final next = mode == FloorPlanMode.edit
? FloorPlanMode.view
: FloorPlanMode.edit;
ref.read(floorPlanModeProvider.notifier).state = next;
_editorKey.currentState?.setMode(next);
},
),
IconButton(
tooltip: 'Sensor move mode',
icon: const Icon(Icons.sensors),
onPressed: () {
ref.read(floorPlanModeProvider.notifier).state =
FloorPlanMode.sensorMove;
_editorKey.currentState?.setMode(FloorPlanMode.sensorMove);
},
),
] else
IconButton(
tooltip: 'Done',
icon: const Icon(Icons.check),
onPressed: () {
ref.read(floorPlanModeProvider.notifier).state =
FloorPlanMode.view;
_editorKey.currentState?.setMode(FloorPlanMode.view);
},
),
],
],
),
body: Column(
@@ -131,8 +162,6 @@ class _FloorPlanScreenState extends ConsumerState<FloorPlanScreen> {
key: _editorKey,
mode: mode,
onSensorTapped: (id) {
ref.read(selectedSensorIdProvider.notifier).state =
id;
showSensorDetailSheet(context, int.parse(id));
},
onSensorMoved: (sensorId, roomId, x, y) async {
@@ -163,8 +192,6 @@ class _FloorPlanScreenState extends ConsumerState<FloorPlanScreen> {
},
onRoomAdded: (x, y) => _showAddRoomDialog(x, y),
onEditRoomTapped: _showRoomEditSheet,
onEditSensorTapped: (id) =>
showSensorDetailSheet(context, int.parse(id)),
onAddSensorTapped: () => showModalBottomSheet<void>(
context: context,
isScrollControlled: true,
@@ -20,7 +20,6 @@ class FloorPlanEditor extends StatefulWidget {
this.onRoomsUpdated,
this.onRoomAdded,
this.onEditRoomTapped,
this.onEditSensorTapped,
this.onAddSensorTapped,
});
@@ -31,7 +30,6 @@ class FloorPlanEditor extends StatefulWidget {
final void Function(List<Room> rooms)? onRoomsUpdated;
final void Function(double x, double y)? onRoomAdded;
final void Function(int roomId)? onEditRoomTapped;
final void Function(String sensorId)? onEditSensorTapped;
final void Function()? onAddSensorTapped;
@override
@@ -44,7 +42,7 @@ class FloorPlanEditorState extends State<FloorPlanEditor> {
final _pending = <Future<void> Function()>[];
int? _selectedRoomId;
String? _selectedSensorId;
String? _repositioningSensorId;
Completer<({int? roomId, double? x, double? y})>? _placementCompleter;
@override
@@ -78,19 +76,17 @@ class FloorPlanEditorState extends State<FloorPlanEditor> {
switch (data['type'] as String?) {
case 'sensorTapped':
final id = data['id'] as String;
if (widget.mode == FloorPlanMode.edit) {
setState(() {
_selectedSensorId = id;
_selectedRoomId = null;
});
} else {
widget.onSensorTapped(id);
Vibration.hasVibrator()
.then((t) { if(t) Vibration.vibrate(duration: 20); });
}
widget.onSensorTapped(id);
Vibration.hasVibrator()
.then((t) { if (t) Vibration.vibrate(duration: 20); });
case 'sensorMoved':
final id = data['id'] as String;
if (_repositioningSensorId == id) {
setState(() => _repositioningSensorId = null);
setRepositioningSensor(null);
}
widget.onSensorMoved(
data['id'] as String,
id,
(data['roomId'] as num).toInt(),
(data['x'] as num).toDouble(),
(data['y'] as num).toDouble(),
@@ -106,15 +102,9 @@ class FloorPlanEditorState extends State<FloorPlanEditor> {
(data['y'] as num).toDouble(),
);
case 'roomTapped':
setState(() {
_selectedRoomId = (data['id'] as num).toInt();
_selectedSensorId = null;
});
setState(() => _selectedRoomId = (data['id'] as num).toInt());
case 'selectionCleared':
setState(() {
_selectedRoomId = null;
_selectedSensorId = null;
});
setState(() => _selectedRoomId = null);
case 'positionAtCenter':
final roomId = (data['roomId'] as num?)?.toInt();
final x = (data['x'] as num?)?.toDouble();
@@ -204,7 +194,7 @@ class FloorPlanEditorState extends State<FloorPlanEditor> {
Future<void> setMode(FloorPlanMode mode) {
setState(() {
_selectedRoomId = null;
_selectedSensorId = null;
_repositioningSensorId = null;
});
return _run(() async {
await _controller.runJavaScript(
@@ -213,6 +203,16 @@ class FloorPlanEditorState extends State<FloorPlanEditor> {
});
}
Future<void> setRepositioningSensor(String? id) {
setState(() => _repositioningSensorId = id);
return _run(() async {
final jsId = id == null ? 'null' : jsonEncode(id);
await _controller.runJavaScript(
'window.companion.setRepositioningSensor($jsId)',
);
});
}
Future<void> addRoom() {
return _run(() async {
await _controller.runJavaScript('window.companion.addRoom()');
@@ -245,16 +245,6 @@ class FloorPlanEditorState extends State<FloorPlanEditor> {
crossAxisAlignment: CrossAxisAlignment.end,
children: [
if (inEditMode) ...[
if (_selectedSensorId != null) ...[
FloatingActionButton.small(
heroTag: 'editor-info-sensor',
tooltip: 'Sensor details',
onPressed: () =>
widget.onEditSensorTapped?.call(_selectedSensorId!),
child: const Icon(Icons.info_outline),
),
const SizedBox(height: 8),
],
if (_selectedRoomId != null) ...[
FloatingActionButton.small(
heroTag: 'editor-edit-room',