mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-04-27 19:25:11 +08:00
Unforked ExceptionsManager, AlertManager and AppState
This commit is contained in:
7
ReactKit/Modules/RCTAppState.h
Normal file
7
ReactKit/Modules/RCTAppState.h
Normal file
@@ -0,0 +1,7 @@
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
|
||||
#import "RCTBridgeModule.h"
|
||||
|
||||
@interface RCTAppState : NSObject<RCTBridgeModule>
|
||||
|
||||
@end
|
||||
105
ReactKit/Modules/RCTAppState.m
Normal file
105
ReactKit/Modules/RCTAppState.m
Normal 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
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user