138 lines
6.0 KiB
Swift
138 lines
6.0 KiB
Swift
//
|
|
// AlertsPositionDataTableViewCell.swift
|
|
// Earthquake Network
|
|
//
|
|
// Created by Busi Andrea on 08/10/2020.
|
|
// Copyright © 2020 Earthquake Network. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Solar
|
|
|
|
@objc
|
|
class AlertsPositionDataTableViewCell: EQNBaseContainerTableViewCell {
|
|
|
|
override var headerText: String { NSLocalizedString("weather_location", comment: "") }
|
|
|
|
private lazy var dateFormatter: DateFormatter = {
|
|
let formatter = DateFormatter()
|
|
formatter.dateStyle = .none
|
|
formatter.timeStyle = .short
|
|
return formatter
|
|
}()
|
|
|
|
// MARK: - UI
|
|
|
|
private lazy var positionImage: UIImageView = {
|
|
let imageView = UIImageView(image: .init(named: "world_old"))
|
|
imageView.translatesAutoresizingMaskIntoConstraints = false
|
|
imageView.contentMode = .scaleAspectFit
|
|
imageView.heightAnchor.constraint(equalToConstant: 30.0).isActive = true
|
|
imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor).isActive = true
|
|
return imageView
|
|
}()
|
|
|
|
private lazy var positionLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.translatesAutoresizingMaskIntoConstraints = false
|
|
label.numberOfLines = 0
|
|
label.textColor = AppTheme.shared.cardTextColor
|
|
label.font = .preferredFont(forTextStyle: .body)
|
|
return label
|
|
}()
|
|
|
|
private lazy var sunriseImage: UIImageView = {
|
|
let imageView = UIImageView(image: .init(named: "sunrise"))
|
|
imageView.translatesAutoresizingMaskIntoConstraints = false
|
|
imageView.contentMode = .scaleAspectFit
|
|
imageView.heightAnchor.constraint(equalToConstant: 30.0).isActive = true
|
|
imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor).isActive = true
|
|
return imageView
|
|
}()
|
|
|
|
private lazy var sunriseTimeLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.translatesAutoresizingMaskIntoConstraints = false
|
|
label.numberOfLines = 0
|
|
label.textColor = AppTheme.shared.cardTextColor
|
|
label.font = .preferredFont(forTextStyle: .body)
|
|
return label
|
|
}()
|
|
|
|
private lazy var sunsetImage: UIImageView = {
|
|
let imageView = UIImageView(image: .init(named: "sunset"))
|
|
imageView.translatesAutoresizingMaskIntoConstraints = false
|
|
imageView.contentMode = .scaleAspectFit
|
|
imageView.heightAnchor.constraint(equalToConstant: 30.0).isActive = true
|
|
imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor).isActive = true
|
|
return imageView
|
|
}()
|
|
|
|
private lazy var sunsetTimeLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.translatesAutoresizingMaskIntoConstraints = false
|
|
label.numberOfLines = 0
|
|
label.textColor = AppTheme.shared.cardTextColor
|
|
label.font = .preferredFont(forTextStyle: .body)
|
|
return label
|
|
}()
|
|
|
|
// MARK: - Internal
|
|
|
|
override func setupUI() {
|
|
super.setupUI()
|
|
|
|
containerView.addSubview(positionImage)
|
|
containerView.addSubview(positionLabel)
|
|
containerView.addSubview(sunriseImage)
|
|
containerView.addSubview(sunriseTimeLabel)
|
|
containerView.addSubview(sunsetImage)
|
|
containerView.addSubview(sunsetTimeLabel)
|
|
|
|
positionImage.leadingAnchor.constraint(equalTo: containerView.leadingAnchor, constant: .cardPadding).isActive = true
|
|
positionImage.centerYAnchor.constraint(equalTo: positionLabel.centerYAnchor).isActive = true
|
|
positionImage.trailingAnchor.constraint(equalTo: positionLabel.leadingAnchor, constant: .cardPadding.negative).isActive = true
|
|
positionLabel.topAnchor.constraint(equalTo: topView.bottomAnchor, constant: .cardVerticalSpacing).isActive = true
|
|
positionLabel.trailingAnchor.constraint(equalTo: containerView.trailingAnchor, constant: .cardPadding.negative).isActive = true
|
|
|
|
sunriseImage.leadingAnchor.constraint(equalTo: positionImage.leadingAnchor).isActive = true
|
|
sunriseImage.centerYAnchor.constraint(equalTo: sunriseTimeLabel.centerYAnchor).isActive = true
|
|
sunriseImage.trailingAnchor.constraint(equalTo: sunriseTimeLabel.leadingAnchor, constant: .cardPadding.negative).isActive = true
|
|
sunriseTimeLabel.topAnchor.constraint(equalTo: positionLabel.bottomAnchor, constant: .cardVerticalSpacing.x2).isActive = true
|
|
sunriseTimeLabel.trailingAnchor.constraint(equalTo: positionLabel.trailingAnchor).isActive = true
|
|
|
|
sunsetImage.leadingAnchor.constraint(equalTo: sunriseImage.leadingAnchor).isActive = true
|
|
sunsetImage.centerYAnchor.constraint(equalTo: sunsetTimeLabel.centerYAnchor).isActive = true
|
|
sunsetImage.trailingAnchor.constraint(equalTo: sunsetTimeLabel.leadingAnchor, constant: .cardPadding.negative).isActive = true
|
|
sunsetTimeLabel.topAnchor.constraint(equalTo: sunriseTimeLabel.bottomAnchor, constant: .cardVerticalSpacing.x2).isActive = true
|
|
sunsetTimeLabel.trailingAnchor.constraint(equalTo: sunriseTimeLabel.trailingAnchor).isActive = true
|
|
sunsetTimeLabel.bottomAnchor.constraint(equalTo: containerView.bottomAnchor, constant: .cardPadding.negative).isActive = true
|
|
}
|
|
|
|
override func updateUI() {
|
|
super.updateUI()
|
|
|
|
positionLabel.text = "n.d."
|
|
sunriseTimeLabel.text = "n.d."
|
|
sunsetTimeLabel.text = "n.d."
|
|
}
|
|
|
|
// MARK: - Public
|
|
|
|
@objc
|
|
func update(with position: CLLocation?) {
|
|
guard let position else { return }
|
|
|
|
positionLabel.text = EQNUtility.coordinateString(coordinate: position.coordinate)
|
|
|
|
guard let solar = Solar(coordinate: position.coordinate) else { return }
|
|
let timeZone = TimeZone.current.localizedName(for: .generic, locale: .current) ?? TimeZone.current.identifier
|
|
if let sunrise = solar.sunrise {
|
|
sunriseTimeLabel.text = dateFormatter.string(from: sunrise) + " \(timeZone)"
|
|
}
|
|
if let sunset = solar.sunset {
|
|
sunsetTimeLabel.text = dateFormatter.string(from: sunset) + " \(timeZone)"
|
|
}
|
|
}
|
|
}
|