-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathMainCoordinator.swift
203 lines (178 loc) · 7.21 KB
/
MainCoordinator.swift
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//
// MainCoordinator.swift
// Cryptomator
//
// Created by Philipp Schmid on 04.01.21.
// Copyright © 2021 Skymatic GmbH. All rights reserved.
//
import CryptomatorCommonCore
import Promises
import StoreKit
import UIKit
class MainCoordinator: NSObject, Coordinator, UINavigationControllerDelegate {
var navigationController: UINavigationController = BaseNavigationController()
var childCoordinators = [Coordinator]()
lazy var rootViewController: UISplitViewController = {
let splitViewController = UISplitViewController()
splitViewController.preferredDisplayMode = .oneBesideSecondary
splitViewController.delegate = self
return splitViewController
}()
private weak var lastVaultInfo: VaultInfo?
func start() {
let vaultListViewController = VaultListViewController(with: VaultListViewModel())
vaultListViewController.coordinator = self
navigationController.viewControllers = [vaultListViewController]
rootViewController.viewControllers = [
navigationController,
EmptyDetailViewController()
]
}
func showOnboarding() {
let modalNavigationController = OnboardingNavigationController()
modalNavigationController.isModalInPresentation = true
let child = OnboardingCoordinator(navigationController: modalNavigationController)
childCoordinators.append(child)
navigationController.topViewController?.present(modalNavigationController, animated: true)
child.start()
}
func showTrialExpired() {
let modalNavigationController = TrialExpiredNavigationController()
let child = TrialExpiredCoordinator(navigationController: modalNavigationController)
childCoordinators.append(child)
navigationController.topViewController?.present(modalNavigationController, animated: true)
child.start()
}
func addVault() {
let modalNavigationController = BaseNavigationController()
let child = AddVaultCoordinator(navigationController: modalNavigationController)
child.parentCoordinator = self
childCoordinators.append(child)
navigationController.topViewController?.present(modalNavigationController, animated: true)
child.start()
}
func showSettings() {
let modalNavigationController = BaseNavigationController()
let child = SettingsCoordinator(navigationController: modalNavigationController)
child.parentCoordinator = self
childCoordinators.append(child)
navigationController.topViewController?.present(modalNavigationController, animated: true)
child.start()
}
func showVaultDetail(for vaultInfo: VaultInfo) {
lastVaultInfo = vaultInfo
let detailNavigationController = BaseNavigationController()
let child = VaultDetailCoordinator(vaultInfo: vaultInfo, navigationController: detailNavigationController)
childCoordinators.append(child)
child.start()
child.removedVaultDelegate = self
rootViewController.showDetailViewController(detailNavigationController, sender: nil)
}
// Temporarily added for Spring 2025 Sale
func showPurchase() {
let modalNavigationController = BaseNavigationController()
let child = PurchaseCoordinator(navigationController: modalNavigationController)
childCoordinators.append(child)
navigationController.topViewController?.present(modalNavigationController, animated: true)
child.start()
}
// MARK: - UINavigationControllerDelegate
func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
// Read the view controller we’re moving from.
guard let fromViewController = navigationController.transitionCoordinator?.viewController(forKey: .from) else {
return
}
// Check whether our view controller array already contains that view controller. If it does it means we’re pushing a different view controller on top rather than popping it, so exit.
if navigationController.viewControllers.contains(fromViewController) {
return
}
}
// MARK: - Internal
private func hideVaultDetail() {
rootViewController.showDetailViewController(EmptyDetailViewController(), sender: nil)
}
}
extension MainCoordinator: RemoveVaultDelegate {
func removedVault(_ vault: VaultInfo) {
if !rootViewController.isCollapsed, vault == lastVaultInfo {
hideVaultDetail()
} else if let navigationController = rootViewController.viewControllers.first as? UINavigationController {
navigationController.popToRootViewController(animated: true)
}
lastVaultInfo = nil
}
}
extension MainCoordinator: StoreObserverDelegate {
func purchaseDidSucceed(transaction: PurchaseTransaction) {
switch transaction {
case .fullVersion, .yearlySubscription:
showFullVersionAlert()
// Temporarily added for Spring 2025 Sale
NotificationCenter.default.post(name: .purchasedFullVersionNotification, object: nil)
case let .freeTrial(expiresOn):
showTrialAlert(expirationDate: expiresOn)
case .unknown:
break
}
}
private func showFullVersionAlert() {
showAlert { [weak self] in
guard let navigationController = self?.navigationController else {
return
}
PurchaseAlert.showForFullVersion(title: LocalizedString.getValue("purchase.unlockedFullVersion.title"), on: navigationController).then {
if let windowScene = navigationController.view.window?.windowScene {
SKStoreReviewController.requestReview(in: windowScene)
}
}
}
}
private func showTrialAlert(expirationDate: Date) {
showAlert { [weak self] in
guard let navigationController = self?.navigationController else {
return
}
_ = PurchaseAlert.showForTrial(title: LocalizedString.getValue("purchase.beginFreeTrial.alert.title"), expirationDate: expirationDate, on: navigationController)
}
}
private func showAlert(_ showAlertCall: @escaping () -> Void) {
guard navigationController.presentedViewController == nil else {
DispatchQueue.main.asyncAfter(deadline: .now() + 5.0) { [weak self] in
self?.showAlert(showAlertCall)
}
return
}
}
}
extension MainCoordinator: UISplitViewControllerDelegate {
func splitViewController(_ splitViewController: UISplitViewController, collapseSecondary secondaryViewController: UIViewController, onto primaryViewController: UIViewController) -> Bool {
return true
}
}
private class CryptoBotViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .cryptomatorBackground
let imageView = UIImageView(image: UIImage(named: "bot"))
imageView.contentMode = .scaleAspectFit
imageView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(imageView)
NSLayoutConstraint.activate([
imageView.centerXAnchor.constraint(equalTo: view.layoutMarginsGuide.centerXAnchor),
imageView.centerYAnchor.constraint(equalTo: view.layoutMarginsGuide.centerYAnchor),
imageView.topAnchor.constraint(greaterThanOrEqualTo: view.layoutMarginsGuide.topAnchor),
imageView.bottomAnchor.constraint(lessThanOrEqualTo: view.layoutMarginsGuide.bottomAnchor),
imageView.leadingAnchor.constraint(greaterThanOrEqualTo: view.readableContentGuide.leadingAnchor),
imageView.trailingAnchor.constraint(lessThanOrEqualTo: view.readableContentGuide.trailingAnchor)
])
}
}
private class EmptyDetailViewController: BaseNavigationController {
init() {
super.init(rootViewController: CryptoBotViewController())
}
@available(*, unavailable)
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}