| 
 | 1 | +//  | 
 | 2 | +// Copyright © 2025 Stream.io Inc. All rights reserved.  | 
 | 3 | +//  | 
 | 4 | + | 
 | 5 | +import Combine  | 
 | 6 | +import Foundation  | 
 | 7 | + | 
 | 8 | +/// Manages proximity-related functionality for a call, including policy management  | 
 | 9 | +/// and proximity state observation. Only active on phone devices.  | 
 | 10 | +final class ProximityManager: @unchecked Sendable {  | 
 | 11 | + | 
 | 12 | +    @Injected(\.streamVideo) private var streamVideo  | 
 | 13 | +    @Injected(\.currentDevice) private var currentDevice  | 
 | 14 | +    @Injected(\.proximityMonitor) private var proximityMonitor  | 
 | 15 | + | 
 | 16 | +    /// Whether proximity monitoring is supported on the current device  | 
 | 17 | +    var isSupported: Bool { currentDevice.deviceType == .phone }  | 
 | 18 | + | 
 | 19 | +    /// Weak reference to the associated call  | 
 | 20 | +    private weak var call: Call?  | 
 | 21 | +    /// Thread-safe storage for registered proximity policies  | 
 | 22 | +    @Atomic private var policies: [ObjectIdentifier: any ProximityPolicy] = [:]  | 
 | 23 | + | 
 | 24 | +    /// Cancellable for active call observation  | 
 | 25 | +    private var activeCallCancellable: AnyCancellable?  | 
 | 26 | +    /// Cancellable for proximity state observation  | 
 | 27 | +    private var observationCancellable: AnyCancellable?  | 
 | 28 | + | 
 | 29 | +    /// Creates a new proximity manager for the specified call  | 
 | 30 | +    /// - Parameter call: Call instance to manage proximity for  | 
 | 31 | +    init(_ call: Call) {  | 
 | 32 | +        self.call = call  | 
 | 33 | + | 
 | 34 | +        if isSupported {  | 
 | 35 | +            activeCallCancellable = streamVideo  | 
 | 36 | +                .state  | 
 | 37 | +                .$activeCall  | 
 | 38 | +                .sinkTask { @MainActor [weak self] in self?.didUpdateActiveCall($0) }  | 
 | 39 | +        }  | 
 | 40 | +    }  | 
 | 41 | + | 
 | 42 | +    deinit {  | 
 | 43 | +        activeCallCancellable?.cancel()  | 
 | 44 | +        observationCancellable?.cancel()  | 
 | 45 | +    }  | 
 | 46 | + | 
 | 47 | +    // MARK: - Policies  | 
 | 48 | + | 
 | 49 | +    /// Adds a proximity policy to be managed by this instance  | 
 | 50 | +    /// - Parameter policy: Policy to add  | 
 | 51 | +    /// - Throws: ClientError if call is nil  | 
 | 52 | +    func add(_ policy: any ProximityPolicy) throws {  | 
 | 53 | +        guard isSupported else { return }  | 
 | 54 | + | 
 | 55 | +        guard let call else {  | 
 | 56 | +            throw ClientError("ProximityPolicy identifier:\(policy) cannot be attached while Call is nil.")  | 
 | 57 | +        }  | 
 | 58 | +        policies[type(of: policy).identifier] = policy  | 
 | 59 | +        log.info("ProximityPolicy identifier:\(policy) has been attached on Call id:\(call.callId) type:\(call.callType).")  | 
 | 60 | +    }  | 
 | 61 | + | 
 | 62 | +    /// Removes a proximity policy from management  | 
 | 63 | +    /// - Parameter policy: Policy to remove  | 
 | 64 | +    func remove(_ policy: any ProximityPolicy) {  | 
 | 65 | +        guard isSupported else { return }  | 
 | 66 | + | 
 | 67 | +        policies[type(of: policy).identifier] = nil  | 
 | 68 | + | 
 | 69 | +        guard let call else {  | 
 | 70 | +            return  | 
 | 71 | +        }  | 
 | 72 | +        log.info("ProximityPolicy identifier:\(policy) has been removed from Call id:\(call.callId) type:\(call.callType).")  | 
 | 73 | +    }  | 
 | 74 | + | 
 | 75 | +    // MARK: - Private Helpers  | 
 | 76 | + | 
 | 77 | +    /// Handles active call changes by starting/stopping proximity observation  | 
 | 78 | +    /// - Parameter activeCall: New active call or nil if no active call  | 
 | 79 | +    @MainActor  | 
 | 80 | +    private func didUpdateActiveCall(_ activeCall: Call?) {  | 
 | 81 | +        if  | 
 | 82 | +            let activeCall,  | 
 | 83 | +            call?.cId == activeCall.cId,  | 
 | 84 | +            policies.isEmpty == false,  | 
 | 85 | +            observationCancellable == nil {  | 
 | 86 | +            proximityMonitor.startObservation()  | 
 | 87 | +            observationCancellable = proximityMonitor  | 
 | 88 | +                .statePublisher  | 
 | 89 | +                .removeDuplicates()  | 
 | 90 | +                .sink { [weak self] in self?.didUpdateProximity($0) }  | 
 | 91 | +            log.info("Proximity observation has started.")  | 
 | 92 | +        } else if  | 
 | 93 | +            activeCall == nil,  | 
 | 94 | +            observationCancellable != nil  | 
 | 95 | +        {  | 
 | 96 | +            observationCancellable?.cancel()  | 
 | 97 | +            observationCancellable = nil  | 
 | 98 | +            proximityMonitor.stopObservation()  | 
 | 99 | +            log.info("Proximity observation has stopped.")  | 
 | 100 | +        } else {  | 
 | 101 | +            /* No-op */  | 
 | 102 | +        }  | 
 | 103 | +    }  | 
 | 104 | + | 
 | 105 | +    /// Notifies all registered policies of a proximity state change  | 
 | 106 | +    /// - Parameter proximity: New proximity state  | 
 | 107 | +    private func didUpdateProximity(_ proximity: ProximityState) {  | 
 | 108 | +        guard let call else {  | 
 | 109 | +            return  | 
 | 110 | +        }  | 
 | 111 | + | 
 | 112 | +        for policy in policies {  | 
 | 113 | +            policy.value.didUpdateProximity(proximity, on: call)  | 
 | 114 | +        }  | 
 | 115 | +    }  | 
 | 116 | +}  | 
0 commit comments