2015-03-23 22:07:33 +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-03-12 19:51:44 +00:00
|
|
|
|
|
|
|
#import "RCTAppState.h"
|
|
|
|
|
|
|
|
#import "RCTAssert.h"
|
|
|
|
#import "RCTBridge.h"
|
|
|
|
#import "RCTEventDispatcher.h"
|
2015-09-22 17:43:56 +00:00
|
|
|
#import "RCTUtils.h"
|
2015-03-12 19:51:44 +00:00
|
|
|
|
|
|
|
static NSString *RCTCurrentAppBackgroundState()
|
|
|
|
{
|
2016-06-06 14:57:55 +00:00
|
|
|
RCTAssertMainQueue();
|
2016-05-23 16:08:51 +00:00
|
|
|
|
2015-03-12 19:51:44 +00:00
|
|
|
static NSDictionary *states;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
states = @{
|
|
|
|
@(UIApplicationStateActive): @"active",
|
2016-03-10 16:29:52 +00:00
|
|
|
@(UIApplicationStateBackground): @"background"
|
2015-03-12 19:51:44 +00:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2015-09-22 17:43:56 +00:00
|
|
|
if (RCTRunningInAppExtension()) {
|
|
|
|
return @"extension";
|
|
|
|
}
|
|
|
|
|
|
|
|
return states[@(RCTSharedApplication().applicationState)] ?: @"unknown";
|
2015-03-12 19:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@implementation RCTAppState
|
|
|
|
{
|
|
|
|
NSString *_lastKnownState;
|
|
|
|
}
|
|
|
|
|
2015-04-09 15:46:53 +00:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2016-05-23 16:08:51 +00:00
|
|
|
- (dispatch_queue_t)methodQueue
|
|
|
|
{
|
|
|
|
return dispatch_get_main_queue();
|
|
|
|
}
|
|
|
|
|
2015-03-12 19:51:44 +00:00
|
|
|
#pragma mark - Lifecycle
|
|
|
|
|
2016-05-23 16:08:51 +00:00
|
|
|
- (NSArray<NSString *> *)supportedEvents
|
2016-05-03 16:08:03 +00:00
|
|
|
{
|
2016-05-23 16:08:51 +00:00
|
|
|
return @[@"appStateDidChange", @"memoryWarning"];
|
2016-05-03 16:08:03 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 16:08:51 +00:00
|
|
|
- (void)startObserving
|
2015-03-12 19:51:44 +00:00
|
|
|
{
|
2015-11-25 11:09:00 +00:00
|
|
|
for (NSString *name in @[UIApplicationDidBecomeActiveNotification,
|
|
|
|
UIApplicationDidEnterBackgroundNotification,
|
2016-03-10 16:29:52 +00:00
|
|
|
UIApplicationDidFinishLaunchingNotification,
|
|
|
|
UIApplicationWillResignActiveNotification,
|
|
|
|
UIApplicationWillEnterForegroundNotification]) {
|
|
|
|
|
2015-05-31 21:33:59 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
2016-03-10 16:29:52 +00:00
|
|
|
selector:@selector(handleAppStateDidChange:)
|
2015-11-25 11:09:00 +00:00
|
|
|
name:name
|
2015-05-31 21:33:59 +00:00
|
|
|
object:nil];
|
2015-03-12 19:51:44 +00:00
|
|
|
}
|
2015-11-25 11:09:00 +00:00
|
|
|
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(handleMemoryWarning)
|
|
|
|
name:UIApplicationDidReceiveMemoryWarningNotification
|
|
|
|
object:nil];
|
2015-03-12 19:51:44 +00:00
|
|
|
}
|
|
|
|
|
2016-05-23 16:08:51 +00:00
|
|
|
- (void)stopObserving
|
2015-03-12 19:51:44 +00:00
|
|
|
{
|
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - App Notification Methods
|
|
|
|
|
2016-05-23 16:08:51 +00:00
|
|
|
- (void)handleMemoryWarning
|
|
|
|
{
|
|
|
|
[self sendEventWithName:@"memoryWarning" body:nil];
|
|
|
|
}
|
|
|
|
|
2016-03-10 16:29:52 +00:00
|
|
|
- (void)handleAppStateDidChange:(NSNotification *)notification
|
2015-03-12 19:51:44 +00:00
|
|
|
{
|
2016-03-10 16:29:52 +00:00
|
|
|
NSString *newState;
|
|
|
|
|
|
|
|
if ([notification.name isEqualToString:UIApplicationWillResignActiveNotification]) {
|
|
|
|
newState = @"inactive";
|
|
|
|
} else if ([notification.name isEqualToString:UIApplicationWillEnterForegroundNotification]) {
|
2016-03-22 01:38:13 +00:00
|
|
|
newState = @"background";
|
2016-03-10 16:29:52 +00:00
|
|
|
} else {
|
|
|
|
newState = RCTCurrentAppBackgroundState();
|
|
|
|
}
|
|
|
|
|
2015-03-12 19:51:44 +00:00
|
|
|
if (![newState isEqualToString:_lastKnownState]) {
|
|
|
|
_lastKnownState = newState;
|
2016-05-23 16:08:51 +00:00
|
|
|
[self sendEventWithName:@"appStateDidChange"
|
|
|
|
body:@{@"app_state": _lastKnownState}];
|
2015-03-12 19:51:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Public API
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current background/foreground state of the app
|
|
|
|
*/
|
2015-04-09 15:46:53 +00:00
|
|
|
RCT_EXPORT_METHOD(getCurrentAppState:(RCTResponseSenderBlock)callback
|
|
|
|
error:(__unused RCTResponseSenderBlock)error)
|
2015-03-12 19:51:44 +00:00
|
|
|
{
|
2016-05-23 16:08:51 +00:00
|
|
|
callback(@[@{@"app_state": RCTCurrentAppBackgroundState()}]);
|
2015-03-12 19:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|