Skip to content

Commit

Permalink
observe app lifecycle only on Apple platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev committed Oct 10, 2024
1 parent 1f074e4 commit 6f8e37a
Showing 1 changed file with 41 additions and 36 deletions.
77 changes: 41 additions & 36 deletions Sources/Auth/AuthClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,67 +80,72 @@ public final class AuthClient: Sendable {
observeAppLifecycleChanges()
}

private func observeAppLifecycleChanges() {
#if canImport(UIKit)
#if canImport(WatchKit)
if #available(watchOS 7.0, *) {
#if canImport(ObjectiveC)
private func observeAppLifecycleChanges() {
#if canImport(UIKit)
#if canImport(WatchKit)
if #available(watchOS 7.0, *) {
NotificationCenter.default.addObserver(
self,
selector: #selector(handleDidBecomeActive),
name: WKExtension.applicationDidBecomeActiveNotification,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(handleWillResignActive),
name: WKExtension.applicationWillResignActiveNotification,
object: nil
)
}
#else
NotificationCenter.default.addObserver(
self,
selector: #selector(handleDidBecomeActive),
name: WKExtension.applicationDidBecomeActiveNotification,
name: UIApplication.didBecomeActiveNotification,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(handleWillResignActive),
name: WKExtension.applicationWillResignActiveNotification,
name: UIApplication.willResignActiveNotification,
object: nil
)
}
#else
#endif
#elseif canImport(AppKit)
NotificationCenter.default.addObserver(
self,
selector: #selector(handleDidBecomeActive),
name: UIApplication.didBecomeActiveNotification,
name: NSApplication.didBecomeActiveNotification,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(handleWillResignActive),
name: UIApplication.willResignActiveNotification,
name: NSApplication.willResignActiveNotification,
object: nil
)
#endif
#elseif canImport(AppKit)
NotificationCenter.default.addObserver(
self,
selector: #selector(handleDidBecomeActive),
name: NSApplication.didBecomeActiveNotification,
object: nil
)
NotificationCenter.default.addObserver(
self,
selector: #selector(handleWillResignActive),
name: NSApplication.willResignActiveNotification,
object: nil
)
#endif
}

@objc
private func handleDidBecomeActive() {
if configuration.autoRefreshToken {
startAutoRefresh()
}
}

@objc
private func handleWillResignActive() {
if configuration.autoRefreshToken {
stopAutoRefresh()
@objc
private func handleDidBecomeActive() {
if configuration.autoRefreshToken {
startAutoRefresh()
}
}
}

@objc
private func handleWillResignActive() {
if configuration.autoRefreshToken {
stopAutoRefresh()
}
}
#else
private func observeAppLifecycleChanges() {
// no-op
}
#endif
/// Listen for auth state changes.
/// - Parameter listener: Block that executes when a new event is emitted.
/// - Returns: A handle that can be used to manually unsubscribe.
Expand Down

0 comments on commit 6f8e37a

Please sign in to comment.