From d3a8e410d07a603ea37ef8f3df8adee2a53bcafb Mon Sep 17 00:00:00 2001 From: Andrea Busi Date: Sun, 7 Mar 2021 17:44:39 +0100 Subject: [PATCH] refactor: Seismic map detail now inherit from new base map controller --- .../project.pbxproj | 10 +-- ...ismicNetworksMapDetailViewController.swift | 81 +++++++++---------- .../SeismicNetworksViewController.swift | 14 ++-- .../Earthquake Network-Bridging-Header.h | 1 - .../Map annotation/EQNMapAnnotationSeismic.h | 29 ------- .../Map annotation/EQNMapAnnotationSeismic.m | 50 ------------ .../EQNMapAnnotationSeismic.swift | 48 +++++++++++ .../Storyboards/Base.lproj/Main.storyboard | 73 +---------------- 8 files changed, 101 insertions(+), 205 deletions(-) delete mode 100644 Sources/Earthquake Network/Models/Map annotation/EQNMapAnnotationSeismic.h delete mode 100644 Sources/Earthquake Network/Models/Map annotation/EQNMapAnnotationSeismic.m create mode 100644 Sources/Earthquake Network/Models/Map annotation/EQNMapAnnotationSeismic.swift diff --git a/Sources/Earthquake Network.xcodeproj/project.pbxproj b/Sources/Earthquake Network.xcodeproj/project.pbxproj index 7245cb7..b3ab086 100644 --- a/Sources/Earthquake Network.xcodeproj/project.pbxproj +++ b/Sources/Earthquake Network.xcodeproj/project.pbxproj @@ -7,6 +7,7 @@ objects = { /* Begin PBXBuildFile section */ + 651901B925F5358700CAFF20 /* EQNMapAnnotationSeismic.swift in Sources */ = {isa = PBXBuildFile; fileRef = 651901B825F5358700CAFF20 /* EQNMapAnnotationSeismic.swift */; }; 6525A82625E13FD4008FE0D0 /* SeismicNetworkAdvertiseTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6525A82525E13FD4008FE0D0 /* SeismicNetworkAdvertiseTableViewCell.swift */; }; 6535600425F398CD00BB57D2 /* Costanti+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6535600325F398CD00BB57D2 /* Costanti+Extensions.swift */; }; 653C67E225F3CC2E00FE52AC /* EQNCustomAnnotationView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 653C67E125F3CC2E00FE52AC /* EQNCustomAnnotationView.swift */; }; @@ -47,7 +48,6 @@ 8C7A3B66225A5EA40045B266 /* NSDictionary+EQNExtensions.m in Sources */ = {isa = PBXBuildFile; fileRef = 8C7A3B64225A5EA30045B266 /* NSDictionary+EQNExtensions.m */; }; 8C7CD64821F7D0F800835812 /* EQNMapAnnotationSeismicNetwork.m in Sources */ = {isa = PBXBuildFile; fileRef = 8CD08637218B3BEE0000CB5E /* EQNMapAnnotationSeismicNetwork.m */; }; 8C8EBBA721540039002784BA /* EQNUser.m in Sources */ = {isa = PBXBuildFile; fileRef = 8C8EBBA621540039002784BA /* EQNUser.m */; }; - 8CA46BA12194532E00C63C16 /* EQNMapAnnotationSeismic.m in Sources */ = {isa = PBXBuildFile; fileRef = 8CA46BA02194532E00C63C16 /* EQNMapAnnotationSeismic.m */; }; 8CADAA9421B2627D0044E256 /* LogViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 8CADAA9321B2627D0044E256 /* LogViewController.m */; }; 8CAFD7C521825E4A00F8BD29 /* EQNSisma.m in Sources */ = {isa = PBXBuildFile; fileRef = 8CAFD7C421825E4A00F8BD29 /* EQNSisma.m */; }; 8CBD3DC72149B9AD0070C963 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 8CBD3DC62149B9AD0070C963 /* AppDelegate.m */; }; @@ -277,6 +277,7 @@ /* Begin PBXFileReference section */ 25A8BFFE29D46740E8A8A7A3 /* Pods_Earthquake_Network.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Earthquake_Network.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 40CD2E5581CF2FA3D52F392D /* Pods-Earthquake Network.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Earthquake Network.release.xcconfig"; path = "Pods/Target Support Files/Pods-Earthquake Network/Pods-Earthquake Network.release.xcconfig"; sourceTree = ""; }; + 651901B825F5358700CAFF20 /* EQNMapAnnotationSeismic.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EQNMapAnnotationSeismic.swift; sourceTree = ""; }; 6525A82525E13FD4008FE0D0 /* SeismicNetworkAdvertiseTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SeismicNetworkAdvertiseTableViewCell.swift; sourceTree = ""; }; 6535600325F398CD00BB57D2 /* Costanti+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Costanti+Extensions.swift"; sourceTree = ""; }; 653C67E125F3CC2E00FE52AC /* EQNCustomAnnotationView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EQNCustomAnnotationView.swift; sourceTree = ""; }; @@ -341,8 +342,6 @@ 8C7A3B65225A5EA40045B266 /* NSDictionary+EQNExtensions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSDictionary+EQNExtensions.h"; sourceTree = ""; }; 8C8EBBA521540039002784BA /* EQNUser.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EQNUser.h; sourceTree = ""; }; 8C8EBBA621540039002784BA /* EQNUser.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EQNUser.m; sourceTree = ""; }; - 8CA46B9F2194532E00C63C16 /* EQNMapAnnotationSeismic.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EQNMapAnnotationSeismic.h; sourceTree = ""; }; - 8CA46BA02194532E00C63C16 /* EQNMapAnnotationSeismic.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = EQNMapAnnotationSeismic.m; sourceTree = ""; }; 8CADAA9221B2627D0044E256 /* LogViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LogViewController.h; sourceTree = ""; }; 8CADAA9321B2627D0044E256 /* LogViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LogViewController.m; sourceTree = ""; }; 8CAFD7C321825E4A00F8BD29 /* EQNSisma.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = EQNSisma.h; sourceTree = ""; }; @@ -604,8 +603,7 @@ 65DBFB5225E2A2580041CBA6 /* Map annotation */ = { isa = PBXGroup; children = ( - 8CA46B9F2194532E00C63C16 /* EQNMapAnnotationSeismic.h */, - 8CA46BA02194532E00C63C16 /* EQNMapAnnotationSeismic.m */, + 651901B825F5358700CAFF20 /* EQNMapAnnotationSeismic.swift */, 8C602248218EDBE200C799C2 /* EQNMapAnnotationPastquakes.h */, 8C602249218EDBE200C799C2 /* EQNMapAnnotationPastquakes.m */, 8CD08636218B3BEE0000CB5E /* EQNMapAnnotationSeismicNetwork.h */, @@ -1595,6 +1593,7 @@ 8CF6604F214C0E58009F4314 /* EQNCalibrazione.m in Sources */, DCBB84F0252CFC4600F12633 /* AlertsNoLocationTableViewCell.swift in Sources */, DCD3E3C024D15576007C78D4 /* PurchaseProVersionViewController.swift in Sources */, + 651901B925F5358700CAFF20 /* EQNMapAnnotationSeismic.swift in Sources */, 8CF05B51218C41FB0055012B /* PickerViewController.m in Sources */, DC99A50324E66E270071BC9F /* EQNCommandProtocol.swift in Sources */, DCB45BC8250E86E100DB2D0C /* SeismicSettingsViewController.swift in Sources */, @@ -1626,7 +1625,6 @@ 8CBD3DC72149B9AD0070C963 /* AppDelegate.m in Sources */, DC974AFF251748B300A139EC /* SeismicFiltersViewController.swift in Sources */, DC105641251E7ECE002579BB /* UIFont+Extensions.swift in Sources */, - 8CA46BA12194532E00C63C16 /* EQNMapAnnotationSeismic.m in Sources */, 6535600425F398CD00BB57D2 /* Costanti+Extensions.swift in Sources */, DCB28CEE24FB8400001F557E /* SettingsViewController.swift in Sources */, DCB528212560161C005288E5 /* AlertSimulatorViewController.swift in Sources */, diff --git a/Sources/Earthquake Network/Controllers/Seismic Networks/SeismicNetworksMapDetailViewController.swift b/Sources/Earthquake Network/Controllers/Seismic Networks/SeismicNetworksMapDetailViewController.swift index 62f8d1b..95d9bcb 100644 --- a/Sources/Earthquake Network/Controllers/Seismic Networks/SeismicNetworksMapDetailViewController.swift +++ b/Sources/Earthquake Network/Controllers/Seismic Networks/SeismicNetworksMapDetailViewController.swift @@ -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 } } diff --git a/Sources/Earthquake Network/Controllers/Seismic Networks/SeismicNetworksViewController.swift b/Sources/Earthquake Network/Controllers/Seismic Networks/SeismicNetworksViewController.swift index d37882a..57596b9 100644 --- a/Sources/Earthquake Network/Controllers/Seismic Networks/SeismicNetworksViewController.swift +++ b/Sources/Earthquake Network/Controllers/Seismic Networks/SeismicNetworksViewController.swift @@ -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) { diff --git a/Sources/Earthquake Network/Earthquake Network-Bridging-Header.h b/Sources/Earthquake Network/Earthquake Network-Bridging-Header.h index 6b1e7ed..96b7c2d 100644 --- a/Sources/Earthquake Network/Earthquake Network-Bridging-Header.h +++ b/Sources/Earthquake Network/Earthquake Network-Bridging-Header.h @@ -13,7 +13,6 @@ #import "EQNMapAnnotationPastquakes.h" #import "EQNGeneratoreURLServer.h" #import "ServerRequest.h" -#import "EQNMapAnnotationSeismic.h" #import "EQNSegnalazione.h" #import "EQNPastquakes.h" #import "EQNMapAnnotationSeismicNetwork.h" diff --git a/Sources/Earthquake Network/Models/Map annotation/EQNMapAnnotationSeismic.h b/Sources/Earthquake Network/Models/Map annotation/EQNMapAnnotationSeismic.h deleted file mode 100644 index 3a05c2d..0000000 --- a/Sources/Earthquake Network/Models/Map annotation/EQNMapAnnotationSeismic.h +++ /dev/null @@ -1,29 +0,0 @@ -// -// EQNMapAnnotationSeismic.h -// Earthquake Network -// -// Refactored by Andrea Busi -// Copyright © 2021 Earthquake Network. All rights reserved. -// - -#import -#import - -NS_ASSUME_NONNULL_BEGIN - -static NSString * const EQNMapAnnotationSeismicIdentifier = @"EQNMapAnnotationSeismicIdentifier"; - - -@interface EQNMapAnnotationSeismic : NSObject - -@property (nonatomic, readonly) CLLocationCoordinate2D coordinate; -@property (nonatomic, copy) NSString *title; -@property (nonatomic, readonly) double magnitude; - -- (instancetype)initWithTitle:(NSString *)title location:(CLLocationCoordinate2D)coordinate intensita:(double)magnitude; - -- (MKAnnotationView *)annotationView; - -@end - -NS_ASSUME_NONNULL_END diff --git a/Sources/Earthquake Network/Models/Map annotation/EQNMapAnnotationSeismic.m b/Sources/Earthquake Network/Models/Map annotation/EQNMapAnnotationSeismic.m deleted file mode 100644 index bdab586..0000000 --- a/Sources/Earthquake Network/Models/Map annotation/EQNMapAnnotationSeismic.m +++ /dev/null @@ -1,50 +0,0 @@ -// -// EQNMapAnnotationSeismic.m -// Earthquake Network -// -// Refactored by Andrea Busi -// Copyright © 2021 Earthquake Network. All rights reserved. -// - -#import "EQNMapAnnotationSeismic.h" - -@implementation EQNMapAnnotationSeismic - -#pragma mark - Init - -- (instancetype)initWithTitle:(NSString *)title location:(CLLocationCoordinate2D )coordinate intensita:(double)magnitude{ - - self = [super init]; - if (self) { - _title = title; - _coordinate = coordinate; - _magnitude = magnitude; - } - return self; -} - -#pragma mark - Annotation - -- (MKAnnotationView *)annotationView -{ - MKAnnotationView *annotationView = [[MKAnnotationView alloc] initWithAnnotation:self reuseIdentifier:EQNMapAnnotationSeismicIdentifier]; - annotationView.enabled = YES; - annotationView.canShowCallout = YES; - NSString *imageString; - - NSLog(@"magnitudo %f", self.magnitude); - - if (self.magnitude > 4.0){ - imageString = @"dyamond_red"; - } else if (self.magnitude < 3.0) { - imageString = @"dyamond_green"; - } else { - imageString = @"dyamond_yellow"; - } - - annotationView.image = [UIImage imageNamed:imageString]; - annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure]; - return annotationView; -} - -@end diff --git a/Sources/Earthquake Network/Models/Map annotation/EQNMapAnnotationSeismic.swift b/Sources/Earthquake Network/Models/Map annotation/EQNMapAnnotationSeismic.swift new file mode 100644 index 0000000..5d8269d --- /dev/null +++ b/Sources/Earthquake Network/Models/Map annotation/EQNMapAnnotationSeismic.swift @@ -0,0 +1,48 @@ +// +// EQNMapAnnotationSeismic.swift +// Earthquake Network +// +// Created by Andrea Busi on 07/03/21. +// Copyright © 2021 Earthquake Network. All rights reserved. +// + +import Foundation +import MapKit + + +class EQNMapAnnotationSeismic: NSObject, MKAnnotation { + + var coordinate: CLLocationCoordinate2D + + private let magnitute: Double + private let date: Date? + + // MARK: - Init + + init(seismic: EQNSisma) { + self.coordinate = CLLocationCoordinate2D(latitude: seismic.coordinate.coordinate.latitude, longitude: seismic.coordinate.coordinate.longitude) + self.magnitute = seismic.magnitude.doubleValue + self.date = seismic.date + + super.init() + } + + // MARK: - Public + + var image: UIImage? { + if magnitute > 4.0 { + return UIImage(named: "dyamond_red") + } else if magnitute < 3.0 { + return UIImage(named: "dyamond_green") + } else { + return UIImage(named: "dyamond_yellow") + } + } + + var title: String? { + if let date = date { + return EQNUtility.formattedTimeDifference(from: date) + } + return "" + } +} diff --git a/Sources/Earthquake Network/Storyboards/Base.lproj/Main.storyboard b/Sources/Earthquake Network/Storyboards/Base.lproj/Main.storyboard index 108785b..cc1c814 100644 --- a/Sources/Earthquake Network/Storyboards/Base.lproj/Main.storyboard +++ b/Sources/Earthquake Network/Storyboards/Base.lproj/Main.storyboard @@ -360,7 +360,6 @@ In più sostieni il progetto di ricerca il quale non riceve finanziamenti estern - @@ -1465,51 +1464,6 @@ In più sostieni il progetto di ricerca il quale non riceve finanziamenti estern - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -2123,13 +2077,13 @@ Sisma rilevato da 10 smartphone - - - - - - - - - - - - - - - - - - - -