@ -13,6 +13,16 @@ public class OWSProximityMonitoringManagerImpl: NSObject, OWSProximityMonitoring
var lifetimes : [ Weak < AnyObject > ] = [ ]
var lifetimes : [ Weak < AnyObject > ] = [ ]
let serialQueue = DispatchQueue ( label : " ProximityMonitoringManagerImpl " )
let serialQueue = DispatchQueue ( label : " ProximityMonitoringManagerImpl " )
public override init ( ) {
super . init ( )
// TODO: c h a n g e t o ` r u n N o w O r W h e n A p p W i l l B e c o m e R e a d y ` w h e n
// r e v e r s e i n t e g r a t i n g
AppReadiness . runNowOrWhenAppIsReady {
self . setup ( )
}
}
// MARK:
// MARK:
var device : UIDevice {
var device : UIDevice {
@ -39,20 +49,44 @@ public class OWSProximityMonitoringManagerImpl: NSObject, OWSProximityMonitoring
}
}
}
}
@objc
public func setup ( ) {
NotificationCenter . default . addObserver ( self , selector : #selector ( proximitySensorStateDidChange ( notification : ) ) , name : . UIDeviceProximityStateDidChange , object : nil )
}
@objc
func proximitySensorStateDidChange ( notification : Notification ) {
Logger . debug ( " " )
// T h i s i s c r a z y , b u t i f w e d i s a b l e ` d e v i c e . i s P r o x i m i t y M o n i t o r i n g E n a b l e d ` w h i l e
// ` d e v i c e . p r o x i m i t y S t a t e ` i s t r u e ( w h i l e t h e d e v i c e i s h e l d t o t h e e a r )
// t h e n ` d e v i c e . p r o x i m i t y S t a t e ` r e m a i n s t r u e , e v e n a f t e r w e b r i n g t h e p h o n e
// a w a y f r o m t h e e a r a n d r e - e n a b l e m o n i t o r i n g .
//
// T o r e s o l v e t h i s , w e w a i t t o d i s a b l e p r o x i m i t y m o n i t o r i n g u n t i l ` p r o x i m i t y S t a t e `
// i s f a l s e .
if self . device . proximityState {
self . add ( lifetime : self )
} else {
self . remove ( lifetime : self )
}
}
func reconcile ( ) {
func reconcile ( ) {
if _isDebugAssertConfiguration ( ) {
if _isDebugAssertConfiguration ( ) {
assertOnQueue ( serialQueue )
assertOnQueue ( serialQueue )
}
}
lifetimes = lifetimes . filter { $0 . value != nil }
lifetimes = lifetimes . filter { $0 . value != nil }
if lifetimes . isEmpty {
if lifetimes . isEmpty {
Logger . debug ( " disabling proximity monitoring " )
DispatchQueue . main . async {
DispatchQueue . main . async {
Logger . debug ( " disabling proximity monitoring " )
self . device . isProximityMonitoringEnabled = false
self . device . isProximityMonitoringEnabled = false
}
}
} else {
} else {
Logger . debug ( " enabling proximity monitoring for lifetimes: \( lifetimes ) " )
let lifetimes = self . lifetimes
DispatchQueue . main . async {
DispatchQueue . main . async {
Logger . debug ( " willEnable proximity monitoring for lifetimes: \( lifetimes ) , proximityState: \( self . device . proximityState ) " )
self . device . isProximityMonitoringEnabled = true
self . device . isProximityMonitoringEnabled = true
Logger . debug ( " didEnable proximity monitoring for lifetimes: \( lifetimes ) , proximityState: \( self . device . proximityState ) " )
}
}
}
}
}
}