Skip to content
Snippets Groups Projects
AttributeSwitcher.swift 3.08 KiB
Newer Older
Bruno Muniz's avatar
Bruno Muniz committed
import UIKit
import Shared

final class AttributeSwitcher: UIView {
Ahmed Shehata's avatar
Ahmed Shehata committed
    struct State {
        var content: String
        var isVisible: Bool
    }

    private let titleLabel = UILabel()
    private let contentLabel = UILabel()
    private let stackView = UIStackView()
    private(set) var switcherView = UISwitch()
    private let verticalStackView = UIStackView()

    private(set) var addButton: UIControl = {
        let label = UILabel()
        let icon = UIImageView()
        let control = UIControl()

        icon.image = Asset.scanAdd.image
        label.text = Localized.Scan.Display.Share.add
        label.textColor = Asset.brandPrimary.color

        control.addSubview(icon)
        control.addSubview(label)

        icon.snp.makeConstraints {
            $0.left.equalToSuperview()
            $0.top.equalToSuperview()
            $0.bottom.equalToSuperview()
            $0.width.equalTo(icon.snp.height)
        }

        label.snp.makeConstraints {
            $0.left.equalTo(icon.snp.right).offset(5)
            $0.top.equalToSuperview()
            $0.right.equalToSuperview()
            $0.bottom.equalToSuperview()
        }

        return control
    }()
Bruno Muniz's avatar
Bruno Muniz committed

    public init() {
        super.init(frame: .zero)

        contentLabel.textColor = Asset.neutralActive.color
        titleLabel.textColor = Asset.neutralWeak.color
        switcherView.onTintColor = Asset.brandPrimary.color

        contentLabel.numberOfLines = 0
        contentLabel.font = Fonts.Mulish.regular.font(size: 16.0)
        titleLabel.font = Fonts.Mulish.bold.font(size: 12.0)

        addSubview(stackView)

Ahmed Shehata's avatar
Ahmed Shehata committed
        verticalStackView.spacing = 5
Bruno Muniz's avatar
Bruno Muniz committed
        verticalStackView.axis = .vertical
        verticalStackView.addArrangedSubview(titleLabel)
        verticalStackView.addArrangedSubview(contentLabel)

Ahmed Shehata's avatar
Ahmed Shehata committed
        switcherView.setContentCompressionResistancePriority(.required, for: .vertical)
        switcherView.setContentCompressionResistancePriority(.required, for: .horizontal)
Bruno Muniz's avatar
Bruno Muniz committed

        stackView.addArrangedSubview(verticalStackView)
Ahmed Shehata's avatar
Ahmed Shehata committed
        stackView.addArrangedSubview(FlexibleSpace())
Bruno Muniz's avatar
Bruno Muniz committed

Ahmed Shehata's avatar
Ahmed Shehata committed
        let otherHStack = UIStackView()
        otherHStack.addArrangedSubview(addButton)
        otherHStack.addArrangedSubview(switcherView)
Bruno Muniz's avatar
Bruno Muniz committed

Ahmed Shehata's avatar
Ahmed Shehata committed
        let otherVStack = UIStackView()
        otherVStack.axis = .vertical
        otherVStack.addArrangedSubview(otherHStack)
        otherVStack.addArrangedSubview(FlexibleSpace())

        stackView.addArrangedSubview(otherVStack)

        stackView.snp.makeConstraints {
            $0.edges.equalToSuperview()
Bruno Muniz's avatar
Bruno Muniz committed
        }
    }

    required init?(coder: NSCoder) { nil }

Ahmed Shehata's avatar
Ahmed Shehata committed
    func setup(state: State?, title: String) {
Bruno Muniz's avatar
Bruno Muniz committed
        titleLabel.text = title

Ahmed Shehata's avatar
Ahmed Shehata committed
        guard let state = state else {
            addButton.isHidden = false
            switcherView.isHidden = true
            contentLabel.text = Localized.Scan.Display.Share.notAdded
Bruno Muniz's avatar
Bruno Muniz committed
            return
        }
Ahmed Shehata's avatar
Ahmed Shehata committed

        addButton.isHidden = true
        switcherView.isHidden = false
        switcherView.isOn = state.isVisible
        contentLabel.text = state.isVisible ? state.content : Localized.Scan.Display.Share.hidden
Bruno Muniz's avatar
Bruno Muniz committed
    }
}