// // SettingEnableTableViewCell.swift // Earthquake Network // // Created by Busi Andrea on 25/08/2020. // Copyright © 2020 Earthquake Network. All rights reserved. // import UIKit class SettingEnableTableViewCell: UITableViewCell { static let Identifier = "EnableCell" var valueChanged: ((Bool) -> Void)? var isDisabled: Bool = false { didSet { updateUI() } } // MARK: - Properties lazy var titleLabel: UILabel = { let label = UILabel() label.translatesAutoresizingMaskIntoConstraints = false label.numberOfLines = 0 label.font = UIFont.preferredFont(forTextStyle: .body) return label }() lazy var descriptionLabel: UILabel = { let label = UILabel() label.translatesAutoresizingMaskIntoConstraints = false label.numberOfLines = 0 label.font = UIFont.preferredFont(forTextStyle: .subheadline) return label }() lazy var toggleSwitch: UISwitch = { let toggle = UISwitch() toggle.setContentHuggingPriority(.required, for: .horizontal) toggle.setContentCompressionResistancePriority(.required, for: .horizontal) toggle.addTarget(self, action: #selector(toggleChanged(_:)), for: .valueChanged) return toggle }() lazy var errorLabel: UILabel = { let label = UILabel() label.translatesAutoresizingMaskIntoConstraints = false label.numberOfLines = 0 label.font = UIFont.preferredFont(forTextStyle: .subheadline) label.textColor = AppTheme.Colors.red label.text = nil return label }() // MARK: - Init override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) { super.init(style: style, reuseIdentifier: reuseIdentifier) setupUI() } required init?(coder: NSCoder) { super.init(coder: coder) setupUI() } // MARK: - Private private func setupUI() { selectionStyle = .none let stackView = UIStackView() stackView.translatesAutoresizingMaskIntoConstraints = false stackView.axis = .horizontal stackView.distribution = .fill stackView.alignment = .center stackView.spacing = 8 stackView.addArrangedSubview(titleLabel) stackView.addArrangedSubview(toggleSwitch) contentView.addSubview(stackView) contentView.addSubview(descriptionLabel) contentView.addSubview(errorLabel) stackView.topAnchor.constraint(equalTo: contentView.layoutMarginsGuide.topAnchor).isActive = true stackView.trailingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.trailingAnchor).isActive = true stackView.leadingAnchor.constraint(equalTo: contentView.layoutMarginsGuide.leadingAnchor).isActive = true descriptionLabel.topAnchor.constraint(equalTo: stackView.bottomAnchor, constant: 8).isActive = true descriptionLabel.trailingAnchor.constraint(equalTo: stackView.trailingAnchor).isActive = true descriptionLabel.leadingAnchor.constraint(equalTo: stackView.leadingAnchor).isActive = true //descriptionLabel.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor).isActive = true errorLabel.topAnchor.constraint(equalTo: descriptionLabel.bottomAnchor, constant: 8.0).isActive = true errorLabel.trailingAnchor.constraint(equalTo: descriptionLabel.trailingAnchor).isActive = true errorLabel.leadingAnchor.constraint(equalTo: descriptionLabel.leadingAnchor).isActive = true errorLabel.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor).isActive = true } private func updateUI() { toggleSwitch.isEnabled = !isDisabled titleLabel.textColor = isDisabled ? .lightGray : .black descriptionLabel.textColor = isDisabled ? .lightGray : .black } // MARK: - Action @objc private func toggleChanged(_ sender: UISwitch) { valueChanged?(sender.isOn) } }