180 lines
6.9 KiB
Swift
180 lines
6.9 KiB
Swift
//
|
|
// AlertsSeismicNotificationTableViewCell.swift
|
|
// Earthquake Network
|
|
//
|
|
// Created by Busi Andrea on 05/10/2020.
|
|
// Copyright © 2020 Earthquake Network. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import MapKit
|
|
|
|
class AlertsSeismicNotificationExpandedTableViewCell: EQNBaseTableViewCell, MKMapViewDelegate {
|
|
|
|
typealias DefaultCompletion = () -> Void
|
|
|
|
@objc var notification: [String: Any]? {
|
|
didSet {
|
|
startCountdown()
|
|
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 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()
|
|
}
|
|
|
|
// 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 startCountdown() {
|
|
guard let notification = notification else { return }
|
|
|
|
// calculate the impact timestamp and start a timer for the countdown label
|
|
if let impactTimestamp = EQNUtility.calculateUserSeismicTimestamp(fromUserInfo: notification) {
|
|
self.impactTimestamp = impactTimestamp
|
|
countdownTimer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(countdownTimerFired(_:)), userInfo: nil, repeats: true)
|
|
countdownTimer?.fire()
|
|
}
|
|
}
|
|
|
|
private func updateUI() {
|
|
notificationTitleLabel.text = ""
|
|
notificationDescriptionLabel.text = ""
|
|
|
|
guard let notification = notification,
|
|
let aps = notification["aps"] as? [String: Any],
|
|
let alert = aps["alert"] as? [String: Any] else { return }
|
|
|
|
let intensity = notification.eqn_intValue(for: "intensity") ?? 0
|
|
containerView.backgroundColor = color(for: intensity)
|
|
|
|
if let title = alert["loc-key"] as? String, let args = alert["loc-args"] as? [String], let arg = args.first {
|
|
notificationTitleLabel.text = String(format: NSLocalizedString(title, comment: ""), arg)
|
|
}
|
|
|
|
// get coordinate
|
|
var coordinate: CLLocation?
|
|
if let latitude = notification.eqn_doubleValue(for: "latitude"),
|
|
let longitude = notification.eqn_doubleValue(for: "longitude") {
|
|
|
|
coordinate = CLLocation(latitude: latitude, longitude: longitude)
|
|
}
|
|
|
|
if let coordinate = coordinate,
|
|
let counter = notification["counter"],
|
|
let dateString = notification["datetime"] as? String,
|
|
let date = EQNUtility.getDateFrom(dateString) {
|
|
|
|
let distance = EQNUser.default().lastPosition?.distance(from: 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: ""), "\(counter)")
|
|
}
|
|
|
|
if let coordinate = coordinate {
|
|
let span = MKCoordinateSpan(latitudeDelta: 10.5, longitudeDelta: 10.5)
|
|
let region = MKCoordinateRegion(center: coordinate.coordinate, span: span)
|
|
|
|
mapView.setCenter(coordinate.coordinate, animated: false)
|
|
mapView.setRegion(region, animated: true)
|
|
|
|
let annotation = EQNMapAnnotationPastquake(title: "", coordinate: coordinate.coordinate, intensity: 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
|
|
}
|
|
|
|
private func color(for intensity: Int) -> UIColor {
|
|
switch intensity {
|
|
case 0: return UIColor(red: 208.0/255.0, green: 234.0/255.0, blue: 201.0/255.0, alpha:1.0)
|
|
case 1: return UIColor(red: 254.0/255.0, green: 252.0/255.0, blue: 203.0/255.0, alpha:1.0)
|
|
case 2: return UIColor(red: 254.0/255.0, green: 186.0/255.0, blue: 186.0/255.0, alpha:1.0)
|
|
default: return UIColor.white
|
|
}
|
|
}
|
|
|
|
// MARK: - Actions
|
|
|
|
@objc private func countdownTimerFired(_ sender: Timer) {
|
|
guard let impactTimestamp = impactTimestamp else { return }
|
|
|
|
let now = Date()
|
|
let difference = max(impactTimestamp.timeIntervalSince(now), 0)
|
|
waveTimeLabel.text = String(format: "%@ %.0f %@", NSLocalizedString("alert_wave", comment: ""), difference, NSLocalizedString("alert_seconds", comment: ""))
|
|
|
|
if difference <= 0 {
|
|
// stop the countdown
|
|
countdownTimer?.invalidate()
|
|
countdownTimer = nil
|
|
}
|
|
}
|
|
|
|
@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?()
|
|
}
|
|
}
|