feat: scroll to top when tap title view in home scene

This commit is contained in:
CMK 2021-06-21 18:58:58 +08:00
parent f826cacccf
commit b4c4153aaa
3 changed files with 25 additions and 14 deletions

View File

@ -526,6 +526,10 @@ extension HomeTimelineViewController: StatusTableViewCellDelegate {
// MARK: - HomeTimelineNavigationBarTitleViewDelegate
extension HomeTimelineViewController: HomeTimelineNavigationBarTitleViewDelegate {
func homeTimelineNavigationBarTitleView(_ titleView: HomeTimelineNavigationBarTitleView, logoButtonDidPressed sender: UIButton) {
scrollToTop(animated: true)
}
func homeTimelineNavigationBarTitleView(_ titleView: HomeTimelineNavigationBarTitleView, buttonDidPressed sender: UIButton) {
switch titleView.state {
case .newPostButton:

View File

@ -9,6 +9,7 @@ import os.log
import UIKit
protocol HomeTimelineNavigationBarTitleViewDelegate: AnyObject {
func homeTimelineNavigationBarTitleView(_ titleView: HomeTimelineNavigationBarTitleView, logoButtonDidPressed sender: UIButton)
func homeTimelineNavigationBarTitleView(_ titleView: HomeTimelineNavigationBarTitleView, buttonDidPressed sender: UIButton)
}
@ -16,7 +17,7 @@ final class HomeTimelineNavigationBarTitleView: UIView {
let containerView = UIStackView()
let imageView = UIImageView()
let logoButton = HighlightDimmableButton()
let button = RoundedEdgesButton()
let label = UILabel()
@ -25,7 +26,7 @@ final class HomeTimelineNavigationBarTitleView: UIView {
weak var delegate: HomeTimelineNavigationBarTitleViewDelegate?
// output
private(set) var state: HomeTimelineNavigationBarTitleViewModel.State = .logoImage
private(set) var state: HomeTimelineNavigationBarTitleViewModel.State = .logo
override init(frame: CGRect) {
super.init(frame: frame)
@ -50,7 +51,7 @@ extension HomeTimelineNavigationBarTitleView {
containerView.bottomAnchor.constraint(equalTo: bottomAnchor),
])
containerView.addArrangedSubview(imageView)
containerView.addArrangedSubview(logoButton)
button.translatesAutoresizingMaskIntoConstraints = false
containerView.addArrangedSubview(button)
NSLayoutConstraint.activate([
@ -58,12 +59,18 @@ extension HomeTimelineNavigationBarTitleView {
])
containerView.addArrangedSubview(label)
configure(state: .logoImage)
configure(state: .logo)
logoButton.addTarget(self, action: #selector(HomeTimelineNavigationBarTitleView.logoButtonDidPressed(_:)), for: .touchUpInside)
button.addTarget(self, action: #selector(HomeTimelineNavigationBarTitleView.buttonDidPressed(_:)), for: .touchUpInside)
}
}
extension HomeTimelineNavigationBarTitleView {
@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)
}
@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)
@ -73,7 +80,7 @@ extension HomeTimelineNavigationBarTitleView {
extension HomeTimelineNavigationBarTitleView {
func resetContainer() {
imageView.isHidden = true
logoButton.isHidden = true
button.isHidden = true
label.isHidden = true
}
@ -90,11 +97,11 @@ extension HomeTimelineNavigationBarTitleView {
resetContainer()
switch state {
case .logoImage:
imageView.tintColor = Asset.Colors.Label.primary.color
imageView.image = Asset.Asset.mastodonTextLogo.image.withRenderingMode(.alwaysTemplate)
imageView.contentMode = .center
imageView.isHidden = false
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
case .newPostButton:
configureButton(
title: L10n.Scene.HomeTimeline.NavigationBarState.newPosts,
@ -173,7 +180,7 @@ struct HomeTimelineNavigationBarTitleView_Previews: PreviewProvider {
Group {
UIViewPreview(width: 375) {
let titleView = HomeTimelineNavigationBarTitleView()
titleView.configure(state: .logoImage)
titleView.configure(state: .logo)
return titleView
}
.previewLayout(.fixed(width: 375, height: 44))

View File

@ -22,7 +22,7 @@ final class HomeTimelineNavigationBarTitleViewModel {
var networkErrorPublisher = PassthroughSubject<Void, Never>()
// output
let state = CurrentValueSubject<State, Never>(.logoImage)
let state = CurrentValueSubject<State, Never>(.logo)
let hasNewPosts = CurrentValueSubject<Bool, Never>(false)
let isOffline = CurrentValueSubject<Bool, Never>(false)
let isPublishingPost = CurrentValueSubject<Bool, Never>(false)
@ -75,7 +75,7 @@ final class HomeTimelineNavigationBarTitleViewModel {
guard !isPublishingPost else { return .publishingPostLabel }
guard !isOffline else { return .offlineButton }
guard !hasNewPosts else { return .newPostButton }
return .logoImage
return .logo
}
.receive(on: DispatchQueue.main)
.assign(to: \.value, on: state)
@ -100,7 +100,7 @@ final class HomeTimelineNavigationBarTitleViewModel {
extension HomeTimelineNavigationBarTitleViewModel {
// state order by priority from low to high
enum State: String {
case logoImage
case logo
case newPostButton
case offlineButton
case publishingPostLabel