-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathStatsRouter.swift
More file actions
100 lines (84 loc) · 3.3 KB
/
StatsRouter.swift
File metadata and controls
100 lines (84 loc) · 3.3 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import SwiftUI
import UIKit
import SafariServices
import WordPressSharedUI
@MainActor
public protocol StatsRouterScreenFactory: AnyObject {
func makeLikesListViewController(siteID: Int, postID: Int, totalLikes: Int) -> UIViewController
func makeCommentsListViewController(siteID: Int, postID: Int) -> UIViewController
}
public final class StatsRouter: @unchecked Sendable {
@MainActor
var navigationController: UINavigationController? {
let vc = viewController ?? findTopViewController()
return (vc as? UINavigationController) ?? vc?.navigationController
}
public weak var viewController: UIViewController?
let factory: StatsRouterScreenFactory
public init(viewController: UIViewController? = nil, factory: StatsRouterScreenFactory) {
self.viewController = viewController
self.factory = factory
}
@MainActor
private func findTopViewController() -> UIViewController? {
guard let window = UIApplication.shared.mainWindow else {
return nil
}
var topController = window.rootViewController
while let presented = topController?.presentedViewController {
topController = presented
}
return topController
}
@MainActor
func navigate<Content: View>(to view: Content, title: String? = nil) {
let viewController = UIHostingController(rootView: view)
if let title {
// This ensures that it gets rendered instantly on navigation
viewController.title = title
}
navigationController?.pushViewController(viewController, animated: true)
}
@MainActor
func navigateToLikesList(siteID: Int, postID: Int, totalLikes: Int) {
let likesVC = factory.makeLikesListViewController(siteID: siteID, postID: postID, totalLikes: totalLikes)
navigationController?.pushViewController(likesVC, animated: true)
}
@MainActor
func navigateToCommentsList(siteID: Int, postID: Int) {
let commentsVC = factory.makeCommentsListViewController(siteID: siteID, postID: postID)
navigationController?.pushViewController(commentsVC, animated: true)
}
@MainActor
func openURL(_ url: URL) {
// Open URL in in-app Safari
let safariViewController = SFSafariViewController(url: url)
let vc = viewController ?? findTopViewController()
vc?.present(safariViewController, animated: true)
}
}
private extension UIApplication {
@objc var mainWindow: UIWindow? {
connectedScenes
.compactMap { ($0 as? UIWindowScene)?.keyWindow }
.first
}
}
class MockStatsRouterScreenFactory: StatsRouterScreenFactory {
func makeCommentsListViewController(siteID: Int, postID: Int) -> UIViewController {
UIHostingController(rootView: Text(Strings.Errors.generic))
}
func makeLikesListViewController(siteID: Int, postID: Int, totalLikes: Int) -> UIViewController {
UIHostingController(rootView: Text(Strings.Errors.generic))
}
}
// MARK: - Environment Key
private struct StatsRouterKey: EnvironmentKey {
static let defaultValue = StatsRouter(factory: MockStatsRouterScreenFactory())
}
extension EnvironmentValues {
var router: StatsRouter {
get { self[StatsRouterKey.self] }
set { self[StatsRouterKey.self] = newValue }
}
}