From 309de462bd73d39f44473d79471e2e6a41c0960c Mon Sep 17 00:00:00 2001 From: Joshua Gross Date: Wed, 27 Nov 2019 12:53:14 -0800 Subject: [PATCH] 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 --- ReactCommon/fabric/components/modal/ModalHostViewState.h | 4 +++- ReactCommon/fabric/components/scrollview/ScrollViewState.h | 2 +- ReactCommon/fabric/components/text/paragraph/ParagraphState.h | 4 +++- .../textinput/androidtextinput/AndroidTextInputState.h | 4 +++- ReactCommon/fabric/core/state/ConcreteState.h | 2 +- ReactCommon/fabric/core/state/StateData.h | 2 +- 6 files changed, 12 insertions(+), 6 deletions(-) diff --git a/ReactCommon/fabric/components/modal/ModalHostViewState.h b/ReactCommon/fabric/components/modal/ModalHostViewState.h index aa13a7c1e19..fc0533fbba3 100644 --- a/ReactCommon/fabric/components/modal/ModalHostViewState.h +++ b/ReactCommon/fabric/components/modal/ModalHostViewState.h @@ -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 diff --git a/ReactCommon/fabric/components/scrollview/ScrollViewState.h b/ReactCommon/fabric/components/scrollview/ScrollViewState.h index 6fa90897230..240ffbfba79 100644 --- a/ReactCommon/fabric/components/scrollview/ScrollViewState.h +++ b/ReactCommon/fabric/components/scrollview/ScrollViewState.h @@ -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 {}; }; diff --git a/ReactCommon/fabric/components/text/paragraph/ParagraphState.h b/ReactCommon/fabric/components/text/paragraph/ParagraphState.h index e9726898abd..f3dcf3abfa1 100644 --- a/ReactCommon/fabric/components/text/paragraph/ParagraphState.h +++ b/ReactCommon/fabric/components/text/paragraph/ParagraphState.h @@ -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; diff --git a/ReactCommon/fabric/components/textinput/androidtextinput/AndroidTextInputState.h b/ReactCommon/fabric/components/textinput/androidtextinput/AndroidTextInputState.h index 05faed89501..11f1d516f6c 100644 --- a/ReactCommon/fabric/components/textinput/androidtextinput/AndroidTextInputState.h +++ b/ReactCommon/fabric/components/textinput/androidtextinput/AndroidTextInputState.h @@ -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; diff --git a/ReactCommon/fabric/core/state/ConcreteState.h b/ReactCommon/fabric/core/state/ConcreteState.h index 86d5d1c0ab7..8781bc69d55 100644 --- a/ReactCommon/fabric/core/state/ConcreteState.h +++ b/ReactCommon/fabric/core/state/ConcreteState.h @@ -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 diff --git a/ReactCommon/fabric/core/state/StateData.h b/ReactCommon/fabric/core/state/StateData.h index 20ecee06a4d..930a8f3b788 100644 --- a/ReactCommon/fabric/core/state/StateData.h +++ b/ReactCommon/fabric/core/state/StateData.h @@ -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 };