From 1c54d02ef4f478d5bc22871d0dbcf8cc2421f0d4 Mon Sep 17 00:00:00 2001 From: Kevin McKee Date: Tue, 1 Jul 2025 10:33:00 -0700 Subject: [PATCH 1/3] Converting the NetworkMonitor to @Observable --- Sources/OAuthKit/Network/NetworkMonitor.swift | 43 ++++++++++--------- Sources/OAuthKit/OAuth.swift | 18 +++----- 2 files changed, 29 insertions(+), 32 deletions(-) diff --git a/Sources/OAuthKit/Network/NetworkMonitor.swift b/Sources/OAuthKit/Network/NetworkMonitor.swift index 8a8a4a8..febdc39 100644 --- a/Sources/OAuthKit/Network/NetworkMonitor.swift +++ b/Sources/OAuthKit/Network/NetworkMonitor.swift @@ -5,43 +5,46 @@ // Created by Kevin McKee on 5/30/24. // -import Combine import Network +import Observation private let queueLabel = "oauthkit.NetworkMonitor" /// A type that broadcasts network reachability via Combine event publishing. -@MainActor final class NetworkMonitor { - - /// The private pass through publisher. - private var publisher = PassthroughSubject() - - /// Provides a pass through subject used to broadcast events to downstream subscribers. - /// The subject will automatically drop events if there are no subscribers, or its current demand is zero. - public lazy var networkStatus = publisher.eraseToAnyPublisher() +@MainActor +@Observable +final class NetworkMonitor { + @ObservationIgnored private let pathMonitor = NWPathMonitor() - private let queue = DispatchQueue(label: queueLabel) /// Returns true if the network has an available wifi interface. var onWifi = false /// Returns true if the network has an available cellular interface. var onCellular = false + /// Returns true if the network has an wired ethernet interface. + var onWiredEthernet = false + + /// Returns true if the network is online with any available interface. + var isOnline: Bool { + onWifi || onCellular || onWiredEthernet + } + + /// Initializer. + init() { } - /// Initializer that starts the network monitor and begins publishing updates. - init() { - pathMonitor.pathUpdateHandler = { [weak self] path in - guard let self else { return } - Task { @MainActor in - self.handle(path: path) - } + /// Starts the network monitor (conforms to AsyncSequence). + func start() async { + for await path in pathMonitor { + handle(path: path) } - pathMonitor.start(queue: queue) } - func handle(path: NWPath) { + /// Handles the snapshot view of the network path state. + /// - Parameter path: the snapshot view of the network path state + private func handle(path: NWPath) { onWifi = path.usesInterfaceType(.wifi) onCellular = path.usesInterfaceType(.cellular) - publisher.send(path.status == .satisfied) + onWiredEthernet = path.usesInterfaceType(.wiredEthernet) } } diff --git a/Sources/OAuthKit/OAuth.swift b/Sources/OAuthKit/OAuth.swift index 5505561..60e6dc3 100644 --- a/Sources/OAuthKit/OAuth.swift +++ b/Sources/OAuthKit/OAuth.swift @@ -4,7 +4,6 @@ // // Created by Kevin McKee on 5/14/24. // -import Combine import Foundation #if canImport(LocalAuthentication) import LocalAuthentication @@ -74,10 +73,6 @@ public final class OAuth { @ObservationIgnored var requireAuthenticationWithBiometricsOrCompanion: Bool = false - /// Combine subscribers. - @ObservationIgnored - private var subscribers = Set() - /// The json decoder @ObservationIgnored private let decoder: JSONDecoder = .init() @@ -229,7 +224,7 @@ private extension OAuth { self.keychain = .init(applicationTag) } } - subscribe() + monitor() restore() } @@ -270,12 +265,11 @@ private extension OAuth { #endif } - /// Subsribes to event publishers. - func subscribe() { - // Subscribe to network status events - networkMonitor.networkStatus.sink { (_) in - // TODO: Add Handler - }.store(in: &subscribers) + /// Starts the network monitor. + func monitor() { + Task { + await networkMonitor.start() + } } /// Publishes state on the main thread. From bc249d1a9cbd6fcb5998ebb2e25dacea51af1931 Mon Sep 17 00:00:00 2001 From: Kevin McKee Date: Tue, 1 Jul 2025 10:49:44 -0700 Subject: [PATCH 2/3] Removing Combine references. --- README.md | 2 +- Sources/OAuthKit/Views/OAWebViewCoordinator.swift | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index 9d7b3fe..9b221a9 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ # OAuthKit -OAuthKit is a contemporary, event-driven Swift Package that utilizes the [Observation](https://developer.apple.com/documentation/observation) and [Combine](https://developer.apple.com/documentation/combine) Frameworks to implement the observer design pattern and publish [OAuth 2.0](https://oauth.net/2/) events. This enables application developers to effortlessly configure OAuth Providers and concentrate on developing exceptional applications rather than being preoccupied with the intricacies of authorization flows. +OAuthKit is a contemporary, event-driven Swift Package that utilizes the [Observation](https://developer.apple.com/documentation/observation) Frameworks to implement the observer design pattern and publish [OAuth 2.0](https://oauth.net/2/) events. This enables application developers to effortlessly configure OAuth Providers and concentrate on developing exceptional applications rather than being preoccupied with the intricacies of authorization flows.
## OAuthKit Usage diff --git a/Sources/OAuthKit/Views/OAWebViewCoordinator.swift b/Sources/OAuthKit/Views/OAWebViewCoordinator.swift index 6e94e94..cf1992e 100644 --- a/Sources/OAuthKit/Views/OAWebViewCoordinator.swift +++ b/Sources/OAuthKit/Views/OAWebViewCoordinator.swift @@ -6,7 +6,6 @@ // #if canImport(WebKit) -import Combine import SwiftUI import WebKit From 2cd7b997fe33325404c875282a65bbaab94798fb Mon Sep 17 00:00:00 2001 From: Kevin McKee Date: Tue, 1 Jul 2025 10:51:40 -0700 Subject: [PATCH 3/3] README fix --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9b221a9..3f5f505 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ # OAuthKit -OAuthKit is a contemporary, event-driven Swift Package that utilizes the [Observation](https://developer.apple.com/documentation/observation) Frameworks to implement the observer design pattern and publish [OAuth 2.0](https://oauth.net/2/) events. This enables application developers to effortlessly configure OAuth Providers and concentrate on developing exceptional applications rather than being preoccupied with the intricacies of authorization flows. +OAuthKit is a contemporary, event-driven Swift Package that utilizes the [Observation](https://developer.apple.com/documentation/observation) Framework to implement the observer design pattern and publish [OAuth 2.0](https://oauth.net/2/) events. This enables application developers to effortlessly configure OAuth Providers and concentrate on developing exceptional applications rather than being preoccupied with the intricacies of authorization flows.
## OAuthKit Usage