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 // MARK: - HomeTimelineNavigationBarTitleViewDelegate
extension HomeTimelineViewController: HomeTimelineNavigationBarTitleViewDelegate { extension HomeTimelineViewController: HomeTimelineNavigationBarTitleViewDelegate {
func homeTimelineNavigationBarTitleView(_ titleView: HomeTimelineNavigationBarTitleView, logoButtonDidPressed sender: UIButton) {
scrollToTop(animated: true)
}
func homeTimelineNavigationBarTitleView(_ titleView: HomeTimelineNavigationBarTitleView, buttonDidPressed sender: UIButton) { func homeTimelineNavigationBarTitleView(_ titleView: HomeTimelineNavigationBarTitleView, buttonDidPressed sender: UIButton) {
switch titleView.state { switch titleView.state {
case .newPostButton: case .newPostButton:

View File

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

View File

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