diff --git a/android/src/main/java/com/facebook/flipper/plugins/litho/DataUtils.java b/android/src/main/java/com/facebook/flipper/plugins/litho/DataUtils.java index bda096641..81414035b 100644 --- a/android/src/main/java/com/facebook/flipper/plugins/litho/DataUtils.java +++ b/android/src/main/java/com/facebook/flipper/plugins/litho/DataUtils.java @@ -7,7 +7,9 @@ import android.graphics.drawable.Drawable; import com.facebook.flipper.core.FlipperObject; import com.facebook.flipper.plugins.inspector.InspectorValue; import com.facebook.flipper.plugins.inspector.Named; +import com.facebook.litho.StateContainer; import com.facebook.litho.annotations.Prop; +import com.facebook.litho.annotations.State; import java.lang.reflect.Field; import java.util.AbstractMap; import java.util.ArrayList; @@ -82,6 +84,32 @@ public class DataUtils { 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) { if (type == int.class || type == Integer.class) { return true; diff --git a/android/src/main/java/com/facebook/flipper/plugins/litho/DebugComponentDescriptor.java b/android/src/main/java/com/facebook/flipper/plugins/litho/DebugComponentDescriptor.java index 9abfed112..ba83a9136 100644 --- a/android/src/main/java/com/facebook/flipper/plugins/litho/DebugComponentDescriptor.java +++ b/android/src/main/java/com/facebook/flipper/plugins/litho/DebugComponentDescriptor.java @@ -27,7 +27,6 @@ import com.facebook.litho.DebugComponent; import com.facebook.litho.DebugLayoutNode; import com.facebook.litho.LithoView; import com.facebook.litho.StateContainer; -import com.facebook.litho.annotations.State; import com.facebook.yoga.YogaAlign; import com.facebook.yoga.YogaDirection; import com.facebook.yoga.YogaEdge; @@ -266,37 +265,8 @@ public class DebugComponentDescriptor extends NodeDescriptor { } @Nullable - private static FlipperObject getStateData(DebugComponent node) { - if (node.canResolve()) { - 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; + private static FlipperObject getStateData(DebugComponent node) throws Exception { + return DataUtils.getStateData(node, node.getStateContainer()); } @Override diff --git a/android/src/main/java/com/facebook/flipper/plugins/litho/DebugSectionDescriptor.java b/android/src/main/java/com/facebook/flipper/plugins/litho/DebugSectionDescriptor.java index 12080859c..3084ed1b9 100644 --- a/android/src/main/java/com/facebook/flipper/plugins/litho/DebugSectionDescriptor.java +++ b/android/src/main/java/com/facebook/flipper/plugins/litho/DebugSectionDescriptor.java @@ -76,6 +76,11 @@ public class DebugSectionDescriptor extends NodeDescriptor { data.addAll(propData); } + final FlipperObject stateData = getStateData(node); + if (stateData != null) { + data.add(new Named<>("State", stateData)); + } + return data; } @@ -85,6 +90,10 @@ public class DebugSectionDescriptor extends NodeDescriptor { return DataUtils.getPropData(section); } + private static @Nullable FlipperObject getStateData(DebugSection node) throws Exception { + return DataUtils.getStateData(node, node.getStateContainer()); + } + @Override public void setValue(DebugSection node, String[] path, FlipperDynamic value) throws Exception { // TODO T39526148