|
8 | 8 | import Foundation |
9 | 9 | import UIKit |
10 | 10 |
|
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(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 | 11 | @MainActor |
46 | 12 | public protocol Routable: AnyObject { |
47 | 13 | func navigate(to route: Any, how: NavigateType, with data: [String: Any]) |
48 | 14 | func setFactories() |
49 | 15 | } |
50 | 16 |
|
51 | | -public final class AppRouter: Routable { |
52 | | - public static let shared = AppRouter() |
53 | | - |
54 | | - private var factories: [AppRoute: () -> Routable] = [:] |
55 | | - |
56 | | - private init() {} |
57 | | - |
58 | | - public func register(route: AppRoute, factory: @escaping () -> Routable) { |
59 | | - factories[route] = factory |
60 | | - } |
61 | | - |
62 | | - public func navigate(to route: Any, how: NavigateType, with data: [String: Any]) { |
63 | | - guard let appRoute = route as? AppRoute else { return } |
64 | | - guard let factory = factories[appRoute] else { return } |
65 | | - let subRouter = factory() |
66 | | - |
67 | | - switch appRoute { |
68 | | - case .fortune: |
69 | | - break |
70 | | - case .history: |
71 | | - break |
72 | | - case .home: |
73 | | - break |
74 | | - case .onboarding(let onboardingRoute): |
75 | | - guard let onboardingRoute = onboardingRoute else { return } |
76 | | - |
77 | | - subRouter.navigate(to: onboardingRoute, how: how, with: data) |
78 | | - case .setting: |
79 | | - break |
80 | | - } |
81 | | - } |
82 | | - |
83 | | - public func setFactories() {} |
84 | | -} |
85 | | - |
86 | 17 | extension Routable { |
87 | 18 | public func topViewController( |
88 | 19 | from base: UIViewController? = UIApplication.shared.connectedScenes |
|
0 commit comments