refactor: Seismic map detail now inherit from new base map controller

This commit is contained in:
Andrea Busi
2021-03-07 17:44:39 +01:00
parent 4713dd78b3
commit d3a8e410d0
8 changed files with 101 additions and 205 deletions
@@ -9,64 +9,59 @@
import UIKit
import MapKit
class SeismicNetworksMapDetailViewController: EQNBaseViewController, MKMapViewDelegate {
// MARK: - UI
@IBOutlet private weak var mapView: MKMapView!
class SeismicNetworksMapDetailViewController: EQNBaseMapViewController {
// MARK: - State
var seismic: EQNSisma?
// MARK: - View Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
setupUI()
if let seismic = seismic {
addMarker(for: seismic)
}
let seismic: EQNSisma
override var availableFilters: [EQNFiltroMappa] {
// filters are not available for this map
[]
}
// MARK: - Private
// MARK: - Init
private func setupUI() {
let closeButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(closeTapped(_:)))
navigationItem.rightBarButtonItem = closeButton
init(seismic: EQNSisma) {
self.seismic = seismic
super.init()
}
private func addMarker(for seismic: EQNSisma) {
let annotation = EQNMapAnnotationSeismic(title: seismic.place, location: seismic.coordinate.coordinate, intensita: seismic.magnitude.doubleValue)
mapView.addAnnotation(annotation)
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Public
override func registerMapAnnotationViews() {
mapView.register(EQNCustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: EQNCustomAnnotationView.Identifier)
}
override func loadDataSource() {
let annotation = EQNMapAnnotationSeismic(seismic: seismic)
updateMap(with: [annotation])
}
override func centerMap() {
// center map
let span = MKCoordinateSpan(latitudeDelta: 10.5, longitudeDelta: 10.5)
let region = MKCoordinateRegion(center: seismic.coordinate.coordinate, span: span)
mapView.setCenter(seismic.coordinate.coordinate, animated: false)
mapView.setRegion(region, animated: true)
}
// MARK: - Actions
// MARK: - Map
@objc private func closeTapped(_ sender: Any) {
dismiss(animated: true)
}
// MARK: - MKMapViewDelegate
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if let sismaAnnotation = annotation as? EQNMapAnnotationSeismic {
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: EQNMapAnnotationSeismicIdentifier)
if annotationView == nil {
annotationView = sismaAnnotation.annotationView()
} else {
annotationView?.annotation = sismaAnnotation
}
return annotationView
override func setupAnnotationView(for annotation: MKAnnotation, on mapView: MKMapView) -> MKAnnotationView? {
guard let annotation = annotation as? EQNMapAnnotationSeismic else {
return nil
}
return nil
let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: EQNCustomAnnotationView.Identifier, for: annotation) as! EQNCustomAnnotationView
annotationView.image = annotation.image
annotationView.title = annotation.title
return annotationView
}
}
@@ -17,7 +17,6 @@ class SeismicNetworksViewController: UIViewController, UITableViewDelegate, UITa
case advertise(GADNativeAd)
}
private static let SegueIdentifierMap = "ShowMapDetail"
private static let SegueIdentifierFilters = "ShowFilters"
private static let SegueIdentifierSettings = "ShowSettings"
private static let SegueIdentifierSeismicNetworks = "ShowSeismicNetworks"
@@ -82,10 +81,6 @@ class SeismicNetworksViewController: UIViewController, UITableViewDelegate, UITa
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
switch segue.identifier {
case Self.SegueIdentifierMap:
if let seismic = sender as? EQNSisma, let navController = segue.destination as? UINavigationController, let controller = navController.viewControllers.first as? SeismicNetworksMapDetailViewController {
controller.seismic = seismic
}
case Self.SegueIdentifierFilters:
if let controller = segue.destination as? SeismicFiltersViewController {
controller.delegate = self
@@ -107,6 +102,11 @@ class SeismicNetworksViewController: UIViewController, UITableViewDelegate, UITa
}
}
private func showMapDetail(for seismic: EQNSisma) {
let controller = SeismicNetworksMapDetailViewController(seismic: seismic)
present(controller, animated: true, completion: nil)
}
// MARK: - Notifications
@objc func didReceiveDownloadCompleteNotification(_ sender: Notification) {
@@ -215,7 +215,7 @@ class SeismicNetworksViewController: UIViewController, UITableViewDelegate, UITa
if case .seismic(let seismic) = row {
tableView.deselectRow(at: indexPath, animated: true)
performSegue(withIdentifier: Self.SegueIdentifierMap, sender: seismic)
showMapDetail(for: seismic)
}
}
@@ -340,7 +340,7 @@ extension SeismicNetworksViewController: SeismicNetworkTableViewCellDelegate {
func seismicNetworkCellDidTapMapDetail(_ cell: SeismicNetworkTableViewCell) {
guard let index = tableView?.indexPath(for: cell), case let .seismic(seismic) = rows[index.row] else { return }
performSegue(withIdentifier: Self.SegueIdentifierMap, sender: seismic)
showMapDetail(for: seismic)
}
func seismicNetworkCellDidTapCalendar(_ cell: SeismicNetworkTableViewCell) {