mirror of https://github.com/oxen-io/session-ios
Merge branch 'mkirk/disable-proximity' into release/2.31.0
commit
2e3a32e7dc
@ -0,0 +1,59 @@
|
||||
//
|
||||
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||
//
|
||||
|
||||
@objc
|
||||
public protocol OWSProximityMonitoringManager: class {
|
||||
func add(lifetime: AnyObject)
|
||||
func remove(lifetime: AnyObject)
|
||||
}
|
||||
|
||||
@objc
|
||||
public class OWSProximityMonitoringManagerImpl: NSObject, OWSProximityMonitoringManager {
|
||||
var lifetimes: [Weak<AnyObject>] = []
|
||||
let serialQueue = DispatchQueue(label: "ProximityMonitoringManagerImpl")
|
||||
|
||||
// MARK:
|
||||
|
||||
var device: UIDevice {
|
||||
return UIDevice.current
|
||||
}
|
||||
|
||||
// MARK:
|
||||
|
||||
@objc
|
||||
public func add(lifetime: AnyObject) {
|
||||
serialQueue.sync {
|
||||
if !lifetimes.contains { $0.value === lifetime } {
|
||||
lifetimes.append(Weak(value: lifetime))
|
||||
}
|
||||
reconcile()
|
||||
}
|
||||
}
|
||||
|
||||
@objc
|
||||
public func remove(lifetime: AnyObject) {
|
||||
serialQueue.sync {
|
||||
lifetimes = lifetimes.filter { $0.value !== lifetime }
|
||||
reconcile()
|
||||
}
|
||||
}
|
||||
|
||||
func reconcile() {
|
||||
if _isDebugAssertConfiguration() {
|
||||
assertOnQueue(serialQueue)
|
||||
}
|
||||
lifetimes = lifetimes.filter { $0.value != nil }
|
||||
if lifetimes.isEmpty {
|
||||
Logger.debug("disabling proximity monitoring")
|
||||
DispatchQueue.main.async {
|
||||
self.device.isProximityMonitoringEnabled = false
|
||||
}
|
||||
} else {
|
||||
Logger.debug("enabling proximity monitoring for lifetimes: \(lifetimes)")
|
||||
DispatchQueue.main.async {
|
||||
self.device.isProximityMonitoringEnabled = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue