Skip to content

Commit e7e6f1c

Browse files
committed
Simulator #15
1 parent d6e2f5b commit e7e6f1c

File tree

4 files changed

+37
-14
lines changed

4 files changed

+37
-14
lines changed

lib/src/codelessly.dart

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import 'utils/codelessly_http_client.dart';
1515
typedef NavigationListener = void Function(
1616
BuildContext context,
1717
String? layoutId,
18+
String? canvasId,
1819
);
20+
1921
typedef BreakpointsListener = void Function(
2022
BuildContext context, Breakpoint breakpoint);
2123

@@ -155,6 +157,10 @@ class Codelessly {
155157

156158
String? get currentNavigatedLayoutId => _currentNavigatedLayoutId;
157159

160+
String? _currentNavigatedCanvasId;
161+
162+
String? get currentNavigatedCanvasId => _currentNavigatedCanvasId;
163+
158164
final Map<String, NavigationListener> _navigationListeners = {};
159165

160166
final Map<String, BreakpointsListener> _breakpointsListeners = {};
@@ -763,11 +769,15 @@ class Codelessly {
763769
/// Calls navigation listeners when navigation happens.
764770
/// Provided [context] must be of the destination widget. This context
765771
/// can be used to retrieve new [CodelesslyContext].
766-
void notifyNavigationListeners(BuildContext context, String? layoutId) {
772+
void notifyNavigationListeners(
773+
BuildContext context, {
774+
required String? layoutId,
775+
required String? canvasId,
776+
}) {
767777
_currentNavigatedLayoutId = layoutId;
768-
778+
_currentNavigatedCanvasId = canvasId;
769779
for (final listener in [..._navigationListeners.values]) {
770-
listener(context, layoutId);
780+
listener(context, layoutId, canvasId);
771781
}
772782
}
773783

lib/src/functions/functions_repository.dart

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ class FunctionsRepository {
244244

245245
final Codelessly codelessly = context.read<Codelessly>();
246246
final String? myLayoutId = codelessly.currentNavigatedLayoutId;
247+
final String? myCanvasId = codelessly.currentNavigatedCanvasId;
247248

248249
if (action.navigationType == NavigationType.pop) {
249250
await Navigator.maybePop(context, parsedParams);
@@ -295,7 +296,11 @@ class FunctionsRepository {
295296
// to, because the Navigator.push future has completed and came
296297
// back to this screen.
297298
if (context.mounted) {
298-
codelessly.notifyNavigationListeners(context, myLayoutId);
299+
codelessly.notifyNavigationListeners(
300+
context,
301+
layoutId: myLayoutId,
302+
canvasId: myCanvasId,
303+
);
299304
}
300305
} else if (action.navigationType == NavigationType.replace) {
301306
await Navigator.pushReplacement(
@@ -312,7 +317,11 @@ class FunctionsRepository {
312317
// to, because the Navigator.push future has completed and came
313318
// back to this screen.
314319
if (context.mounted) {
315-
codelessly.notifyNavigationListeners(context, myLayoutId);
320+
codelessly.notifyNavigationListeners(
321+
context,
322+
layoutId: myLayoutId,
323+
canvasId: myCanvasId,
324+
);
316325
}
317326
}
318327
}
@@ -329,6 +338,7 @@ class FunctionsRepository {
329338

330339
final Codelessly codelessly = context.read<Codelessly>();
331340
final String? myLayoutId = codelessly.currentNavigatedLayoutId;
341+
final String? myCanvasId = codelessly.currentNavigatedCanvasId;
332342

333343
// Check if a layout exists for the action's [destinationId].
334344
final String? layoutId = codelessly.dataManager.publishModel?.layouts.values
@@ -369,7 +379,11 @@ class FunctionsRepository {
369379
),
370380
);
371381
if (context.mounted) {
372-
codelessly.notifyNavigationListeners(context, myLayoutId);
382+
codelessly.notifyNavigationListeners(
383+
context,
384+
layoutId: myLayoutId,
385+
canvasId: myCanvasId,
386+
);
373387
}
374388
}
375389

lib/src/transformers/node_transformers/passive_canvas_transformer.dart

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,6 @@ class _PassiveCanvasWidgetState extends State<PassiveCanvasWidget> {
402402
if (!mounted) return;
403403

404404
codelessly = context.read<Codelessly>();
405-
final String? myLayoutId = codelessly!
406-
.dataManager.publishModel?.layouts.dataMap.entries
407-
.firstWhere((entry) {
408-
return entry.value.canvasIds.contains(widget.node.id);
409-
}).key;
410405

411406
// Set the system UI brightness to the canvas brightness.
412407
codelessly!.setSystemUIBrightness(widget.node.properties.brightness);
@@ -415,8 +410,8 @@ class _PassiveCanvasWidgetState extends State<PassiveCanvasWidget> {
415410
// again, such as if this canvas was navigated away from, but then the view
416411
// pops and goes back to this canvas.
417412
codelessly!.addNavigationListener('canvas-${widget.node.id}',
418-
(event, layoutId) {
419-
if (myLayoutId == layoutId) {
413+
(event, layoutId, canvasId) {
414+
if (canvasId == widget.node.id) {
420415
codelessly!.setSystemUIBrightness(widget.node.properties.brightness);
421416
}
422417
});

lib/src/ui/codelessly_widget.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,7 @@ class _CodelesslyWidgetState extends State<CodelesslyWidget> {
593593
return _NavigationBuilder(
594594
key: ValueKey(effectiveLayoutID),
595595
layoutId: effectiveLayoutID,
596+
canvasId: canvasID,
596597
builder: (context) {
597598
return _effectiveLayoutBuilder(context, layoutWidget);
598599
},
@@ -665,11 +666,13 @@ class _CodelesslyWidgetState extends State<CodelesslyWidget> {
665666
class _NavigationBuilder extends StatefulWidget {
666667
final WidgetBuilder builder;
667668
final String? layoutId;
669+
final String? canvasId;
668670

669671
const _NavigationBuilder({
670672
super.key,
671673
required this.builder,
672674
required this.layoutId,
675+
required this.canvasId,
673676
});
674677

675678
@override
@@ -684,7 +687,8 @@ class _NavigationBuilderState extends State<_NavigationBuilder> {
684687
if (!context.mounted) return;
685688
context.read<Codelessly>().notifyNavigationListeners(
686689
context,
687-
widget.layoutId,
690+
layoutId: widget.layoutId,
691+
canvasId: widget.canvasId,
688692
);
689693
});
690694
}

0 commit comments

Comments
 (0)