From 2cbb4cd52618a69ef46c4f08857437594f91d64b Mon Sep 17 00:00:00 2001 From: Andrea Busi Date: Mon, 15 Mar 2021 16:39:28 +0100 Subject: [PATCH] refactor: Edit custom annotation view to support a 2 labels layout --- .../NotificationViewController.m | 6 +- ...micNotificationExpandedTableViewCell.swift | 4 +- .../Alerts/PasquakesMapViewController.swift | 4 +- .../SegnalazioniMapViewController.swift | 4 +- .../UI/EQNCustomAnnotationView.swift | 65 ++++++++++++++----- 5 files changed, 58 insertions(+), 25 deletions(-) diff --git a/Sources/EQNNotificationContent/NotificationViewController.m b/Sources/EQNNotificationContent/NotificationViewController.m index ea5e295..e4b0d12 100644 --- a/Sources/EQNNotificationContent/NotificationViewController.m +++ b/Sources/EQNNotificationContent/NotificationViewController.m @@ -40,7 +40,7 @@ { [super viewDidLoad]; - [self.mapView registerClass:[EQNCustomAnnotationView class] forAnnotationViewWithReuseIdentifier:EQNCustomAnnotationView.Identifier]; + [self.mapView registerClass:[EQNCustomAnnotationView class] forAnnotationViewWithReuseIdentifier:EQNCustomAnnotationView.SingleLineIdentifier]; } - (void)didReceiveNotification:(UNNotification *)notification @@ -109,7 +109,7 @@ if ([annotation isKindOfClass:[EQNMapAnnotationPastquake class]]) { EQNMapAnnotationPastquake *pastquake = (EQNMapAnnotationPastquake *)annotation; - EQNCustomAnnotationView *annotationView = (EQNCustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:EQNCustomAnnotationView.Identifier]; + EQNCustomAnnotationView *annotationView = (EQNCustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:EQNCustomAnnotationView.SingleLineIdentifier]; annotationView.image = pastquake.image; annotationView.title = pastquake.title; return annotationView; @@ -117,7 +117,7 @@ } else if ([annotation isKindOfClass:[EQNMapAnnotationUserReport class]]) { EQNMapAnnotationUserReport *report = (EQNMapAnnotationUserReport *)annotation; - EQNCustomAnnotationView *annotationView = (EQNCustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:EQNCustomAnnotationView.Identifier]; + EQNCustomAnnotationView *annotationView = (EQNCustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:EQNCustomAnnotationView.SingleLineIdentifier]; annotationView.image = report.image; annotationView.title = report.title; return annotationView; diff --git a/Sources/Earthquake Network/Controllers/Alerts/Cells/AlertsSeismicNotificationExpandedTableViewCell.swift b/Sources/Earthquake Network/Controllers/Alerts/Cells/AlertsSeismicNotificationExpandedTableViewCell.swift index 58a74af..dfcc481 100644 --- a/Sources/Earthquake Network/Controllers/Alerts/Cells/AlertsSeismicNotificationExpandedTableViewCell.swift +++ b/Sources/Earthquake Network/Controllers/Alerts/Cells/AlertsSeismicNotificationExpandedTableViewCell.swift @@ -35,7 +35,7 @@ class AlertsSeismicNotificationExpandedTableViewCell: EQNBaseTableViewCell, MKMa mapView.delegate = self mapView.isScrollEnabled = false mapView.isZoomEnabled = false - mapView.register(EQNCustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: EQNCustomAnnotationView.Identifier) + mapView.register(EQNCustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: EQNCustomAnnotationView.SingleLineIdentifier) } } private var impactTimestamp: Date? @@ -112,7 +112,7 @@ class AlertsSeismicNotificationExpandedTableViewCell: EQNBaseTableViewCell, MKMa return nil } - let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: EQNCustomAnnotationView.Identifier, for: annotation) as! EQNCustomAnnotationView + let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: EQNCustomAnnotationView.SingleLineIdentifier, for: annotation) as! EQNCustomAnnotationView annotationView.image = annotation.image annotationView.title = annotation.title return annotationView diff --git a/Sources/Earthquake Network/Controllers/Alerts/PasquakesMapViewController.swift b/Sources/Earthquake Network/Controllers/Alerts/PasquakesMapViewController.swift index fd6a884..39ba0f3 100644 --- a/Sources/Earthquake Network/Controllers/Alerts/PasquakesMapViewController.swift +++ b/Sources/Earthquake Network/Controllers/Alerts/PasquakesMapViewController.swift @@ -33,7 +33,7 @@ class PasquakesMapViewController: EQNBaseMapViewController { // MARK: - Public override func registerMapAnnotationViews() { - mapView.register(EQNCustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: EQNCustomAnnotationView.Identifier) + mapView.register(EQNCustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: EQNCustomAnnotationView.SingleLineIdentifier) } override func loadDataSource() { @@ -142,7 +142,7 @@ class PasquakesMapViewController: EQNBaseMapViewController { return nil } - let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: EQNCustomAnnotationView.Identifier, for: annotation) as! EQNCustomAnnotationView + let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: EQNCustomAnnotationView.SingleLineIdentifier, for: annotation) as! EQNCustomAnnotationView annotationView.image = annotation.image annotationView.title = annotation.title diff --git a/Sources/Earthquake Network/Controllers/Reports/SegnalazioniMapViewController.swift b/Sources/Earthquake Network/Controllers/Reports/SegnalazioniMapViewController.swift index b9094b9..f547bd1 100644 --- a/Sources/Earthquake Network/Controllers/Reports/SegnalazioniMapViewController.swift +++ b/Sources/Earthquake Network/Controllers/Reports/SegnalazioniMapViewController.swift @@ -30,7 +30,7 @@ class SegnalazioniMapViewController: EQNBaseMapViewController { // MARK: - Public override func registerMapAnnotationViews() { - mapView.register(EQNCustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: EQNCustomAnnotationView.Identifier) + mapView.register(EQNCustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: EQNCustomAnnotationView.SingleLineIdentifier) } override func loadDataSource() { @@ -270,7 +270,7 @@ class SegnalazioniMapViewController: EQNBaseMapViewController { return nil } - let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: EQNCustomAnnotationView.Identifier, for: annotation) as! EQNCustomAnnotationView + let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: EQNCustomAnnotationView.SingleLineIdentifier, for: annotation) as! EQNCustomAnnotationView annotationView.image = annotation.image annotationView.title = annotation.title diff --git a/Sources/Earthquake Network/UI/EQNCustomAnnotationView.swift b/Sources/Earthquake Network/UI/EQNCustomAnnotationView.swift index a075a2f..cf50ae7 100644 --- a/Sources/Earthquake Network/UI/EQNCustomAnnotationView.swift +++ b/Sources/Earthquake Network/UI/EQNCustomAnnotationView.swift @@ -12,9 +12,10 @@ import MapKit @objc public class EQNCustomAnnotationView: MKAnnotationView { - @objc static let Identifier = "EQNCustomAnnotationView" - private static let AnnotationFrame = CGRect(x: 0, y: 0, width: 40, height: 40) + @objc static let SingleLineIdentifier = "EQNCustomAnnotationViewSingleLine" + @objc static let DoubleLineIdentifier = "EQNCustomAnnotationViewDoubleLine" + private static let HeightLabel: CGFloat = 15.0 @@ -26,8 +27,13 @@ public class EQNCustomAnnotationView: MKAnnotationView { } @objc public var title: String? { - set { label.text = newValue } - get { label.text } + set { labelTop.text = newValue } + get { labelTop.text } + } + + @objc public var subtitle: String? { + set { labelBottom.text = newValue } + get { labelBottom.text } } // MARK: - UI @@ -38,40 +44,67 @@ public class EQNCustomAnnotationView: MKAnnotationView { return imageView }() - private lazy var label: UILabel = { + private lazy var labelTop: UILabel = { let label = UILabel() - label.font = UIFont.systemFont(ofSize: 12, weight: .medium) + label.font = UIFont.systemFont(ofSize: 11, weight: .medium) label.textAlignment = .center return label }() + private lazy var labelBottom: UILabel = { + let label = UILabel() + label.font = UIFont.systemFont(ofSize: 11, weight: .medium) + label.textAlignment = .center + label.textColor = .red + return label + }() + // MARK: - Init @objc override init(annotation: MKAnnotation?, reuseIdentifier: String?) { super.init(annotation: annotation, reuseIdentifier: reuseIdentifier) - frame = Self.AnnotationFrame + backgroundColor = .clear - setupUI() + if reuseIdentifier == Self.SingleLineIdentifier { + frame = CGRect(x: 0, y: 0, width: 40, height: 40) + setupUI() + } else if reuseIdentifier == Self.DoubleLineIdentifier { + frame = CGRect(x: 0, y: 0, width: 40, height: 55) + setupDoubleLineUI() + } } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") } + // MARK: - Private + private func setupUI() { - backgroundColor = .clear + let labelFrame = CGRect(x: 0, y: 0, width: frame.width, height: Self.HeightLabel) + labelTop.frame = labelFrame + addSubview(labelTop) - let labelFrame = CGRect(x: 0, y: 0, width: Self.AnnotationFrame.width, height: Self.HeightLabel) - label.frame = labelFrame - addSubview(label) - - let imageViewHeight = Self.AnnotationFrame.height - labelFrame.height - imageView.frame = CGRect(x: 0, y: labelFrame.height, width: Self.AnnotationFrame.width, height: imageViewHeight) + let imageViewHeight = frame.height - labelFrame.height + imageView.frame = CGRect(x: 0, y: labelFrame.height, width: frame.width, height: imageViewHeight) addSubview(imageView) - let yOffeset = Self.AnnotationFrame.height / 2.0 - (Self.HeightLabel + imageViewHeight / 2.0) + let yOffeset = frame.height / 2.0 - (Self.HeightLabel + imageViewHeight / 2.0) centerOffset = CGPoint(x: 0, y: yOffeset) } + + private func setupDoubleLineUI() { + let labelTopFrame = CGRect(x: 0, y: 0, width: frame.width, height: Self.HeightLabel) + labelTop.frame = labelTopFrame + addSubview(labelTop) + + let imageViewHeight = frame.height - labelTopFrame.height * 2.0 + imageView.frame = CGRect(x: 0, y: labelTopFrame.height, width: frame.width, height: imageViewHeight) + addSubview(imageView) + + labelBottom.frame = CGRect(x: 0, y: imageView.frame.maxY, width: frame.width, height: Self.HeightLabel) + addSubview(labelBottom) + } }