-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSmallScreenRootViewController.swift
242 lines (209 loc) · 9.24 KB
/
SmallScreenRootViewController.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import UIKit
import CoreLocation
import ReSwift
struct SmallScreenRootViewControllerState: Equatable {
var accessGranted: Bool
var geocoderQueueCount: Int
var mapContentListDetentIdentifer: UISheetPresentationController.Detent.Identifier
var mapContentDetailsDetentIdentifer: UISheetPresentationController.Detent.Identifier
var mapSelectedContact: MapContactSelection?
var mapSelectedPersonLocationForEdit: PersonLocation?
init(newState: AppState) {
accessGranted = (
newState.contactsAuthStatus == .authorized &&
newState.locationAuthStatus == .authorized
)
geocoderQueueCount = newState.geocoderQueueCount
mapContentListDetentIdentifer = newState.mapContactListDetentIdentifier
mapContentDetailsDetentIdentifer = newState.mapContactDetailsDetentIdentifier
mapSelectedContact = newState.mapSelection
mapSelectedPersonLocationForEdit = newState.mapPersonLocationForEdit
}
}
class SmallScreenRootViewController: UITabBarController, StoreSubscriber, UISheetPresentationControllerDelegate, UITabBarControllerDelegate {
private var currentState: SmallScreenRootViewControllerState?
private let mapVC = MapViewController()
private let contactListVC = MapContactListViewController()
private var contactDetailVC: ContactDetailViewController?
private var personLocationEditVC: LocationEditViewController?
private var onboardingVC: OnboardingViewController?
init() {
super.init(nibName: nil, bundle: nil)
let listVC = UINavigationController(rootViewController: ContactListViewController())
listVC.navigationBar.prefersLargeTitles = true
listVC.tabBarItem = UITabBarItem(title: "List", image: .init(systemName: "list.bullet"), selectedImage: .init(systemName: "list.bullet"))
mapVC.tabBarItem = UITabBarItem(title: "Map", image: .init(systemName: "map"), selectedImage: .init(systemName: "map.fill"))
#if AFFINITES_ENABLED
self.viewControllers = [listVC, mapVC]
self.selectedViewController = listVC
#else
self.viewControllers = [mapVC]
self.selectedViewController = mapVC
#endif
self.delegate = self
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
#if AFFINITES_ENABLED
// TODO: this is so, so hacky
view.window!.addSubview(self.tabBar)
#endif
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if selectedViewController == mapVC {
presentContactsList()
}
presentOrDismissOnboarding()
app.store.subscribe(self) { subscription in
return subscription.select(SmallScreenRootViewControllerState.init)
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
app.store.unsubscribe(self)
}
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if (viewController == mapVC) {
presentContactsList()
} else {
contactListVC.view.isHidden = true;
contactListVC.dismiss(animated: false) {
self.contactListVC.view.isHidden = false;
}
}
}
func newState(state: SmallScreenRootViewControllerState) {
let prevState = currentState
currentState = state
if state.mapContentListDetentIdentifer != prevState?.mapContentListDetentIdentifer {
contactListVC.sheetPresentationController?.animateChanges {
contactListVC.sheetPresentationController?.selectedDetentIdentifier = state.mapContentListDetentIdentifer
}
}
if state.mapContentDetailsDetentIdentifer != prevState?.mapContentDetailsDetentIdentifer {
contactDetailVC?.sheetPresentationController?.animateChanges {
contactDetailVC?.sheetPresentationController?.selectedDetentIdentifier = state.mapContentDetailsDetentIdentifer
}
}
if state.mapSelectedContact != prevState?.mapSelectedContact {
if let mapSelectedPersonLocation = state.mapSelectedContact?.personLocation {
presentContactDetails(mapSelectedPersonLocation)
} else {
dismissContactDetails()
}
}
if state.mapSelectedPersonLocationForEdit != prevState?.mapSelectedPersonLocationForEdit {
if let location = state.mapSelectedPersonLocationForEdit {
presentPersonLocationForEdit(personLocation: location)
} else {
dismissPersonLocationForEdit()
}
}
presentOrDismissOnboarding()
}
func sheetPresentationControllerDidChangeSelectedDetentIdentifier(_ sheetPresentationController: UISheetPresentationController) {
if sheetPresentationController == contactListVC.sheetPresentationController {
app.store.dispatch(MapContactListDetentChanged(detentIdentifier: sheetPresentationController.selectedDetentIdentifier!))
} else if sheetPresentationController == contactDetailVC?.sheetPresentationController {
app.store.dispatch(MapContactDetailsDetentChanged(detentIdentifier: sheetPresentationController.selectedDetentIdentifier!))
}
}
private func presentOrDismissOnboarding() {
var needsOnboarding = false
if let currentState = currentState {
needsOnboarding = (
!currentState.accessGranted ||
currentState.geocoderQueueCount > 3 ||
(currentState.geocoderQueueCount > 0 && onboardingVC != nil)
)
}
if needsOnboarding {
presentOnboarding()
} else {
dismissOnboarding()
}
}
private func presentOnboarding() {
if self.onboardingVC != nil {
return
}
let onboardingVC = OnboardingViewController()
onboardingVC.modalPresentationStyle = .overFullScreen
self.onboardingVC = onboardingVC
let keyWindow = UIApplication.shared.connectedScenes
.flatMap { ($0 as? UIWindowScene)?.windows ?? [] }
.first { $0.isKeyWindow }
guard let keyWindow = keyWindow else {
fatalError("No key window")
}
guard var topVC = keyWindow.rootViewController else {
fatalError("No top view controller")
}
while let presentedViewController = topVC.presentedViewController {
topVC = presentedViewController
}
topVC.present(onboardingVC, animated: false)
}
private func dismissOnboarding() {
if let onboardingVC = onboardingVC {
onboardingVC.dismiss(animated: true)
self.onboardingVC = nil
}
}
private func presentContactsList() {
contactListVC.isModalInPresentation = true // prevent dismissal
if let sheet = contactListVC.sheetPresentationController {
sheet.detents = [.collapsed, .small, .large()]
sheet.selectedDetentIdentifier = app.store.state.mapContactListDetentIdentifier
sheet.largestUndimmedDetentIdentifier = .small
sheet.prefersGrabberVisible = true
sheet.delegate = self
}
mapVC.present(contactListVC, animated: false)
}
private func presentContactDetails(_ personLocation: PersonLocation) {
if let contactDetailVC = contactDetailVC {
contactDetailVC.personLocation = personLocation
return // already presented
}
let contactDetailVC = ContactDetailViewController(personLocation: personLocation)
contactDetailVC.isModalInPresentation = true // prevent dismissal
if let sheet = contactDetailVC.sheetPresentationController {
sheet.detents = [.collapsed, .normal, .large()]
sheet.selectedDetentIdentifier = .normal
sheet.largestUndimmedDetentIdentifier = .normal
sheet.prefersGrabberVisible = true
sheet.delegate = self
}
contactListVC.present(contactDetailVC, animated: true)
self.contactDetailVC = contactDetailVC
}
private func dismissContactDetails() {
contactDetailVC?.dismiss(animated: true)
contactDetailVC = nil
}
private func presentPersonLocationForEdit(personLocation: PersonLocation) {
if personLocationEditVC != nil {
return // already presented
}
guard let contactDetailVC = contactDetailVC else {
fatalError("No contact details presented; cannot edit location")
}
let personLocationEditVC = LocationEditViewController(personLocation: personLocation)
personLocationEditVC.isModalInPresentation = true // prevent dismissal
if let sheet = personLocationEditVC.sheetPresentationController {
sheet.detents = [.large()]
sheet.delegate = self
}
contactDetailVC.present(personLocationEditVC, animated: true)
self.personLocationEditVC = personLocationEditVC
}
private func dismissPersonLocationForEdit() {
personLocationEditVC?.dismiss(animated: true)
personLocationEditVC = nil
}
}