// // 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: EQNBaseContainerTableViewCell, MKMapViewDelegate { override var isHeaderVisible: Bool { false } typealias DefaultCompletion = () -> Void @objc var onTapOpenTwitter: DefaultCompletion? @objc var onTapRateApp: DefaultCompletion? @objc var onTapClose: DefaultCompletion? @objc var onTapShareApp: DefaultCompletion? // MARK: - UI private lazy var notificationTitleLabel: UILabel = { let label = UILabel() label.translatesAutoresizingMaskIntoConstraints = false label.numberOfLines = 0 label.textColor = AppTheme.shared.cardTextColor label.font = .preferredFont(forTextStyle: .title1) label.textAlignment = .center return label }() private lazy var notificationIntensityLabel: UILabel = { let label = UILabel() label.translatesAutoresizingMaskIntoConstraints = false label.numberOfLines = 0 label.textColor = AppTheme.shared.cardTextColor label.font = .preferredFont(forTextStyle: .title1) label.textAlignment = .center return label }() private lazy var notificationDescriptionLabel: UILabel = { let label = UILabel() label.translatesAutoresizingMaskIntoConstraints = false label.numberOfLines = 0 label.textColor = AppTheme.shared.cardTextColor label.font = .preferredFont(forTextStyle: .body) label.textAlignment = .center return label }() private lazy var mapView: MKMapView = { let mapView = MKMapView() mapView.translatesAutoresizingMaskIntoConstraints = false mapView.delegate = self mapView.isScrollEnabled = false mapView.isZoomEnabled = false mapView.register(EQNCustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: EQNCustomAnnotationView.SingleLineIdentifier) return mapView }() private lazy var shareButton: UIButton = { let button = EQNRoundedButton.make(target: self, action: #selector(shareAppTapped(_:))) return button }() private lazy var rateAppButton: UIButton = { let button = EQNRoundedButton.make(target: self, action: #selector(rateAppTapped(_:))) return button }() private lazy var viewOnTwitterButton: UIButton = { let button = EQNRoundedButton.make(target: self, action: #selector(viewInTwitterTapped(_:))) return button }() private lazy var closeButton: UIButton = { let button = EQNRoundedButton.make(target: self, action: #selector(closeTapped(_:))) return button }() private lazy var descriptionLabel: UILabel = { let label = UILabel() label.translatesAutoresizingMaskIntoConstraints = false label.numberOfLines = 0 label.textColor = AppTheme.shared.cardTextColor label.font = .preferredFont(forTextStyle: .body) label.textAlignment = .center return label }() // MARK: - Internal override func setupUI() { super.setupUI() let stackView = UIStackView(arrangedSubviews: [shareButton, rateAppButton]) stackView.translatesAutoresizingMaskIntoConstraints = false stackView.axis = .horizontal stackView.distribution = .fillEqually stackView.spacing = .cardVerticalSpacing containerView.addSubview(notificationTitleLabel) containerView.addSubview(notificationIntensityLabel) containerView.addSubview(notificationDescriptionLabel) containerView.addSubview(mapView) containerView.addSubview(stackView) containerView.addSubview(viewOnTwitterButton) containerView.addSubview(descriptionLabel) containerView.addSubview(closeButton) notificationTitleLabel.topAnchor.constraint(equalTo: containerView.topAnchor, constant: .cardPadding).isActive = true notificationTitleLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: .cardPadding).isActive = true notificationTitleLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: .cardPadding.negative).isActive = true notificationIntensityLabel.topAnchor.constraint(equalTo: notificationTitleLabel.bottomAnchor, constant: .cardVerticalSpacing).isActive = true notificationIntensityLabel.leadingAnchor.constraint(equalTo: notificationTitleLabel.leadingAnchor).isActive = true notificationIntensityLabel.trailingAnchor.constraint(equalTo: notificationTitleLabel.trailingAnchor).isActive = true notificationDescriptionLabel.topAnchor.constraint(equalTo: notificationIntensityLabel.bottomAnchor, constant: .cardVerticalSpacing).isActive = true notificationDescriptionLabel.leadingAnchor.constraint(equalTo: notificationTitleLabel.leadingAnchor).isActive = true notificationDescriptionLabel.trailingAnchor.constraint(equalTo: notificationTitleLabel.trailingAnchor).isActive = true mapView.topAnchor.constraint(equalTo: notificationDescriptionLabel.bottomAnchor, constant: .cardVerticalSpacing).isActive = true mapView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true mapView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true mapView.heightAnchor.constraint(greaterThanOrEqualToConstant: 240.0).isActive = true shareButton.heightAnchor.constraint(greaterThanOrEqualToConstant: 40.0).isActive = true rateAppButton.heightAnchor.constraint(equalTo: shareButton.heightAnchor).isActive = true viewOnTwitterButton.heightAnchor.constraint(equalTo: shareButton.heightAnchor).isActive = true closeButton.heightAnchor.constraint(equalTo: shareButton.heightAnchor).isActive = true stackView.topAnchor.constraint(equalTo: mapView.bottomAnchor, constant: .cardVerticalSpacing).isActive = true stackView.leadingAnchor.constraint(equalTo: notificationDescriptionLabel.leadingAnchor).isActive = true stackView.trailingAnchor.constraint(equalTo: notificationDescriptionLabel.trailingAnchor).isActive = true viewOnTwitterButton.topAnchor.constraint(equalTo: stackView.bottomAnchor, constant: .cardVerticalSpacing).isActive = true viewOnTwitterButton.leadingAnchor.constraint(equalTo: stackView.leadingAnchor).isActive = true viewOnTwitterButton.trailingAnchor.constraint(equalTo: stackView.trailingAnchor).isActive = true descriptionLabel.topAnchor.constraint(equalTo: viewOnTwitterButton.bottomAnchor, constant: .cardPadding).isActive = true descriptionLabel.leadingAnchor.constraint(equalTo: viewOnTwitterButton.leadingAnchor).isActive = true descriptionLabel.trailingAnchor.constraint(equalTo: viewOnTwitterButton.trailingAnchor).isActive = true closeButton.topAnchor.constraint(equalTo: descriptionLabel.bottomAnchor, constant: .cardVerticalSpacing).isActive = true closeButton.leadingAnchor.constraint(equalTo: descriptionLabel.leadingAnchor).isActive = true closeButton.trailingAnchor.constraint(equalTo: descriptionLabel.trailingAnchor).isActive = true closeButton.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: .cardVerticalSpacing.negative).isActive = true } override func updateUI() { super.updateUI() 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: "") } // MARK: - Public @objc func update(with notification: EQNRealtimePushNotification?) { // clearn any other previous notifications notificationTitleLabel.text = "Sisma rilevato a 150km (TEST)" notificationIntensityLabel.text = "Previsto uno scuotimento forte" notificationDescriptionLabel.text = "Distanza 150 km - 13 minuti fa" mapView.removeAnnotations(mapView.annotations) guard let notification = notification else { return } 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) } 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 @objc private func shareAppTapped(_ sender: UIButton) { onTapShareApp?() } @objc private func rateAppTapped(_ sender: UIButton) { onTapRateApp?() } @objc private func viewInTwitterTapped(_ sender: UIButton) { onTapOpenTwitter?() } @objc private func closeTapped(_ sender: UIButton) { onTapClose?() } // MARK: - Private private func backgroundColor(for intensity: Double) -> UIColor { switch intensity { case _ where intensity < 0.004: return AppTheme.Colors.cardBackgroundGray case _ where intensity < 0.30: return AppTheme.Colors.cardBackgroundGreen case _ where intensity < 0.70: return AppTheme.Colors.cardBackgroundYellow default: return AppTheme.Colors.cardBackgroundRed } } }