From f109253bbabd0a7d3d066f29f1172b61b8d48b73 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 15 Aug 2021 18:19:25 -0400 Subject: [PATCH] Show toast when there are no new posts --- .../TimelineTableViewController.swift | 12 ++++ Tusker/Views/Toast/ToastConfiguration.swift | 2 + Tusker/Views/Toast/ToastView.swift | 70 ++++++++++++++----- .../Views/Toast/ToastableViewController.swift | 14 ++++ 4 files changed, 79 insertions(+), 19 deletions(-) diff --git a/Tusker/Screens/Timeline/TimelineTableViewController.swift b/Tusker/Screens/Timeline/TimelineTableViewController.swift index 06806332..254bd845 100644 --- a/Tusker/Screens/Timeline/TimelineTableViewController.swift +++ b/Tusker/Screens/Timeline/TimelineTableViewController.swift @@ -236,6 +236,18 @@ class TimelineTableViewController: DiffableTimelineLikeTableViewController, with event: UIEvent?) { super.touchesBegan(touches, with: event) recognizedGesture = false + shouldDismissAutomatically = false + shrinkAnimator = UIViewPropertyAnimator(duration: 0.1, curve: .easeInOut) { self.transform = CGAffineTransform(scaleX: 0.95, y: 0.95) } @@ -145,6 +177,7 @@ class ToastView: UIView { switch recognizer.state { case .began: recognizedGesture = true + shouldDismissAutomatically = false UIView.animate(withDuration: 0.1, delay: 0, options: [.curveEaseInOut, .allowUserInteraction]) { self.transform = .identity } @@ -199,24 +232,23 @@ class ToastView: UIView { } } - func dismissToast(animated: Bool) { - guard animated else { - removeFromSuperview() - return - } - UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseInOut) { - self.transform = CGAffineTransform(translationX: 0, y: self.offscreenTranslation) - } completion: { (_) in - self.removeFromSuperview() - } - } - - func animateAppearance() { - self.transform = CGAffineTransform(translationX: 0, y: offscreenTranslation) - let duration = 0.5 - let velocity = 0.5 - UIView.animate(withDuration: duration, delay: 0, usingSpringWithDamping: 0.65, initialSpringVelocity: velocity, options: []) { - self.transform = .identity + @objc private func scrollViewPanGestureRecognized(_ recognizer: UIPanGestureRecognizer) { + switch recognizer.state { + case .began: + shouldDismissOnScroll = true + + case .changed: + let translation = recognizer.translation(in: recognizer.view).y + if shouldDismissOnScroll && abs(translation) > 50 { + dismissToast(animated: true) + shouldDismissOnScroll = false + } + + case .ended, .cancelled: + shouldDismissOnScroll = false + + default: + break } } diff --git a/Tusker/Views/Toast/ToastableViewController.swift b/Tusker/Views/Toast/ToastableViewController.swift index d5a71759..e7d900f5 100644 --- a/Tusker/Views/Toast/ToastableViewController.swift +++ b/Tusker/Views/Toast/ToastableViewController.swift @@ -11,6 +11,7 @@ import UIKit protocol ToastableViewController: UIViewController { var toastParentView: UIView { get } + var toastScrollView: UIScrollView? { get } } @@ -33,6 +34,7 @@ extension ToastableViewController { } var toastParentView: UIView { view } + var toastScrollView: UIScrollView? { view as? UIScrollView } func showToast(configuration config: ToastConfiguration, animated: Bool) { currentToast?.dismissToast(animated: false) @@ -67,6 +69,18 @@ extension ToastableViewController { if animated { toast.animateAppearance() } + + if config.dismissOnScroll, + let scrollView = toastScrollView { + toast.setupDismissOnScroll(connectedTo: scrollView) + } + + if let time = config.dismissAutomaticallyAfter { + DispatchQueue.main.asyncAfter(deadline: .now() + time) { [weak toast] in + guard let toast = toast, toast.shouldDismissAutomatically else { return } + toast.dismissToast(animated: true) + } + } } private func effectiveEdge(edge: ToastConfiguration.Edge) -> ToastConfiguration.Edge {