104 lines
3.2 KiB
Swift
104 lines
3.2 KiB
Swift
//
|
|
// SettingSegmentedTableViewCell.swift
|
|
// Earthquake Network
|
|
//
|
|
// Created by Busi Andrea on 27/08/2020.
|
|
// Copyright © 2020 Earthquake Network. All rights reserved.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
|
|
class SettingSegmentedTableViewCell: UITableViewCell {
|
|
|
|
static let Identifier = "SegmentedCell"
|
|
|
|
var isDisabled: Bool = false {
|
|
didSet {
|
|
updateUI()
|
|
}
|
|
}
|
|
var valueChanged: ((EQNGenericValue) -> Void)?
|
|
private var items = [EQNGenericValue]()
|
|
|
|
// MARK: - Properties
|
|
|
|
lazy var titleLabel: UILabel = {
|
|
let label = UILabel()
|
|
label.translatesAutoresizingMaskIntoConstraints = false
|
|
label.numberOfLines = 0
|
|
label.font = UIFont.preferredFont(forTextStyle: .body)
|
|
label.textColor = AppTheme.shared.textColor
|
|
return label
|
|
}()
|
|
|
|
lazy var segmentedControl: UISegmentedControl = {
|
|
let segmented = UISegmentedControl()
|
|
segmented.translatesAutoresizingMaskIntoConstraints = false
|
|
segmented.addTarget(self, action: #selector(segmentedControlChanged(_:)), for: .valueChanged)
|
|
return segmented
|
|
}()
|
|
|
|
// 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 = .vertical
|
|
stackView.distribution = .equalSpacing
|
|
stackView.spacing = 8
|
|
|
|
stackView.addArrangedSubview(titleLabel)
|
|
stackView.addArrangedSubview(segmentedControl)
|
|
|
|
contentView.addSubview(stackView)
|
|
|
|
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
|
|
stackView.bottomAnchor.constraint(equalTo: contentView.layoutMarginsGuide.bottomAnchor).isActive = true
|
|
}
|
|
|
|
private func updateUI() {
|
|
let theme = AppTheme.shared
|
|
titleLabel.textColor = isDisabled ? theme.textDisabledColor : theme.textColor
|
|
segmentedControl.isEnabled = !isDisabled
|
|
}
|
|
|
|
// MARK: - Actions
|
|
|
|
@objc private func segmentedControlChanged(_ sender: UISegmentedControl) {
|
|
let item = items[sender.selectedSegmentIndex]
|
|
valueChanged?(item)
|
|
}
|
|
|
|
// MARK: - Public
|
|
|
|
@objc func configureControl(with items: [EQNGenericValue], current: EQNGenericValue) {
|
|
self.items = items
|
|
|
|
segmentedControl.removeAllSegments()
|
|
items.enumerated().forEach { (index, value) in
|
|
segmentedControl.insertSegment(withTitle: value.display, at: index, animated: false)
|
|
}
|
|
|
|
if let index = items.firstIndex(of: current) {
|
|
segmentedControl.selectedSegmentIndex = index
|
|
}
|
|
}
|
|
}
|