Skip to content

Commit

Permalink
Merge pull request #107 from kazuhiro4949/hotfix/appearance_call
Browse files Browse the repository at this point in the history
Update for lifecycle when rotating
  • Loading branch information
kazuhiro4949 authored Dec 19, 2019
2 parents 85e87c9 + f4872b1 commit 3e2b103
Show file tree
Hide file tree
Showing 5 changed files with 512 additions and 1 deletion.
9 changes: 9 additions & 0 deletions PagingKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
2BC16A321FFE355F00CB884A /* UnderlineFocusView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2BC16A311FFE355F00CB884A /* UnderlineFocusView.swift */; };
2BC16A341FFE35AA00CB884A /* PagingKitProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2BC16A331FFE35AA00CB884A /* PagingKitProxy.swift */; };
2BC16A361FFE3B3E00CB884A /* TitleLabelMenuViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2BC16A351FFE3B3E00CB884A /* TitleLabelMenuViewCell.swift */; };
2BE185F623AB7AAE00B457ED /* ContentAppearanceHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2BE185F523AB7AAE00B457ED /* ContentAppearanceHandler.swift */; };
2BE185F823AB7B2600B457ED /* ContentAppearanceHandlerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2BE185F723AB7B2600B457ED /* ContentAppearanceHandlerTests.swift */; };
6EC140C51F0940F40054B514 /* PagingKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 6EC140BB1F0940F40054B514 /* PagingKit.framework */; };
6EC140CC1F0940F40054B514 /* PagingKit.h in Headers */ = {isa = PBXBuildFile; fileRef = 6EC140BE1F0940F40054B514 /* PagingKit.h */; settings = {ATTRIBUTES = (Public, ); }; };
6EC140D71F09410F0054B514 /* PagingMenuViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6EC140D51F09410F0054B514 /* PagingMenuViewController.swift */; };
Expand Down Expand Up @@ -44,6 +46,8 @@
2BC16A311FFE355F00CB884A /* UnderlineFocusView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnderlineFocusView.swift; sourceTree = "<group>"; };
2BC16A331FFE35AA00CB884A /* PagingKitProxy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PagingKitProxy.swift; sourceTree = "<group>"; };
2BC16A351FFE3B3E00CB884A /* TitleLabelMenuViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TitleLabelMenuViewCell.swift; sourceTree = "<group>"; };
2BE185F523AB7AAE00B457ED /* ContentAppearanceHandler.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentAppearanceHandler.swift; sourceTree = "<group>"; };
2BE185F723AB7B2600B457ED /* ContentAppearanceHandlerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentAppearanceHandlerTests.swift; sourceTree = "<group>"; };
6EC140BB1F0940F40054B514 /* PagingKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = PagingKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
6EC140BE1F0940F40054B514 /* PagingKit.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = PagingKit.h; sourceTree = "<group>"; };
6EC140BF1F0940F40054B514 /* PagingKit-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "PagingKit-Info.plist"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -97,6 +101,7 @@
6EC140BF1F0940F40054B514 /* PagingKit-Info.plist */,
6EC140D51F09410F0054B514 /* PagingMenuViewController.swift */,
6EC140D61F09410F0054B514 /* PagingContentViewController.swift */,
2BE185F523AB7AAE00B457ED /* ContentAppearanceHandler.swift */,
2B668C5E1F1395BE008A0694 /* PagingMenuView.swift */,
2B283F041FA8A2BD00DA2FE4 /* Array+NSLayoutConstraint.swift */,
2BC16A311FFE355F00CB884A /* UnderlineFocusView.swift */,
Expand All @@ -109,6 +114,7 @@
6EC140C81F0940F40054B514 /* PagingKitTests */ = {
isa = PBXGroup;
children = (
2BE185F723AB7B2600B457ED /* ContentAppearanceHandlerTests.swift */,
2B4F03421F8FA0230015FCC8 /* PagingMenuViewControllerTests.swift */,
2B4F03481F8FB9950015FCC8 /* PagingContentViewControllerTests.swift */,
2B4F03441F8FA0760015FCC8 /* PagingMenuViewTests.swift */,
Expand Down Expand Up @@ -196,6 +202,7 @@
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
English,
en,
);
mainGroup = 6EC140B11F0940F40054B514;
Expand Down Expand Up @@ -235,6 +242,7 @@
files = (
2BC16A341FFE35AA00CB884A /* PagingKitProxy.swift in Sources */,
2BC16A321FFE355F00CB884A /* UnderlineFocusView.swift in Sources */,
2BE185F623AB7AAE00B457ED /* ContentAppearanceHandler.swift in Sources */,
2BC16A361FFE3B3E00CB884A /* TitleLabelMenuViewCell.swift in Sources */,
6EC140D81F09410F0054B514 /* PagingContentViewController.swift in Sources */,
2B668C5F1F1395BE008A0694 /* PagingMenuView.swift in Sources */,
Expand All @@ -249,6 +257,7 @@
files = (
2B4F03491F8FB9950015FCC8 /* PagingContentViewControllerTests.swift in Sources */,
2B4F03431F8FA0230015FCC8 /* PagingMenuViewControllerTests.swift in Sources */,
2BE185F823AB7B2600B457ED /* ContentAppearanceHandlerTests.swift in Sources */,
2B4F03451F8FA0760015FCC8 /* PagingMenuViewTests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
105 changes: 105 additions & 0 deletions PagingKit/ContentAppearanceHandler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//
// ContentAppearanceHandler.swift
// PagingKit
//
// Created by kahayash on 2019/12/19.
// Copyright © 2019 Kazuhiro Hayashi. All rights reserved.
//

import Foundation

protocol ContentsAppearanceHandlerProtocol {
var contentsDequeueHandler: (() -> [UIViewController?]?)? { get set }
func beginDragging(at index: Int)
func stopScrolling(at index: Int)
func callApparance(_ apperance: ContentsAppearanceHandler.Apperance, animated: Bool, at index: Int)
func preReload(at index: Int)
func postReload(at index: Int)
}

class ContentsAppearanceHandler: ContentsAppearanceHandlerProtocol {
enum Apperance {
case viewDidAppear
case viewWillAppear
case viewDidDisappear
case viewWillDisappear
}

private var dissapearingIndex: Int?
var contentsDequeueHandler: (() -> [UIViewController?]?)?


func beginDragging(at index: Int) {
guard let vcs = contentsDequeueHandler?(), index < vcs.endIndex, let vc = vcs[index] else {
return
}

if let dissapearingIndex = dissapearingIndex, dissapearingIndex < vcs.endIndex, let prevVc = vcs[dissapearingIndex] {
prevVc.endAppearanceTransition()
}

vc.beginAppearanceTransition(false, animated: false)
dissapearingIndex = index
}

func stopScrolling(at index: Int) {
guard let vcs = contentsDequeueHandler?(), index < vcs.endIndex, let vc = vcs[index] else {
return
}

if let dissapearingIndex = dissapearingIndex, dissapearingIndex < vcs.endIndex, let prevVc = vcs[dissapearingIndex] {
prevVc.endAppearanceTransition()
}

vc.beginAppearanceTransition(true, animated: false)
vc.endAppearanceTransition()
dissapearingIndex = nil
}

func callApparance(_ apperance: Apperance, animated: Bool, at index: Int) {
guard let vcs = contentsDequeueHandler?(), index < vcs.endIndex, let vc = vcs[index] else {
return
}

if let dissapearingIndex = dissapearingIndex,
dissapearingIndex < vcs.endIndex,
let prevVc = vcs[dissapearingIndex],
dissapearingIndex == index {

prevVc.endAppearanceTransition()
}
dissapearingIndex = nil

switch apperance {
case .viewDidAppear, .viewDidDisappear:
vc.endAppearanceTransition()
case .viewWillAppear:
vc.beginAppearanceTransition(true, animated: animated)
case .viewWillDisappear:
vc.beginAppearanceTransition(false, animated: animated)
}
}

func preReload(at index: Int) {
guard let vcs = contentsDequeueHandler?(), index < vcs.endIndex, let vc = vcs[index] else {
return
}

vc.beginAppearanceTransition(false, animated: false)
vc.endAppearanceTransition()

}



func postReload(at index: Int) {
guard let vcs = contentsDequeueHandler?(), index < vcs.endIndex, let vc = vcs[index] else {
return
}

vc.beginAppearanceTransition(true, animated: false)
vc.endAppearanceTransition()

}
}

58 changes: 57 additions & 1 deletion PagingKit/PagingContentViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ public class PagingContentViewController: UIViewController {
fileprivate var numberOfPages: Int = 0
fileprivate var explicitPaging: ExplicitPaging?

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 @@ -163,6 +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)
let preferredPage = page ?? leftSidePageIndex
leftSidePageIndex = preferredPage
initialLoad(with: preferredPage)
Expand All @@ -172,7 +175,8 @@ public class PagingContentViewController: UIViewController {
self?.view.layoutIfNeeded()
},
completion: { [weak self] _ in
self?.scroll(to: preferredPage, animated: false) { _ in
self?.scroll(to: preferredPage, needsCallAppearance: false, animated: false) { _ in
self?.appearanceHandler.postReload(at: preferredPage)
completion?()
}
}
Expand All @@ -185,14 +189,28 @@ public class PagingContentViewController: UIViewController {
/// - page: A index defining an content of the content view controller.
/// - animated: true if the scrolling should be animated, false if it should be immediate.
public func scroll(to page: Int, animated: Bool, completion: ((Bool) -> Void)? = nil) {
scroll(to: page, needsCallAppearance: true, animated: animated, completion: completion)
}


private func scroll(to page: Int, needsCallAppearance: Bool, animated: Bool, completion: ((Bool) -> Void)? = nil) {
delegate?.contentViewController(viewController: self, willBeginPagingAt: leftSidePageIndex, animated: animated)

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

loadPagesIfNeeded(page: page)
leftSidePageIndex = page

delegate?.contentViewController(viewController: self, willFinishPagingAt: leftSidePageIndex, animated: animated)
move(to: page, animated: animated) { [weak self] (finished) in
guard let _self = self, finished else { return }

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

completion?(finished)
_self.delegate?.contentViewController(viewController: _self, didFinishPagingAt: _self.leftSidePageIndex, animated: animated)
}
Expand Down Expand Up @@ -255,6 +273,30 @@ public class PagingContentViewController: UIViewController {
view.addSubview(scrollView)
view.addConstraints([.top, .bottom, .leading, .trailing].anchor(from: scrollView, to: view))
view.backgroundColor = .clear

appearanceHandler.contentsDequeueHandler = { [weak self] in
self?.cachedViewControllers
}
}

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

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

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

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

override public func viewDidLayoutSubviews() {
Expand Down Expand Up @@ -287,6 +329,11 @@ public class PagingContentViewController: UIViewController {
super.viewWillTransition(to: size, with: coordinator)
}


public override var shouldAutomaticallyForwardAppearanceMethods: Bool {
return false
}

fileprivate func removeAll() {
scrollView.subviews.forEach { $0.removeFromSuperview() }
children.forEach { $0.removeFromParent() }
Expand Down Expand Up @@ -333,10 +380,15 @@ public class PagingContentViewController: UIViewController {

extension PagingContentViewController: UIScrollViewDelegate {
public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
guard !(scrollView.isDragging && scrollView.isDecelerating) else {
return
}

explicitPaging = ExplicitPaging(oneTimeHandler: { [weak self, leftSidePageIndex = leftSidePageIndex] in
guard let _self = self else { return }
_self.delegate?.contentViewController(viewController: _self, willBeginPagingAt: leftSidePageIndex, animated: false)
_self.explicitPaging?.start()
_self.appearanceHandler.beginDragging(at: leftSidePageIndex)
})
leftSidePageIndex = Int(scrollView.contentOffset.x / scrollView.bounds.width)
delegate?.contentViewController(viewController: self, willBeginManualScrollOn: leftSidePageIndex)
Expand All @@ -363,6 +415,8 @@ extension PagingContentViewController: UIScrollViewDelegate {
loadPagesIfNeeded()
delegate?.contentViewController(viewController: self, didEndManualScrollOn: leftSidePageIndex)
if explicitPaging.isPaging {
appearanceHandler.stopScrolling(at: leftSidePageIndex)

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

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

0 comments on commit 3e2b103

Please sign in to comment.