Skip to content

Commit

Permalink
Fix running iOS timers when the proximity sensor is engaged (#41262)
Browse files Browse the repository at this point in the history
Summary:
When the proximity sensor is engaged and it detects "close", the screen is disabled so timers stop working. Treat the close proximity status as if the app went into the background so CADisplayLink based timers are not used.

bypass-github-export-checks

## Changelog:

[iOS] [Fixed] - Fix running timers when the proximity sensor detects close

Pull Request resolved: #41262

Reviewed By: dmytrorykun

Differential Revision: D50839017

Pulled By: cipolleschi

fbshipit-source-id: 3f7dc47d346eb88b687c8219fc905cf2a42262fe
  • Loading branch information
saghul authored and facebook-github-bot committed Nov 2, 2023
1 parent a257e9f commit 79eac96
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion packages/react-native/React/CoreModules/RCTTiming.mm
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ - (void)setup
_timers = [NSMutableDictionary new];
_inBackground = NO;
RCTExecuteOnMainQueue(^{
if (!self->_inBackground && [RCTSharedApplication() applicationState] == UIApplicationStateBackground) {
if (!self->_inBackground &&
([RCTSharedApplication() applicationState] == UIApplicationStateBackground ||
[UIDevice currentDevice].proximityState)) {
[self appDidMoveToBackground];
}
});
Expand All @@ -151,6 +153,11 @@ - (void)setup
name:name
object:nil];
}

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(proximityChanged)
name:UIDeviceProximityStateDidChangeNotification
object:nil];
}

- (void)dealloc
Expand Down Expand Up @@ -187,6 +194,16 @@ - (void)appDidMoveToForeground
[self startTimers];
}

- (void)proximityChanged
{
BOOL isClose = [UIDevice currentDevice].proximityState;
if (isClose) {
[self appDidMoveToBackground];
} else {
[self appDidMoveToForeground];
}
}

- (void)stopTimers
{
if (_inBackground) {
Expand Down

0 comments on commit 79eac96

Please sign in to comment.