2015-03-23 20:28:42 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
#import "RCTStatusBarManager.h"
|
|
|
|
|
2015-08-19 13:37:53 +00:00
|
|
|
#import "RCTEventDispatcher.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
#import "RCTLog.h"
|
2015-09-22 17:43:56 +00:00
|
|
|
#import "RCTUtils.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2016-10-16 22:37:45 +00:00
|
|
|
#if !TARGET_OS_TV
|
2015-06-05 15:46:17 +00:00
|
|
|
@implementation RCTConvert (UIStatusBar)
|
|
|
|
|
|
|
|
RCT_ENUM_CONVERTER(UIStatusBarStyle, (@{
|
|
|
|
@"default": @(UIStatusBarStyleDefault),
|
|
|
|
@"light-content": @(UIStatusBarStyleLightContent),
|
2016-09-30 19:49:05 +00:00
|
|
|
@"dark-content": @(UIStatusBarStyleDefault),
|
2015-06-05 15:46:17 +00:00
|
|
|
}), UIStatusBarStyleDefault, integerValue);
|
|
|
|
|
|
|
|
RCT_ENUM_CONVERTER(UIStatusBarAnimation, (@{
|
|
|
|
@"none": @(UIStatusBarAnimationNone),
|
|
|
|
@"fade": @(UIStatusBarAnimationFade),
|
|
|
|
@"slide": @(UIStatusBarAnimationSlide),
|
|
|
|
}), UIStatusBarAnimationNone, integerValue);
|
|
|
|
|
|
|
|
@end
|
2016-10-16 22:37:45 +00:00
|
|
|
#endif
|
2015-06-05 15:46:17 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@implementation RCTStatusBarManager
|
|
|
|
|
|
|
|
static BOOL RCTViewControllerBasedStatusBarAppearance()
|
|
|
|
{
|
|
|
|
static BOOL value;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
2015-06-05 15:46:17 +00:00
|
|
|
value = [[[NSBundle mainBundle] objectForInfoDictionaryKey:
|
|
|
|
@"UIViewControllerBasedStatusBarAppearance"] ?: @YES boolValue];
|
2015-02-20 04:10:52 +00:00
|
|
|
});
|
2015-03-11 02:03:59 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2015-04-08 12:42:43 +00:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
Added native event emitter
Summary:
This is a solution for the problem I raised in https://www.facebook.com/groups/react.native.community/permalink/768218933313687/
I've added a new native base class, `RCTEventEmitter` as well as an equivalent JS class/module `NativeEventEmitter` (RCTEventEmitter.js and EventEmitter.js were taken already).
Instead of arbitrary modules sending events via `bridge.eventDispatcher`, the idea is that any module that sends events should now subclass `RCTEventEmitter`, and provide an equivalent JS module that subclasses `NativeEventEmitter`.
JS code that wants to observe the events should now observe it via the specific JS module rather than via `RCTDeviceEventEmitter` directly. e.g. to observer a keyboard event, instead of writing:
const RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
RCTDeviceEventEmitter.addListener('keyboardWillShow', (event) => { ... });
You'd now write:
const Keyboard = require('Keyboard');
Keyboard.addListener('keyboardWillShow', (event) => { ... });
Within a component, you can also use the `Subscribable.Mixin` as you would previously, but instead of:
this.addListenerOn(RCTDeviceEventEmitter, 'keyboardWillShow', ...);
Write:
this.addListenerOn(Keyboard, 'keyboardWillShow', ...);
This approach allows the native `RCTKeyboardObserver` module to be created lazily the first time a listener is added, and to stop sending events when the last listener is removed. It also allows us to validate that the event strings being observed and omitted match the supported events for that module.
As a proof-of-concept, I've converted the `RCTStatusBarManager` and `RCTKeyboardObserver` modules to use the new system. I'll convert the rest in a follow up diff.
For now, the new `NativeEventEmitter` JS module wraps the `RCTDeviceEventEmitter` JS module, and just uses the native `RCTEventEmitter` module for bookkeeping. This allows for full backwards compatibility (code that is observing the event via `RCTDeviceEventEmitter` instead of the specific module will still work as expected, albeit with a warning). Once all legacy calls have been removed, this could be refactored to something more elegant internally, whilst maintaining the same public interface.
Note: currently, all device events still share a single global namespace, since they're really all registered on the same emitter instance internally. We should move away from that as soon as possible because it's not intuitive and will likely lead to strange bugs if people add generic events such as "onChange" or "onError" to their modules (which is common practice for components, where it's not a problem).
Reviewed By: javache
Differential Revision: D3269966
fbshipit-source-id: 1412daba850cd373020e1086673ba38ef9193050
2016-05-11 13:26:53 +00:00
|
|
|
- (NSArray<NSString *> *)supportedEvents
|
2016-05-04 14:06:09 +00:00
|
|
|
{
|
Added native event emitter
Summary:
This is a solution for the problem I raised in https://www.facebook.com/groups/react.native.community/permalink/768218933313687/
I've added a new native base class, `RCTEventEmitter` as well as an equivalent JS class/module `NativeEventEmitter` (RCTEventEmitter.js and EventEmitter.js were taken already).
Instead of arbitrary modules sending events via `bridge.eventDispatcher`, the idea is that any module that sends events should now subclass `RCTEventEmitter`, and provide an equivalent JS module that subclasses `NativeEventEmitter`.
JS code that wants to observe the events should now observe it via the specific JS module rather than via `RCTDeviceEventEmitter` directly. e.g. to observer a keyboard event, instead of writing:
const RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
RCTDeviceEventEmitter.addListener('keyboardWillShow', (event) => { ... });
You'd now write:
const Keyboard = require('Keyboard');
Keyboard.addListener('keyboardWillShow', (event) => { ... });
Within a component, you can also use the `Subscribable.Mixin` as you would previously, but instead of:
this.addListenerOn(RCTDeviceEventEmitter, 'keyboardWillShow', ...);
Write:
this.addListenerOn(Keyboard, 'keyboardWillShow', ...);
This approach allows the native `RCTKeyboardObserver` module to be created lazily the first time a listener is added, and to stop sending events when the last listener is removed. It also allows us to validate that the event strings being observed and omitted match the supported events for that module.
As a proof-of-concept, I've converted the `RCTStatusBarManager` and `RCTKeyboardObserver` modules to use the new system. I'll convert the rest in a follow up diff.
For now, the new `NativeEventEmitter` JS module wraps the `RCTDeviceEventEmitter` JS module, and just uses the native `RCTEventEmitter` module for bookkeeping. This allows for full backwards compatibility (code that is observing the event via `RCTDeviceEventEmitter` instead of the specific module will still work as expected, albeit with a warning). Once all legacy calls have been removed, this could be refactored to something more elegant internally, whilst maintaining the same public interface.
Note: currently, all device events still share a single global namespace, since they're really all registered on the same emitter instance internally. We should move away from that as soon as possible because it's not intuitive and will likely lead to strange bugs if people add generic events such as "onChange" or "onError" to their modules (which is common practice for components, where it's not a problem).
Reviewed By: javache
Differential Revision: D3269966
fbshipit-source-id: 1412daba850cd373020e1086673ba38ef9193050
2016-05-11 13:26:53 +00:00
|
|
|
return @[@"statusBarFrameDidChange",
|
|
|
|
@"statusBarFrameWillChange"];
|
2016-05-04 14:06:09 +00:00
|
|
|
}
|
|
|
|
|
2016-10-16 22:37:45 +00:00
|
|
|
#if !TARGET_OS_TV
|
|
|
|
|
Added native event emitter
Summary:
This is a solution for the problem I raised in https://www.facebook.com/groups/react.native.community/permalink/768218933313687/
I've added a new native base class, `RCTEventEmitter` as well as an equivalent JS class/module `NativeEventEmitter` (RCTEventEmitter.js and EventEmitter.js were taken already).
Instead of arbitrary modules sending events via `bridge.eventDispatcher`, the idea is that any module that sends events should now subclass `RCTEventEmitter`, and provide an equivalent JS module that subclasses `NativeEventEmitter`.
JS code that wants to observe the events should now observe it via the specific JS module rather than via `RCTDeviceEventEmitter` directly. e.g. to observer a keyboard event, instead of writing:
const RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
RCTDeviceEventEmitter.addListener('keyboardWillShow', (event) => { ... });
You'd now write:
const Keyboard = require('Keyboard');
Keyboard.addListener('keyboardWillShow', (event) => { ... });
Within a component, you can also use the `Subscribable.Mixin` as you would previously, but instead of:
this.addListenerOn(RCTDeviceEventEmitter, 'keyboardWillShow', ...);
Write:
this.addListenerOn(Keyboard, 'keyboardWillShow', ...);
This approach allows the native `RCTKeyboardObserver` module to be created lazily the first time a listener is added, and to stop sending events when the last listener is removed. It also allows us to validate that the event strings being observed and omitted match the supported events for that module.
As a proof-of-concept, I've converted the `RCTStatusBarManager` and `RCTKeyboardObserver` modules to use the new system. I'll convert the rest in a follow up diff.
For now, the new `NativeEventEmitter` JS module wraps the `RCTDeviceEventEmitter` JS module, and just uses the native `RCTEventEmitter` module for bookkeeping. This allows for full backwards compatibility (code that is observing the event via `RCTDeviceEventEmitter` instead of the specific module will still work as expected, albeit with a warning). Once all legacy calls have been removed, this could be refactored to something more elegant internally, whilst maintaining the same public interface.
Note: currently, all device events still share a single global namespace, since they're really all registered on the same emitter instance internally. We should move away from that as soon as possible because it's not intuitive and will likely lead to strange bugs if people add generic events such as "onChange" or "onError" to their modules (which is common practice for components, where it's not a problem).
Reviewed By: javache
Differential Revision: D3269966
fbshipit-source-id: 1412daba850cd373020e1086673ba38ef9193050
2016-05-11 13:26:53 +00:00
|
|
|
- (void)startObserving
|
2015-08-19 13:37:53 +00:00
|
|
|
{
|
2015-11-25 11:09:00 +00:00
|
|
|
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
|
|
|
|
[nc addObserver:self selector:@selector(applicationDidChangeStatusBarFrame:) name:UIApplicationDidChangeStatusBarFrameNotification object:nil];
|
|
|
|
[nc addObserver:self selector:@selector(applicationWillChangeStatusBarFrame:) name:UIApplicationWillChangeStatusBarFrameNotification object:nil];
|
2015-08-19 13:37:53 +00:00
|
|
|
}
|
|
|
|
|
Added native event emitter
Summary:
This is a solution for the problem I raised in https://www.facebook.com/groups/react.native.community/permalink/768218933313687/
I've added a new native base class, `RCTEventEmitter` as well as an equivalent JS class/module `NativeEventEmitter` (RCTEventEmitter.js and EventEmitter.js were taken already).
Instead of arbitrary modules sending events via `bridge.eventDispatcher`, the idea is that any module that sends events should now subclass `RCTEventEmitter`, and provide an equivalent JS module that subclasses `NativeEventEmitter`.
JS code that wants to observe the events should now observe it via the specific JS module rather than via `RCTDeviceEventEmitter` directly. e.g. to observer a keyboard event, instead of writing:
const RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
RCTDeviceEventEmitter.addListener('keyboardWillShow', (event) => { ... });
You'd now write:
const Keyboard = require('Keyboard');
Keyboard.addListener('keyboardWillShow', (event) => { ... });
Within a component, you can also use the `Subscribable.Mixin` as you would previously, but instead of:
this.addListenerOn(RCTDeviceEventEmitter, 'keyboardWillShow', ...);
Write:
this.addListenerOn(Keyboard, 'keyboardWillShow', ...);
This approach allows the native `RCTKeyboardObserver` module to be created lazily the first time a listener is added, and to stop sending events when the last listener is removed. It also allows us to validate that the event strings being observed and omitted match the supported events for that module.
As a proof-of-concept, I've converted the `RCTStatusBarManager` and `RCTKeyboardObserver` modules to use the new system. I'll convert the rest in a follow up diff.
For now, the new `NativeEventEmitter` JS module wraps the `RCTDeviceEventEmitter` JS module, and just uses the native `RCTEventEmitter` module for bookkeeping. This allows for full backwards compatibility (code that is observing the event via `RCTDeviceEventEmitter` instead of the specific module will still work as expected, albeit with a warning). Once all legacy calls have been removed, this could be refactored to something more elegant internally, whilst maintaining the same public interface.
Note: currently, all device events still share a single global namespace, since they're really all registered on the same emitter instance internally. We should move away from that as soon as possible because it's not intuitive and will likely lead to strange bugs if people add generic events such as "onChange" or "onError" to their modules (which is common practice for components, where it's not a problem).
Reviewed By: javache
Differential Revision: D3269966
fbshipit-source-id: 1412daba850cd373020e1086673ba38ef9193050
2016-05-11 13:26:53 +00:00
|
|
|
- (void)stopObserving
|
2015-08-19 13:37:53 +00:00
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
2015-04-20 19:06:02 +00:00
|
|
|
- (dispatch_queue_t)methodQueue
|
|
|
|
{
|
|
|
|
return dispatch_get_main_queue();
|
|
|
|
}
|
|
|
|
|
2015-08-19 13:37:53 +00:00
|
|
|
- (void)emitEvent:(NSString *)eventName forNotification:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
CGRect frame = [notification.userInfo[UIApplicationStatusBarFrameUserInfoKey] CGRectValue];
|
|
|
|
NSDictionary *event = @{
|
|
|
|
@"frame": @{
|
2015-11-16 11:15:31 +00:00
|
|
|
@"x": @(frame.origin.x),
|
|
|
|
@"y": @(frame.origin.y),
|
|
|
|
@"width": @(frame.size.width),
|
|
|
|
@"height": @(frame.size.height),
|
2015-08-19 13:37:53 +00:00
|
|
|
},
|
|
|
|
};
|
Added native event emitter
Summary:
This is a solution for the problem I raised in https://www.facebook.com/groups/react.native.community/permalink/768218933313687/
I've added a new native base class, `RCTEventEmitter` as well as an equivalent JS class/module `NativeEventEmitter` (RCTEventEmitter.js and EventEmitter.js were taken already).
Instead of arbitrary modules sending events via `bridge.eventDispatcher`, the idea is that any module that sends events should now subclass `RCTEventEmitter`, and provide an equivalent JS module that subclasses `NativeEventEmitter`.
JS code that wants to observe the events should now observe it via the specific JS module rather than via `RCTDeviceEventEmitter` directly. e.g. to observer a keyboard event, instead of writing:
const RCTDeviceEventEmitter = require('RCTDeviceEventEmitter');
RCTDeviceEventEmitter.addListener('keyboardWillShow', (event) => { ... });
You'd now write:
const Keyboard = require('Keyboard');
Keyboard.addListener('keyboardWillShow', (event) => { ... });
Within a component, you can also use the `Subscribable.Mixin` as you would previously, but instead of:
this.addListenerOn(RCTDeviceEventEmitter, 'keyboardWillShow', ...);
Write:
this.addListenerOn(Keyboard, 'keyboardWillShow', ...);
This approach allows the native `RCTKeyboardObserver` module to be created lazily the first time a listener is added, and to stop sending events when the last listener is removed. It also allows us to validate that the event strings being observed and omitted match the supported events for that module.
As a proof-of-concept, I've converted the `RCTStatusBarManager` and `RCTKeyboardObserver` modules to use the new system. I'll convert the rest in a follow up diff.
For now, the new `NativeEventEmitter` JS module wraps the `RCTDeviceEventEmitter` JS module, and just uses the native `RCTEventEmitter` module for bookkeeping. This allows for full backwards compatibility (code that is observing the event via `RCTDeviceEventEmitter` instead of the specific module will still work as expected, albeit with a warning). Once all legacy calls have been removed, this could be refactored to something more elegant internally, whilst maintaining the same public interface.
Note: currently, all device events still share a single global namespace, since they're really all registered on the same emitter instance internally. We should move away from that as soon as possible because it's not intuitive and will likely lead to strange bugs if people add generic events such as "onChange" or "onError" to their modules (which is common practice for components, where it's not a problem).
Reviewed By: javache
Differential Revision: D3269966
fbshipit-source-id: 1412daba850cd373020e1086673ba38ef9193050
2016-05-11 13:26:53 +00:00
|
|
|
[self sendEventWithName:eventName body:event];
|
2015-08-19 13:37:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)applicationDidChangeStatusBarFrame:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
[self emitEvent:@"statusBarFrameDidChange" forNotification:notification];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)applicationWillChangeStatusBarFrame:(NSNotification *)notification
|
|
|
|
{
|
|
|
|
[self emitEvent:@"statusBarFrameWillChange" forNotification:notification];
|
|
|
|
}
|
|
|
|
|
2016-01-04 12:30:37 +00:00
|
|
|
RCT_EXPORT_METHOD(getHeight:(RCTResponseSenderBlock)callback)
|
|
|
|
{
|
|
|
|
callback(@[@{
|
2017-04-01 10:16:52 +00:00
|
|
|
@"height": @(RCTSharedApplication().statusBarFrame.size.height),
|
2016-01-04 12:30:37 +00:00
|
|
|
}]);
|
|
|
|
}
|
|
|
|
|
2015-04-08 15:52:48 +00:00
|
|
|
RCT_EXPORT_METHOD(setStyle:(UIStatusBarStyle)statusBarStyle
|
|
|
|
animated:(BOOL)animated)
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-04-18 17:43:20 +00:00
|
|
|
if (RCTViewControllerBasedStatusBarAppearance()) {
|
|
|
|
RCTLogError(@"RCTStatusBarManager module requires that the \
|
|
|
|
UIViewControllerBasedStatusBarAppearance key in the Info.plist is set to NO");
|
|
|
|
} else {
|
2015-09-22 17:43:56 +00:00
|
|
|
[RCTSharedApplication() setStatusBarStyle:statusBarStyle
|
|
|
|
animated:animated];
|
2015-04-18 17:43:20 +00:00
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 15:52:48 +00:00
|
|
|
RCT_EXPORT_METHOD(setHidden:(BOOL)hidden
|
|
|
|
withAnimation:(UIStatusBarAnimation)animation)
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
2015-04-18 17:43:20 +00:00
|
|
|
if (RCTViewControllerBasedStatusBarAppearance()) {
|
|
|
|
RCTLogError(@"RCTStatusBarManager module requires that the \
|
|
|
|
UIViewControllerBasedStatusBarAppearance key in the Info.plist is set to NO");
|
|
|
|
} else {
|
2015-09-22 17:43:56 +00:00
|
|
|
[RCTSharedApplication() setStatusBarHidden:hidden
|
|
|
|
withAnimation:animation];
|
2015-04-18 17:43:20 +00:00
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-07-23 23:19:27 +00:00
|
|
|
RCT_EXPORT_METHOD(setNetworkActivityIndicatorVisible:(BOOL)visible)
|
|
|
|
{
|
2015-09-22 17:43:56 +00:00
|
|
|
RCTSharedApplication().networkActivityIndicatorVisible = visible;
|
2015-07-23 23:19:27 +00:00
|
|
|
}
|
|
|
|
|
2016-10-16 22:37:45 +00:00
|
|
|
#endif //TARGET_OS_TV
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@end
|