Skip to content

Commit

Permalink
refactor pagerOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Barreto committed Jan 28, 2016
1 parent 5616059 commit a088f9f
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 42 deletions.
9 changes: 6 additions & 3 deletions Example/Example/BarExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,12 @@ class BarExampleViewController: BarPagerTabStripViewController {

override func reloadPagerTabStripView() {
isReload = true
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.SkipIntermediateViewControllers) : (pagerOptions.remove(.SkipIntermediateViewControllers) ?? pagerOptions)
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.IsProgressiveIndicator) : (pagerOptions.remove(.IsProgressiveIndicator) ?? pagerOptions)
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.IsElasticIndicatorLimit) : (pagerOptions.remove(.IsElasticIndicatorLimit) ?? pagerOptions)
if rand() % 2 == 0 {
pagerBehaviour = .Progressive(skipIntermediteViewControllers: rand() % 2 == 0 , elasticIndicatorLimit: rand() % 2 == 0 )
}
else {
pagerBehaviour = .Common(skipIntermediteViewControllers: rand() % 2 == 0)
}
super.reloadPagerTabStripView()
}
}
12 changes: 6 additions & 6 deletions Example/Example/ButtonBarExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ class ButtonBarExampleViewController: ButtonBarPagerTabStripViewController {
override func viewDidLoad() {
super.viewDidLoad()

if pagerOptions.contains(.IsProgressiveIndicator) {
pagerOptions = pagerOptions.remove(.IsProgressiveIndicator)!
}

buttonBarView.selectedBar.backgroundColor = .orangeColor()
buttonBarView.backgroundColor = UIColor(red: 7/255, green: 185/255, blue: 155/255, alpha: 1)
}
Expand Down Expand Up @@ -71,8 +67,12 @@ class ButtonBarExampleViewController: ButtonBarPagerTabStripViewController {

override func reloadPagerTabStripView() {
isReload = true
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.IsProgressiveIndicator) : (pagerOptions.remove(.IsProgressiveIndicator) ?? pagerOptions)
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.IsElasticIndicatorLimit) : (pagerOptions.remove(.IsElasticIndicatorLimit) ?? pagerOptions)
if rand() % 2 == 0 {
pagerBehaviour = .Progressive(skipIntermediteViewControllers: rand() % 2 == 0 , elasticIndicatorLimit: rand() % 2 == 0 )
}
else {
pagerBehaviour = .Common(skipIntermediteViewControllers: rand() % 2 == 0)
}
super.reloadPagerTabStripView()
}
}
12 changes: 6 additions & 6 deletions Example/Example/NavButtonBarExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ class NavButtonBarExampleViewController: ButtonBarPagerTabStripViewController {
//-
super.viewDidLoad()

if pagerOptions.contains(.IsProgressiveIndicator) {
pagerOptions = pagerOptions.remove(.IsProgressiveIndicator)!
}

buttonBarView.removeFromSuperview()
navigationController?.navigationBar.addSubview(buttonBarView)

Expand Down Expand Up @@ -92,8 +88,12 @@ class NavButtonBarExampleViewController: ButtonBarPagerTabStripViewController {

override func reloadPagerTabStripView() {
isReload = true
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.IsProgressiveIndicator) : (pagerOptions.remove(.IsProgressiveIndicator) ?? pagerOptions)
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.IsElasticIndicatorLimit) : (pagerOptions.remove(.IsElasticIndicatorLimit) ?? pagerOptions)
if rand() % 2 == 0 {
pagerBehaviour = .Progressive(skipIntermediteViewControllers: rand() % 2 == 0 , elasticIndicatorLimit: rand() % 2 == 0 )
}
else {
pagerBehaviour = .Common(skipIntermediteViewControllers: rand() % 2 == 0)
}
super.reloadPagerTabStripView()
}

Expand Down
2 changes: 1 addition & 1 deletion Example/Example/ReloadExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ReloadExampleViewController: UIViewController {
return bool ? "YES" : "NO"
}

titleLabel.text = "Progressive = \(stringFromBool(pagerTabStripViewController.pagerOptions.contains(.IsProgressiveIndicator))) ElasticLimit = \(stringFromBool(pagerTabStripViewController.pagerOptions.contains(.IsElasticIndicatorLimit)))"
titleLabel.text = "Progressive = \(stringFromBool(pagerTabStripViewController.pagerBehaviour.isProgressiveIndicator)) ElasticLimit = \(stringFromBool(pagerTabStripViewController.pagerBehaviour.isElasticIndicatorLimit))"

(navigationItem.titleView as? UILabel)?.text = titleLabel.text
navigationItem.titleView?.sizeToFit()
Expand Down
3 changes: 2 additions & 1 deletion Example/Example/SegmentedExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ class SegmentedExampleViewController: SegmentedPagerTabStripViewController {

@IBAction func reloadTapped(sender: UIBarButtonItem) {
isReload = true
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.SkipIntermediateViewControllers) : (pagerOptions.remove(.SkipIntermediateViewControllers) ?? pagerOptions)
pagerBehaviour = .Common(skipIntermediteViewControllers: rand() % 2 == 0)
pagerBehaviour.skipIntermediateViewControllers
reloadPagerTabStripView()
}
}
7 changes: 0 additions & 7 deletions Example/Example/TwitterExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ import XLPagerTabStrip
class TwitterExampleViewController: TwitterPagerTabStripViewController {
var isReload = false

override func viewDidLoad() {
super.viewDidLoad()

pagerOptions = pagerOptions.union(.IsProgressiveIndicator)
pagerOptions = pagerOptions.union(.IsElasticIndicatorLimit)
}

override func childViewControllersForPagerTabStripViewController(pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {

let child_1 = TableChildExampleViewController(style: .Plain, itemInfo: "TableView")
Expand Down
2 changes: 2 additions & 0 deletions Example/Example/Youtube/YoutubeExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class YoutubeExampleViewController: BaseButtonBarPagerTabStripViewController<You
settings.style.buttonBarLeftContentInset = 20
settings.style.buttonBarRightContentInset = 20



changeCurrentIndexProgressive = { [weak self] (oldCell: YoutubeIconCell?, newCell: YoutubeIconCell?, progressPercentage: CGFloat, changeCurrentIndex: Bool, animated: Bool) -> Void in
guard changeCurrentIndex == true else { return }
oldCell?.iconImage.tintColor = self?.unselectedIconColor
Expand Down
4 changes: 2 additions & 2 deletions Sources/BaseButtonBarPagerTabStripViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public class BaseButtonBarPagerTabStripViewController<ButtonBarCellType : UIColl

let oldCell = buttonBarView.cellForItemAtIndexPath(NSIndexPath(forItem: currentIndex, inSection: 0)) as! ButtonBarCellType
let newCell = buttonBarView.cellForItemAtIndexPath(NSIndexPath(forItem: indexPath.item, inSection: 0)) as! ButtonBarCellType
if pagerOptions.contains(.IsProgressiveIndicator) {
if pagerBehaviour.isProgressiveIndicator {
if let changeCurrentIndexProgressive = changeCurrentIndexProgressive {
changeCurrentIndexProgressive(oldCell: oldCell, newCell: newCell, progressPercentage: 1, changeCurrentIndex: true, animated: true)
}
Expand Down Expand Up @@ -235,7 +235,7 @@ public class BaseButtonBarPagerTabStripViewController<ButtonBarCellType : UIColl

configureCell(cell, childInfo: childInfo)

if pagerOptions.contains(.IsProgressiveIndicator) {
if pagerBehaviour.isProgressiveIndicator {
if let changeCurrentIndexProgressive = changeCurrentIndexProgressive {
changeCurrentIndexProgressive(oldCell: currentIndex == indexPath.item ? nil : cell, newCell: currentIndex == indexPath.item ? cell : nil, progressPercentage: 1, changeCurrentIndex: true, animated: false)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/ButtonBarPagerTabStripViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ public class ButtonBarPagerTabStripViewController: PagerTabStripViewController,

let oldCell = buttonBarView.cellForItemAtIndexPath(NSIndexPath(forItem: currentIndex, inSection: 0)) as! ButtonBarViewCell
let newCell = buttonBarView.cellForItemAtIndexPath(NSIndexPath(forItem: indexPath.item, inSection: 0)) as! ButtonBarViewCell
if pagerOptions.contains(.IsProgressiveIndicator) {
if pagerBehaviour.isProgressiveIndicator {
if let changeCurrentIndexProgressive = changeCurrentIndexProgressive {
changeCurrentIndexProgressive(oldCell: oldCell, newCell: newCell, progressPercentage: 1, changeCurrentIndex: true, animated: true)
}
Expand Down Expand Up @@ -282,7 +282,7 @@ public class ButtonBarPagerTabStripViewController: PagerTabStripViewController,

configureCell(cell, childInfo: childInfo)

if pagerOptions.contains(.IsProgressiveIndicator) {
if pagerBehaviour.isProgressiveIndicator {
if let changeCurrentIndexProgressive = changeCurrentIndexProgressive {
changeCurrentIndexProgressive(oldCell: currentIndex == indexPath.item ? nil : cell, newCell: currentIndex == indexPath.item ? cell : nil, progressPercentage: 1, changeCurrentIndex: true, animated: false)
}
Expand Down
57 changes: 47 additions & 10 deletions Sources/PagerTabStripOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,55 @@

import Foundation

public struct PagerTabStripOptions : OptionSetType {
//public struct PagerTabStripOptions : OptionSetType {
//
// private enum PagerTabStripOption : Int {
// case SkipIntermediateViewControllers = 1, IsProgressiveIndicator = 2, IsElasticIndicatorLimit = 4
// }
//
// public let rawValue: Int
// public init(rawValue: Int) { self.rawValue = rawValue }
// private init(_ option:PagerTabStripOption){ self.rawValue = option.rawValue }
//
// public static let SkipIntermediateViewControllers = PagerTabStripOptions(.SkipIntermediateViewControllers)
// public static let IsProgressiveIndicator = PagerTabStripOptions(.IsProgressiveIndicator)
// public static let IsElasticIndicatorLimit = PagerTabStripOptions(.IsElasticIndicatorLimit)
//
//}

public enum PagerTabStripBehaviour {
case Common(skipIntermediteViewControllers: Bool)
case Progressive(skipIntermediteViewControllers: Bool, elasticIndicatorLimit: Bool)

private enum PagerTabStripOption : Int {
case SkipIntermediateViewControllers = 1, IsProgressiveIndicator = 2, IsElasticIndicatorLimit = 4
public var skipIntermediateViewControllers: Bool {
switch self {
case .Common(let skipIntermediteControllers):
return skipIntermediteControllers
case .Progressive(let skipIntermediteControllers, _):
return skipIntermediteControllers
}
}

public let rawValue: Int
public init(rawValue: Int) { self.rawValue = rawValue }
private init(_ option:PagerTabStripOption){ self.rawValue = option.rawValue }

public static let SkipIntermediateViewControllers = PagerTabStripOptions(.SkipIntermediateViewControllers)
public static let IsProgressiveIndicator = PagerTabStripOptions(.IsProgressiveIndicator)
public static let IsElasticIndicatorLimit = PagerTabStripOptions(.IsElasticIndicatorLimit)

public var isProgressiveIndicator: Bool {
switch self {
case .Common(_):
return false
case .Progressive(_, _):
return true
}
}

public var isElasticIndicatorLimit: Bool {
switch self {
case .Common(_):
return false
case .Progressive(_, let elasticIndicatorLimit):
return elasticIndicatorLimit
}
}
}




8 changes: 4 additions & 4 deletions Sources/PagerTabStripViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public class PagerTabStripViewController: UIViewController, UIScrollViewDelegate
public weak var delegate: PagerTabStripViewControllerDelegate?
public weak var datasource: PagerTabStripViewControllerDataSource?

public var pagerOptions = PagerTabStripOptions.SkipIntermediateViewControllers.union(.IsProgressiveIndicator).union(.IsElasticIndicatorLimit)
public var pagerBehaviour = PagerTabStripBehaviour.Progressive(skipIntermediteViewControllers: true, elasticIndicatorLimit: true)

public private(set) var viewControllers = [UIViewController]()
public private(set) var currentIndex = 0
Expand Down Expand Up @@ -131,7 +131,7 @@ public class PagerTabStripViewController: UIViewController, UIScrollViewDelegate
currentIndex = index
return
}
if animated && pagerOptions.contains(.SkipIntermediateViewControllers) && abs(currentIndex - index) > 1 {
if animated && pagerBehaviour.skipIntermediateViewControllers && abs(currentIndex - index) > 1 {
var tmpViewControllers = viewControllers
let currentChildVC = viewControllers[currentIndex]
let fromIndex = currentIndex < index ? index - 1 : index + 1
Expand Down Expand Up @@ -253,7 +253,7 @@ public class PagerTabStripViewController: UIViewController, UIScrollViewDelegate
currentIndex = newCurrentIndex
let changeCurrentIndex = newCurrentIndex != oldCurrentIndex

if let progressiveDeledate = self as? PagerTabStripViewControllerIsProgressiveDelegate where pagerOptions.contains(.IsProgressiveIndicator) {
if let progressiveDeledate = self as? PagerTabStripViewControllerIsProgressiveDelegate where pagerBehaviour.isProgressiveIndicator {

let (fromIndex, toIndex, scrollPercentage) = progressiveIndicatorData(virtualPage)
try! progressiveDeledate.pagerTabStripViewController(self, updateIndicatorFromIndex: fromIndex, toIndex: toIndex, withProgressPercentage: scrollPercentage, indexWasChanged: changeCurrentIndex)
Expand Down Expand Up @@ -355,7 +355,7 @@ public class PagerTabStripViewController: UIViewController, UIScrollViewDelegate
}
}
}
let scrollPercentage = pagerOptions.contains(.IsElasticIndicatorLimit) ? self.scrollPercentage : ((toIndex < 0 || toIndex >= count) ? 0.0 : self.scrollPercentage)
let scrollPercentage = pagerBehaviour.isElasticIndicatorLimit ? self.scrollPercentage : ((toIndex < 0 || toIndex >= count) ? 0.0 : self.scrollPercentage)
return (fromIndex, toIndex, scrollPercentage)
}

Expand Down

0 comments on commit a088f9f

Please sign in to comment.