Files
eqn.ios/Sources/Earthquake Network/Models/Map annotation/EQNMapAnnotationSeismic.swift
T

83 lines
2.8 KiB
Swift

//
// EQNMapAnnotationSeismic.swift
// Earthquake Network
//
// Created by Andrea Busi on 07/03/21.
// Copyright © 2021 Earthquake Network. All rights reserved.
//
import UIKit
import MapKit
class EQNMapAnnotationSeismic: NSObject, MKAnnotation {
let coordinate: CLLocationCoordinate2D
let title: String?
let subtitle: String?
let textColor: UIColor
let seismic: EQNSisma
// MARK: - Init
init(seismic: EQNSisma) {
self.seismic = seismic
self.coordinate = CLLocationCoordinate2D(latitude: seismic.coordinate.coordinate.latitude,
longitude: seismic.coordinate.coordinate.longitude)
self.title = seismic.provider
if let date = seismic.date {
self.subtitle = EQNUtility.formattedTimeDifference(from: date)
} else {
self.subtitle = nil
}
self.textColor = Self.textColor(for: seismic)
super.init()
}
// MARK: - Helpers
func image(
height: CGFloat,
isUserSelection: Bool
) -> UIImage? {
let color = Self.imageColor(for: seismic)
let borderColor = isUserSelection ? AppTheme.Colors.red : AppTheme.Colors.darkGray
return UIImage.circle(diameter: height, color: color, borderWidth: 2.0, borderColor: borderColor)
}
// MARK: - Private
private class func textColor(for seismic: EQNSisma) -> UIColor {
let magnitude = seismic.magnitude.doubleValue
if magnitude < 2.0 {
return UIColor(red: 0.0, green: 200.0/255.0, blue: 200.0/255.0, alpha: 1.0)
} else if magnitude < 3.5 {
return UIColor(red: 0.0, green: 200.0/255.0, blue: 0.0, alpha: 1.0)
} else if magnitude < 4.5 {
return UIColor(red: 240.0/255.0, green: 190.0/255.0, blue: 0.0, alpha: 1.0)
} else if magnitude < 5.5 {
return UIColor(red: 200.0/255.0, green: 0.0, blue: 0.0, alpha: 1.0)
} else {
return UIColor(red: 200.0/255.0, green: 0.0, blue: 200.0/255.0, alpha: 1.0)
}
}
private class func imageColor(for seismic: EQNSisma) -> UIColor {
let magnitude = seismic.magnitude.doubleValue
if magnitude < 2.0 {
return UIColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0) // white
} else if magnitude < 3.5 {
return UIColor(red: 0.0, green: 1.0, blue: 0.0, alpha: 1.0) // green
} else if magnitude < 4.5 {
return UIColor(red: 1.0, green: 1.0, blue: 0.0, alpha: 1.0) // yellow
} else if magnitude < 5.5 {
return UIColor(red: 1.0, green: 0.0, blue: 0.0, alpha: 1.0) // red
} else {
return UIColor(red: 1.0, green: 0.0, blue: 1.0, alpha: 1.0) // magenta
}
}
}