From 294e31b7c258be636f3b5bc816228cd4339e1d0f Mon Sep 17 00:00:00 2001 From: Ramanpreet Nara Date: Wed, 20 Nov 2019 11:11:51 -0800 Subject: [PATCH] Make RCTDevLoadingView TurboModule-compatible Summary: RCTDevLoadingView wasn't hooked up to the codegen. This diff makes RCTDevLoadingView type-safe and also makes it TurboModule-compatible. Changelog: [iOS][Added] - Make RCTDevLoadingView TurboModule-compatible Reviewed By: PeteTheHeat Differential Revision: D16969764 fbshipit-source-id: 47e6682eea3fc49d3f0448c636b5f616d5bce220 --- .../FBReactNativeSpec/FBReactNativeSpec.h | 4 +- Libraries/Utilities/NativeDevLoadingView.js | 4 +- React/CoreModules/BUCK | 3 ++ React/CoreModules/CoreModulesPlugins.h | 1 + React/CoreModules/CoreModulesPlugins.mm | 1 + .../RCTDevLoadingView.h | 7 --- .../RCTDevLoadingView.mm} | 48 +++++++++++++------ .../DevSupport/RCTDevLoadingViewSetEnabled.h | 11 +++++ .../DevSupport/RCTDevLoadingViewSetEnabled.m | 22 +++++++++ .../specs/NativeDevLoadingViewSpec.java | 3 +- 10 files changed, 77 insertions(+), 27 deletions(-) rename React/{DevSupport => CoreModules}/RCTDevLoadingView.h (67%) rename React/{DevSupport/RCTDevLoadingView.m => CoreModules/RCTDevLoadingView.mm} (78%) create mode 100644 React/DevSupport/RCTDevLoadingViewSetEnabled.h create mode 100644 React/DevSupport/RCTDevLoadingViewSetEnabled.m diff --git a/Libraries/FBReactNativeSpec/FBReactNativeSpec/FBReactNativeSpec.h b/Libraries/FBReactNativeSpec/FBReactNativeSpec/FBReactNativeSpec.h index 637161e82a7..a56b32f495e 100644 --- a/Libraries/FBReactNativeSpec/FBReactNativeSpec/FBReactNativeSpec.h +++ b/Libraries/FBReactNativeSpec/FBReactNativeSpec/FBReactNativeSpec.h @@ -848,8 +848,8 @@ namespace facebook { @protocol NativeDevLoadingViewSpec - (void)showMessage:(NSString *)message - color:(NSDictionary *)color - backgroundColor:(NSDictionary *)backgroundColor; + color:(NSNumber *)color + backgroundColor:(NSNumber *)backgroundColor; - (void)hide; @end diff --git a/Libraries/Utilities/NativeDevLoadingView.js b/Libraries/Utilities/NativeDevLoadingView.js index e672771778b..bf8d311ea1b 100644 --- a/Libraries/Utilities/NativeDevLoadingView.js +++ b/Libraries/Utilities/NativeDevLoadingView.js @@ -16,8 +16,8 @@ import * as TurboModuleRegistry from '../TurboModule/TurboModuleRegistry'; export interface Spec extends TurboModule { +showMessage: ( message: string, - color: Object, - backgroundColor: Object, + color: ?number, + backgroundColor: ?number, ) => void; +hide: () => void; } diff --git a/React/CoreModules/BUCK b/React/CoreModules/BUCK index 21bd66f4258..2c518ea1d30 100644 --- a/React/CoreModules/BUCK +++ b/React/CoreModules/BUCK @@ -105,6 +105,9 @@ rn_apple_library( ) + react_module_plugin_providers( name = "WebSocketModule", native_class_func = "RCTWebSocketModuleCls", + ) + react_module_plugin_providers( + name = "DevLoadingView", + native_class_func = "RCTDevLoadingViewCls", ), plugins_header = "FBCoreModulesPlugins.h", preprocessor_flags = OBJC_ARC_PREPROCESSOR_FLAGS + get_debug_preprocessor_flags() + rn_extra_build_flags() + [ diff --git a/React/CoreModules/CoreModulesPlugins.h b/React/CoreModules/CoreModulesPlugins.h index 660c86a281d..94a9584ca47 100644 --- a/React/CoreModules/CoreModulesPlugins.h +++ b/React/CoreModules/CoreModulesPlugins.h @@ -51,6 +51,7 @@ Class RCTRedBoxCls(void) __attribute__((used)); Class RCTTVNavigationEventEmitterCls(void) __attribute__((used)); Class RCTWebSocketExecutorCls(void) __attribute__((used)); Class RCTWebSocketModuleCls(void) __attribute__((used)); +Class RCTDevLoadingViewCls(void) __attribute__((used)); #ifdef __cplusplus } diff --git a/React/CoreModules/CoreModulesPlugins.mm b/React/CoreModules/CoreModulesPlugins.mm index d1a69375116..d8e2b784ae4 100644 --- a/React/CoreModules/CoreModulesPlugins.mm +++ b/React/CoreModules/CoreModulesPlugins.mm @@ -40,6 +40,7 @@ Class RCTCoreModulesClassProvider(const char *name) { {"TVNavigationEventEmitter", RCTTVNavigationEventEmitterCls}, {"WebSocketExecutor", RCTWebSocketExecutorCls}, {"WebSocketModule", RCTWebSocketModuleCls}, + {"DevLoadingView", RCTDevLoadingViewCls}, }; auto p = sCoreModuleClassMap.find(name); diff --git a/React/DevSupport/RCTDevLoadingView.h b/React/CoreModules/RCTDevLoadingView.h similarity index 67% rename from React/DevSupport/RCTDevLoadingView.h rename to React/CoreModules/RCTDevLoadingView.h index 4fe67f1b767..9fdbf707597 100644 --- a/React/DevSupport/RCTDevLoadingView.h +++ b/React/CoreModules/RCTDevLoadingView.h @@ -6,17 +6,10 @@ */ #import - #import @class RCTLoadingProgress; @interface RCTDevLoadingView : NSObject - -+ (void)setEnabled:(BOOL)enabled; -- (void)showMessage:(NSString *)message color:(UIColor *)color backgroundColor:(UIColor *)backgroundColor; -- (void)showWithURL:(NSURL *)URL; - (void)updateProgress:(RCTLoadingProgress *)progress; -- (void)hide; - @end diff --git a/React/DevSupport/RCTDevLoadingView.m b/React/CoreModules/RCTDevLoadingView.mm similarity index 78% rename from React/DevSupport/RCTDevLoadingView.m rename to React/CoreModules/RCTDevLoadingView.mm index f2dbd6b3897..82eb0b0327d 100644 --- a/React/DevSupport/RCTDevLoadingView.m +++ b/React/CoreModules/RCTDevLoadingView.mm @@ -10,14 +10,22 @@ #import #import +#import #import +#import #import #import +#import + +#import "CoreModulesPlugins.h" + +using namespace facebook::react; + +@interface RCTDevLoadingView () +@end #if RCT_DEV | RCT_ENABLE_LOADING_VIEW -static BOOL isEnabled = YES; - @implementation RCTDevLoadingView { UIWindow *_window; @@ -29,11 +37,6 @@ static BOOL isEnabled = YES; RCT_EXPORT_MODULE() -+ (void)setEnabled:(BOOL)enabled -{ - isEnabled = enabled; -} - + (BOOL)requiresMainQueueSetup { return YES; @@ -57,9 +60,14 @@ RCT_EXPORT_MODULE() } } -RCT_EXPORT_METHOD(showMessage:(NSString *)message color:(UIColor *)color backgroundColor:(UIColor *)backgroundColor) +RCT_EXPORT_METHOD(showMessage:(NSString *)message color:(NSNumber *)color backgroundColor:(NSNumber *)backgroundColor) { - if (!isEnabled) { + [self showMessage:message withColor:[RCTConvert UIColor:color] andBackgroundColor:[RCTConvert UIColor:backgroundColor]]; +} + +-(void)showMessage:(NSString *)message withColor:(UIColor *)color andBackgroundColor:(UIColor *)backgroundColor +{ + if (!RCTDevLoadingViewGetEnabled()) { return; } @@ -99,7 +107,7 @@ RCT_EXPORT_METHOD(showMessage:(NSString *)message color:(UIColor *)color backgro RCT_EXPORT_METHOD(hide) { - if (!isEnabled) { + if (!RCTDevLoadingViewGetEnabled()) { return; } @@ -141,8 +149,8 @@ RCT_EXPORT_METHOD(hide) } [self showMessage:message - color:color - backgroundColor:backgroundColor]; + withColor:color + andBackgroundColor:backgroundColor]; } - (void)updateProgress:(RCTLoadingProgress *)progress @@ -155,6 +163,11 @@ RCT_EXPORT_METHOD(hide) }); } +- (std::shared_ptr)getTurboModuleWithJsInvoker:(std::shared_ptr)jsInvoker +{ + return std::make_shared(self, jsInvoker); +} + @end #else @@ -162,12 +175,19 @@ RCT_EXPORT_METHOD(hide) @implementation RCTDevLoadingView + (NSString *)moduleName { return nil; } -+ (void)setEnabled:(BOOL)enabled { } -- (void)showMessage:(NSString *)message color:(UIColor *)color backgroundColor:(UIColor *)backgroundColor { } +- (void)showMessage:(NSString *)message color:(NSNumber *)color backgroundColor:(NSNumber *)backgroundColor { } - (void)showWithURL:(NSURL *)URL { } - (void)updateProgress:(RCTLoadingProgress *)progress { } - (void)hide { } +- (std::shared_ptr)getTurboModuleWithJsInvoker:(std::shared_ptr)jsInvoker +{ + return std::make_shared(self, jsInvoker); +} @end #endif + +Class RCTDevLoadingViewCls(void) { + return RCTDevLoadingView.class; +} diff --git a/React/DevSupport/RCTDevLoadingViewSetEnabled.h b/React/DevSupport/RCTDevLoadingViewSetEnabled.h new file mode 100644 index 00000000000..1ad182578cd --- /dev/null +++ b/React/DevSupport/RCTDevLoadingViewSetEnabled.h @@ -0,0 +1,11 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import + +RCT_EXTERN void RCTDevLoadingViewSetEnabled(BOOL enabled); +RCT_EXTERN BOOL RCTDevLoadingViewGetEnabled(void); diff --git a/React/DevSupport/RCTDevLoadingViewSetEnabled.m b/React/DevSupport/RCTDevLoadingViewSetEnabled.m new file mode 100644 index 00000000000..90a5ff15f43 --- /dev/null +++ b/React/DevSupport/RCTDevLoadingViewSetEnabled.m @@ -0,0 +1,22 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import "RCTDevLoadingViewSetEnabled.h" + +#if RCT_DEV | RCT_ENABLE_LOADING_VIEW +static BOOL isDevLoadingViewEnabled = YES; +#else +static BOOL isDevLoadingViewEnabled = NO; +#endif + +void RCTDevLoadingViewSetEnabled(BOOL enabled) { + isDevLoadingViewEnabled = enabled; +} + +BOOL RCTDevLoadingViewGetEnabled() { + return isDevLoadingViewEnabled; +} diff --git a/ReactAndroid/src/main/java/com/facebook/fbreact/specs/NativeDevLoadingViewSpec.java b/ReactAndroid/src/main/java/com/facebook/fbreact/specs/NativeDevLoadingViewSpec.java index 30cb6d15958..f9e0c38c94c 100644 --- a/ReactAndroid/src/main/java/com/facebook/fbreact/specs/NativeDevLoadingViewSpec.java +++ b/ReactAndroid/src/main/java/com/facebook/fbreact/specs/NativeDevLoadingViewSpec.java @@ -16,7 +16,6 @@ import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.ReactModuleWithSpec; -import com.facebook.react.bridge.ReadableMap; import com.facebook.react.turbomodule.core.interfaces.TurboModule; public abstract class NativeDevLoadingViewSpec extends ReactContextBaseJavaModule implements ReactModuleWithSpec, TurboModule { @@ -28,5 +27,5 @@ public abstract class NativeDevLoadingViewSpec extends ReactContextBaseJavaModul public abstract void hide(); @ReactMethod - public abstract void showMessage(String message, ReadableMap color, ReadableMap backgroundColor); + public abstract void showMessage(String message, Double color, Double backgroundColor); }