Files
eqn.ios/Sources/Earthquake Network/UI/EQNBaseContainerTableViewCell.swift
T
2024-06-15 15:15:42 +02:00

113 lines
4.0 KiB
Swift

//
// EQNBaseContainerTableViewCell.swift
// Earthquake Network
//
// Created by Andrea Busi on 14/06/24.
// Copyright © 2024 Earthquake Network. All rights reserved.
//
import UIKit
import Shogun
extension CGFloat {
fileprivate static let cardVerticalMargin: CGFloat = 4.0
fileprivate static let cardHorizontalMargin: CGFloat = 8.0
/// Padding between the card border and the internal content
static let cardPadding: CGFloat = 8.0
/// Spacing between items inside a card
static let cardVerticalSpacing: CGFloat = 10.0
}
class EQNBaseContainerTableViewCell: UITableViewCell {
// MARK: - UI
lazy var containerView: UIView = {
let view = UIView(frame: .zero)
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .white
view.layer.cornerRadius = AppTheme.shared.cardCornerRadius
view.layer.masksToBounds = false
// add shadow
view.layer.shadowColor = UIColor.black.cgColor
view.layer.shadowOpacity = 0.5
view.layer.shadowOffset = CGSize(width: 0, height: 2)
view.layer.shadowRadius = 2
return view
}()
private lazy var headerLabel: UILabel = {
let label = EQNEdgeInsetLabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.backgroundColor = AppTheme.Colors.lightBlue
label.font = .systemFont(ofSize: 17.0, weight: .medium)
label.textColor = .white
return label
}()
private lazy var emptyTopView: UIView = {
let view = UIView(frame: .zero)
view.backgroundColor = .clear
return view
}()
/// Returns the top view to use to attach descendant constraints
var topView: UIView {
isHeaderVisible ? headerLabel : emptyTopView
}
/// If `true` an header on the top left corner will be visible
var isHeaderVisible: Bool { true }
/// Text to display inside the header
var headerText: String { "" }
// MARK: - Init
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupUI()
updateUI()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupUI()
updateUI()
}
// MARK: - Internal
func setupUI() {
selectionStyle = .default
backgroundColor = .clear
contentView.addSubview(containerView)
containerView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: .cardVerticalMargin).isActive = true
containerView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: .cardHorizontalMargin.negative).isActive = true
containerView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: .cardHorizontalMargin).isActive = true
containerView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: .cardVerticalMargin.negative).isActive = true
if isHeaderVisible {
containerView.addSubview(headerLabel)
headerLabel.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
headerLabel.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
headerLabel.heightAnchor.constraint(equalToConstant: 26.0).isActive = true
} else {
containerView.addSubview(emptyTopView)
emptyTopView.topAnchor.constraint(equalTo: containerView.topAnchor).isActive = true
emptyTopView.leadingAnchor.constraint(equalTo: containerView.leadingAnchor).isActive = true
emptyTopView.trailingAnchor.constraint(equalTo: containerView.trailingAnchor).isActive = true
emptyTopView.heightAnchor.constraint(equalToConstant: 0.0).isActive = true
}
}
func updateUI() {
// setup titles, colors and UI stuff here
headerLabel.text = headerText.firstCharacterCapitalized
}
}