|
8 | 8 | import Foundation |
9 | 9 | import UIKit |
10 | 10 |
|
11 | | -public protocol Routable { |
12 | | - func navigate(to route: AppRoute, from: Routable?, with data: [String: Any]) |
| 11 | +public enum AppRoute : Hashable { |
| 12 | + case fortune |
| 13 | + case history |
| 14 | + case home |
| 15 | + case onboarding(onboardingRoute : OnboardingRoute?) |
| 16 | + case setting |
| 17 | + |
| 18 | + public func hash(into hasher: inout Hasher) { |
| 19 | + switch self { |
| 20 | + case .fortune: |
| 21 | + hasher.combine(0) |
| 22 | + case .history: |
| 23 | + hasher.combine(1) |
| 24 | + case .home: |
| 25 | + hasher.combine(2) |
| 26 | + case .onboarding(onboardingRoute: let onboardingRoute): |
| 27 | + hasher.combine(3) |
| 28 | + case .setting: |
| 29 | + hasher.combine(4) |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public static func == (lhs: AppRoute, rhs: AppRoute) -> Bool { |
| 34 | + switch (lhs, rhs) { |
| 35 | + case (.fortune, .fortune), (.history, .history), (.home, .home), (.setting, .setting): |
| 36 | + return true |
| 37 | + case (.onboarding, .onboarding): |
| 38 | + return true |
| 39 | + default: |
| 40 | + return false |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +@MainActor |
| 46 | +public protocol Routable : AnyObject { |
| 47 | + func navigate(to route: Any, how : NavigateType, with data: [String: Any]) |
13 | 48 | } |
14 | 49 |
|
15 | 50 | public final class AppRouter: Routable { |
16 | | - public static let shared = AppRouter() |
17 | | - |
18 | | - private var factories: [AppRoute: () -> Routable] = [:] |
19 | | - |
20 | | - private init() {} |
21 | | - |
22 | | - public func register(route: AppRoute, factory: @escaping () -> Routable) { |
23 | | - factories[route] = factory |
24 | | - } |
25 | | - |
26 | | - public func navigate(to route: AppRoute, from: Routable?, with data: [String: Any]) { |
27 | | - guard let router = factories[route]?() else { return } |
28 | | - guard let navigateType = data["navigateType"] as? NavigateType else { return } |
29 | | - |
30 | | - // TODO: topview 찾고 -> UIWindow의 rootviewcon을 찾아서 -> Navigation이 될꺼고 -> 띄우는걸로 |
31 | | - // TODO: 모듈마다 모듈 내부 view 이동 router -> 해당 router는 approtuer 에서 관리 |
32 | | - // 여기서는 그냥 각 모듈을 present, push 하는 정도 역할만 |
33 | | - // 만약 스택을 비워야 한다면 해당 모듈에서 스택을 비우고 가는 걸로 |
34 | | - |
35 | | - // switch navigateType { |
36 | | - // case .push: |
37 | | - // if let navigationController = from?.navigationController { |
38 | | - // navigationController.pushViewController(viewController, animated: true) |
39 | | - // } |
40 | | - // case .present: |
41 | | - // from?.present(viewController, animated: true) |
42 | | - // case .fullscreen: |
43 | | - // viewController.modalPresentationStyle = .fullScreen |
44 | | - // from?.present(viewController, animated: true) |
45 | | - // case .currentContext: |
46 | | - // viewController.modalPresentationStyle = .currentContext |
47 | | - // from?.present(viewController, animated: true) |
48 | | - // case .overFullScreen: |
49 | | - // viewController.modalPresentationStyle = .overFullScreen |
50 | | - // from?.present(viewController, animated: true) |
51 | | - // case .overCurrentContext: |
52 | | - // viewController.modalPresentationStyle = .overCurrentContext |
53 | | - // from?.present(viewController, animated: true) |
54 | | - // case .custom: |
55 | | - // if let transitioningDelegate = data["transitioningDelegate"] as? UIViewControllerTransitioningDelegate { |
56 | | - // viewController.modalPresentationStyle = .custom |
57 | | - // viewController.transitioningDelegate = transitioningDelegate |
58 | | - // } |
59 | | - // from?.present(viewController, animated: true) |
60 | | - // } |
61 | | - |
62 | | - } |
| 51 | + public static let shared = AppRouter() |
| 52 | + |
| 53 | + private var factories: [AppRoute: () -> Routable] = [:] |
| 54 | + |
| 55 | + private init() {} |
| 56 | + |
| 57 | + public func register(route: AppRoute, factory: @escaping () -> Routable) { |
| 58 | + factories[route] = factory |
| 59 | + } |
| 60 | + |
| 61 | + public func navigate(to route: Any, how : NavigateType, with data: [String: Any]) { |
| 62 | + guard let appRoute = route as? AppRoute else { return } |
| 63 | + guard let factory = factories[appRoute] else { return } |
| 64 | + let subRouter = factory() |
| 65 | + |
| 66 | + switch appRoute { |
| 67 | + case .fortune: |
| 68 | + break |
| 69 | + case .history: |
| 70 | + break |
| 71 | + case .home: |
| 72 | + break |
| 73 | + case .onboarding(let onboardingRoute): |
| 74 | + guard let onboardingRoute = onboardingRoute else { return } |
| 75 | + subRouter.navigate(to: onboardingRoute, how: how, with: data) |
| 76 | + case .setting: |
| 77 | + break |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +extension Routable { |
| 83 | + public func topViewController( from base: UIViewController? = UIApplication.shared.connectedScenes |
| 84 | + .compactMap { ($0 as? UIWindowScene)?.keyWindow } |
| 85 | + .first?.rootViewController |
| 86 | + ) -> UIViewController? { |
| 87 | + if let nav = base as? UINavigationController { |
| 88 | + return topViewController(from: nav.visibleViewController) |
| 89 | + } |
| 90 | + if let tab = base as? UITabBarController { |
| 91 | + return topViewController(from: tab.selectedViewController) |
| 92 | + } |
| 93 | + if let presented = base?.presentedViewController { |
| 94 | + return topViewController(from: presented) |
| 95 | + } |
| 96 | + return base |
| 97 | + } |
| 98 | + |
| 99 | + public func manageViewController(_ viewController: UIViewController, how: NavigateType) { |
| 100 | + switch how { |
| 101 | + case .push: |
| 102 | + if let navigationController = topViewController()?.navigationController { |
| 103 | + navigationController.pushViewController(viewController, animated: true) |
| 104 | + } |
| 105 | + case .present: |
| 106 | + topViewController()?.present(viewController, animated: true, completion: nil) |
| 107 | + case .fullscreen: |
| 108 | + viewController.modalPresentationStyle = .fullScreen |
| 109 | + topViewController()?.present(viewController, animated: true, completion: nil) |
| 110 | + case .currentContext: |
| 111 | + viewController.modalPresentationStyle = .currentContext |
| 112 | + topViewController()?.present(viewController, animated: true, completion: nil) |
| 113 | + case .overFullScreen: |
| 114 | + viewController.modalPresentationStyle = .overFullScreen |
| 115 | + topViewController()?.present(viewController, animated: true, completion: nil) |
| 116 | + case .overCurrentContext: |
| 117 | + viewController.modalPresentationStyle = .overCurrentContext |
| 118 | + topViewController()?.present(viewController, animated: true, completion: nil) |
| 119 | + case .custom: |
| 120 | + viewController.modalPresentationStyle = .custom |
| 121 | + topViewController()?.present(viewController, animated: true, completion: nil) |
| 122 | + case .clear: |
| 123 | + if let navigationController = topViewController()?.navigationController { |
| 124 | + navigationController.viewControllers.removeAll() |
| 125 | + navigationController.pushViewController(viewController, animated: true) |
| 126 | + } else { |
| 127 | + topViewController()?.dismiss(animated: true, completion: nil) |
| 128 | + } |
| 129 | + } |
| 130 | + } |
63 | 131 | } |
0 commit comments