MapsViewController.swift
18.2 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//
// MapsViewController.swift
// SwiftWarplyFramework
//
// Created by Manos Chorianopoulos on 20/9/22.
//
import UIKit
import MapKit
@objc public class MapsViewController: UIViewController, MKMapViewDelegate {
// ui
@IBOutlet weak var mainView: UIView!
@IBOutlet weak var backgroundImage: UIImageView!
@IBOutlet private var mapView: MKMapView!
@IBOutlet weak var closeButton: UIButton!
@IBOutlet private var titleView: UILabel!
@IBOutlet private var nameImageView: UIImageView!
@IBOutlet private var nameView: UILabel!
@IBOutlet private var hoursView: UILabel!
@IBOutlet private var phoneView: UILabel!
@IBOutlet private var addressView: UILabel!
@IBOutlet private var directionsButton: UIButton!
@IBOutlet private var infoTopConstraint: NSLayoutConstraint!
@IBOutlet weak var topBorderLine: UIImageView!
@IBOutlet weak var phoneImageView: UIImageView!
@IBOutlet weak var addressImageView: UIImageView!
// public
public var couponSet: swiftApi.CouponSetItemModel?
public var isMarket: Bool? = false
var merchantsArray:Array<swiftApi.MerchantModel> = []
var selectedMerchant: swiftApi.MerchantModel?
var annotationGroup = [MKPointAnnotation]()
var parentMerchant: swiftApi.MerchantModel?
var parentMerchantsArray:Array<swiftApi.MerchantModel> = []
//
var loading: Bool = false
let initialLocation = CLLocation(latitude: 37.9641262, longitude: 23.7468592) // greece
//
public override func viewDidLoad() {
super.viewDidLoad()
self.hidesBottomBarWhenPushed = true
self.infoTopConstraint.constant = 0
setBackButton()
setNavigationTitle("Καταστήματα")
backgroundImage.image = UIImage(named: "coupons_scrollview_white", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
phoneImageView.image = UIImage(named: "ic_phone", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
addressImageView.image = UIImage(named: "ic_address", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)
mapView.delegate = self
mapView.centerToLocation(initialLocation, regionRadius: 1000000)
// mapView.clipsToBounds = true
// mapView.layer.cornerRadius = 30
// mapView.layer.maskedCorners = [ .layerMinXMinYCorner] // Top left corner radius
// let image = UIImage(named: "top_border_line", in: MyEmptyClass.resourceBundle(), compatibleWith: nil)!
// var aspectR: CGFloat = 0.0
//
// aspectR = image.size.width/image.size.height
// topBorderLine.translatesAutoresizingMaskIntoConstraints = false
// topBorderLine.image = image
// topBorderLine.contentMode = .scaleAspectFill
//
// NSLayoutConstraint.activate([
// topBorderLine.topAnchor.constraint(equalTo: mainView.topAnchor, constant: 0),
// topBorderLine.leadingAnchor.constraint(equalTo: mainView.leadingAnchor, constant: 0),
// topBorderLine.trailingAnchor.constraint(equalTo: mainView.trailingAnchor, constant: 0),
// topBorderLine.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width),
// topBorderLine.heightAnchor.constraint(equalTo: topBorderLine.widthAnchor, multiplier: 1/aspectR)
// ])
closeButton.imageView?.layer.transform = CATransform3DMakeScale(1.5, 1.5, 1.5)
closeButton.setImage(UIImage(named: "ic_close_3", in: MyEmptyClass.resourceBundle(), compatibleWith: nil), for: .normal)
titleView.font = UIFont(name: "PFSquareSansPro-Bold", size: 19)
titleView.textColor = UIColor(rgb: 0x757575)
nameView.font = UIFont(name: "PFSquareSansPro-Medium", size: 18)
nameView.textColor = UIColor(rgb: 0x415564)
hoursView.font = UIFont(name: "PFSquareSansPro-Regular", size: 18)
hoursView.textColor = UIColor(rgb: 0xAEAEAE)
hoursView.font = UIFont(name: "PFSquareSansPro-Medium", size: 18)
hoursView.textColor = UIColor(rgb: 0x415564)
addressView.font = UIFont(name: "PFSquareSansPro-Medium", size: 18)
addressView.textColor = UIColor(rgb: 0x415564)
//directionsButton.titleLabel?.font = UIFont(name: "PFSquareSansPro-Medium", size: 16)
directionsButton.setTitle("Οδηγίες", for: .normal)
directionsButton.setImage(UIImage(named: "ic_directions", in: MyEmptyClass.resourceBundle(), compatibleWith: nil), for: .normal)
// directionsButton.sizeToFit()
directionsButton.titleLabel?.font = UIFont(name: "PFSquareSansPro-Medium", size: 18)
directionsButton.setTitleColor(.white, for: .normal)
directionsButton.tintColor = .white
directionsButton.backgroundColor = UIColor(red: 0.05, green: 0.65, blue: 0.00, alpha: 1.00)
directionsButton.layer.cornerRadius = 16.0
// directionsButton.imageEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 5);
// directionsButton.titleEdgeInsets = UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 0);
directionsButton.frame = CGRect(x: 0.0, y: 0.0, width: directionsButton.intrinsicContentSize.width+10, height: 50)
// directionsButton.contentEdgeInsets = UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15)
directionsButton.setInsets(forContentPadding: UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15), imageTitlePadding: 10)
load()
}
public override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
swiftApi().logTrackersEvent("screen", "ShopsScreen")
self.navigationController?.hideHairline()
}
// mvp
@objc func load() {
if (loading) {
return;
}
showLoading()
if (couponSet != nil) {
if let merchantUuid: String = couponSet?.merchant_uuid {
swiftApi().getMultilingualMerchantsAsync([], false, 0.0, [], "", 0, [merchantUuid], getMerchantsCallback)
showContent()
}
} else {
let appUuid = swiftApi().getAppUuid()
// 2a6adade35f1484994a3d4c8f006b909 category is for SM only
var categoryUuids = [""]
if (self.isMarket == true) {
categoryUuids = appUuid == "0086a2088301440792091b9f814c2267" ? ["2a6adade35f1484994a3d4c8f006b909"] : ["adcace6cab6049c7b7271bc85bc2b26d"]
} else {
categoryUuids = appUuid == "0086a2088301440792091b9f814c2267" ? [""] : ["adcace6cab6049c7b7271bc85bc2b26d"]
}
swiftApi().getMultilingualMerchantsAsync(categoryUuids, false, 0.0, [], "", 0, [], getMerchantsCallback)
showContent()
}
}
func getMerchantsCallback (_ merchantsData: Array<swiftApi.MerchantModel>?) -> Void {
if (merchantsData != nil) {
DispatchQueue.main.async {
let filteredMerchants = (merchantsData ?? []).filter({ return (($0._latitude != 0.0) && ($0._longitude != 0.0)) })
let parentMerchants = (merchantsData ?? []).filter({ return (($0._latitude == 0.0) && ($0._longitude == 0.0)) })
// if (self.isMarket == true) {
self.merchantsArray = filteredMerchants
self.parentMerchantsArray = parentMerchants
// if let parent = (merchantsData ?? []).first(where: {(($0._latitude == 0.0) && ($0._longitude == 0.0))}) {
// self.parentMerchant = parent
// }
// } else {
// self.merchantsArray = merchantsData ?? []
// }
self.loadMapPins()
// let filteredMerchants = self.merchantsArray.filter({ return (($0._latitude != 0.0) && ($0._longitude != 0.0)) })
if (filteredMerchants.count == 0) {
self.showDialog("Καταστήματα συνεργάτη", "Ο συνεργάτης διαθέτει μόνο ηλεκτρονικό κατάστημα για ηλεκτρονικές παραγγελίες.")
}
}
return
}
}
func showDialog(_ alertTitle: String, _ alertSubTitle: String) -> Void {
let alert = UIAlertController(title: alertTitle, message: alertSubTitle, preferredStyle: .alert)
let cancelButton = UIAlertAction(title: "Άκυρο", style: .default, handler: { action in
switch action.style{
case .default:
self.navigationController?.popViewController(animated: true)
// self.dismiss(animated: true, completion: {})
case .cancel:
print("cancel")
case .destructive:
print("destructive")
}
})
// cancelButton.setValue(UIColor(rgb: 0xFC5757), forKey: "titleTextColor")
alert.addAction(cancelButton)
alert.addAction(UIAlertAction(title: "Δες το eshop", style: .default, handler: { action in
switch action.style{
case .default:
if (self.couponSet != nil) {
if let merchantUuid: String = self.couponSet?.merchant_uuid {
for item in swiftApi().getMerchantList() {
if (item._uuid == merchantUuid) {
let eshopWebsite = item._website
if (eshopWebsite != "") {
guard let websiteUrl = URL(string: eshopWebsite) else {
print("Error creating URL")
return
}
// check if link can be opened.
guard UIApplication.shared.canOpenURL(websiteUrl) else {
return
}
swiftApi().logTrackersEvent("click", "SeeShopWebsite")
UIApplication.shared.open(websiteUrl, options: [:], completionHandler: nil)
break;
}
}
}
}
}
// if (self.merchantsArray.count > 0) {
// for item in self.merchantsArray {
// let eshopWebsite = item._website
//
// if (eshopWebsite != "") {
// guard let websiteUrl = URL(string: eshopWebsite) else {
// print("Error creating URL")
// return
// }
//
// // check if link can be opened.
// guard UIApplication.shared.canOpenURL(websiteUrl) else {
// return
// }
//
// swiftApi().logTrackersEvent("click", "SeeShopWebsite")
// UIApplication.shared.open(websiteUrl, options: [:], completionHandler: nil)
// break;
// }
// }
// }
case .cancel:
print("cancel")
case .destructive:
print("destructive")
}
}))
self.present(alert, animated: true, completion: nil)
}
private func showLoading() {
loading = true
}
private func showError() {
}
private func showContent() {
loading = false
}
// private
@IBAction func closeButtonAction(_ sender: Any) {
print("closeButton Pressed!!!")
hidePinDetailsView()
let selectedAnnotations = mapView.selectedAnnotations
for annotation in selectedAnnotations {
mapView.deselectAnnotation(annotation, animated: false)
}
}
@IBAction func directionsButtomAction(_ sender: Any) {
guard let lon = selectedMerchant?._longitude, let lat = selectedMerchant?._latitude else {
return
}
// guard your URL instances
// guard let googleMapsUrl = URL(string: String(format: "https://www.google.com/maps/@%.6f,%.6f,6z", lat, lon)),
// let appleMapsUrl = URL(string: String(format: "http://maps.apple.com/?ll=%.6f,%.6f", lat, lon)) else {
// print("Error creating URLs")
// return
// }
// guard let googleMapsUrl = URL(string: "https://www.google.co.in/maps/dir/?saddr=&daddr=\(lat),\(lon)&directionsmode=driving"),
// let appleMapsUrl = URL(string: "http://maps.apple.com/?daddr=\(lat),\(lon)") else {
// print("Error creating URLs")
// return
// }
//
// // check if link can be opened with google maps.
// guard UIApplication.shared.canOpenURL(googleMapsUrl) else {
// UIApplication.shared.open(appleMapsUrl, options: [:], completionHandler: nil)
// return
// }
//
// UIApplication.shared.open(googleMapsUrl, options: [:], completionHandler: nil)
guard let appleMapsUrl = URL(string: "http://maps.apple.com/?daddr=\(lat),\(lon)") else {
print("Error creating URLs")
return
}
UIApplication.shared.open(appleMapsUrl, options: [:], completionHandler: nil)
}
private func loadMapPins() {
for item in merchantsArray {
let pin = MerchantAnnotation(item)
mapView.addAnnotation(pin)
// self.annotationGroup.append(pin)
// self.mapView.addAnnotations(self.annotationGroup)
// self.mapView.showAnnotations(self.annotationGroup, animated: true)
// let annotation = MKPointAnnotation()
// annotation.coordinate.longitude = item._longitude
// annotation.coordinate.latitude = item._latitude
// annotation.title = item._admin_name //This is the line to remove the optional annotation.title? from.
// annotation.subtitle = item._admin_name
// self.annotationGroup.append(annotation)
// self.mapView.addAnnotations(self.annotationGroup)
// self.mapView.showAnnotations(self.annotationGroup, animated: true)
// let annotation = MKPointAnnotation()
// let centerCoordinate = CLLocationCoordinate2D(latitude: item._latitude, longitude: item._longitude)
// annotation.coordinate = centerCoordinate
// annotation.title = "Pass Title here"
// mapView.addAnnotation(annotation)
}
// mapView.showAnnotations(mapView.annotations, animated: true)
}
private func loadPinDetailsView(_ annotation: MerchantAnnotation) {
self.selectedMerchant = annotation.model
if let parent = self.parentMerchantsArray.first(where: {($0._uuid == self.selectedMerchant?._parent)}) {
self.parentMerchant = parent
}
// self.titleView.text = self.selectedMerchant?._admin_name
self.titleView.text = self.parentMerchant?._admin_name
if (self.isMarket == true) {
self.nameImageView.load(link: self.parentMerchant?._img_preview ?? "", placeholder: UIImage(), cache: URLCache())
} else {
self.nameImageView.load(link: self.selectedMerchant?._img_preview ?? "", placeholder: UIImage(), cache: URLCache())
}
self.nameView.text = self.selectedMerchant?._name
// TODO: Make dynamic
self.hoursView.text = ""
self.phoneView.text = self.selectedMerchant?._telephone
self.addressView.text = self.selectedMerchant?._address
}
private func showPinDetailsView() {
self.infoTopConstraint.constant = -320
UIView.animate(withDuration: 1.0) {
self.view.layoutIfNeeded()
}
}
private func hidePinDetailsView() {
self.selectedMerchant = nil
self.infoTopConstraint.constant = 0
UIView.animate(withDuration: 1.0) {
self.view.layoutIfNeeded()
}
}
// map view delegate
// public func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
//
//// guard !(annotation is MKUserLocation) else {
//// return nil
//// }
//
// var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "warply_custom")
// if (annotationView == nil) {
// print("=== annotationView == nil ===")
//
// annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "warply_custom")
// annotationView?.canShowCallout = false
// } else {
// print("=== annotationView != nil ===")
//
// annotationView?.annotation = annotation
// }
//
// // TODO: add custom pin image?
// //annotationView?.image = UIImage(named: "custom")
//
// print("=== returned annotation ===")
// print(annotation)
//
// return annotationView
// }
public func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
let merchant = view.annotation as? MerchantAnnotation
let merchantModel = merchant!.model
swiftApi().logTrackersEvent("click", ("ShopsScreenMarker:" + (merchantModel._uuid)))
loadPinDetailsView(merchant!)
showPinDetailsView()
}
public func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) {
hidePinDetailsView()
}
}
private extension MKMapView {
func centerToLocation(_ location: CLLocation, regionRadius: CLLocationDistance = 1000) {
let coordinateRegion = MKCoordinateRegion(
center: location.coordinate,
latitudinalMeters: regionRadius,
longitudinalMeters: regionRadius)
setRegion(coordinateRegion, animated: true)
}
}