Use UIButton.configuration for ... configuration (#1218)

This commit is contained in:
Nathan Mattes 2024-02-07 17:17:48 +01:00
parent 706d882cb0
commit 5103677e26
1 changed files with 18 additions and 61 deletions

View File

@ -9,74 +9,31 @@ import UIKit
import MastodonAsset
import MastodonLocalization
public final class ProfileRelationshipActionButton: RoundedEdgesButton {
public let activityIndicatorView: UIActivityIndicatorView = {
let activityIndicatorView = UIActivityIndicatorView(style: .medium)
activityIndicatorView.color = Asset.Colors.Label.primaryReverse.color
return activityIndicatorView
}()
public override init(frame: CGRect) {
super.init(frame: frame)
_init()
}
public required init?(coder: NSCoder) {
super.init(coder: coder)
_init()
}
}
extension ProfileRelationshipActionButton {
private func _init() {
cornerRadius = 10
titleLabel?.font = .systemFont(ofSize: 17, weight: .semibold)
activityIndicatorView.translatesAutoresizingMaskIntoConstraints = false
addSubview(activityIndicatorView)
NSLayoutConstraint.activate([
activityIndicatorView.centerXAnchor.constraint(equalTo: centerXAnchor),
activityIndicatorView.centerYAnchor.constraint(equalTo: centerYAnchor),
])
activityIndicatorView.hidesWhenStopped = true
activityIndicatorView.stopAnimating()
configureAppearance()
}
public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
configureAppearance()
}
}
extension ProfileRelationshipActionButton {
public final class ProfileRelationshipActionButton: UIButton {
public func configure(actionOptionSet: RelationshipActionOptionSet) {
setTitle(actionOptionSet.title, for: .normal)
titleEdgeInsets = UIEdgeInsets(top: 0, left: 4, bottom: 0, right: 4)
activityIndicatorView.stopAnimating()
var configuration = UIButton.Configuration.filled()
configuration.attributedTitle = AttributedString(
actionOptionSet.title,
attributes: AttributeContainer([
.font: UIFont.systemFont(ofSize: 17, weight: .semibold),
.foregroundColor: Asset.Colors.Label.primaryReverse.color
])
)
configuration.contentInsets = NSDirectionalEdgeInsets(top: 0, leading: 4, bottom: 0, trailing: 4)
configuration.baseBackgroundColor = Asset.Scene.Profile.RelationshipButton.background.color
configuration.background.cornerRadius = 10
if let option = actionOptionSet.highPriorityAction(except: .editOptions), option == .blocked || option == .suspended {
isEnabled = false
} else if actionOptionSet.contains(.updating) {
isEnabled = false
activityIndicatorView.startAnimating()
configuration.showsActivityIndicator = true
} else {
isEnabled = true
}
}
private func configureAppearance() {
setTitleColor(Asset.Colors.Label.primaryReverse.color, for: .normal)
setTitleColor(Asset.Colors.Label.primaryReverse.color.withAlphaComponent(0.5), for: .highlighted)
setBackgroundImage(.placeholder(color: Asset.Scene.Profile.RelationshipButton.background.color), for: .normal)
setBackgroundImage(.placeholder(color: Asset.Scene.Profile.RelationshipButton.backgroundHighlighted.color), for: .highlighted)
setBackgroundImage(.placeholder(color: Asset.Scene.Profile.RelationshipButton.backgroundHighlighted.color), for: .disabled)
self.configuration = configuration
}
}