How to create a custom chain in Swift Combine?

自作多情 提交于 2020-06-17 15:56:05

问题


I'm trying to create a LocationManager wrapper for Combine. I have a publisher and some functions that trigger the publisher. However, I'd like to combine them in one with a custom command.

Here's what I got so far:

@available(OSX 10.15, iOS 13, tvOS 13, watchOS 6, *)
public class LocationProxy: NSObject {
    private lazy var manager = CLLocationManager()

    private static let authorizationSubject = PassthroughSubject<Bool, Never>()
    public private(set) lazy var authorizationPublisher: AnyPublisher<Bool, Never> = Self.authorizationSubject.eraseToAnyPublisher()

    var isAuthorized: Bool { CLLocationManager.isAuthorized }

    func isAuthorized(for type: LocationAPI.AuthorizationType) -> Bool {
        guard CLLocationManager.locationServicesEnabled() else { return false }

        #if os(macOS)
        return type == .always && CLLocationManager.authorizationStatus() == .authorizedAlways
        #else
        return (type == .whenInUse && CLLocationManager.authorizationStatus() == .authorizedWhenInUse)
            || (type == .always && CLLocationManager.authorizationStatus() == .authorizedAlways)
        #endif
    }

    func requestAuthorization(for type: LocationAPI.AuthorizationType = .whenInUse) {
        // Handle authorized and exit
        guard !isAuthorized(for: type) else {
            Self.authorizationSubject.send(true)
            return
        }

        // Request appropiate authorization before exit
        defer {
            #if os(macOS)
            if #available(OSX 10.15, *) {
                manager.requestAlwaysAuthorization()
            }
            #elseif os(tvOS)
            manager.requestWhenInUseAuthorization()
            #else
            switch type {
            case .whenInUse:
                manager.requestWhenInUseAuthorization()
            case .always:
                manager.requestAlwaysAuthorization()
            }
            #endif
        }

        // Handle mismatched allowed and exit
        guard !isAuthorized else {
            // Process callback in case authorization dialog not launched by OS
            // since user will be notified first time only and ignored subsequently
            Self.authorizationSubject.send(false)
            return
        }

        // Handle denied and exit
        guard CLLocationManager.authorizationStatus() == .notDetermined else {
            Self.authorizationSubject.send(false)
            return
        }
    }

    public func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        guard status != .notDetermined else { return }
        Self.authorizationSubject.send(isAuthorized)
    }
}

To use it, I have to subscribe first, then call the request authorization function:

cancellable = locationProxy.authorizationPublisher
    .sink { status in
        print(status)
    }

locationProxy.requestAuthorization()

Is there a way to structure the code to subscribe and request authorization in one call, something like this:

cancellable = locationProxy.authorizationPublisher
    .sink { status in
        print(status)
    }
    .requestAuthorization()

回答1:


Your requestAuthorization should return the publisher:

func requestAuthorization(for type: LocationAPI.AuthorizationType = .whenInUse) -> AnyPublisher<Bool, Never> {
  // start authorization flow

  return authorizationPublisher
}

I would also recommend that you use a CurrentValueSubject instead of a PassthroughSubject so you can always get the "current" status when subscribing to the authorizationPublisher.



来源:https://stackoverflow.com/questions/62307501/how-to-create-a-custom-chain-in-swift-combine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!