1// Copyright © 2018 650 Industries. All rights reserved.
2
3#import "EXScopedReactNativeAdapter.h"
4#import "EXUnversioned.h"
5
6@interface EXReactNativeAdapter (Protected)
7
8- (void)handleAppStateDidChange:(NSNotification *)notification;
9
10@end
11
12@interface EXScopedReactNativeAdapter ()
13
14// property inherited from EXReactNativeAdapter
15@property (nonatomic, assign) BOOL isForegrounded;
16
17@end
18
19@implementation EXScopedReactNativeAdapter
20
21@dynamic isForegrounded;
22
23- (void)setBridge:(RCTBridge *)bridge
24{
25  if (bridge) {
26    [super setBridge:bridge];
27
28    [[NSNotificationCenter defaultCenter] addObserver:self
29                                             selector:@selector(handleAppStateDidChange:)
30                                                 name:EX_UNVERSIONED(@"EXKernelBridgeDidForegroundNotification")
31                                               object:self.bridge];
32    [[NSNotificationCenter defaultCenter] addObserver:self
33                                             selector:@selector(handleAppStateDidChange:)
34                                                 name:EX_UNVERSIONED(@"EXKernelBridgeDidBackgroundNotification")
35                                               object:self.bridge];
36    [self setAppStateToForeground];
37  } else {
38    [[NSNotificationCenter defaultCenter] removeObserver:self];
39  }
40}
41
42- (void)dealloc
43{
44  [[NSNotificationCenter defaultCenter] removeObserver:self];
45}
46
47- (void)handleAppStateDidChange:(NSNotification *)notification
48{
49  if (!self.isForegrounded && [notification.name isEqualToString:EX_UNVERSIONED(@"EXKernelBridgeDidForegroundNotification")]) {
50    [self setAppStateToForeground];
51  } else if (self.isForegrounded && [notification.name isEqualToString:EX_UNVERSIONED(@"EXKernelBridgeDidBackgroundNotification")]) {
52    [self setAppStateToBackground];
53  } else {
54    [super handleAppStateDidChange:notification];
55  }
56}
57
58@end
59