118 lines
4.8 KiB
Swift
118 lines
4.8 KiB
Swift
//
|
|
// SegnalazioniSendReportCell.swift
|
|
// Earthquake Network
|
|
//
|
|
// Created by Andrea Busi on 04/04/21.
|
|
// Copyright © 2021 Earthquake Network. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Shogun
|
|
|
|
class SegnalazioniSendReportCell: EQNBaseContainerTableViewCell {
|
|
|
|
private struct Report: Equatable {
|
|
let magnitude: Int
|
|
let text: String
|
|
let color: UIColor
|
|
|
|
static func == (lhs: Self, rhs: Self) -> Bool {
|
|
lhs.magnitude == rhs.magnitude
|
|
}
|
|
}
|
|
|
|
@objc var onTapReport: (_ magnitude: Int) -> Void = { _ in }
|
|
|
|
override var headerText: String { NSLocalizedString("main_feel", comment: "") }
|
|
|
|
// MARK: - UI
|
|
|
|
private let reports: [Report] = [
|
|
.init(magnitude: 20, text: NSLocalizedString("mercalli_II", comment: ""), color: .init(named: "Mercalli 20")!),
|
|
.init(magnitude: 30, text: NSLocalizedString("mercalli_III", comment: ""), color: .init(named: "Mercalli 30")!),
|
|
.init(magnitude: 40, text: NSLocalizedString("mercalli_IV", comment: ""), color: .init(named: "Mercalli 40")!),
|
|
.init(magnitude: 50, text: NSLocalizedString("mercalli_V", comment: ""), color: .init(named: "Mercalli 50")!),
|
|
.init(magnitude: 60, text: NSLocalizedString("mercalli_VI", comment: ""), color: .init(named: "Mercalli 60")!),
|
|
.init(magnitude: 70, text: NSLocalizedString("mercalli_VII", comment: ""), color: .init(named: "Mercalli 70")!),
|
|
.init(magnitude: 80, text: NSLocalizedString("mercalli_VIII", comment: ""), color: .init(named: "Mercalli 80")!),
|
|
.init(magnitude: 90, text: NSLocalizedString("mercalli_IX", comment: ""), color: .init(named: "Mercalli 90")!),
|
|
.init(magnitude: 100, text: NSLocalizedString("mercalli_X", comment: ""), color: .init(named: "Mercalli 100")!),
|
|
.init(magnitude: 110, text: NSLocalizedString("mercalli_XI", comment: ""), color: .init(named: "Mercalli 110")!),
|
|
.init(magnitude: 120, text: NSLocalizedString("mercalli_XII", comment: ""), color: .init(named: "Mercalli 120")!)
|
|
]
|
|
|
|
// MARK: - Internal
|
|
|
|
override func setupUI() {
|
|
super.setupUI()
|
|
|
|
var previousView = topView
|
|
reports.enumerated().forEach { index, report in
|
|
let view = createContentView(magnitude: report.magnitude, text: report.text, color: report.color)
|
|
containerView.addSubview(view)
|
|
|
|
view.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
|
|
view.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true
|
|
|
|
let padding: CGFloat = report == reports.first ? .cardPadding : 0
|
|
view.topAnchor.constraint(equalTo: previousView.bottomAnchor, constant: padding).isActive = true
|
|
|
|
if report == reports.last {
|
|
view.bottomAnchor.constraint(equalTo: containerView.bottomAnchor).isActive = true
|
|
}
|
|
|
|
previousView = view
|
|
}
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
private func createContentView(
|
|
magnitude: Int,
|
|
text: String,
|
|
color: UIColor
|
|
) -> UIView {
|
|
let view = UIView(frame: .zero)
|
|
view.translatesAutoresizingMaskIntoConstraints = false
|
|
view.backgroundColor = color
|
|
|
|
let label = UILabel(frame: .zero)
|
|
label.translatesAutoresizingMaskIntoConstraints = false
|
|
label.font = .preferredFont(forTextStyle: .body)
|
|
label.textColor = AppTheme.shared.cardTextColor
|
|
label.numberOfLines = 0
|
|
label.text = text
|
|
|
|
let button = UIButton(type: .system)
|
|
button.translatesAutoresizingMaskIntoConstraints = false
|
|
button.backgroundColor = .clear
|
|
button.tag = magnitude
|
|
button.addTarget(self, action: #selector(onTapMagnitudeButton(_:)), for: .touchUpInside)
|
|
|
|
view.addSubview(label)
|
|
view.addSubview(button)
|
|
|
|
// use a custom vertical spacing to make single lines bigger
|
|
let verticalSpacing: CGFloat = 15.0
|
|
label.topAnchor.constraint(equalTo: view.topAnchor, constant: verticalSpacing).isActive = true
|
|
label.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: .cardPadding).isActive = true
|
|
label.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: .cardPadding.negative).isActive = true
|
|
label.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -verticalSpacing).isActive = true
|
|
button.constraint(to: view)
|
|
|
|
return view
|
|
}
|
|
|
|
// MARK: - Actions
|
|
|
|
@IBAction private func onTapReportButton(_ sender: UIButton) {
|
|
let magnitude = sender.tag
|
|
onTapReport(magnitude)
|
|
}
|
|
|
|
@objc private func onTapMagnitudeButton(_ sender: UIButton) {
|
|
let magnitude = sender.tag
|
|
onTapReport(magnitude)
|
|
}
|
|
}
|