-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathMapsViewController.swift
More file actions
437 lines (317 loc) · 13.9 KB
/
MapsViewController.swift
File metadata and controls
437 lines (317 loc) · 13.9 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
//
// MapsViewController.swift
// PatternsSwift
//
// Created by mrustaa on 02/05/2020.
// Copyright © 2020 mrustaa. All rights reserved.
//
import UIKit
import MapKit
import CoreLocation
import ContainerControllerSwift
class MapsViewController: StoryboardController, MapsContainerControllerDelegate, LocationContainerControllerDelegate, RouteContainerControllerDelegate, MenuContainerControllerDelegate {
// MARK: - IBOutlets
@IBOutlet weak var mapView: MKMapView!
@IBOutlet weak var mapWeatherView: MapsWeatherView!
@IBOutlet weak var mapButtons: MapsButtons!
@IBOutlet weak var mapButtonsPaddingTop: NSLayoutConstraint!
@IBOutlet weak var mapButtonsPaddingRight: NSLayoutConstraint!
@IBOutlet weak var statusBlur: UIVisualEffectView!
@IBOutlet weak var statusBarBlurHeight: NSLayoutConstraint!
// MARK: - Properties
var mapManager: MapViewManager!
var mapsContainer: MapsContainerController!
var locationContainer: LocationContainerController?
var routeContainer: RouteContainerController?
var menuContainer: MenuContainerController!
var darkStyle: Bool = false
var selectedIndex: Int = 0
var showOnce: Bool = false
// MARK: - Life Cycle
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(rotated), name: UIDevice.orientationDidChangeNotification, object: nil)
updateMapManager()
showMapsContainer()
updateMapViewButtons()
updateMapViewWeatherView()
updateMapViewTopPadding()
startAnimationMapElements()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
setNeedsStatusBarAppearanceUpdate()
navBar(hide: true)
if !showOnce {
mapsContainer.move(type: .hide, animation: false)
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if !showOnce {
showOnce = true
mapsContainer.move(type: .middle)
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
navBar(hide: false)
}
func navBar(hide: Bool) {
self.navigationController?.setNavigationBarHidden(hide, animated: true)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
mapsContainer.remove()
mapsContainer = nil
NotificationCenter.default.removeObserver(self)
}
// MARK: - StatusBar Style
override var preferredStatusBarStyle: UIStatusBarStyle {
return darkStyle ? .lightContent : .default
}
// MARK: - Rotation Callback
@objc func rotated() {
if !UIDevice.current.orientation.isRotateAllowed { return }
updateMapViewTopPadding()
}
func updateMapViewTopPadding() {
let padding: CGFloat = 8
statusBarBlurHeight.constant = ContainerDevice.statusBarHeight
let paddingX: CGFloat = (ContainerDevice.isIphoneX ? ContainerDevice.isIphoneXTop : padding)
var paddingWeatherTop: CGFloat = 0.0
var paddingTop: CGFloat = 0.0
var paddingRight: CGFloat = 0.0
let paddingStatusBar: CGFloat = (ContainerDevice.statusBarHeight + padding)
switch ContainerDevice.orientation {
case .portrait:
paddingTop = paddingStatusBar
paddingRight = padding
paddingWeatherTop = padding
case .landscapeLeft:
paddingTop = ContainerDevice.isIpad ? paddingStatusBar : paddingX
paddingRight = ContainerDevice.isIphoneX ? 44 : padding
paddingWeatherTop = paddingX
case .landscapeRight:
paddingTop = ContainerDevice.isIpad ? paddingStatusBar : paddingX
paddingRight = paddingX
paddingWeatherTop = paddingX
}
var width: CGFloat = 0.0
var height: CGFloat = 0.0
switch ContainerDevice.orientation {
case .portrait:
width = ContainerDevice.screenMin
height = ContainerDevice.screenMax
case .landscapeLeft,
.landscapeRight:
width = ContainerDevice.screenMax
height = ContainerDevice.screenMin
}
mapWeatherView.x = (width - mapWeatherView.width - paddingRight)
if ContainerDevice.isPortrait, !ContainerDevice.isIpad {
mapWeatherView.y = (mapsContainer.view.y - mapWeatherView.height - paddingWeatherTop)
} else {
mapWeatherView.y = (height - mapWeatherView.height - paddingWeatherTop)
}
mapButtonsPaddingTop.constant = paddingTop
mapButtonsPaddingRight.constant = paddingRight
mapManager.compass.x = (ContainerDevice.width - 47)
mapManager.compass.y = (paddingTop + mapButtons.height + 12)
}
// MARK: - Map Manager
func updateMapManager() {
mapManager = MapViewManager(mapView: mapView)
mapManager.compass.y = (mapButtons.bottom + 12)
mapManager.selectPinCallback = { [weak self] in
guard let _self = self else { return }
_self.mapsContainer.showLocationDetails()
}
mapManager.changeRegionCallback = { [weak self] in
guard let _self = self else { return }
_self.mapButtons.changeButtonLocation(fill: false)
}
}
// MARK: - Map Long-Press
@IBAction func handleLong(_ recognizer: UILongPressGestureRecognizer) {
if routeContainer != nil { return }
switch recognizer.state {
case .began:
mapManager.addMapPinFrom(longPress: recognizer)
mapsContainer.showLocationDetails()
default: break
}
}
// MARK: - Map Buttons Update
func updateMapViewButtons() {
mapButtons.addBlur(darkStyle: darkStyle)
mapButtons.alpha = 0.0
mapButtons.buttonsActionCallback = { [weak self] (_ index: Int) in
guard let _self = self else { return }
if (index == 0) {
_self.showMenuContainer()
} else {
_self.mapButtons.changeButtonLocation(fill: true)
if let coor = _self.mapView.userLocation.location?.coordinate {
_self.mapView.setCenter(coor, animated: true)
}
}
}
}
func updateMapViewWeatherView() {
mapWeatherView.addBlur(darkStyle: darkStyle)
mapWeatherView.alpha = 0.0
}
func startAnimationMapElements() {
UIView.animate(withDuration: 1.0, animations: { [weak self] in
guard let _self = self else { return }
_self.mapWeatherView.alpha = 1.0
_self.mapButtons.alpha = 1.0
})
}
// MARK: - Map Buttons Alpha
func mapButtons(alpha: CGFloat, animation: Bool = true) {
if ContainerDevice.isPortrait, !ContainerDevice.isIpad {
if animation {
UIView.animate(withDuration: 0.15, animations: { [weak self] in
guard let _self = self else { return }
_self.mapButtons.alpha = alpha
_self.mapWeatherView.alpha = alpha
})
} else {
mapButtons.alpha = alpha
mapWeatherView.alpha = alpha
}
} else {
mapButtons.alpha = 1.0
mapWeatherView.alpha = 1.0
}
}
// MARK: - Change Position MapElements
func changePositionMapsElements(container: ContainerController, position: CGFloat, type: ContainerMoveType, animation: Bool) {
if type == .hide { return }
let result = (container.positionMiddle - position)
var alpha: CGFloat = (1.0 - (result / 20.0))
if 20.0 < result {
alpha = 0.0
} else if result < 0.0 {
alpha = 1.0
}
if animation {
if type == .top {
mapButtons(alpha: 0.0, animation: true)
} else {
mapButtons(alpha: 1.0, animation: true)
}
if type != .top {
if ContainerDevice.isPortrait, !ContainerDevice.isIpad {
mapWeatherView.y = (position - mapWeatherView.height - 8)
}
}
} else {
mapButtons(alpha: alpha, animation: false)
if ContainerDevice.isPortrait, !ContainerDevice.isIpad {
if result < 0.0 {
mapWeatherView.y = (position - mapWeatherView.height - 8)
} else {
mapWeatherView.y = (container.positionMiddle - mapWeatherView.height - 8)
}
}
}
if animation, type != .top {
view.endEditing(true)
}
}
// MARK: - Change Index Menu
func menuChange(index: Int) {
selectedIndex = index
darkStyle = (index == 2)
mapsContainer.update(darkStyle: darkStyle)
locationContainer?.update(darkStyle: darkStyle)
routeContainer?.update(darkStyle: darkStyle)
mapButtons.addBlur(darkStyle: darkStyle)
mapWeatherView.addBlur(darkStyle: darkStyle)
switch selectedIndex {
case 0: mapView.mapType = .standard
case 1: mapView.mapType = .mutedStandard
case 2: mapView.mapType = .hybrid
default: break
}
let style: UIBlurEffect.Style = darkStyle ? .systemUltraThinMaterialDark : .regular
statusBlur.effect = UIBlurEffect(style: style)
setNeedsStatusBarAppearanceUpdate()
}
// MARK: - Show Maps-Container
func showMapsContainer() {
mapsContainer = MapsContainerController(addTo: self, darkStyle: darkStyle)
mapsContainer.mapsDelegate = self
}
// MARK: Delegate
func mapsContainerController(showLocationDetails mapsContainerController: MapsContainerController) {
mapsContainer.move(type: .hide)
showLocationDetailsContainer()
mapManager.selectPinAnimation(show: true)
}
func mapsContainerController(move mapsContainerController: MapsContainerController, position: CGFloat, type: ContainerMoveType, animation: Bool) {
changePositionMapsElements(container: mapsContainerController, position: position, type: type, animation: animation)
}
//MARK: - Show Location-Details
func showLocationDetailsContainer() {
if locationContainer != nil { return }
locationContainer = LocationContainerController(addTo: self, darkStyle: darkStyle)
locationContainer?.locationDelegate = self
locationContainer?.move(type: ContainerDevice.isPortrait ? .middle : .top)
}
// MARK: Delegate
func locationContainerController(showRoute locationContainerController: LocationContainerController) {
locationContainer?.move(type: .hide)
showRouteContainer()
mapManager.showRouteOnMapMyLocation()
}
func locationContainerController(close locationContainerController: LocationContainerController) {
mapManager.selectPinAnimation(show: false)
let animation = locationContainerController.oldMoveType != .middle
mapsContainer.move(type: .middle, animation: animation)
}
func locationContainerController(closeComplection locationContainerController: LocationContainerController) {
locationContainer = nil
}
func locationContainerController(move locationContainerController: LocationContainerController, position: CGFloat, type: ContainerMoveType, animation: Bool) {
changePositionMapsElements(container: locationContainerController, position: position, type: type, animation: animation)
}
//MARK: - Show Route
func showRouteContainer() {
if routeContainer != nil { return }
routeContainer = RouteContainerController(addTo: self, darkStyle: darkStyle)
routeContainer?.routeDelegate = self
routeContainer?.move(type: ContainerDevice.isPortrait ? .middle : .top)
}
// MARK: Delegate
func routeContainerController(close routeContainerController: RouteContainerController) {
mapManager.closeRoute(showSelectPin: true)
locationContainer?.move(type: .middle)
}
func routeContainerController(closeComplection routeContainerController: RouteContainerController) {
routeContainer = nil
}
func routeContainerController(move routeContainerController: RouteContainerController, position: CGFloat, type: ContainerMoveType, animation: Bool) {
changePositionMapsElements(container: routeContainerController, position: position, type: type, animation: animation)
}
// MARK: - Show Menu-Container
func showMenuContainer() {
if menuContainer != nil { return }
menuContainer = MenuContainerController(addTo: self, darkStyle: darkStyle, selectedIndex: selectedIndex)
menuContainer?.menuDelegate = self
mapButtons(alpha: 0.0)
}
// MARK: Delegate
func menuContainerController(close menuContainerController: MenuContainerController) {
mapButtons(alpha: 1.0)
}
func menuContainerController(closeComplection menuContainerController: MenuContainerController) {
menuContainer = nil
}
func menuContainerController(segment menuContainerController: MenuContainerController, selectedIndex: Int) {
menuChange(index: selectedIndex)
}
}