2021-03-29 11:44:52 +02:00
|
|
|
//
|
|
|
|
// HomeTimelineNavigationBarTitleView.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/3/15.
|
|
|
|
//
|
|
|
|
|
|
|
|
import os.log
|
|
|
|
import UIKit
|
2021-07-16 10:21:47 +02:00
|
|
|
import MastodonUI
|
2022-01-27 14:23:39 +01:00
|
|
|
import MastodonAsset
|
|
|
|
import MastodonLocalization
|
2021-03-29 11:44:52 +02:00
|
|
|
|
2021-05-08 05:03:34 +02:00
|
|
|
protocol HomeTimelineNavigationBarTitleViewDelegate: AnyObject {
|
2021-06-21 12:58:58 +02:00
|
|
|
func homeTimelineNavigationBarTitleView(_ titleView: HomeTimelineNavigationBarTitleView, logoButtonDidPressed sender: UIButton)
|
2021-03-29 11:44:52 +02:00
|
|
|
func homeTimelineNavigationBarTitleView(_ titleView: HomeTimelineNavigationBarTitleView, buttonDidPressed sender: UIButton)
|
|
|
|
}
|
|
|
|
|
|
|
|
final class HomeTimelineNavigationBarTitleView: UIView {
|
|
|
|
|
|
|
|
let containerView = UIStackView()
|
|
|
|
|
2021-06-21 12:58:58 +02:00
|
|
|
let logoButton = HighlightDimmableButton()
|
2021-03-29 11:44:52 +02:00
|
|
|
let button = RoundedEdgesButton()
|
|
|
|
let label = UILabel()
|
|
|
|
|
|
|
|
// input
|
|
|
|
private var blockingState: HomeTimelineNavigationBarTitleViewModel.State?
|
|
|
|
weak var delegate: HomeTimelineNavigationBarTitleViewDelegate?
|
|
|
|
|
|
|
|
// output
|
2021-06-21 12:58:58 +02:00
|
|
|
private(set) var state: HomeTimelineNavigationBarTitleViewModel.State = .logo
|
2021-03-29 11:44:52 +02:00
|
|
|
|
|
|
|
override init(frame: CGRect) {
|
|
|
|
super.init(frame: frame)
|
|
|
|
_init()
|
|
|
|
}
|
|
|
|
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
super.init(coder: coder)
|
|
|
|
_init()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
extension HomeTimelineNavigationBarTitleView {
|
|
|
|
private func _init() {
|
|
|
|
containerView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
addSubview(containerView)
|
2022-11-17 17:45:27 +01:00
|
|
|
containerView.pinToParent()
|
2021-03-29 11:44:52 +02:00
|
|
|
|
2021-06-21 12:58:58 +02:00
|
|
|
containerView.addArrangedSubview(logoButton)
|
2021-03-29 11:44:52 +02:00
|
|
|
button.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
containerView.addArrangedSubview(button)
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
button.heightAnchor.constraint(equalToConstant: 24).priority(.defaultHigh)
|
|
|
|
])
|
|
|
|
containerView.addArrangedSubview(label)
|
|
|
|
|
2021-06-21 12:58:58 +02:00
|
|
|
configure(state: .logo)
|
|
|
|
logoButton.addTarget(self, action: #selector(HomeTimelineNavigationBarTitleView.logoButtonDidPressed(_:)), for: .touchUpInside)
|
2021-03-29 11:44:52 +02:00
|
|
|
button.addTarget(self, action: #selector(HomeTimelineNavigationBarTitleView.buttonDidPressed(_:)), for: .touchUpInside)
|
2022-03-18 17:30:00 +01:00
|
|
|
|
|
|
|
logoButton.accessibilityIdentifier = "TitleButton"
|
|
|
|
button.accessibilityIdentifier = "TitleButton"
|
2021-03-29 11:44:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension HomeTimelineNavigationBarTitleView {
|
2021-06-21 12:58:58 +02:00
|
|
|
@objc private func logoButtonDidPressed(_ sender: UIButton) {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
delegate?.homeTimelineNavigationBarTitleView(self, logoButtonDidPressed: sender)
|
|
|
|
}
|
|
|
|
|
2021-03-29 11:44:52 +02:00
|
|
|
@objc private func buttonDidPressed(_ sender: UIButton) {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s", ((#file as NSString).lastPathComponent), #line, #function)
|
|
|
|
delegate?.homeTimelineNavigationBarTitleView(self, buttonDidPressed: sender)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension HomeTimelineNavigationBarTitleView {
|
|
|
|
|
|
|
|
func resetContainer() {
|
2021-06-21 12:58:58 +02:00
|
|
|
logoButton.isHidden = true
|
2021-03-29 11:44:52 +02:00
|
|
|
button.isHidden = true
|
|
|
|
label.isHidden = true
|
|
|
|
}
|
|
|
|
|
|
|
|
func configure(state: HomeTimelineNavigationBarTitleViewModel.State) {
|
|
|
|
os_log(.info, log: .debug, "%{public}s[%{public}ld], %{public}s: configure title view: %s", ((#file as NSString).lastPathComponent), #line, #function, state.rawValue)
|
|
|
|
self.state = state
|
|
|
|
|
|
|
|
// check state block or not
|
|
|
|
guard blockingState == nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
resetContainer()
|
|
|
|
|
|
|
|
switch state {
|
2021-06-21 12:58:58 +02:00
|
|
|
case .logo:
|
|
|
|
logoButton.tintColor = Asset.Colors.Label.primary.color
|
|
|
|
logoButton.setImage(Asset.Asset.mastodonTextLogo.image.withRenderingMode(.alwaysTemplate), for: .normal)
|
|
|
|
logoButton.contentMode = .center
|
|
|
|
logoButton.isHidden = false
|
2022-12-18 16:48:25 +01:00
|
|
|
logoButton.accessibilityLabel = "Logo Button" // TODO :i18n
|
2022-04-29 09:55:31 +02:00
|
|
|
logoButton.accessibilityHint = "Tap to scroll to top and tap again to previous location"
|
2021-03-29 11:44:52 +02:00
|
|
|
case .newPostButton:
|
|
|
|
configureButton(
|
|
|
|
title: L10n.Scene.HomeTimeline.NavigationBarState.newPosts,
|
|
|
|
textColor: .white,
|
2022-06-02 11:31:23 +02:00
|
|
|
backgroundColor: Asset.Colors.brand.color
|
2021-03-29 11:44:52 +02:00
|
|
|
)
|
|
|
|
button.isHidden = false
|
2022-04-29 09:55:31 +02:00
|
|
|
button.accessibilityLabel = L10n.Scene.HomeTimeline.NavigationBarState.newPosts
|
2021-03-29 11:44:52 +02:00
|
|
|
case .offlineButton:
|
|
|
|
configureButton(
|
|
|
|
title: L10n.Scene.HomeTimeline.NavigationBarState.offline,
|
|
|
|
textColor: .white,
|
2021-07-20 13:24:24 +02:00
|
|
|
backgroundColor: Asset.Colors.danger.color
|
2021-03-29 11:44:52 +02:00
|
|
|
)
|
|
|
|
button.isHidden = false
|
2022-04-29 09:55:31 +02:00
|
|
|
button.accessibilityLabel = L10n.Scene.HomeTimeline.NavigationBarState.offline
|
2021-03-29 11:44:52 +02:00
|
|
|
case .publishingPostLabel:
|
|
|
|
label.font = .systemFont(ofSize: 17, weight: .semibold)
|
|
|
|
label.textColor = Asset.Colors.Label.primary.color
|
|
|
|
label.text = L10n.Scene.HomeTimeline.NavigationBarState.publishing
|
|
|
|
label.textAlignment = .center
|
|
|
|
label.isHidden = false
|
2022-04-29 09:55:31 +02:00
|
|
|
button.accessibilityLabel = L10n.Scene.HomeTimeline.NavigationBarState.publishing
|
2021-03-29 11:44:52 +02:00
|
|
|
case .publishedButton:
|
|
|
|
blockingState = state
|
|
|
|
configureButton(
|
|
|
|
title: L10n.Scene.HomeTimeline.NavigationBarState.published,
|
|
|
|
textColor: .white,
|
2021-04-06 10:42:45 +02:00
|
|
|
backgroundColor: Asset.Colors.successGreen.color
|
2021-03-29 11:44:52 +02:00
|
|
|
)
|
|
|
|
button.isHidden = false
|
2022-04-29 09:55:31 +02:00
|
|
|
button.accessibilityLabel = L10n.Scene.HomeTimeline.NavigationBarState.published
|
2021-03-29 11:44:52 +02:00
|
|
|
|
|
|
|
let presentDuration: TimeInterval = 0.33
|
|
|
|
let scaleAnimator = UIViewPropertyAnimator(duration: presentDuration, timingParameters: UISpringTimingParameters())
|
|
|
|
button.transform = CGAffineTransform(scaleX: 0.5, y: 0.5)
|
|
|
|
scaleAnimator.addAnimations {
|
|
|
|
self.button.transform = .identity
|
|
|
|
}
|
|
|
|
let alphaAnimator = UIViewPropertyAnimator(duration: presentDuration, curve: .easeInOut)
|
|
|
|
button.alpha = 0.3
|
|
|
|
alphaAnimator.addAnimations {
|
|
|
|
self.button.alpha = 1
|
|
|
|
}
|
|
|
|
scaleAnimator.startAnimation()
|
|
|
|
alphaAnimator.startAnimation()
|
|
|
|
|
|
|
|
let dismissDuration: TimeInterval = 3
|
|
|
|
let dissolveAnimator = UIViewPropertyAnimator(duration: dismissDuration, curve: .easeInOut)
|
|
|
|
dissolveAnimator.addAnimations({
|
|
|
|
self.button.alpha = 0
|
|
|
|
}, delayFactor: 0.9) // at 2.7s
|
|
|
|
dissolveAnimator.addCompletion { _ in
|
|
|
|
self.blockingState = nil
|
|
|
|
self.configure(state: self.state)
|
|
|
|
self.button.alpha = 1
|
|
|
|
}
|
|
|
|
dissolveAnimator.startAnimation()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func configureButton(title: String, textColor: UIColor, backgroundColor: UIColor) {
|
|
|
|
button.setBackgroundImage(.placeholder(color: backgroundColor), for: .normal)
|
|
|
|
button.setBackgroundImage(.placeholder(color: backgroundColor.withAlphaComponent(0.5)), for: .highlighted)
|
|
|
|
button.setTitleColor(textColor, for: .normal)
|
|
|
|
button.setTitleColor(textColor.withAlphaComponent(0.5), for: .highlighted)
|
|
|
|
button.setTitle(title, for: .normal)
|
|
|
|
button.contentEdgeInsets = UIEdgeInsets(top: 1, left: 16, bottom: 1, right: 16)
|
|
|
|
button.titleLabel?.font = .systemFont(ofSize: 15, weight: .bold)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#if canImport(SwiftUI) && DEBUG
|
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct HomeTimelineNavigationBarTitleView_Previews: PreviewProvider {
|
|
|
|
|
|
|
|
static var previews: some View {
|
|
|
|
Group {
|
|
|
|
UIViewPreview(width: 375) {
|
|
|
|
let titleView = HomeTimelineNavigationBarTitleView()
|
2021-06-21 12:58:58 +02:00
|
|
|
titleView.configure(state: .logo)
|
2021-03-29 11:44:52 +02:00
|
|
|
return titleView
|
|
|
|
}
|
|
|
|
.previewLayout(.fixed(width: 375, height: 44))
|
|
|
|
UIViewPreview(width: 150) {
|
|
|
|
let titleView = HomeTimelineNavigationBarTitleView()
|
|
|
|
titleView.configure(state: .newPostButton)
|
|
|
|
return titleView
|
|
|
|
}
|
|
|
|
.previewLayout(.fixed(width: 150, height: 24))
|
|
|
|
UIViewPreview(width: 120) {
|
|
|
|
let titleView = HomeTimelineNavigationBarTitleView()
|
|
|
|
titleView.configure(state: .offlineButton)
|
|
|
|
return titleView
|
|
|
|
}
|
|
|
|
.previewLayout(.fixed(width: 120, height: 24))
|
|
|
|
UIViewPreview(width: 375) {
|
|
|
|
let titleView = HomeTimelineNavigationBarTitleView()
|
|
|
|
titleView.configure(state: .publishingPostLabel)
|
|
|
|
return titleView
|
|
|
|
}
|
|
|
|
.previewLayout(.fixed(width: 375, height: 44))
|
|
|
|
UIViewPreview(width: 120) {
|
|
|
|
let titleView = HomeTimelineNavigationBarTitleView()
|
|
|
|
titleView.configure(state: .publishedButton)
|
|
|
|
return titleView
|
|
|
|
}
|
|
|
|
.previewLayout(.fixed(width: 120, height: 24))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|