diff --git a/SignalMessaging/ViewControllers/AttachmentApproval/AttachmentApprovalInputAccessoryView.swift b/SignalMessaging/ViewControllers/AttachmentApproval/AttachmentApprovalInputAccessoryView.swift index b983274e2..eadc0afda 100644 --- a/SignalMessaging/ViewControllers/AttachmentApproval/AttachmentApprovalInputAccessoryView.swift +++ b/SignalMessaging/ViewControllers/AttachmentApproval/AttachmentApprovalInputAccessoryView.swift @@ -101,11 +101,7 @@ class AttachmentApprovalInputAccessoryView: UIView { // MARK: - public var shouldHideControls = false { - didSet { - updateContents() - } - } + private var shouldHideControls = false private func updateContents() { var hasCurrentCaption = false @@ -124,6 +120,12 @@ class AttachmentApprovalInputAccessoryView: UIView { currentCaptionWrapper.isHidden = isEditingCaptions || !hasCurrentCaption attachmentTextToolbar.isHidden = isEditingCaptions + updateFirstResponder() + + layoutSubviews() + } + + private func updateFirstResponder() { if (shouldHideControls) { if attachmentCaptionToolbar.textView.isFirstResponder { attachmentCaptionToolbar.textView.resignFirstResponder() @@ -135,17 +137,30 @@ class AttachmentApprovalInputAccessoryView: UIView { if !attachmentCaptionToolbar.textView.isFirstResponder { attachmentCaptionToolbar.textView.becomeFirstResponder() } + } else { + if attachmentCaptionToolbar.textView.isFirstResponder { + attachmentCaptionToolbar.textView.resignFirstResponder() + } } // NOTE: We don't automatically make attachmentTextToolbar.textView // first responder; - - layoutSubviews() } public func update(isEditingCaptions: Bool, - currentAttachmentItem: SignalAttachmentItem?) { + currentAttachmentItem: SignalAttachmentItem?, + shouldHideControls: Bool) { + // De-bounce + guard self.isEditingCaptions != isEditingCaptions || + self.currentAttachmentItem != currentAttachmentItem || + self.shouldHideControls != shouldHideControls else { + + updateFirstResponder() + return + } + self.isEditingCaptions = isEditingCaptions self.currentAttachmentItem = currentAttachmentItem + self.shouldHideControls = shouldHideControls updateContents() } @@ -159,6 +174,12 @@ class AttachmentApprovalInputAccessoryView: UIView { return CGSize.zero } } + + public var hasFirstResponder: Bool { + return (isFirstResponder || + attachmentCaptionToolbar.textView.isFirstResponder || + attachmentTextToolbar.textView.isFirstResponder) + } } // MARK: - diff --git a/SignalMessaging/ViewControllers/AttachmentApproval/AttachmentApprovalViewController.swift b/SignalMessaging/ViewControllers/AttachmentApproval/AttachmentApprovalViewController.swift index cba9ceffa..dc837b294 100644 --- a/SignalMessaging/ViewControllers/AttachmentApproval/AttachmentApprovalViewController.swift +++ b/SignalMessaging/ViewControllers/AttachmentApproval/AttachmentApprovalViewController.swift @@ -184,7 +184,6 @@ public class AttachmentApprovalViewController: UIPageViewController, UIPageViewC private func updateContents() { updateNavigationBar() updateInputAccessory() - updateControlVisibility() touchInterceptorView.isHidden = !isEditingCaptions } @@ -206,7 +205,16 @@ public class AttachmentApprovalViewController: UIPageViewController, UIPageViewC currentPageViewController = pageViewControllers.first } let currentAttachmentItem: SignalAttachmentItem? = currentPageViewController?.attachmentItem - bottomToolView.update(isEditingCaptions: isEditingCaptions, currentAttachmentItem: currentAttachmentItem) + + let hasPresentedView = self.presentedViewController != nil + let isToolbarFirstResponder = bottomToolView.hasFirstResponder + if !shouldHideControls, !isFirstResponder, !hasPresentedView, !isToolbarFirstResponder { + becomeFirstResponder() + } + + bottomToolView.update(isEditingCaptions: isEditingCaptions, + currentAttachmentItem: currentAttachmentItem, + shouldHideControls: shouldHideControls) } // MARK: - Navigation Bar @@ -327,15 +335,6 @@ public class AttachmentApprovalViewController: UIPageViewController, UIPageViewC return pageViewController.shouldHideControls } - private func updateControlVisibility() { - let hasPresentedView = self.presentedViewController != nil - - if !shouldHideControls, !isFirstResponder, !hasPresentedView { - becomeFirstResponder() - } - bottomToolView.shouldHideControls = shouldHideControls - } - // MARK: - View Helpers func remove(attachmentItem: SignalAttachmentItem) { @@ -411,8 +410,6 @@ public class AttachmentApprovalViewController: UIPageViewController, UIPageViewC updateMediaRail() } } - - updateContents() } // MARK: - UIPageViewControllerDataSource @@ -463,6 +460,18 @@ public class AttachmentApprovalViewController: UIPageViewController, UIPageViewC return super.viewControllers!.map { $0 as! AttachmentPrepViewController } } + @objc + public override func setViewControllers(_ viewControllers: [UIViewController]?, direction: UIPageViewController.NavigationDirection, animated: Bool, completion: ((Bool) -> Void)? = nil) { + super.setViewControllers(viewControllers, + direction: direction, + animated: animated) { [weak self] (finished) in + if let completion = completion { + completion(finished) + } + self?.updateContents() + } + } + var currentItem: SignalAttachmentItem! { get { return currentPageViewController.attachmentItem @@ -682,7 +691,7 @@ extension AttachmentApprovalViewController: AttachmentPrepViewControllerDelegate } func prepViewControllerUpdateControls() { - updateControlVisibility() + updateInputAccessory() } }