Add appProperty to ReactRootView

Summary:
To get on par with iOS this PR adds an `appProperty` to the `ReactRootView`. The documentation on the iOS side is here: [https://facebook.github.io/react-native/docs/communication-ios.html#properties.](https://facebook.github.io/react-native/docs/communication-ios.html#properties.)

You can pass in initial props with the `startReactApplication` method:
```java
…
Bundle initialProps = new Bundle();

bundle.putString(“initialKey”, “initialValue”);
mReactRootView.startReactApplication(mReactInstanceManager, "HelloWorld", initialProps);
setContentView(mReactRootView);
…
```

And later on properties can be updated this way:
```java
…
// Update props
Bundle updatedProps = mReactRootView.getAppProperties();

updatedProps.putString(“someOtherKey”, “someOtherValue”);

mReactRootView.setAppProperties(updatedProps);

// Replace props
Bundle newProps = new Bundle();

newProps.putString(“someKey”, “someValue”);

mReactRootView.setAppProperties(newProps);
…
Closes https://github.com/facebook/react-native/pull/13430

Reviewed By: AaaChiuuu

Differential Revision: D4896483

Pulled By: javache

fbshipit-source-id: 7c752d6bbf5dc500874b49dcff80db772e83915f
This commit is contained in:
Michael Schneider 2017-04-25 10:11:08 -07:00 коммит произвёл Facebook Github Bot
Родитель 3d2a9ebdda
Коммит 57b0039ce1
2 изменённых файлов: 42 добавлений и 13 удалений

Просмотреть файл

@ -839,14 +839,7 @@ public class ReactInstanceManager {
UIManagerModule uiManagerModule = catalystInstance.getNativeModule(UIManagerModule.class); UIManagerModule uiManagerModule = catalystInstance.getNativeModule(UIManagerModule.class);
int rootTag = uiManagerModule.addMeasuredRootView(rootView); int rootTag = uiManagerModule.addMeasuredRootView(rootView);
rootView.setRootViewTag(rootTag); rootView.setRootViewTag(rootTag);
@Nullable Bundle launchOptions = rootView.getLaunchOptions(); rootView.runApplication();
WritableMap initialProps = Arguments.makeNativeMap(launchOptions);
String jsAppModuleName = rootView.getJSModuleName();
WritableNativeMap appParams = new WritableNativeMap();
appParams.putDouble("rootTag", rootTag);
appParams.putMap("initialProps", initialProps);
catalystInstance.getJSModule(AppRegistry.class).runApplication(jsAppModuleName, appParams);
UiThreadUtil.runOnUiThread(new Runnable() { UiThreadUtil.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {

Просмотреть файл

@ -25,11 +25,14 @@ import android.view.WindowManager;
import com.facebook.common.logging.FLog; import com.facebook.common.logging.FLog;
import com.facebook.infer.annotation.Assertions; import com.facebook.infer.annotation.Assertions;
import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.CatalystInstance;
import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.UiThreadUtil; import com.facebook.react.bridge.UiThreadUtil;
import com.facebook.react.bridge.WritableMap; import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.common.ReactConstants; import com.facebook.react.common.ReactConstants;
import com.facebook.react.common.annotations.VisibleForTesting; import com.facebook.react.common.annotations.VisibleForTesting;
import com.facebook.react.modules.appregistry.AppRegistry;
import com.facebook.react.modules.core.DeviceEventManagerModule; import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.modules.deviceinfo.DeviceInfoModule; import com.facebook.react.modules.deviceinfo.DeviceInfoModule;
import com.facebook.react.uimanager.DisplayMetricsHolder; import com.facebook.react.uimanager.DisplayMetricsHolder;
@ -68,7 +71,7 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView
private @Nullable ReactInstanceManager mReactInstanceManager; private @Nullable ReactInstanceManager mReactInstanceManager;
private @Nullable String mJSModuleName; private @Nullable String mJSModuleName;
private @Nullable Bundle mLaunchOptions; private @Nullable Bundle mAppProperties;
private @Nullable CustomGlobalLayoutListener mCustomGlobalLayoutListener; private @Nullable CustomGlobalLayoutListener mCustomGlobalLayoutListener;
private @Nullable ReactRootViewEventListener mRootViewEventListener; private @Nullable ReactRootViewEventListener mRootViewEventListener;
private int mRootViewTag; private int mRootViewTag;
@ -197,7 +200,7 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView
public void startReactApplication( public void startReactApplication(
ReactInstanceManager reactInstanceManager, ReactInstanceManager reactInstanceManager,
String moduleName, String moduleName,
@Nullable Bundle launchOptions) { @Nullable Bundle initialProperties) {
UiThreadUtil.assertOnUiThread(); UiThreadUtil.assertOnUiThread();
// TODO(6788889): Use POJO instead of bundle here, apparently we can't just use WritableMap // TODO(6788889): Use POJO instead of bundle here, apparently we can't just use WritableMap
@ -209,7 +212,7 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView
mReactInstanceManager = reactInstanceManager; mReactInstanceManager = reactInstanceManager;
mJSModuleName = moduleName; mJSModuleName = moduleName;
mLaunchOptions = launchOptions; mAppProperties = initialProperties;
if (!mReactInstanceManager.hasStartedCreatingInitialContext()) { if (!mReactInstanceManager.hasStartedCreatingInitialContext()) {
mReactInstanceManager.createReactContextInBackground(); mReactInstanceManager.createReactContextInBackground();
@ -249,8 +252,41 @@ public class ReactRootView extends SizeMonitoringFrameLayout implements RootView
return Assertions.assertNotNull(mJSModuleName); return Assertions.assertNotNull(mJSModuleName);
} }
/* package */ @Nullable Bundle getLaunchOptions() { public @Nullable Bundle getAppProperties() {
return mLaunchOptions; return mAppProperties;
}
public void setAppProperties(@Nullable Bundle appProperties) {
UiThreadUtil.assertOnUiThread();
mAppProperties = appProperties;
runApplication();
}
/**
* Calls into JS to start the React application. Can be called multiple times with the
* same rootTag, which will re-render the application from the root.
*/
/* package */ void runApplication() {
if (mReactInstanceManager == null || !mIsAttachedToInstance) {
return;
}
ReactContext reactContext = mReactInstanceManager.getCurrentReactContext();
if (reactContext == null) {
return;
}
CatalystInstance catalystInstance = reactContext.getCatalystInstance();
WritableNativeMap appParams = new WritableNativeMap();
appParams.putDouble("rootTag", getRootViewTag());
@Nullable Bundle appProperties = getAppProperties();
if (appProperties != null) {
appParams.putMap("initialProps", Arguments.fromBundle(appProperties));
}
String jsAppModuleName = getJSModuleName();
catalystInstance.getJSModule(AppRegistry.class).runApplication(jsAppModuleName, appParams);
} }
/** /**