Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/src/data/data_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,8 @@ class DataManager {

// Process Layouts
SDKPublishLayout? layout;
if (model!.layouts.containsKey(layoutID)) {
final bool isCached = model!.layouts.containsKey(layoutID);
if (isCached) {
DebugLogger.instance.printInfo(
'\tLayout [$layoutID] is already cached. Skipping download.',
name: name,
Expand Down Expand Up @@ -1446,8 +1447,6 @@ class DataManager {
name: name,
);
}

model.layouts[layoutID] = layout;
}

assert(layout != null, 'Layout should not be null at this point.');
Expand Down Expand Up @@ -1545,6 +1544,13 @@ class DataManager {
);
}

// Add a downloaded layout only once its variables and conditions are in
// the model. A CodelesslyWidget builds a layout as soon as the model has
// it, and only loads the layout's variables and conditions then, so a
// layout added first would render without them if anything rebuilt the
// widget while they were still downloading.
if (!isCached) model.layouts[layoutID] = layout!;

await emitPublishModel();
savePublishModel();

Expand Down
25 changes: 24 additions & 1 deletion lib/src/ui/layout_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,37 @@ class _CodelesslyLayoutBuilderState extends State<CodelesslyLayoutBuilder> {
}
}

/// The layout's variables and conditions that [loadLayout] last loaded.
SDKLayoutVariables? loadedVariables;
SDKLayoutConditions? loadedConditions;

@override
void didUpdateWidget(covariant CodelesslyLayoutBuilder oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.layout.id != widget.layout.id ||
oldWidget.canvasId != widget.canvasId ||
oldWidget.layout.lastUpdated != widget.layout.lastUpdated) {
oldWidget.layout.lastUpdated != widget.layout.lastUpdated ||
didVariablesOrConditionsChange(context)) {
loadLayout(context);
}
}

/// Whether the publish model has different variables or conditions for
/// this layout than [loadLayout] last loaded. They can arrive after the
/// layout, e.g. when the layout was cached without them, or be published
/// on their own.
bool didVariablesOrConditionsChange(BuildContext context) {
final SDKPublishModel? model = context
.read<Codelessly>()
.dataManager
.publishModel;
if (model == null) return false;
bool changed(Object? loaded, Object? current) =>
!identical(loaded, current) && loaded != current;
return changed(loadedVariables, model.variables[widget.layout.id]) ||
changed(loadedConditions, model.conditions[widget.layout.id]);
}

/// Loads the layout into the [nodeRegistry] and updates the
/// [CodelesslyContext] with the node values.
void loadLayout(BuildContext context) {
Expand Down Expand Up @@ -219,6 +240,8 @@ class _CodelesslyLayoutBuilderState extends State<CodelesslyLayoutBuilder> {
}
}
final publishModel = context.read<Codelessly>().dataManager.publishModel!;
loadedVariables = publishModel.variables[widget.layout.id];
loadedConditions = publishModel.conditions[widget.layout.id];

final Map<String, VariableData> variablesMap =
publishModel.variables[widget.layout.id]?.variables[widget.canvasId] ??
Expand Down
Loading
Loading