Skip to content

Commit

Permalink
adds support to use Combine with ConnectivityObserver
Browse files Browse the repository at this point in the history
  • Loading branch information
brennanMKE committed Nov 28, 2021
1 parent 729f468 commit a004580
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ This package includes `ConnectivityMonitor` which internally uses `NetworkMonito

Simply call the `start` function and provide a path handler to get updates. Call the `cancel` function to discontinue monitoring.

## Combine

Support for Combine is provided by `ConnectivityObserver` which has the same `start` and `cancel` functions as `ConnectivityMonitor` but it returns `AnyPublisher`. It can be used to observe path changes.

## Swift Package

This project is set up as a Swift Package which can be used by the [Swift Package Manager] (SPM) with Xcode. In your `Package.swift` add this package with the code below.
Expand Down
19 changes: 19 additions & 0 deletions Sources/ConnectivityKit/ConnectivityMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public enum ConnectivityInterfaceType: String {
public struct ConnectivityInterface {
public let name: String
public let type: ConnectivityInterfaceType

public init(name: String, type: ConnectivityInterfaceType) {
self.name = name
self.type = type
}
}

extension ConnectivityInterface: CustomStringConvertible {
Expand All @@ -44,6 +49,20 @@ public struct ConnectivityPath {
public let supportsDNS: Bool
public let supportsIPv4: Bool
public let supportsIPv6: Bool

public init(status: ConnectivityStatus = .unsatisfied,
availableInterfaces: [ConnectivityInterface] = [],
isExpensive: Bool = false,
supportsDNS: Bool = false,
supportsIPv4: Bool = false,
supportsIPv6: Bool = false) {
self.status = status
self.availableInterfaces = availableInterfaces
self.isExpensive = isExpensive
self.supportsDNS = supportsDNS
self.supportsIPv4 = supportsIPv4
self.supportsIPv6 = supportsIPv6
}
}

extension ConnectivityPath: CustomStringConvertible {
Expand Down
55 changes: 55 additions & 0 deletions Sources/ConnectivityKit/ConnectivityObserver.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#if canImport(Combine)

import Foundation
import Combine

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
public class ConnectivityObserver {
private var monitor: AnyConnectivityMonitor?
private var subject: PassthroughSubject<ConnectivityPath, Never>?

public init() {}

/// Starts monitor and returns a publisher
/// - Parameter pathUpdateQueue: queue which is runs the upate
/// - Returns: AnyPublisher for updates on path changes
public func start(pathUpdateQueue: DispatchQueue = .main) -> AnyPublisher<ConnectivityPath, Never> {
let publisher: AnyPublisher<ConnectivityPath, Never>

if let subject = subject {
publisher = subject.eraseToAnyPublisher()
} else {
let subject = PassthroughSubject<ConnectivityPath, Never>()
let monitor = NetworkMonitor()
monitor.start(pathUpdateQueue: pathUpdateQueue, pathUpdateHandler: handlePath(_:))

self.subject = subject
self.monitor = monitor

publisher = subject.eraseToAnyPublisher()
}

return publisher
}

/// Cancel monitoring changes to network path
public func cancel() {
guard let monitor = monitor, let subject = subject else {
return
}
defer {
self.subject = nil
self.monitor = nil
}

monitor.cancel()
subject.send(completion: .finished)
}

private func handlePath(_ path: ConnectivityPath) -> Void {
subject?.send(path)
}

}

#endif
3 changes: 3 additions & 0 deletions Sources/ConnectivityKit/NetworkMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ class NetworkMonitor: AnyConnectivityMonitor {
@available(iOS 12.0, macOS 10.14, tvOS 12.0, watchOS 6.0, *)
func cancel() {
guard let monitor = monitor else { return }
defer {
self.monitor = nil
}
monitor.cancel()
}

Expand Down

0 comments on commit a004580

Please sign in to comment.