Unforked ExceptionsManager, AlertManager and AppState

This commit is contained in:
Nick Lockwood
2015-03-11 13:53:30 -07:00
parent 30d3117f96
commit 2424711a03
12 changed files with 376 additions and 10 deletions

View File

@@ -0,0 +1,7 @@
// Copyright 2004-present Facebook. All Rights Reserved.
#import "RCTBridgeModule.h"
@interface RCTAppState : NSObject<RCTBridgeModule>
@end

View File

@@ -0,0 +1,105 @@
// Copyright 2004-present Facebook. All Rights Reserved.
#import "RCTAppState.h"
#import "RCTAssert.h"
#import "RCTBridge.h"
#import "RCTEventDispatcher.h"
static NSString *RCTCurrentAppBackgroundState()
{
static NSDictionary *states;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
states = @{
@(UIApplicationStateActive): @"active",
@(UIApplicationStateBackground): @"background",
@(UIApplicationStateInactive): @"inactive"
};
});
return states[@([[UIApplication sharedApplication] applicationState])] ?: @"unknown";
}
@implementation RCTAppState
{
NSString *_lastKnownState;
}
@synthesize bridge = _bridge;
#pragma mark - Lifecycle
- (instancetype)init
{
if ((self = [super init])) {
_lastKnownState = RCTCurrentAppBackgroundState();
for (NSString *name in @[UIApplicationDidBecomeActiveNotification,
UIApplicationDidEnterBackgroundNotification,
UIApplicationDidFinishLaunchingNotification]) {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleAppStateDidChange)
name:name
object:nil];
}
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark - App Notification Methods
- (void)handleAppStateDidChange
{
NSString *newState = RCTCurrentAppBackgroundState();
if (![newState isEqualToString:_lastKnownState]) {
_lastKnownState = newState;
[_bridge.eventDispatcher sendDeviceEventWithName:@"appStateDidChange"
body:@{@"app_state": _lastKnownState}];
}
}
#pragma mark - Public API
/**
* Get the current background/foreground state of the app
*/
- (void)getCurrentAppState:(RCTResponseSenderBlock)callback
error:(__unused RCTResponseSenderBlock)error
{
RCT_EXPORT();
callback(@[@{@"app_state": _lastKnownState}]);
}
/**
* Update the application icon badge number on the home screen
*/
- (void)setApplicationIconBadgeNumber:(NSInteger)number
{
RCT_EXPORT();
[UIApplication sharedApplication].applicationIconBadgeNumber = number;
}
/**
* Get the current application icon badge number on the home screen
*/
- (void)getApplicationIconBadgeNumber:(RCTResponseSenderBlock)callback
{
RCT_EXPORT();
callback(@[
@([UIApplication sharedApplication].applicationIconBadgeNumber)
]);
}
@end

View File

@@ -4,6 +4,14 @@
#import "RCTBridgeModule.h"
@interface RCTExceptionsManager : NSObject <RCTBridgeModule>
@protocol RCTExceptionsManagerDelegate <NSObject>
- (void)unhandledJSExceptionWithMessage:(NSString *)message stack:(NSArray *)stack;
@end
@interface RCTExceptionsManager : NSObject <RCTBridgeModule>
- (instancetype)initWithDelegate:(id<RCTExceptionsManagerDelegate>)delegate NS_DESIGNATED_INITIALIZER;
@end

View File

@@ -5,12 +5,32 @@
#import "RCTRedBox.h"
@implementation RCTExceptionsManager
{
__weak id<RCTExceptionsManagerDelegate> _delegate;
}
- (instancetype)initWithDelegate:(id<RCTExceptionsManagerDelegate>)delegate
{
if ((self = [super init])) {
_delegate = delegate;
}
return self;
}
- (instancetype)init
{
return [self initWithDelegate:nil];
}
- (void)reportUnhandledExceptionWithMessage:(NSString *)message stack:(NSArray *)stack
{
RCT_EXPORT(reportUnhandledException);
[[RCTRedBox sharedInstance] showErrorMessage:message withStack:stack];
if (_delegate) {
[_delegate unhandledJSExceptionWithMessage:message stack:stack];
} else {
[[RCTRedBox sharedInstance] showErrorMessage:message withStack:stack];
}
}
- (void)updateExceptionMessage:(NSString *)message stack:(NSArray *)stack