1// Copyright 2015-present 650 Industries. All rights reserved. 2 3#import "EXAppState.h" 4 5#import <React/RCTAssert.h> 6#import <React/RCTBridge.h> 7#import <React/RCTEventDispatcher.h> 8#import <React/RCTUtils.h> 9 10@interface EXAppState () 11 12@property (nonatomic, assign) BOOL isObserving; 13 14@end 15 16@implementation EXAppState 17 18+ (NSString *)moduleName { return @"RCTAppState"; } 19 20- (instancetype)init 21{ 22 if (self = [super init]) { 23 _lastKnownState = @"active"; 24 } 25 return self; 26} 27 28+ (BOOL)requiresMainQueueSetup 29{ 30 return YES; 31} 32 33- (dispatch_queue_t)methodQueue 34{ 35 return dispatch_get_main_queue(); 36} 37 38- (NSDictionary *)constantsToExport 39{ 40 return @{@"initialAppState": @"active"}; 41} 42 43#pragma mark - Lifecycle 44 45- (NSArray<NSString *> *)supportedEvents 46{ 47 return @[@"appStateDidChange", @"memoryWarning"]; 48} 49 50- (void)startObserving 51{ 52 _isObserving = YES; 53 [[NSNotificationCenter defaultCenter] addObserver:self 54 selector:@selector(handleMemoryWarning) 55 name:UIApplicationDidReceiveMemoryWarningNotification 56 object:nil]; 57} 58 59- (void)stopObserving 60{ 61 _isObserving = NO; 62 [[NSNotificationCenter defaultCenter] removeObserver:self]; 63} 64 65#pragma mark - App Notification Methods 66 67- (void)handleMemoryWarning 68{ 69 [self sendEventWithName:@"memoryWarning" body:nil]; 70} 71 72- (void)setState:(NSString *)state 73{ 74 if (![state isEqualToString:_lastKnownState]) { 75 _lastKnownState = state; 76 if (_isObserving) { 77 [self sendEventWithName:@"appStateDidChange" 78 body:@{@"app_state": _lastKnownState}]; 79 } 80 } 81} 82 83RCT_EXPORT_METHOD(getCurrentAppState:(RCTResponseSenderBlock)callback 84 error:(__unused RCTResponseSenderBlock)error) 85{ 86 callback(@[@{@"app_state": _lastKnownState}]); 87} 88 89@end 90