Skip to content

Commit

Permalink
Merge pull request #18848 from wordpress-mobile/feature/follow-conver…
Browse files Browse the repository at this point in the history
…sation-button-callback

Feature Highlight Tooltip follow button callback
  • Loading branch information
alpavanoglu authored Jun 10, 2022
2 parents aad4d2a + 9ed2473 commit 2c69a95
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ struct FeatureHighlightStore {
static let followConversationTooltipCounterKey = "follow-conversation-tooltip-counter"
}

@UserDefault(Keys.didUserDismissTooltipKey, defaultValue: true)
@UserDefault(Keys.didUserDismissTooltipKey, defaultValue: false)
static var didDismissTooltip: Bool

@UserDefault(Keys.followConversationTooltipCounterKey, defaultValue: 0)
static var followConversationTooltipCounter: Int

/// Tooltip will only be shown 3 times if the user never interacts with it.
static var shouldShowTooltip: Bool {
followConversationTooltipCounter < 3 || !didDismissTooltip
followConversationTooltipCounter < 3 && !didDismissTooltip
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,28 @@ final class TooltipPresenter {
animateTooltipIn()
}

func dismissTooltip() {
UIView.animate(
withDuration: Constants.tooltipAnimationDuration,
delay: 0,
options: .curveEaseOut
) {
guard let tooltipTopConstraint = self.tooltipTopConstraint else {
return
}

self.tooltip.alpha = 0
tooltipTopConstraint.constant += Constants.tooltipTopConstraintAnimationOffset
self.containerView.layoutIfNeeded()
} completion: { isSuccess in
self.anchor = nil
self.primaryTooltipAction?()
self.tooltip.removeFromSuperview()
self.spotlightView?.removeFromSuperview()
NotificationCenter.default.removeObserver(self)
}
}

private func animateTooltipIn() {
UIView.animate(
withDuration: Constants.tooltipAnimationDuration,
Expand All @@ -157,27 +179,7 @@ final class TooltipPresenter {
}

private func configureDismissal() {
tooltip.dismissalAction = {
UIView.animate(
withDuration: Constants.tooltipAnimationDuration,
delay: 0,
options: .curveEaseOut
) {
guard let tooltipTopConstraint = self.tooltipTopConstraint else {
return
}

self.tooltip.alpha = 0
tooltipTopConstraint.constant += Constants.tooltipTopConstraintAnimationOffset
self.containerView.layoutIfNeeded()
} completion: { isSuccess in
self.anchor = nil
self.primaryTooltipAction?()
self.tooltip.removeFromSuperview()
self.spotlightView?.removeFromSuperview()
NotificationCenter.default.removeObserver(self)
}
}
tooltip.dismissalAction = dismissTooltip
}

private func setUpTooltipConstraints() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ class ReaderDetailViewController: UIViewController, ReaderDetailView {
// of this call is accurate, the calculation returns wrong result on that case.
// This manually delays the configuration and hacks the issue.
// We can remove this once the culprit is out.
DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
if self.shouldConfigureTooltipPresenter() {
self.configureTooltipPresenter { [weak self] in
self?.scrollToTooltip()
Expand Down Expand Up @@ -476,6 +476,14 @@ class ReaderDetailViewController: UIViewController, ReaderDetailView {
// Set the delegate here so the table isn't shown until fetching is complete.
commentsTableView.delegate = commentsTableViewDelegate
commentsTableView.dataSource = commentsTableViewDelegate
commentsTableViewDelegate.followButtonTappedClosure = { [weak self] in
guard let tooltipPresenter = self?.tooltipPresenter else {
return
}

FeatureHighlightStore.didDismissTooltip = true
tooltipPresenter.dismissTooltip()
}

commentsTableViewDelegate.updateWith(post: post,
comments: approvedComments,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class ReaderDetailCommentsHeader: UITableViewHeaderFooterView, NibReusable {
@IBOutlet private weak var followButton: UIButton!
private var post: ReaderPost?
private var readerCommentsFollowPresenter: ReaderCommentsFollowPresenter?
private var followButtonTappedClosure: (() ->Void)?

private var totalComments = 0 {
didSet {
Expand All @@ -34,10 +35,16 @@ class ReaderDetailCommentsHeader: UITableViewHeaderFooterView, NibReusable {

// MARK: - Configure

func configure(post: ReaderPost, totalComments: Int, presentingViewController: UIViewController) {
func configure(
post: ReaderPost,
totalComments: Int,
presentingViewController: UIViewController,
followButtonTappedClosure: (() -> Void)?
) {
self.post = post
self.totalComments = totalComments
self.followConversationEnabled = post.commentsOpen && post.canSubscribeComments
self.followButtonTappedClosure = followButtonTappedClosure

configureButton()

Expand Down Expand Up @@ -121,6 +128,9 @@ private extension ReaderDetailCommentsHeader {
@objc func followButtonTapped() {
isSubscribedComments ? readerCommentsFollowPresenter?.showNotificationSheet(sourceView: followButton) :
readerCommentsFollowPresenter?.handleFollowConversationButtonTapped()
if !isSubscribedComments {
followButtonTappedClosure?()
}
}

struct Titles {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class ReaderDetailCommentsTableViewDelegate: NSObject, UITableViewDataSource, UI
private var presentingViewController: UIViewController?
private weak var buttonDelegate: BorderedButtonTableViewCellDelegate?
private(set) var headerView: ReaderDetailCommentsHeader?
var followButtonTappedClosure: (() ->Void)?

private var totalRows = 0
private var hideButton = true
Expand Down Expand Up @@ -106,7 +107,8 @@ class ReaderDetailCommentsTableViewDelegate: NSObject, UITableViewDataSource, UI
header.configure(
post: post,
totalComments: totalComments,
presentingViewController: presentingViewController
presentingViewController: presentingViewController,
followButtonTappedClosure: followButtonTappedClosure
)
headerView = header
return header
Expand Down

0 comments on commit 2c69a95

Please sign in to comment.