Skip to content

Commit 6d7bf20

Browse files
committed
Simulator #2
1 parent 92e9d06 commit 6d7bf20

File tree

4 files changed

+52
-26
lines changed

4 files changed

+52
-26
lines changed

lib/src/codelessly.dart

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,9 @@ class Codelessly {
148148
/// If it is null, it means it has not yet been initialized.
149149
CloudDatabase? get cloudDatabase => dataManager.cloudDatabase;
150150

151-
final List<NavigationListener> _navigationListeners = [];
151+
final Map<String, NavigationListener> _navigationListeners = {};
152152

153-
final List<BreakpointsListener> _breakpointsListeners = [];
153+
final Map<String, BreakpointsListener> _breakpointsListeners = {};
154154

155155
/// Creates a new instance of [Codelessly].
156156
Codelessly({
@@ -236,6 +236,7 @@ class Codelessly {
236236
_previewDataManager = null;
237237
_config = null;
238238
_navigationListeners.clear();
239+
_breakpointsListeners.clear();
239240
_client.close();
240241
}
241242

@@ -746,37 +747,37 @@ class Codelessly {
746747
/// Provided [context] must be of the destination widget. This context
747748
/// can be used to retrieve new [CodelesslyContext].
748749
void notifyNavigationListeners(BuildContext context) {
749-
_navigationListeners.forEach((listener) => listener(context));
750+
for (final listener in [..._navigationListeners.values]) {
751+
listener(context);
752+
}
750753
}
751754

752755
/// Adds a global listener for navigation.
753-
void addNavigationListener(NavigationListener callback) {
754-
if (!_navigationListeners.contains(callback)) {
755-
_navigationListeners.add(callback);
756-
}
756+
void addNavigationListener(String label, NavigationListener callback) {
757+
_navigationListeners[label] = callback;
757758
}
758759

759760
/// Removes a global navigation listener.
760-
void removeNavigationListener(NavigationListener callback) {
761-
_navigationListeners.remove(callback);
761+
void removeNavigationListener(String label) {
762+
_navigationListeners.remove(label);
762763
}
763764

764765
/// Calls navigation listeners when navigation happens.
765766
/// Provided [context] must be of the destination widget. This context
766767
/// can be used to retrieve new [CodelesslyContext].
767-
void notifyBreakpointsListener(BuildContext context, Breakpoint breakpoint) {
768-
_breakpointsListeners.forEach((listener) => listener(context, breakpoint));
768+
void notifyBreakpointsListeners(BuildContext context, Breakpoint breakpoint) {
769+
for (final listener in [..._breakpointsListeners.values]) {
770+
listener(context, breakpoint);
771+
}
769772
}
770773

771774
/// Adds a global listener for navigation.
772-
void addBreakpointsListener(BreakpointsListener callback) {
773-
if (!_breakpointsListeners.contains(callback)) {
774-
_breakpointsListeners.add(callback);
775-
}
775+
void addBreakpointsListener(String label, BreakpointsListener callback) {
776+
_breakpointsListeners[label] = callback;
776777
}
777778

778779
/// Removes a global navigation listener.
779-
void removeBreakpointsListener(BreakpointsListener callback) {
780-
_breakpointsListeners.remove(callback);
780+
void removeBreakpointsListener(String label) {
781+
_breakpointsListeners.remove(label);
781782
}
782783
}

lib/src/functions/functions_repository.dart

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -242,10 +242,13 @@ class FunctionsRepository {
242242

243243
_log('Performing navigation action with params: $parsedParams');
244244

245+
final Codelessly codelessly = context.read<Codelessly>();
245246
if (action.navigationType == NavigationType.pop) {
246247
await Navigator.maybePop(context, parsedParams);
248+
if (context.mounted) {
249+
codelessly.notifyNavigationListeners(context);
250+
}
247251
} else {
248-
final Codelessly codelessly = context.read<Codelessly>();
249252
// Check if a layout exists for the action's [destinationId].
250253
final String? layoutId = codelessly
251254
.dataManager.publishModel?.layouts[action.destinationId]?.id ??
@@ -289,8 +292,9 @@ class FunctionsRepository {
289292
),
290293
),
291294
);
292-
// ignore: use_build_context_synchronously
293-
codelessly.notifyNavigationListeners(context);
295+
if (context.mounted) {
296+
codelessly.notifyNavigationListeners(context);
297+
}
294298
} else if (action.navigationType == NavigationType.replace) {
295299
await Navigator.pushReplacement(
296300
context,
@@ -301,8 +305,9 @@ class FunctionsRepository {
301305
),
302306
),
303307
);
304-
// ignore: use_build_context_synchronously
305-
codelessly.notifyNavigationListeners(context);
308+
if (context.mounted) {
309+
codelessly.notifyNavigationListeners(context);
310+
}
306311
}
307312
}
308313
}
@@ -355,8 +360,9 @@ class FunctionsRepository {
355360
),
356361
),
357362
);
358-
// ignore: use_build_context_synchronously
359-
codelessly.notifyNavigationListeners(context);
363+
if (context.mounted) {
364+
codelessly.notifyNavigationListeners(context);
365+
}
360366
}
361367

362368
static void launchURL(BuildContext context, LinkAction action) {
@@ -897,7 +903,6 @@ ${response.body.contains('{') ? const JsonEncoder.withIndent(' ').convert(json.
897903
if (filteredReactions.isEmpty) return false;
898904

899905
for (final reaction in filteredReactions) {
900-
// ignore: use_build_context_synchronously
901906
if (!context.mounted) continue;
902907
final future = FunctionsRepository.performAction(
903908
context,

lib/src/transformers/node_transformers/passive_page_view_transformer.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,25 @@ class _PassivePageViewWidgetState extends State<PassivePageViewWidget> {
7171
oldWidget.node.properties.initialPage) {
7272
controller.jumpToPage(widget.node.properties.initialPage);
7373
}
74+
75+
final ScopedValues scopedValues = ScopedValues.of(context);
76+
77+
// Get the value of bound variable and update the controller text if it's
78+
// different from the current controller text.
79+
// widget.node.variables['inputValue'] only works when a sync variable is specified. If it is not then
80+
// we rely on initial text to be able to see if controller text needs to be updated.
81+
if (widget.node.variables['indexValue'] != null) {
82+
final int? currentPropertyValue =
83+
PropertyValueDelegate.getPropertyValue<int>(
84+
widget.node,
85+
'indexValue',
86+
scopedValues: scopedValues,
87+
);
88+
if (currentPropertyValue != null &&
89+
controller.page?.round() != currentPropertyValue) {
90+
controller.jumpToPage(currentPropertyValue);
91+
}
92+
}
7493
}
7594

7695
@override

lib/src/ui/codelessly_widget.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ class _CodelesslyWidgetState extends State<CodelesslyWidget> {
353353

354354
// Notify breakpoints listeners.
355355
_effectiveController.effectiveCodelessly
356-
.notifyBreakpointsListener(context, breakpoint);
356+
.notifyBreakpointsListeners(context, breakpoint);
357357
}
358358
}
359359
}
@@ -674,6 +674,7 @@ class _NavigationBuilderState extends State<_NavigationBuilder> {
674674
void initState() {
675675
super.initState();
676676
SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
677+
if (!context.mounted) return;
677678
context.read<Codelessly>().notifyNavigationListeners(context);
678679
});
679680
}

0 commit comments

Comments
 (0)