Create SectionTree in plugin

Summary: Create Sections hierarchy from data received when a changeset is applied and send it to the plugin to render it

Reviewed By: adityasharat

Differential Revision: D16120480

fbshipit-source-id: 30b824d8d6e65b537c3e98b7ee3438055f6f1ec2
This commit is contained in:
Mihaela Ogrezeanu
2019-07-10 06:00:13 -07:00
committed by Facebook Github Bot
parent a952f988d6
commit 169836fc2f

View File

@@ -56,6 +56,8 @@ public class ChangesetDebug implements ChangesetDebugListener {
final String sourceName = SectionsLogEventUtils.applyNewChangeSetSourceToString(attribution);
createSectionTree(rootSection, "", tree, oldSection);
sSectionsFlipperPlugin.onChangesetApplied(
sourceName + " " + extra,
isEventAsync(attribution),
@@ -74,4 +76,60 @@ public class ChangesetDebug implements ChangesetDebugListener {
return false;
}
}
/** Finds the section with the same global key in the previous tree, if it existed. */
private static Section findSectionInPreviousTree(Section previousRoot, String globalKey) {
if (previousRoot == null) {
return null;
}
if (previousRoot.getGlobalKey().equals(globalKey)) {
return previousRoot;
}
if (previousRoot.getChildren() == null) {
return null;
}
int count = previousRoot.getChildren().size();
for (int i = 0; i < count; i++) {
Section child = previousRoot.getChildren().get(i);
final Section previousSection = findSectionInPreviousTree(child, globalKey);
if (previousSection != null) {
return previousSection;
}
}
return null;
}
static void createSectionTree(
Section rootSection, String parentKey, FlipperArray.Builder tree, Section oldRootSection) {
if (rootSection == null) {
return;
}
final String globalKey = rootSection.getGlobalKey();
final FlipperObject.Builder nodeBuilder = new FlipperObject.Builder();
final Section oldSection = findSectionInPreviousTree(oldRootSection, globalKey);
final boolean isDirty = ChangesetDebugConfiguration.isSectionDirty(oldSection, rootSection);
nodeBuilder.put("identifier", globalKey);
nodeBuilder.put("name", rootSection.getSimpleName());
nodeBuilder.put("parent", parentKey);
nodeBuilder.put("isDirty", isDirty);
nodeBuilder.put("isReused", !isDirty);
nodeBuilder.put("didTriggerStateUpdate", false); // TODO
tree.put(nodeBuilder.build());
if (rootSection.getChildren() == null) {
return;
}
for (int i = 0; i < rootSection.getChildren().size(); i++) {
createSectionTree(rootSection.getChildren().get(i), globalKey, tree, oldRootSection);
}
}
}