-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopViewControllerProvider.swift
More file actions
47 lines (39 loc) · 1.37 KB
/
Copy pathTopViewControllerProvider.swift
File metadata and controls
47 lines (39 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//
// TopViewControllerProvider.swift
// DevLogInfra
//
// Created by 최윤진 on 2/12/26.
//
import UIKit
import DevLogData
final class TopViewControllerProvider: Sendable {
@MainActor
func topViewController() -> UIViewController? {
guard let keyWindow = keyWindow() else {
return nil
}
let rootController = keyWindow.rootViewController
return topViewController(controller: rootController)
}
@MainActor
func keyWindow() -> UIWindow? {
return UIApplication.shared.connectedScenes
.compactMap { $0 as? UIWindowScene }
.flatMap { $0.windows }
.first { $0.isKeyWindow }
}
@MainActor
private func topViewController(controller: UIViewController?) -> UIViewController? {
if let navigationController = controller as? UINavigationController {
return topViewController(controller: navigationController.visibleViewController)
}
if let tabController = controller as? UITabBarController,
let selectedController = tabController.selectedViewController {
return topViewController(controller: selectedController)
}
if let presentedController = controller?.presentedViewController {
return topViewController(controller: presentedController)
}
return controller
}
}