Skip to content

Commit

Permalink
support multitask
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuhiro4949 committed Jan 31, 2020
1 parent 34e5c7e commit b983d81
Showing 1 changed file with 15 additions and 23 deletions.
38 changes: 15 additions & 23 deletions PagingKit/PagingContentViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public class PagingContentViewController: UIViewController {
fileprivate var numberOfPages: Int = 0
fileprivate var explicitPaging: ExplicitPaging?

var appearanceHandler: ContentsAppearanceHandlerProtocol?
var appearanceHandler: ContentsAppearanceHandlerProtocol = ContentsAppearanceHandler()

/// The object that acts as the delegate of the content view controller.
public weak var delegate: PagingContentViewControllerDelegate?
Expand Down Expand Up @@ -165,7 +165,7 @@ public class PagingContentViewController: UIViewController {
/// - Parameter page: An index to show after reloading.
public func reloadData(with page: Int? = nil, completion: (() -> Void)? = nil) {
removeAll()
appearanceHandler?.preReload(at: leftSidePageIndex)
appearanceHandler.preReload(at: leftSidePageIndex)
let preferredPage = page ?? leftSidePageIndex
leftSidePageIndex = preferredPage
initialLoad(with: preferredPage)
Expand All @@ -176,7 +176,7 @@ public class PagingContentViewController: UIViewController {
},
completion: { [weak self] _ in
self?.scroll(to: preferredPage, needsCallAppearance: false, animated: false) { _ in
self?.appearanceHandler?.postReload(at: preferredPage)
self?.appearanceHandler.postReload(at: preferredPage)
completion?()
}
}
Expand All @@ -197,7 +197,7 @@ public class PagingContentViewController: UIViewController {
delegate?.contentViewController(viewController: self, willBeginPagingAt: leftSidePageIndex, animated: animated)

if needsCallAppearance {
appearanceHandler?.beginDragging(at: leftSidePageIndex)
appearanceHandler.beginDragging(at: leftSidePageIndex)
}

loadPagesIfNeeded(page: page)
Expand All @@ -208,7 +208,7 @@ public class PagingContentViewController: UIViewController {
guard let _self = self, finished else { return }

if needsCallAppearance {
_self.appearanceHandler?.stopScrolling(at: _self.leftSidePageIndex)
_self.appearanceHandler.stopScrolling(at: _self.leftSidePageIndex)
}

completion?(finished)
Expand Down Expand Up @@ -274,34 +274,29 @@ public class PagingContentViewController: UIViewController {
view.addConstraints([.top, .bottom, .leading, .trailing].anchor(from: scrollView, to: view))
view.backgroundColor = .clear

setupAppearanceHandler()
}

private func setupAppearanceHandler() {
appearanceHandler = ContentsAppearanceHandler()
appearanceHandler?.contentsDequeueHandler = { [weak self] in
appearanceHandler.contentsDequeueHandler = { [weak self] in
self?.cachedViewControllers
}
}

public override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
appearanceHandler?.callApparance(.viewWillAppear, animated: animated, at: leftSidePageIndex)
appearanceHandler.callApparance(.viewWillAppear, animated: animated, at: leftSidePageIndex)
}

public override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
appearanceHandler?.callApparance(.viewDidAppear, animated: animated, at: leftSidePageIndex)
appearanceHandler.callApparance(.viewDidAppear, animated: animated, at: leftSidePageIndex)
}

public override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
appearanceHandler?.callApparance(.viewWillDisappear, animated: animated, at: leftSidePageIndex)
appearanceHandler.callApparance(.viewWillDisappear, animated: animated, at: leftSidePageIndex)
}

public override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
appearanceHandler?.callApparance(.viewDidDisappear, animated: animated, at: leftSidePageIndex)
appearanceHandler.callApparance(.viewDidDisappear, animated: animated, at: leftSidePageIndex)
}

override public func viewDidLayoutSubviews() {
Expand All @@ -324,13 +319,10 @@ public class PagingContentViewController: UIViewController {
}

override public func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
appearanceHandler = nil
coordinator.animate(alongsideTransition: { [weak self] (context) in
guard let _self = self else { return }
_self.scroll(to: _self.leftSidePageIndex, animated: false)
}, completion: { [weak self] _ in
self?.setupAppearanceHandler()
})
_self.scroll(to: _self.leftSidePageIndex, needsCallAppearance: false, animated: false)
}, completion: nil)

super.viewWillTransition(to: size, with: coordinator)
}
Expand Down Expand Up @@ -394,7 +386,7 @@ extension PagingContentViewController: UIScrollViewDelegate {
guard let _self = self else { return }
_self.delegate?.contentViewController(viewController: _self, willBeginPagingAt: leftSidePageIndex, animated: false)
_self.explicitPaging?.start()
_self.appearanceHandler?.beginDragging(at: leftSidePageIndex)
_self.appearanceHandler.beginDragging(at: leftSidePageIndex)
})
leftSidePageIndex = Int(scrollView.contentOffset.x / scrollView.bounds.width)
delegate?.contentViewController(viewController: self, willBeginManualScrollOn: leftSidePageIndex)
Expand All @@ -421,7 +413,7 @@ extension PagingContentViewController: UIScrollViewDelegate {
loadPagesIfNeeded()
delegate?.contentViewController(viewController: self, didEndManualScrollOn: leftSidePageIndex)
if explicitPaging.isPaging {
appearanceHandler?.stopScrolling(at: leftSidePageIndex)
appearanceHandler.stopScrolling(at: leftSidePageIndex)

delegate?.contentViewController(viewController: self, didFinishPagingAt: leftSidePageIndex, animated: true)
}
Expand All @@ -437,7 +429,7 @@ extension PagingContentViewController: UIScrollViewDelegate {
loadPagesIfNeeded()
delegate?.contentViewController(viewController: self, didEndManualScrollOn: leftSidePageIndex)
if explicitPaging.isPaging {
appearanceHandler?.stopScrolling(at: leftSidePageIndex)
appearanceHandler.stopScrolling(at: leftSidePageIndex)

delegate?.contentViewController(viewController: self, willFinishPagingAt: leftSidePageIndex, animated: false)
delegate?.contentViewController(viewController: self, didFinishPagingAt: leftSidePageIndex, animated: false)
Expand Down

0 comments on commit b983d81

Please sign in to comment.