// // EQNMapAnnotationUserReport.swift // Earthquake Network // // Created by Andrea Busi on 06/03/21. // Copyright © 2021 Earthquake Network. All rights reserved. // import Foundation import MapKit import Shogun @objc class EQNMapAnnotationUserReport: NSObject, MKAnnotation { @objc var coordinate: CLLocationCoordinate2D @objc var title: String? @objc var subtitle: String? @objc var timeDifference: String? private let magnitude: Int var report: EQNSegnalazione? // MARK: - Init convenience init(report: EQNSegnalazione) { let coordinate = report.coordinate.coordinate let magnitude = report.intensity self.init(magnitude: magnitude, coordinate: coordinate) self.report = report self.timeDifference = EQNUtility.formattedTimeDifference(from: report.date) } @objc init( magnitude: Int, coordinate: CLLocationCoordinate2D ) { self.magnitude = magnitude self.coordinate = coordinate self.title = Self.title(for: magnitude) self.subtitle = Self.subtitle(for: magnitude) } // MARK: - Helper @objc(imageWithHeight:) func image( with height: CGFloat ) -> UIImage? { guard let color = UIColor(named: "Mercalli \(magnitude)") else { print("[EQNMapAnnotationUserReport] Unable to get a color for magnitude: \(magnitude)") return nil } return UIImage.circle(diameter: height, color: color, borderWidth: 1.0, borderColor: .black) } private static func title(for magnitude: Int) -> String { let grade = magnitude / 10 let roman = grade.romanNumber() return String(format: NSLocalizedString("mercalli_intensity", comment: ""), roman) } private static func subtitle(for magnitude: Int) -> String { let grade = magnitude / 10 let roman = grade.romanNumber() let string = NSLocalizedString("mercalli_\(roman)", comment: "") // strings are in the format like "II - Appena percepito" // so we are going to remove the "II" from the string let components = string.components(separatedBy: " - ") if components.count > 1 { return components[1] } return string } }