Skip to content

Commit a1594b5

Browse files
committed
refactor: MainActor 호출 부분이 필요할 시 MainActor을 지정해주는 것으로 변경
1 parent 59ee9f7 commit a1594b5

2 files changed

Lines changed: 39 additions & 23 deletions

File tree

DevLog/Data/Common/Error+.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,7 @@ enum FirestoreError: Error, LocalizedError {
2121
}
2222
}
2323
}
24+
25+
enum UIError: Error {
26+
case notFoundTopViewController
27+
}

DevLog/Infra/Service/SocialLogin/GoogleAuthenticationService.swift

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,30 @@ final class GoogleAuthenticationService: AuthenticationService {
1717
private let functions = Functions.functions(region: "asia-northeast3")
1818
private let messaging = Messaging.messaging()
1919
private var user: User? { Auth.auth().currentUser }
20+
private let provider = TopViewControllerProvider()
2021

22+
@MainActor
2123
func signIn() async throws -> AuthenticationDataResponse {
22-
guard let topVC = topViewController() else {
23-
throw URLError(.cannotFindHost)
24+
guard let topViewController = provider.topViewController() else {
25+
throw UIError.notFoundTopViewController
2426
}
25-
26-
let gidSignIn = try await GIDSignIn.sharedInstance.signIn(withPresenting: topVC)
27-
28-
guard let idToken = gidSignIn.user.idToken?.tokenString else {
27+
28+
let signIn = try await GIDSignIn.sharedInstance.signIn(withPresenting: topViewController)
29+
30+
guard let idToken = signIn.user.idToken?.tokenString else {
2931
throw URLError(.badServerResponse)
3032
}
3133

32-
let accessToken = gidSignIn.user.accessToken.tokenString
34+
let accessToken = signIn.user.accessToken.tokenString
3335
let credential = GoogleAuthProvider.credential(withIDToken: idToken, accessToken: accessToken)
3436

3537
let result = try await Auth.auth().signIn(with: credential)
3638

37-
if let photoURL = gidSignIn.user.profile?.imageURL(withDimension: 200) {
39+
if let photoURL = signIn.user.profile?.imageURL(withDimension: 200) {
3840
let changeRequest = result.user.createProfileChangeRequest()
3941
changeRequest.photoURL = photoURL
40-
changeRequest.displayName = gidSignIn.user.profile?.name
41-
42+
changeRequest.displayName = signIn.user.profile?.name
43+
4244
try await changeRequest.commitChanges()
4345
}
4446

@@ -73,8 +75,11 @@ final class GoogleAuthenticationService: AuthenticationService {
7375
}
7476

7577
func link(uid: String, email: String) async throws {
76-
guard let topViewController = topViewController() else {
77-
throw URLError(.cannotFindHost)
78+
let topViewController = await MainActor.run {
79+
provider.topViewController()
80+
}
81+
guard let topViewController = topViewController else {
82+
throw UIError.notFoundTopViewController
7883
}
7984

8085
if GIDSignIn.sharedInstance.hasPreviousSignIn() {
@@ -110,27 +115,34 @@ final class GoogleAuthenticationService: AuthenticationService {
110115

111116
}
112117

113-
extension GoogleAuthenticationService {
114-
func topViewController(controller: UIViewController? = nil) -> UIViewController? {
118+
final class TopViewControllerProvider {
119+
@MainActor
120+
func topViewController() -> UIViewController? {
115121
let keyWindow = UIApplication.shared.connectedScenes
116122
.compactMap { $0 as? UIWindowScene }
117123
.flatMap { $0.windows }
118124
.first { $0.isKeyWindow }
119125

120-
let controller = controller ?? keyWindow?.rootViewController
121-
126+
let rootController = keyWindow?.rootViewController
127+
128+
return topViewController(controller: rootController)
129+
}
130+
131+
@MainActor
132+
private func topViewController(controller: UIViewController?) -> UIViewController? {
122133
if let navigationController = controller as? UINavigationController {
123134
return topViewController(controller: navigationController.visibleViewController)
124135
}
125-
126-
if let tabController = controller as? UITabBarController, let selected = tabController.selectedViewController {
127-
return topViewController(controller: selected)
136+
137+
if let tabController = controller as? UITabBarController,
138+
let selectedController = tabController.selectedViewController {
139+
return topViewController(controller: selectedController)
128140
}
129-
130-
if let presented = controller?.presentedViewController {
131-
return topViewController(controller: presented)
141+
142+
if let presentedController = controller?.presentedViewController {
143+
return topViewController(controller: presentedController)
132144
}
133-
145+
134146
return controller
135147
}
136148
}

0 commit comments

Comments
 (0)