Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# OAuthKit
<img src="https://github.com/user-attachments/assets/039ee445-42af-433d-9793-56fc36330952" height="100" align="left"/>

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) 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.
<br clear="left"/>

## OAuthKit Usage
Expand Down
43 changes: 23 additions & 20 deletions Sources/OAuthKit/Network/NetworkMonitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bool, Never>()

/// 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)
}
}
18 changes: 6 additions & 12 deletions Sources/OAuthKit/OAuth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
//
// Created by Kevin McKee on 5/14/24.
//
import Combine
import Foundation
#if canImport(LocalAuthentication)
import LocalAuthentication
Expand Down Expand Up @@ -74,10 +73,6 @@ public final class OAuth {
@ObservationIgnored
var requireAuthenticationWithBiometricsOrCompanion: Bool = false

/// Combine subscribers.
@ObservationIgnored
private var subscribers = Set<AnyCancellable>()

/// The json decoder
@ObservationIgnored
private let decoder: JSONDecoder = .init()
Expand Down Expand Up @@ -229,7 +224,7 @@ private extension OAuth {
self.keychain = .init(applicationTag)
}
}
subscribe()
monitor()
restore()
}

Expand Down Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion Sources/OAuthKit/Views/OAWebViewCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
//

#if canImport(WebKit)
import Combine
import SwiftUI
import WebKit

Expand Down