Tusker/Tusker/Views/Poll/PollOptionCheckboxView.swift

75 lines
2.0 KiB
Swift

//
// PollOptionCheckboxView.swift
// Tusker
//
// Created by Shadowfacts on 4/25/21.
// Copyright © 2021 Shadowfacts. All rights reserved.
//
import UIKit
class PollOptionCheckboxView: UIView {
var isChecked: Bool = false {
didSet {
updateStyle()
}
}
var readOnly: Bool = true {
didSet {
updateStyle()
}
}
var voted: Bool = false {
didSet {
updateStyle()
}
}
private let imageView: UIImageView
init(multiple: Bool) {
imageView = UIImageView(image: UIImage(systemName: "checkmark")!)
super.init(frame: .zero)
let size: CGFloat = 20
layer.cornerRadius = (multiple ? 0.1 : 0.5) * size
layer.borderWidth = 2
imageView.translatesAutoresizingMaskIntoConstraints = false
imageView.isHidden = true
addSubview(imageView)
updateStyle()
NSLayoutConstraint.activate([
widthAnchor.constraint(equalTo: heightAnchor),
widthAnchor.constraint(equalToConstant: size),
imageView.widthAnchor.constraint(equalTo: widthAnchor, constant: -3),
imageView.heightAnchor.constraint(equalTo: heightAnchor, constant: -3),
imageView.centerXAnchor.constraint(equalTo: centerXAnchor),
imageView.centerYAnchor.constraint(equalTo: centerYAnchor),
])
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func updateStyle() {
imageView.isHidden = !isChecked
if voted || readOnly {
layer.borderColor = UIColor.clear.cgColor
} else if isChecked {
layer.borderColor = tintColor.cgColor
} else {
layer.borderColor = UIColor.gray.cgColor
}
backgroundColor = isChecked && !voted ? tintColor : .clear
imageView.tintColor = voted ? .label : .white
}
}