From b4c4153aaa40de35da9d8160d9cef7c754882357 Mon Sep 17 00:00:00 2001 From: CMK Date: Mon, 21 Jun 2021 18:58:58 +0800 Subject: [PATCH] feat: scroll to top when tap title view in home scene --- .../HomeTimelineViewController.swift | 4 +++ .../HomeTimelineNavigationBarTitleView.swift | 29 ++++++++++++------- ...eTimelineNavigationBarTitleViewModel.swift | 6 ++-- 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/Mastodon/Scene/HomeTimeline/HomeTimelineViewController.swift b/Mastodon/Scene/HomeTimeline/HomeTimelineViewController.swift index 529f2d81..b40a7f3a 100644 --- a/Mastodon/Scene/HomeTimeline/HomeTimelineViewController.swift +++ b/Mastodon/Scene/HomeTimeline/HomeTimelineViewController.swift @@ -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: diff --git a/Mastodon/Scene/HomeTimeline/View/HomeTimelineNavigationBarTitleView.swift b/Mastodon/Scene/HomeTimeline/View/HomeTimelineNavigationBarTitleView.swift index 91020f12..ac39ce1a 100644 --- a/Mastodon/Scene/HomeTimeline/View/HomeTimelineNavigationBarTitleView.swift +++ b/Mastodon/Scene/HomeTimeline/View/HomeTimelineNavigationBarTitleView.swift @@ -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)) diff --git a/Mastodon/Scene/HomeTimeline/View/HomeTimelineNavigationBarTitleViewModel.swift b/Mastodon/Scene/HomeTimeline/View/HomeTimelineNavigationBarTitleViewModel.swift index e1fc3174..71b4dda8 100644 --- a/Mastodon/Scene/HomeTimeline/View/HomeTimelineNavigationBarTitleViewModel.swift +++ b/Mastodon/Scene/HomeTimeline/View/HomeTimelineNavigationBarTitleViewModel.swift @@ -22,7 +22,7 @@ final class HomeTimelineNavigationBarTitleViewModel { var networkErrorPublisher = PassthroughSubject() // output - let state = CurrentValueSubject(.logoImage) + let state = CurrentValueSubject(.logo) let hasNewPosts = CurrentValueSubject(false) let isOffline = CurrentValueSubject(false) let isPublishingPost = CurrentValueSubject(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