Support setting partial state from Android, and filling in data with previous State constructor

Summary:
iOS and other platforms have direct access to C++ StateT structs, whereas Java only has access to a Java map equivalent - state updates from Java can't update complex types, or must incur significant cost to reconstruct large objects from their folly::dynamic representation (not to mention the complexity of implementing the Java-to-C++ struct converters). Thus it's hard for Java to update StateT's with complex types on the C++ side.

This diff makes a minor change to Android's updateState which uses both the folly::dynamic data from Java as well as the previous State, so each StateT can have fields that are read-only from the Java perspective.

Motivation: For AndroidTextInput we need to set params from Java, without being able to send all of the State params from Java.

In this diff, we introduce a new State constructor that takes the previous State value and a folly::dynamic. It is up to each State implementation how the additional parameter will be used.

We migrate every existing component except for AndroidTextInput in this diff.

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D18672365

fbshipit-source-id: 4469e0a3c7658c204089c6fed39394979883f124
This commit is contained in:
Joshua Gross
2019-11-27 12:55:47 -08:00
committed by Facebook Github Bot
parent 07580f0f7e
commit 309de462bd
6 changed files with 12 additions and 6 deletions
@@ -29,7 +29,9 @@ class ModalHostViewState final {
ModalHostViewState(Size screenSize_) : screenSize(screenSize_){};
#ifdef ANDROID
ModalHostViewState(folly::dynamic data)
ModalHostViewState(
ModalHostViewState const &previousState,
folly::dynamic data)
: screenSize(Size{(Float)data["screenWidth"].getDouble(),
(Float)data["screenHeight"].getDouble()}){};
#endif
@@ -29,7 +29,7 @@ class ScrollViewState final {
#ifdef ANDROID
ScrollViewState() = default;
ScrollViewState(folly::dynamic data){};
ScrollViewState(ScrollViewState const &previousState, folly::dynamic data){};
folly::dynamic getDynamic() const {
return {};
};
@@ -51,7 +51,9 @@ class ParagraphState final {
paragraphAttributes(paragraphAttributes),
layoutManager(layoutManager) {}
ParagraphState() = default;
ParagraphState(folly::dynamic const &data) {
ParagraphState(
ParagraphState const &previousState,
folly::dynamic const &data) {
assert(false && "Not supported");
};
folly::dynamic getDynamic() const;
@@ -51,7 +51,9 @@ class AndroidTextInputState final {
paragraphAttributes(paragraphAttributes),
layoutManager(layoutManager) {}
AndroidTextInputState() = default;
AndroidTextInputState(folly::dynamic const &data) {
AndroidTextInputState(
AndroidTextInputState const &previousState,
folly::dynamic const &data) {
assert(false && "Not supported");
};
folly::dynamic getDynamic() const;
@@ -91,7 +91,7 @@ class ConcreteState : public State {
}
void updateState(folly::dynamic data) const override {
updateState(std::move(Data(data)));
updateState(std::move(Data(data_, data)));
}
#endif
+1 -1
View File
@@ -25,7 +25,7 @@ struct StateData final {
#ifdef ANDROID
StateData() = default;
StateData(folly::dynamic data){};
StateData(StateData const &previousState, folly::dynamic data){};
folly::dynamic getDynamic() const;
#endif
};