call helper function to locate root vc from main thread

This commit is contained in:
Satindar Dhillon
2024-01-24 14:10:55 -08:00
parent 1550a43b81
commit 62df69fd26
2 changed files with 17 additions and 22 deletions

View File

@ -59,7 +59,8 @@ import Views
}
func handleGoogleAuth(authenticator: Authenticator) async {
let googleAuthResponse = await authenticator.handleGoogleAuth()
let presentingVC = presentingViewController()
let googleAuthResponse = await authenticator.handleGoogleAuth(presentingVC: presentingVC)
switch googleAuthResponse {
case let .loginError(error):
@ -71,3 +72,15 @@ import Views
}
}
}
@MainActor private func presentingViewController() -> PlatformViewController? {
#if os(iOS)
let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene
return scene?.windows
.filter(\.isKeyWindow)
.first?
.rootViewController
#elseif os(macOS)
return NSApplication.shared.windows.first
#endif
}

View File

@ -10,9 +10,9 @@ public enum GoogleAuthResponse {
}
extension Authenticator {
public func handleGoogleAuth() async -> GoogleAuthResponse {
public func handleGoogleAuth(presentingVC: PlatformViewController?) async -> GoogleAuthResponse {
let idToken = await withCheckedContinuation { continuation in
googleSignIn { continuation.resume(returning: $0) }
googleSignIn(presenting: presentingVC) { continuation.resume(returning: $0) }
}
guard let idToken = idToken else { return .loginError(error: .unauthorized) }
@ -50,13 +50,7 @@ extension Authenticator {
}
}
func googleSignIn(completion: @escaping (String?) -> Void) {
#if os(iOS)
let presenting = presentingViewController()
#else
let presenting = NSApplication.shared.windows.first
#endif
func googleSignIn(presenting: PlatformViewController?, completion: @escaping (String?) -> Void) {
guard let presenting = presenting else {
completion(nil)
return
@ -82,15 +76,3 @@ extension Authenticator {
}
}
}
private func presentingViewController() -> PlatformViewController? {
#if os(iOS)
let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene
return scene?.windows
.filter(\.isKeyWindow)
.first?
.rootViewController
#elseif os(macOS)
return nil
#endif
}