Show state data in flipper Sections inspector
Summary: as title; refactored getStateData for components so we can reuse Depends on D14386744 Reviewed By: passy Differential Revision: D14386910 fbshipit-source-id: 088605b920fd5b489d007e5daa69f4cd53fc3072
This commit is contained in:
committed by
Facebook Github Bot
parent
5195bfc0e4
commit
2d357a2ced
@@ -7,7 +7,9 @@ import android.graphics.drawable.Drawable;
|
|||||||
import com.facebook.flipper.core.FlipperObject;
|
import com.facebook.flipper.core.FlipperObject;
|
||||||
import com.facebook.flipper.plugins.inspector.InspectorValue;
|
import com.facebook.flipper.plugins.inspector.InspectorValue;
|
||||||
import com.facebook.flipper.plugins.inspector.Named;
|
import com.facebook.flipper.plugins.inspector.Named;
|
||||||
|
import com.facebook.litho.StateContainer;
|
||||||
import com.facebook.litho.annotations.Prop;
|
import com.facebook.litho.annotations.Prop;
|
||||||
|
import com.facebook.litho.annotations.State;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.util.AbstractMap;
|
import java.util.AbstractMap;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@@ -82,6 +84,32 @@ public class DataUtils {
|
|||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
static FlipperObject getStateData(Object node, StateContainer stateContainer) throws Exception {
|
||||||
|
if (stateContainer == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
final FlipperObject.Builder state = new FlipperObject.Builder();
|
||||||
|
|
||||||
|
boolean hasState = false;
|
||||||
|
for (Field f : stateContainer.getClass().getDeclaredFields()) {
|
||||||
|
f.setAccessible(true);
|
||||||
|
|
||||||
|
final State annotation = f.getAnnotation(State.class);
|
||||||
|
if (annotation != null) {
|
||||||
|
if (DataUtils.isTypeMutable(f.getType())) {
|
||||||
|
state.put(f.getName(), InspectorValue.mutable(f.get(stateContainer)));
|
||||||
|
} else {
|
||||||
|
state.put(f.getName(), InspectorValue.immutable(f.get(stateContainer)));
|
||||||
|
}
|
||||||
|
hasState = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return hasState ? state.build() : null;
|
||||||
|
}
|
||||||
|
|
||||||
static boolean isTypeMutable(Class<?> type) {
|
static boolean isTypeMutable(Class<?> type) {
|
||||||
if (type == int.class || type == Integer.class) {
|
if (type == int.class || type == Integer.class) {
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ import com.facebook.litho.DebugComponent;
|
|||||||
import com.facebook.litho.DebugLayoutNode;
|
import com.facebook.litho.DebugLayoutNode;
|
||||||
import com.facebook.litho.LithoView;
|
import com.facebook.litho.LithoView;
|
||||||
import com.facebook.litho.StateContainer;
|
import com.facebook.litho.StateContainer;
|
||||||
import com.facebook.litho.annotations.State;
|
|
||||||
import com.facebook.yoga.YogaAlign;
|
import com.facebook.yoga.YogaAlign;
|
||||||
import com.facebook.yoga.YogaDirection;
|
import com.facebook.yoga.YogaDirection;
|
||||||
import com.facebook.yoga.YogaEdge;
|
import com.facebook.yoga.YogaEdge;
|
||||||
@@ -266,37 +265,8 @@ public class DebugComponentDescriptor extends NodeDescriptor<DebugComponent> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private static FlipperObject getStateData(DebugComponent node) {
|
private static FlipperObject getStateData(DebugComponent node) throws Exception {
|
||||||
if (node.canResolve()) {
|
return DataUtils.getStateData(node, node.getStateContainer());
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
final StateContainer stateContainer = node.getStateContainer();
|
|
||||||
if (stateContainer == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
final FlipperObject.Builder state = new FlipperObject.Builder();
|
|
||||||
|
|
||||||
boolean hasState = false;
|
|
||||||
for (Field f : stateContainer.getClass().getDeclaredFields()) {
|
|
||||||
try {
|
|
||||||
f.setAccessible(true);
|
|
||||||
|
|
||||||
final State annotation = f.getAnnotation(State.class);
|
|
||||||
if (annotation != null) {
|
|
||||||
if (DataUtils.isTypeMutable(f.getType())) {
|
|
||||||
state.put(f.getName(), InspectorValue.mutable(f.get(stateContainer)));
|
|
||||||
} else {
|
|
||||||
state.put(f.getName(), InspectorValue.immutable(f.get(stateContainer)));
|
|
||||||
}
|
|
||||||
hasState = true;
|
|
||||||
}
|
|
||||||
} catch (Exception ignored) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return hasState ? state.build() : null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -76,6 +76,11 @@ public class DebugSectionDescriptor extends NodeDescriptor<DebugSection> {
|
|||||||
data.addAll(propData);
|
data.addAll(propData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
final FlipperObject stateData = getStateData(node);
|
||||||
|
if (stateData != null) {
|
||||||
|
data.add(new Named<>("State", stateData));
|
||||||
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -85,6 +90,10 @@ public class DebugSectionDescriptor extends NodeDescriptor<DebugSection> {
|
|||||||
return DataUtils.getPropData(section);
|
return DataUtils.getPropData(section);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static @Nullable FlipperObject getStateData(DebugSection node) throws Exception {
|
||||||
|
return DataUtils.getStateData(node, node.getStateContainer());
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setValue(DebugSection node, String[] path, FlipperDynamic value) throws Exception {
|
public void setValue(DebugSection node, String[] path, FlipperDynamic value) throws Exception {
|
||||||
// TODO T39526148
|
// TODO T39526148
|
||||||
|
|||||||
Reference in New Issue
Block a user