mastodon-ios/Mastodon/Scene/Share/View/Button/PrimaryActionButton.swift

110 lines
3.5 KiB
Swift
Raw Normal View History

//
2021-02-22 16:16:13 +01:00
// PrimaryActionButton.swift
// Mastodon
//
// Created by BradGao on 2021/2/20.
//
import UIKit
import MastodonAsset
import MastodonLocalization
2021-02-22 16:16:13 +01:00
class PrimaryActionButton: UIButton {
2021-02-25 09:38:24 +01:00
2022-01-05 08:11:35 +01:00
private var originalButtonTitle: String?
2021-02-25 09:38:24 +01:00
lazy var activityIndicator: UIActivityIndicatorView = {
let indicator = UIActivityIndicatorView(style: .medium)
indicator.color = .white
2021-02-25 09:38:24 +01:00
indicator.hidesWhenStopped = true
indicator.translatesAutoresizingMaskIntoConstraints = false
return indicator
}()
2022-01-05 08:11:35 +01:00
var adjustsBackgroundImageWhenUserInterfaceStyleChanges = true
2022-01-04 11:30:21 +01:00
var action: Action = .next {
didSet {
setupAppearance(action: action)
}
}
2022-01-05 08:11:35 +01:00
var isLoading: Bool = false
2021-02-25 09:38:24 +01:00
override init(frame: CGRect) {
super.init(frame: frame)
_init()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
_init()
}
}
2022-01-04 11:30:21 +01:00
extension PrimaryActionButton {
public enum Action {
case back
case next
}
}
extension PrimaryActionButton {
private func _init() {
titleLabel?.font = UIFontMetrics(forTextStyle: .headline).scaledFont(for: .systemFont(ofSize: 17, weight: .semibold))
setTitleColor(.white, for: .normal)
2022-01-04 11:30:21 +01:00
setupAppearance(action: action)
2021-06-22 14:52:30 +02:00
applyCornerRadius(radius: 10)
}
2022-01-04 11:30:21 +01:00
func setupAppearance(action: Action) {
switch action {
case .back:
setTitleColor(Asset.Colors.Label.primary.color, for: .normal)
setBackgroundImage(UIImage.placeholder(color: Asset.Scene.Onboarding.navigationBackButtonBackground.color), for: .normal)
setBackgroundImage(UIImage.placeholder(color: Asset.Scene.Onboarding.navigationBackButtonBackgroundHighlighted.color), for: .highlighted)
setBackgroundImage(UIImage.placeholder(color: Asset.Colors.disabled.color), for: .disabled)
case .next:
setTitleColor(Asset.Colors.Label.primaryReverse.color, for: .normal)
setBackgroundImage(UIImage.placeholder(color: Asset.Scene.Onboarding.navigationNextButtonBackground.color), for: .normal)
setBackgroundImage(UIImage.placeholder(color: Asset.Scene.Onboarding.navigationNextButtonBackgroundHighlighted.color), for: .highlighted)
setBackgroundImage(UIImage.placeholder(color: Asset.Colors.disabled.color), for: .disabled)
}
2021-06-22 14:52:30 +02:00
}
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if adjustsBackgroundImageWhenUserInterfaceStyleChanges {
2022-01-04 11:30:21 +01:00
setupAppearance(action: action)
}
}
2021-02-25 09:38:24 +01:00
func showLoading() {
guard !isLoading else { return }
isEnabled = false
isLoading = true
originalButtonTitle = title(for: .disabled)
self.setTitle("", for: .disabled)
addSubview(activityIndicator)
NSLayoutConstraint.activate([
activityIndicator.centerXAnchor.constraint(equalTo: self.centerXAnchor),
activityIndicator.centerYAnchor.constraint(equalTo: self.centerYAnchor),
])
activityIndicator.startAnimating()
}
func stopLoading() {
guard isLoading else { return }
isLoading = false
if activityIndicator.superview == self {
activityIndicator.removeFromSuperview()
}
isEnabled = true
self.setTitle(originalButtonTitle, for: .disabled)
}
2021-04-25 09:44:38 +02:00
}