Skip to content

Commit

Permalink
Make RCTStatusBarManager TurboModule-compatible
Browse files Browse the repository at this point in the history
Summary:
See title.

Changelog:
[iOS][Added] - Make RCTStatusBarManager TurboModule-compatible

Reviewed By: shergin

Differential Revision: D18130374

fbshipit-source-id: 3ec226bcff17e47ffd9eba05e32c1eb68d6135b2
  • Loading branch information
RSNara authored and facebook-github-bot committed Nov 1, 2019
1 parent a257083 commit dc12676
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import * as TurboModuleRegistry from '../../TurboModule/TurboModuleRegistry';
export interface Spec extends TurboModule {
+getConstants: () => {|
+HEIGHT: number,
+DEFAULT_BACKGROUND_COLOR: number,
+DEFAULT_BACKGROUND_COLOR?: number,
|};

// TODO(T47754272) Can we remove this method?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2236,7 +2236,7 @@ namespace JS {
struct Builder {
struct Input {
RCTRequired<double> HEIGHT;
RCTRequired<double> DEFAULT_BACKGROUND_COLOR;
folly::Optional<double> DEFAULT_BACKGROUND_COLOR;
};

/** Initialize with a set of values */
Expand Down Expand Up @@ -3378,8 +3378,8 @@ inline JS::NativeStatusBarManagerIOS::Constants::Builder::Builder(const Input i)
NSMutableDictionary *d = [NSMutableDictionary new];
auto HEIGHT = i.HEIGHT.get();
d[@"HEIGHT"] = @(HEIGHT);
auto DEFAULT_BACKGROUND_COLOR = i.DEFAULT_BACKGROUND_COLOR.get();
d[@"DEFAULT_BACKGROUND_COLOR"] = @(DEFAULT_BACKGROUND_COLOR);
auto DEFAULT_BACKGROUND_COLOR = i.DEFAULT_BACKGROUND_COLOR;
d[@"DEFAULT_BACKGROUND_COLOR"] = DEFAULT_BACKGROUND_COLOR.hasValue() ? @((double)DEFAULT_BACKGROUND_COLOR.value()) : nil;
return d;
}) {}
inline JS::NativeStatusBarManagerIOS::Constants::Builder::Builder(Constants i) : _factory(^{
Expand Down
3 changes: 3 additions & 0 deletions React/CoreModules/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ rn_apple_library(
) + react_module_plugin_providers(
name = "Timing",
native_class_func = "RCTTimingCls",
) + react_module_plugin_providers(
name = "StatusBarManager",
native_class_func = "RCTStatusBarManagerCls",
),
plugins_header = "FBCoreModulesPlugins.h",
preprocessor_flags = OBJC_ARC_PREPROCESSOR_FLAGS + get_debug_preprocessor_flags() + rn_extra_build_flags() + [
Expand Down
1 change: 1 addition & 0 deletions React/CoreModules/CoreModulesPlugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Class RCTActionSheetManagerCls(void) __attribute__((used));
Class RCTAlertManagerCls(void) __attribute__((used));
Class RCTAsyncLocalStorageCls(void) __attribute__((used));
Class RCTTimingCls(void) __attribute__((used));
Class RCTStatusBarManagerCls(void) __attribute__((used));

#ifdef __cplusplus
}
Expand Down
1 change: 1 addition & 0 deletions React/CoreModules/CoreModulesPlugins.mm
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Class RCTCoreModulesClassProvider(const char *name) {
{"AlertManager", RCTAlertManagerCls},
{"AsyncLocalStorage", RCTAsyncLocalStorageCls},
{"Timing", RCTTimingCls},
{"StatusBarManager", RCTStatusBarManagerCls},
};

auto p = sCoreModuleClassMap.find(name);
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
*/

#import "RCTStatusBarManager.h"
#import "CoreModulesPlugins.h"

#import "RCTEventDispatcher.h"
#import "RCTLog.h"
#import "RCTUtils.h"
#import <React/RCTEventDispatcher.h>
#import <React/RCTLog.h>
#import <React/RCTUtils.h>

#if !TARGET_OS_TV
#import <FBReactNativeSpec/FBReactNativeSpec.h>

@implementation RCTConvert (UIStatusBar)

+ (UIStatusBarStyle)UIStatusBarStyle:(id)json RCT_DYNAMIC
Expand Down Expand Up @@ -40,7 +43,7 @@ + (UIStatusBarStyle)UIStatusBarStyle:(id)json RCT_DYNAMIC
}
});
return _RCT_CAST(
type, [RCTConvertEnumValue("UIStatusBarStyle", mapping, @(UIStatusBarStyleDefault), json) integerValue]);
UIStatusBarStyle, [RCTConvertEnumValue("UIStatusBarStyle", mapping, @(UIStatusBarStyleDefault), json) integerValue]);
}

RCT_ENUM_CONVERTER(
Expand All @@ -56,6 +59,13 @@ + (UIStatusBarStyle)UIStatusBarStyle:(id)json RCT_DYNAMIC
@end
#endif

#if !TARGET_OS_TV

@interface RCTStatusBarManager() <NativeStatusBarManagerIOSSpec>
@end

#endif

@implementation RCTStatusBarManager

static BOOL RCTViewControllerBasedStatusBarAppearance()
Expand Down Expand Up @@ -134,8 +144,9 @@ - (void)applicationWillChangeStatusBarFrame:(NSNotification *)notification
} ]);
}

RCT_EXPORT_METHOD(setStyle : (UIStatusBarStyle)statusBarStyle animated : (BOOL)animated)
RCT_EXPORT_METHOD(setStyle : (NSString *)style animated : (BOOL)animated)
{
UIStatusBarStyle statusBarStyle = [RCTConvert UIStatusBarStyle:style];
if (RCTViewControllerBasedStatusBarAppearance()) {
RCTLogError(@"RCTStatusBarManager module requires that the \
UIViewControllerBasedStatusBarAppearance key in the Info.plist is set to NO");
Expand All @@ -147,8 +158,9 @@ - (void)applicationWillChangeStatusBarFrame:(NSNotification *)notification
#pragma clang diagnostic pop
}

RCT_EXPORT_METHOD(setHidden : (BOOL)hidden withAnimation : (UIStatusBarAnimation)animation)
RCT_EXPORT_METHOD(setHidden : (BOOL)hidden withAnimation : (NSString *)withAnimation)
{
UIStatusBarAnimation animation = [RCTConvert UIStatusBarAnimation:withAnimation];
if (RCTViewControllerBasedStatusBarAppearance()) {
RCTLogError(@"RCTStatusBarManager module requires that the \
UIViewControllerBasedStatusBarAppearance key in the Info.plist is set to NO");
Expand All @@ -165,6 +177,28 @@ - (void)applicationWillChangeStatusBarFrame:(NSNotification *)notification
RCTSharedApplication().networkActivityIndicatorVisible = visible;
}

- (facebook::react::ModuleConstants<JS::NativeStatusBarManagerIOS::Constants>)getConstants
{
return facebook::react::typedConstants<JS::NativeStatusBarManagerIOS::Constants>({
.HEIGHT = RCTSharedApplication().statusBarFrame.size.height,
.DEFAULT_BACKGROUND_COLOR = folly::none,
});
}

- (facebook::react::ModuleConstants<JS::NativeStatusBarManagerIOS::Constants>)constantsToExport
{
return (facebook::react::ModuleConstants<JS::NativeStatusBarManagerIOS::Constants>)[self getConstants];
}

- (std::shared_ptr<facebook::react::TurboModule>)getTurboModuleWithJsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker
{
return std::make_shared<facebook::react::NativeStatusBarManagerIOSSpecJSI>(self, jsInvoker);
}

#endif // TARGET_OS_TV

@end

Class RCTStatusBarManagerCls(void) {
return RCTStatusBarManager.class;
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ public NativeStatusBarManagerIOSSpec(ReactApplicationContext reactContext) {
Map<String, Object> constants = getTypedExportedConstants();
if (ReactBuildConfig.DEBUG || ReactBuildConfig.IS_INTERNAL_BUILD) {
Set<String> obligatoryFlowConstants = new HashSet<>(Arrays.asList(
"DEFAULT_BACKGROUND_COLOR",
"HEIGHT"
));
Set<String> optionalFlowConstants = new HashSet<>();
Set<String> optionalFlowConstants = new HashSet<>(Arrays.asList(
"DEFAULT_BACKGROUND_COLOR"
));
Set<String> undeclaredConstants = new HashSet<>(constants.keySet());
undeclaredConstants.removeAll(obligatoryFlowConstants);
undeclaredConstants.removeAll(optionalFlowConstants);
Expand Down

0 comments on commit dc12676

Please sign in to comment.