mastodon-app-ufficiale-ipho.../MastodonSDK/Sources/MastodonUI/View/Control/ProfileRelationshipActionBu...

92 lines
3.4 KiB
Swift
Raw Normal View History

//
// ProfileRelationshipActionButton.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021-3-30.
//
import UIKit
import MastodonAsset
import MastodonLocalization
public final class ProfileRelationshipActionButton: RoundedEdgesButton {
public let activityIndicatorView: UIActivityIndicatorView = {
2021-04-09 11:31:43 +02:00
let activityIndicatorView = UIActivityIndicatorView(style: .medium)
2022-02-15 11:15:58 +01:00
activityIndicatorView.color = Asset.Colors.Label.primaryReverse.color
2021-04-09 11:31:43 +02:00
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)
2021-04-09 11:31:43 +02:00
NSLayoutConstraint.activate([
activityIndicatorView.centerXAnchor.constraint(equalTo: centerXAnchor),
activityIndicatorView.centerYAnchor.constraint(equalTo: centerYAnchor),
2021-04-09 11:31:43 +02:00
])
activityIndicatorView.hidesWhenStopped = true
activityIndicatorView.stopAnimating()
configureAppearance()
}
2022-02-15 11:15:58 +01:00
public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
2022-02-15 11:15:58 +01:00
super.traitCollectionDidChange(previousTraitCollection)
configureAppearance()
2022-02-15 11:15:58 +01:00
}
}
extension ProfileRelationshipActionButton {
public func configure(actionOptionSet: RelationshipActionOptionSet) {
setTitle(actionOptionSet.title, for: .normal)
2022-02-15 11:15:58 +01:00
configureAppearance()
2021-08-06 11:45:07 +02:00
titleEdgeInsets = UIEdgeInsets(top: 0, left: 4, bottom: 0, right: 4)
activityIndicatorView.stopAnimating()
2021-04-09 11:31:43 +02:00
if let option = actionOptionSet.highPriorityAction(except: .editOptions), option == .blocked || option == .suspended {
isEnabled = false
2021-04-09 11:31:43 +02:00
} else if actionOptionSet.contains(.updating) {
isEnabled = false
activityIndicatorView.startAnimating()
} else {
isEnabled = true
}
}
2022-02-15 11:15:58 +01:00
private func configureAppearance() {
setTitleColor(Asset.Colors.Label.primaryReverse.color, for: .normal)
setTitleColor(Asset.Colors.Label.primaryReverse.color.withAlphaComponent(0.5), for: .highlighted)
switch traitCollection.userInterfaceStyle {
case .dark:
setBackgroundImage(.placeholder(color: Asset.Scene.Profile.RelationshipButton.backgroundDark.color), for: .normal)
setBackgroundImage(.placeholder(color: Asset.Scene.Profile.RelationshipButton.backgroundHighlightedDark.color), for: .highlighted)
setBackgroundImage(.placeholder(color: Asset.Scene.Profile.RelationshipButton.backgroundHighlightedDark.color), for: .disabled)
default:
setBackgroundImage(.placeholder(color: Asset.Scene.Profile.RelationshipButton.backgroundLight.color), for: .normal)
setBackgroundImage(.placeholder(color: Asset.Scene.Profile.RelationshipButton.backgroundHighlightedLight.color), for: .highlighted)
setBackgroundImage(.placeholder(color: Asset.Scene.Profile.RelationshipButton.backgroundHighlightedLight.color), for: .disabled)
}
2022-02-15 11:15:58 +01:00
}
}