mastodon-ios/Mastodon/Scene/Share/View/ToolBar/ActionToolBarContainer.swift

221 lines
9.3 KiB
Swift
Raw Normal View History

//
// ActionToolBarContainer.swift
// Mastodon
//
// Created by sxiaojian on 2021/2/1.
//
import os.log
import UIKit
protocol ActionToolbarContainerDelegate: class {
func actionToolbarContainer(_ actionToolbarContainer: ActionToolbarContainer, replayButtonDidPressed sender: UIButton)
2021-03-15 11:19:45 +01:00
func actionToolbarContainer(_ actionToolbarContainer: ActionToolbarContainer, reblogButtonDidPressed sender: UIButton)
func actionToolbarContainer(_ actionToolbarContainer: ActionToolbarContainer, starButtonDidPressed sender: UIButton)
func actionToolbarContainer(_ actionToolbarContainer: ActionToolbarContainer, moreButtonDidPressed sender: UIButton)
}
final class ActionToolbarContainer: UIView {
let replyButton = HitTestExpandedButton()
2021-03-15 11:19:45 +01:00
let reblogButton = HitTestExpandedButton()
2021-03-09 08:18:43 +01:00
let favoriteButton = HitTestExpandedButton()
let moreButton = HitTestExpandedButton()
2021-03-15 11:19:45 +01:00
var isReblogButtonHighlight: Bool = false {
didSet { isReblogButtonHighlightStateDidChange(to: isReblogButtonHighlight) }
2021-03-09 08:18:43 +01:00
}
var isFavoriteButtonHighlight: Bool = false {
didSet { isFavoriteButtonHighlightStateDidChange(to: isFavoriteButtonHighlight) }
}
weak var delegate: ActionToolbarContainerDelegate?
private let container = UIStackView()
private var style: Style?
override init(frame: CGRect) {
super.init(frame: frame)
_init()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
_init()
}
}
extension ActionToolbarContainer {
private func _init() {
container.translatesAutoresizingMaskIntoConstraints = false
addSubview(container)
NSLayoutConstraint.activate([
container.topAnchor.constraint(equalTo: topAnchor),
container.leadingAnchor.constraint(equalTo: leadingAnchor),
trailingAnchor.constraint(equalTo: container.trailingAnchor),
bottomAnchor.constraint(equalTo: container.bottomAnchor),
])
replyButton.addTarget(self, action: #selector(ActionToolbarContainer.replyButtonDidPressed(_:)), for: .touchUpInside)
2021-03-15 11:19:45 +01:00
reblogButton.addTarget(self, action: #selector(ActionToolbarContainer.reblogButtonDidPressed(_:)), for: .touchUpInside)
2021-03-09 08:18:43 +01:00
favoriteButton.addTarget(self, action: #selector(ActionToolbarContainer.favoriteButtonDidPressed(_:)), for: .touchUpInside)
moreButton.addTarget(self, action: #selector(ActionToolbarContainer.moreButtonDidPressed(_:)), for: .touchUpInside)
}
}
extension ActionToolbarContainer {
enum Style {
case inline
case plain
var buttonTitleImagePadding: CGFloat {
switch self {
case .inline: return 4.0
case .plain: return 0
}
}
}
func configure(for style: Style) {
guard needsConfigure(for: style) else {
return
}
self.style = style
container.arrangedSubviews.forEach { subview in
container.removeArrangedSubview(subview)
subview.removeFromSuperview()
}
2021-03-15 11:19:45 +01:00
let buttons = [replyButton, reblogButton, favoriteButton, moreButton]
buttons.forEach { button in
2021-02-23 09:45:00 +01:00
button.tintColor = Asset.Colors.Button.actionToolbar.color
button.titleLabel?.font = .monospacedDigitSystemFont(ofSize: 12, weight: .regular)
button.setTitle("", for: .normal)
button.setTitleColor(.secondaryLabel, for: .normal)
button.setInsets(forContentPadding: .zero, imageTitlePadding: style.buttonTitleImagePadding)
}
2021-02-23 08:16:55 +01:00
let replyImage = UIImage(systemName: "arrowshape.turn.up.left.fill", withConfiguration: UIImage.SymbolConfiguration(pointSize: 17, weight: .ultraLight))!.withRenderingMode(.alwaysTemplate)
let reblogImage = UIImage(systemName: "arrow.2.squarepath", withConfiguration: UIImage.SymbolConfiguration(pointSize: 17, weight: .bold))!.withRenderingMode(.alwaysTemplate)
let starImage = UIImage(systemName: "star.fill", withConfiguration: UIImage.SymbolConfiguration(pointSize: 17, weight: .bold))!.withRenderingMode(.alwaysTemplate)
let moreImage = UIImage(systemName: "ellipsis", withConfiguration: UIImage.SymbolConfiguration(pointSize: 17, weight: .bold))!.withRenderingMode(.alwaysTemplate)
switch style {
case .inline:
buttons.forEach { button in
button.contentHorizontalAlignment = .leading
}
2021-02-23 08:16:55 +01:00
replyButton.setImage(replyImage, for: .normal)
2021-03-15 11:19:45 +01:00
reblogButton.setImage(reblogImage, for: .normal)
2021-03-09 08:18:43 +01:00
favoriteButton.setImage(starImage, for: .normal)
2021-02-23 08:16:55 +01:00
moreButton.setImage(moreImage, for: .normal)
container.axis = .horizontal
container.distribution = .fill
replyButton.translatesAutoresizingMaskIntoConstraints = false
2021-03-15 11:19:45 +01:00
reblogButton.translatesAutoresizingMaskIntoConstraints = false
2021-03-09 08:18:43 +01:00
favoriteButton.translatesAutoresizingMaskIntoConstraints = false
moreButton.translatesAutoresizingMaskIntoConstraints = false
container.addArrangedSubview(replyButton)
2021-03-15 11:19:45 +01:00
container.addArrangedSubview(reblogButton)
2021-03-09 08:18:43 +01:00
container.addArrangedSubview(favoriteButton)
container.addArrangedSubview(moreButton)
NSLayoutConstraint.activate([
2021-02-23 08:16:55 +01:00
replyButton.heightAnchor.constraint(equalToConstant: 44).priority(.defaultHigh),
2021-03-15 11:19:45 +01:00
replyButton.heightAnchor.constraint(equalTo: reblogButton.heightAnchor).priority(.defaultHigh),
2021-03-09 08:18:43 +01:00
replyButton.heightAnchor.constraint(equalTo: favoriteButton.heightAnchor).priority(.defaultHigh),
replyButton.heightAnchor.constraint(equalTo: moreButton.heightAnchor).priority(.defaultHigh),
2021-03-15 11:19:45 +01:00
replyButton.widthAnchor.constraint(equalTo: reblogButton.widthAnchor).priority(.defaultHigh),
2021-03-09 08:18:43 +01:00
replyButton.widthAnchor.constraint(equalTo: favoriteButton.widthAnchor).priority(.defaultHigh),
])
moreButton.setContentHuggingPriority(.defaultHigh, for: .horizontal)
moreButton.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
case .plain:
buttons.forEach { button in
button.contentHorizontalAlignment = .center
}
2021-02-23 08:16:55 +01:00
replyButton.setImage(replyImage, for: .normal)
2021-03-15 11:19:45 +01:00
reblogButton.setImage(reblogImage, for: .normal)
2021-03-09 08:18:43 +01:00
favoriteButton.setImage(starImage, for: .normal)
container.axis = .horizontal
container.spacing = 8
container.distribution = .fillEqually
container.addArrangedSubview(replyButton)
2021-03-15 11:19:45 +01:00
container.addArrangedSubview(reblogButton)
2021-03-09 08:18:43 +01:00
container.addArrangedSubview(favoriteButton)
}
}
private func needsConfigure(for style: Style) -> Bool {
guard let oldStyle = self.style else { return true }
return oldStyle != style
}
2021-03-15 11:19:45 +01:00
private func isReblogButtonHighlightStateDidChange(to isHighlight: Bool) {
2021-04-06 10:42:45 +02:00
let tintColor = isHighlight ? Asset.Colors.successGreen.color : Asset.Colors.Button.actionToolbar.color
2021-03-15 11:19:45 +01:00
reblogButton.tintColor = tintColor
reblogButton.setTitleColor(tintColor, for: .normal)
reblogButton.setTitleColor(tintColor, for: .highlighted)
2021-03-09 08:18:43 +01:00
}
private func isFavoriteButtonHighlightStateDidChange(to isHighlight: Bool) {
2021-02-23 09:45:00 +01:00
let tintColor = isHighlight ? Asset.Colors.systemOrange.color : Asset.Colors.Button.actionToolbar.color
2021-03-09 08:18:43 +01:00
favoriteButton.tintColor = tintColor
favoriteButton.setTitleColor(tintColor, for: .normal)
favoriteButton.setTitleColor(tintColor, for: .highlighted)
}
}
extension ActionToolbarContainer {
@objc private func replyButtonDidPressed(_ sender: UIButton) {
os_log("%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
delegate?.actionToolbarContainer(self, replayButtonDidPressed: sender)
}
2021-03-15 11:19:45 +01:00
@objc private func reblogButtonDidPressed(_ sender: UIButton) {
os_log("%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
2021-03-15 11:19:45 +01:00
delegate?.actionToolbarContainer(self, reblogButtonDidPressed: sender)
}
2021-03-09 08:18:43 +01:00
@objc private func favoriteButtonDidPressed(_ sender: UIButton) {
os_log("%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
delegate?.actionToolbarContainer(self, starButtonDidPressed: sender)
}
@objc private func moreButtonDidPressed(_ sender: UIButton) {
os_log("%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
delegate?.actionToolbarContainer(self, moreButtonDidPressed: sender)
}
}
2021-02-23 08:16:55 +01:00
#if DEBUG
import SwiftUI
struct ActionToolbarContainer_Previews: PreviewProvider {
static var previews: some View {
Group {
UIViewPreview(width: 300) {
let toolbar = ActionToolbarContainer()
toolbar.configure(for: .inline)
return toolbar
}
.previewLayout(.fixed(width: 300, height: 44))
.previewDisplayName("Inline")
}
}
}
#endif