// // AlertsSeismicNotificationTableViewCell.swift // Earthquake Network // // Created by Busi Andrea on 05/10/2020. // Copyright © 2020 Earthquake Network. All rights reserved. // import UIKit import MapKit import Shogun class AlertsSeismicNotificationExpandedTableViewCell: EQNBaseTableViewCell, MKMapViewDelegate { typealias DefaultCompletion = () -> Void @objc var notification: EQNRealtimePushNotification? { didSet { updateUI() } } @objc var onTapOpenTwitter: DefaultCompletion? @objc var onTapRateApp: DefaultCompletion? @objc var onTapClose: DefaultCompletion? @objc var onTapShareApp: DefaultCompletion? // MARK: - Internal @IBOutlet weak var notificationTitleLabel: UILabel! @IBOutlet weak var notificationDescriptionLabel: UILabel! @IBOutlet weak var notificationIntensityLabel: UILabel! @IBOutlet weak var waveTimeLabel: UILabel! @IBOutlet weak var mapView: MKMapView! { didSet { mapView.delegate = self mapView.isScrollEnabled = false mapView.isZoomEnabled = false mapView.register(EQNCustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: EQNCustomAnnotationView.SingleLineIdentifier) } } @IBOutlet private weak var shareButton: UIButton! @IBOutlet private weak var rateAppButton: UIButton! @IBOutlet private weak var viewOnTwitterButton: UIButton! @IBOutlet private weak var closeButton: UIButton! @IBOutlet private weak var descriptionLabel: UILabel! private var impactTimestamp: Date? private var countdownTimer: Timer? // MARK: - View Lifecycle override func awakeFromNib() { super.awakeFromNib() localizeUI() setUI() } // MARK: - Private private func localizeUI() { shareButton.setLocalizedTitle(key: "main_share_app") rateAppButton.setLocalizedTitle(key: "main_vote") viewOnTwitterButton.setLocalizedTitle(key: "main_twitter_see") closeButton.setLocalizedTitle(key: "official_close") descriptionLabel.text = NSLocalizedString("map_smartphone_magnitude", comment: "") } private func setUI() { shareButton.layer.borderColor = AppTheme.Colors.darkGray.cgColor rateAppButton.layer.borderColor = AppTheme.Colors.darkGray.cgColor viewOnTwitterButton.layer.borderColor = AppTheme.Colors.darkGray.cgColor closeButton.layer.borderColor = AppTheme.Colors.darkGray.cgColor } private func updateUI() { // clearn any other previous notifications notificationTitleLabel.text = "" notificationDescriptionLabel.text = "" mapView.removeAnnotations(mapView.annotations) guard let notification = notification else { return } containerView.backgroundColor = backgroundColor(for: notification.relativeIntensity()) notificationTitleLabel.text = notification.title notificationIntensityLabel.text = notification.displayBody notificationIntensityLabel.textColor = notification.relativeIntensityColor if let date = notification.dateTime { let distance = EQNUser.default().lastPosition?.distance(from: notification.coordinate) ?? 0.0 let distanceRound = Int(round(distance / 1_000)) let difference = Int(NSDate().timeIntervalSince(date) / 60.0) notificationDescriptionLabel.text = "" + NSLocalizedString("official_card_distance", comment: "") + " \(distanceRound) km" + " - " + EQNUtility.formattedString(forTimeDifference: difference) + "\n" + String(format: NSLocalizedString("map_number", comment: ""), "\(notification.counter)") } let span = MKCoordinateSpan(latitudeDelta: 10.5, longitudeDelta: 10.5) let region = MKCoordinateRegion(center: notification.coordinate.coordinate, span: span) mapView.setCenter(notification.coordinate.coordinate, animated: false) mapView.setRegion(region, animated: true) let annotation = EQNMapAnnotationPastquake(title: "", coordinate: notification.coordinate.coordinate, intensity: notification.intensity) mapView.addAnnotation(annotation) } func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { guard let annotation = annotation as? EQNMapAnnotationPastquake else { return nil } let annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: EQNCustomAnnotationView.SingleLineIdentifier, for: annotation) as! EQNCustomAnnotationView annotationView.image = annotation.image annotationView.title = annotation.title return annotationView } // MARK: - Actions @IBAction private func shareAppTapped(_ sender: UIButton) { onTapShareApp?() } @IBAction private func rateAppTapped(_ sender: UIButton) { onTapRateApp?() } @IBAction private func viewInTwitterTapped(_ sender: UIButton) { onTapOpenTwitter?() } @IBAction private func closeTapped(_ sender: UIButton) { onTapClose?() } // MARK: - Helpers private func backgroundColor(for intensity: Double) -> UIColor { switch intensity { case _ where intensity < 0.004: return UIColor(named: "Gray (card background)")! case _ where intensity < 0.30: return UIColor(named: "Green (card background)")! case _ where intensity < 0.70: return UIColor(named: "Yellow (card background)")! default: return UIColor(named: "Red (card background)")! } } }