refactor: move floor plan editor into own widget
This commit is contained in:
@@ -6,7 +6,8 @@ import '../../domain/models/floor_plan_mode.dart';
|
||||
import '../../domain/models/sensor.dart';
|
||||
import '../../providers.dart';
|
||||
import '../ble_provision/ble_provision_sheet.dart';
|
||||
import 'widgets/konva_web_view.dart';
|
||||
import 'widgets/floor_plan_editor.dart';
|
||||
import 'widgets/room_edit_sheet.dart';
|
||||
|
||||
class FloorPlanScreen extends ConsumerStatefulWidget {
|
||||
const FloorPlanScreen({super.key});
|
||||
@@ -16,22 +17,21 @@ class FloorPlanScreen extends ConsumerStatefulWidget {
|
||||
}
|
||||
|
||||
class _FloorPlanScreenState extends ConsumerState<FloorPlanScreen> {
|
||||
final _konvaKey = GlobalKey<KonvaWebViewState>();
|
||||
final _editorKey = GlobalKey<FloorPlanEditorState>();
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
// Push any already-loaded data once the WebView is ready.
|
||||
WidgetsBinding.instance.addPostFrameCallback((_) => _syncAll());
|
||||
}
|
||||
|
||||
void _syncAll() {
|
||||
ref
|
||||
.read(roomsProvider.future)
|
||||
.then((rooms) => _konvaKey.currentState?.loadFloorPlan(rooms));
|
||||
.then((rooms) => _editorKey.currentState?.loadFloorPlan(rooms));
|
||||
ref
|
||||
.read(sensorsProvider.future)
|
||||
.then((sensors) => _konvaKey.currentState?.loadSensors(sensors));
|
||||
.then((sensors) => _editorKey.currentState?.loadSensors(sensors));
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -40,21 +40,20 @@ class _FloorPlanScreenState extends ConsumerState<FloorPlanScreen> {
|
||||
final roomsAsync = ref.watch(roomsProvider);
|
||||
final sensorsAsync = ref.watch(sensorsProvider);
|
||||
|
||||
// Push data updates to the WebView whenever providers refresh.
|
||||
ref.listen(roomsProvider, (_, next) {
|
||||
next.whenData((r) => _konvaKey.currentState?.loadFloorPlan(r));
|
||||
next.whenData((r) => _editorKey.currentState?.loadFloorPlan(r));
|
||||
});
|
||||
ref.listen(sensorsProvider, (_, next) {
|
||||
next.whenData((s) => _konvaKey.currentState?.loadSensors(s));
|
||||
next.whenData((s) => _editorKey.currentState?.loadSensors(s));
|
||||
});
|
||||
ref.listen(tagPositionsProvider, (_, next) {
|
||||
next.whenData((t) => _konvaKey.currentState?.updateTags(t));
|
||||
next.whenData((t) => _editorKey.currentState?.updateTags(t));
|
||||
});
|
||||
ref.listen(particleCloudProvider, (_, next) {
|
||||
next.whenData((p) => _konvaKey.currentState?.updateParticleCloud(p));
|
||||
next.whenData((p) => _editorKey.currentState?.updateParticleCloud(p));
|
||||
});
|
||||
ref.listen(selectedSensorIdProvider, (_, id) {
|
||||
_konvaKey.currentState?.highlightSensor(id);
|
||||
_editorKey.currentState?.highlightSensor(id);
|
||||
});
|
||||
|
||||
final unplaced = sensorsAsync.valueOrNull
|
||||
@@ -76,7 +75,7 @@ class _FloorPlanScreenState extends ConsumerState<FloorPlanScreen> {
|
||||
? FloorPlanMode.view
|
||||
: FloorPlanMode.edit;
|
||||
ref.read(floorPlanModeProvider.notifier).state = next;
|
||||
_konvaKey.currentState?.setMode(next);
|
||||
_editorKey.currentState?.setMode(next);
|
||||
},
|
||||
),
|
||||
],
|
||||
@@ -86,8 +85,8 @@ class _FloorPlanScreenState extends ConsumerState<FloorPlanScreen> {
|
||||
Expanded(
|
||||
child: Stack(
|
||||
children: [
|
||||
KonvaWebView(
|
||||
key: _konvaKey,
|
||||
FloorPlanEditor(
|
||||
key: _editorKey,
|
||||
mode: mode,
|
||||
onSensorTapped: (id) {
|
||||
ref.read(selectedSensorIdProvider.notifier).state = id;
|
||||
@@ -115,6 +114,17 @@ class _FloorPlanScreenState extends ConsumerState<FloorPlanScreen> {
|
||||
// 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);
|
||||
}),
|
||||
),
|
||||
if (roomsAsync.valueOrNull?.isEmpty ?? false)
|
||||
Center(
|
||||
@@ -150,36 +160,23 @@ class _FloorPlanScreenState extends ConsumerState<FloorPlanScreen> {
|
||||
_UnplacedPanel(sensors: unplaced),
|
||||
],
|
||||
),
|
||||
floatingActionButton: mode == FloorPlanMode.edit
|
||||
? Column(
|
||||
mainAxisSize: MainAxisSize.min,
|
||||
crossAxisAlignment: CrossAxisAlignment.end,
|
||||
children: [
|
||||
FloatingActionButton.small(
|
||||
heroTag: 'add-room',
|
||||
tooltip: 'Add room',
|
||||
onPressed: () => _konvaKey.currentState?.addRoom(),
|
||||
child: const Icon(Icons.add_home_outlined),
|
||||
),
|
||||
const SizedBox(height: 8),
|
||||
FloatingActionButton.extended(
|
||||
heroTag: 'add-sensor',
|
||||
icon: const Icon(Icons.bluetooth),
|
||||
label: const Text('Add sensor'),
|
||||
onPressed: () => showModalBottomSheet<void>(
|
||||
context: context,
|
||||
isScrollControlled: true,
|
||||
builder: (_) => const BleProvisionSheet(),
|
||||
).then((_) {
|
||||
ref.invalidate(sensorsProvider);
|
||||
}),
|
||||
),
|
||||
],
|
||||
)
|
||||
: null,
|
||||
);
|
||||
}
|
||||
|
||||
void _showRoomEditSheet(int roomId) {
|
||||
final rooms = ref.read(roomsProvider).valueOrNull ?? [];
|
||||
final room = rooms.where((r) => r.id == roomId).firstOrNull;
|
||||
if (room == null) return;
|
||||
final floor = ref.read(floorProvider).valueOrNull;
|
||||
if (floor == null) return;
|
||||
showModalBottomSheet<void>(
|
||||
context: context,
|
||||
useRootNavigator: true,
|
||||
isScrollControlled: true,
|
||||
builder: (_) => RoomEditSheet(room: room, floorId: floor.id),
|
||||
).then((_) => ref.invalidate(roomsProvider));
|
||||
}
|
||||
|
||||
Future<void> _showAddRoomDialog(double x, double y) async {
|
||||
final nameCtrl = TextEditingController();
|
||||
final widthCtrl = TextEditingController(text: '5.0');
|
||||
|
||||
Reference in New Issue
Block a user