From abe171ce805ef6e5f34b98a55424ff98f719ae74 Mon Sep 17 00:00:00 2001 From: David Sinclair Date: Fri, 15 Jan 2021 21:26:53 -0800 Subject: [PATCH] #817 (three column layout) - Scrolling story instead of pages. - Change pages vertically only when at the top or bottom of a story. --- .../ios/Classes/DetailObjCViewController.m | 2 + .../ios/Classes/DetailViewController.swift | 8 +++ .../Classes/StoryDetailObjCViewController.m | 2 + .../Classes/VerticalPageViewController.swift | 56 +++++++++++++++++++ 4 files changed, 68 insertions(+) diff --git a/clients/ios/Classes/DetailObjCViewController.m b/clients/ios/Classes/DetailObjCViewController.m index 90d98203b..9084ca1ef 100644 --- a/clients/ios/Classes/DetailObjCViewController.m +++ b/clients/ios/Classes/DetailObjCViewController.m @@ -1278,6 +1278,8 @@ [appDelegate.feedDetailViewController redrawUnreadStory]; // } + [appDelegate.detailViewController allowPagingUp:YES down:NO]; + if (!appDelegate.storiesCollection.inSearch) { [self.currentStoryController becomeFirstResponder]; } diff --git a/clients/ios/Classes/DetailViewController.swift b/clients/ios/Classes/DetailViewController.swift index a26fa175d..4e5433bbf 100644 --- a/clients/ios/Classes/DetailViewController.swift +++ b/clients/ios/Classes/DetailViewController.swift @@ -128,6 +128,14 @@ class DetailViewController: DetailObjCViewController { /// The horizontal page view controller. var horizontalPageViewController: HorizontalPageViewController? + /// Enable paging upwards and/or downwards. + /// + /// - Parameter up: Allow paging up to the previous story. + /// - Parameter down: Allow paging down to the next story. + @objc(allowPagingUp:down:) func allowPaging(up: Bool, down: Bool) { + horizontalPageViewController?.currentController?.allowPaging(up: up, down: down) + } + /// Returns the currently displayed story view controller, or `nil` if none. @objc var currentStoryController: StoryDetailViewController? { return horizontalPageViewController?.currentController?.currentController diff --git a/clients/ios/Classes/StoryDetailObjCViewController.m b/clients/ios/Classes/StoryDetailObjCViewController.m index b248e075c..4cb3cbca4 100644 --- a/clients/ios/Classes/StoryDetailObjCViewController.m +++ b/clients/ios/Classes/StoryDetailObjCViewController.m @@ -1456,6 +1456,8 @@ } [self storeScrollPosition:YES]; + + [appDelegate.detailViewController allowPagingUp:atTop down:atBottom]; } } diff --git a/clients/ios/Classes/VerticalPageViewController.swift b/clients/ios/Classes/VerticalPageViewController.swift index b41ce240a..453d59f4d 100644 --- a/clients/ios/Classes/VerticalPageViewController.swift +++ b/clients/ios/Classes/VerticalPageViewController.swift @@ -45,6 +45,33 @@ class VerticalPageViewController: UIPageViewController { /// The next story view controller, if it has been requested, otherwise `nil`. var nextController: StoryDetailViewController? + /// The internal scroll view that drives the page controller. + var scrollView: UIScrollView? { + return view.subviews.filter { $0 is UIScrollView }.first as? UIScrollView + } + + private var isPageUpEnabled = true + private var isPageDownEnabled = true + + override func viewDidLoad() { + super.viewDidLoad() + + self.scrollView?.delegate = self + } + + /// Updates whether paging up and/or down is enabled. + func allowPaging(up: Bool, down: Bool) { + isPageUpEnabled = up + isPageDownEnabled = down + + if !isPageUpEnabled { + print("Page Up Disabled") + } + if !isPageDownEnabled { + print("Page Down Disabled") + } + } + /// Clear the previous and next story view controllers. func reset() { previousController = nil @@ -67,3 +94,32 @@ class VerticalPageViewController: UIPageViewController { super.setViewControllers(viewControllers, direction: direction, animated: animated, completion: completion) } } + +extension VerticalPageViewController: UIScrollViewDelegate { + func scrollViewDidScroll(_ scrollView: UIScrollView) { + print("contentOffset = \(scrollView.contentOffset.y)") + + if !self.isPageUpEnabled { + disableUpScroll(scrollView) + } + if !self.isPageDownEnabled { + disableDownScroll(scrollView) + } + } + + private func disableUpScroll(_ scrollView: UIScrollView) { + let viewHeight = view.bounds.height + + if scrollView.contentOffset.y < viewHeight { + scrollView.setContentOffset(CGPoint(x: 0, y: viewHeight), animated: false) + } + } + + private func disableDownScroll(_ scrollView: UIScrollView) { + let viewHeight = view.bounds.height + + if scrollView.contentOffset.y > viewHeight { + scrollView.setContentOffset(CGPoint(x: 0, y: viewHeight), animated: false) + } + } +}