Skip to content

Commit a088f9f

Browse files
author
Martin Barreto
committed
refactor pagerOptions
1 parent 5616059 commit a088f9f

11 files changed

+78
-42
lines changed

Example/Example/BarExampleViewController.swift

+6-3
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,12 @@ class BarExampleViewController: BarPagerTabStripViewController {
6868

6969
override func reloadPagerTabStripView() {
7070
isReload = true
71-
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.SkipIntermediateViewControllers) : (pagerOptions.remove(.SkipIntermediateViewControllers) ?? pagerOptions)
72-
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.IsProgressiveIndicator) : (pagerOptions.remove(.IsProgressiveIndicator) ?? pagerOptions)
73-
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.IsElasticIndicatorLimit) : (pagerOptions.remove(.IsElasticIndicatorLimit) ?? pagerOptions)
71+
if rand() % 2 == 0 {
72+
pagerBehaviour = .Progressive(skipIntermediteViewControllers: rand() % 2 == 0 , elasticIndicatorLimit: rand() % 2 == 0 )
73+
}
74+
else {
75+
pagerBehaviour = .Common(skipIntermediteViewControllers: rand() % 2 == 0)
76+
}
7477
super.reloadPagerTabStripView()
7578
}
7679
}

Example/Example/ButtonBarExampleViewController.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ class ButtonBarExampleViewController: ButtonBarPagerTabStripViewController {
3232
override func viewDidLoad() {
3333
super.viewDidLoad()
3434

35-
if pagerOptions.contains(.IsProgressiveIndicator) {
36-
pagerOptions = pagerOptions.remove(.IsProgressiveIndicator)!
37-
}
38-
3935
buttonBarView.selectedBar.backgroundColor = .orangeColor()
4036
buttonBarView.backgroundColor = UIColor(red: 7/255, green: 185/255, blue: 155/255, alpha: 1)
4137
}
@@ -71,8 +67,12 @@ class ButtonBarExampleViewController: ButtonBarPagerTabStripViewController {
7167

7268
override func reloadPagerTabStripView() {
7369
isReload = true
74-
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.IsProgressiveIndicator) : (pagerOptions.remove(.IsProgressiveIndicator) ?? pagerOptions)
75-
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.IsElasticIndicatorLimit) : (pagerOptions.remove(.IsElasticIndicatorLimit) ?? pagerOptions)
70+
if rand() % 2 == 0 {
71+
pagerBehaviour = .Progressive(skipIntermediteViewControllers: rand() % 2 == 0 , elasticIndicatorLimit: rand() % 2 == 0 )
72+
}
73+
else {
74+
pagerBehaviour = .Common(skipIntermediteViewControllers: rand() % 2 == 0)
75+
}
7676
super.reloadPagerTabStripView()
7777
}
7878
}

Example/Example/NavButtonBarExampleViewController.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,6 @@ class NavButtonBarExampleViewController: ButtonBarPagerTabStripViewController {
3535
//-
3636
super.viewDidLoad()
3737

38-
if pagerOptions.contains(.IsProgressiveIndicator) {
39-
pagerOptions = pagerOptions.remove(.IsProgressiveIndicator)!
40-
}
41-
4238
buttonBarView.removeFromSuperview()
4339
navigationController?.navigationBar.addSubview(buttonBarView)
4440

@@ -92,8 +88,12 @@ class NavButtonBarExampleViewController: ButtonBarPagerTabStripViewController {
9288

9389
override func reloadPagerTabStripView() {
9490
isReload = true
95-
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.IsProgressiveIndicator) : (pagerOptions.remove(.IsProgressiveIndicator) ?? pagerOptions)
96-
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.IsElasticIndicatorLimit) : (pagerOptions.remove(.IsElasticIndicatorLimit) ?? pagerOptions)
91+
if rand() % 2 == 0 {
92+
pagerBehaviour = .Progressive(skipIntermediteViewControllers: rand() % 2 == 0 , elasticIndicatorLimit: rand() % 2 == 0 )
93+
}
94+
else {
95+
pagerBehaviour = .Common(skipIntermediteViewControllers: rand() % 2 == 0)
96+
}
9797
super.reloadPagerTabStripView()
9898
}
9999

Example/Example/ReloadExampleViewController.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class ReloadExampleViewController: UIViewController {
7070
return bool ? "YES" : "NO"
7171
}
7272

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

7575
(navigationItem.titleView as? UILabel)?.text = titleLabel.text
7676
navigationItem.titleView?.sizeToFit()

Example/Example/SegmentedExampleViewController.swift

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ class SegmentedExampleViewController: SegmentedPagerTabStripViewController {
6363

6464
@IBAction func reloadTapped(sender: UIBarButtonItem) {
6565
isReload = true
66-
pagerOptions = rand() % 2 == 0 ? pagerOptions.union(.SkipIntermediateViewControllers) : (pagerOptions.remove(.SkipIntermediateViewControllers) ?? pagerOptions)
66+
pagerBehaviour = .Common(skipIntermediteViewControllers: rand() % 2 == 0)
67+
pagerBehaviour.skipIntermediateViewControllers
6768
reloadPagerTabStripView()
6869
}
6970
}

Example/Example/TwitterExampleViewController.swift

-7
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,6 @@ import XLPagerTabStrip
2828
class TwitterExampleViewController: TwitterPagerTabStripViewController {
2929
var isReload = false
3030

31-
override func viewDidLoad() {
32-
super.viewDidLoad()
33-
34-
pagerOptions = pagerOptions.union(.IsProgressiveIndicator)
35-
pagerOptions = pagerOptions.union(.IsElasticIndicatorLimit)
36-
}
37-
3831
override func childViewControllersForPagerTabStripViewController(pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
3932

4033
let child_1 = TableChildExampleViewController(style: .Plain, itemInfo: "TableView")

Example/Example/Youtube/YoutubeExampleViewController.swift

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class YoutubeExampleViewController: BaseButtonBarPagerTabStripViewController<You
5757
settings.style.buttonBarLeftContentInset = 20
5858
settings.style.buttonBarRightContentInset = 20
5959

60+
61+
6062
changeCurrentIndexProgressive = { [weak self] (oldCell: YoutubeIconCell?, newCell: YoutubeIconCell?, progressPercentage: CGFloat, changeCurrentIndex: Bool, animated: Bool) -> Void in
6163
guard changeCurrentIndex == true else { return }
6264
oldCell?.iconImage.tintColor = self?.unselectedIconColor

Sources/BaseButtonBarPagerTabStripViewController.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public class BaseButtonBarPagerTabStripViewController<ButtonBarCellType : UIColl
207207

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

236236
configureCell(cell, childInfo: childInfo)
237237

238-
if pagerOptions.contains(.IsProgressiveIndicator) {
238+
if pagerBehaviour.isProgressiveIndicator {
239239
if let changeCurrentIndexProgressive = changeCurrentIndexProgressive {
240240
changeCurrentIndexProgressive(oldCell: currentIndex == indexPath.item ? nil : cell, newCell: currentIndex == indexPath.item ? cell : nil, progressPercentage: 1, changeCurrentIndex: true, animated: false)
241241
}

Sources/ButtonBarPagerTabStripViewController.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ public class ButtonBarPagerTabStripViewController: PagerTabStripViewController,
242242

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

283283
configureCell(cell, childInfo: childInfo)
284284

285-
if pagerOptions.contains(.IsProgressiveIndicator) {
285+
if pagerBehaviour.isProgressiveIndicator {
286286
if let changeCurrentIndexProgressive = changeCurrentIndexProgressive {
287287
changeCurrentIndexProgressive(oldCell: currentIndex == indexPath.item ? nil : cell, newCell: currentIndex == indexPath.item ? cell : nil, progressPercentage: 1, changeCurrentIndex: true, animated: false)
288288
}

Sources/PagerTabStripOptions.swift

+47-10
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,55 @@
2424

2525
import Foundation
2626

27-
public struct PagerTabStripOptions : OptionSetType {
27+
//public struct PagerTabStripOptions : OptionSetType {
28+
//
29+
// private enum PagerTabStripOption : Int {
30+
// case SkipIntermediateViewControllers = 1, IsProgressiveIndicator = 2, IsElasticIndicatorLimit = 4
31+
// }
32+
//
33+
// public let rawValue: Int
34+
// public init(rawValue: Int) { self.rawValue = rawValue }
35+
// private init(_ option:PagerTabStripOption){ self.rawValue = option.rawValue }
36+
//
37+
// public static let SkipIntermediateViewControllers = PagerTabStripOptions(.SkipIntermediateViewControllers)
38+
// public static let IsProgressiveIndicator = PagerTabStripOptions(.IsProgressiveIndicator)
39+
// public static let IsElasticIndicatorLimit = PagerTabStripOptions(.IsElasticIndicatorLimit)
40+
//
41+
//}
42+
43+
public enum PagerTabStripBehaviour {
44+
case Common(skipIntermediteViewControllers: Bool)
45+
case Progressive(skipIntermediteViewControllers: Bool, elasticIndicatorLimit: Bool)
2846

29-
private enum PagerTabStripOption : Int {
30-
case SkipIntermediateViewControllers = 1, IsProgressiveIndicator = 2, IsElasticIndicatorLimit = 4
47+
public var skipIntermediateViewControllers: Bool {
48+
switch self {
49+
case .Common(let skipIntermediteControllers):
50+
return skipIntermediteControllers
51+
case .Progressive(let skipIntermediteControllers, _):
52+
return skipIntermediteControllers
53+
}
3154
}
3255

33-
public let rawValue: Int
34-
public init(rawValue: Int) { self.rawValue = rawValue }
35-
private init(_ option:PagerTabStripOption){ self.rawValue = option.rawValue }
3656

37-
public static let SkipIntermediateViewControllers = PagerTabStripOptions(.SkipIntermediateViewControllers)
38-
public static let IsProgressiveIndicator = PagerTabStripOptions(.IsProgressiveIndicator)
39-
public static let IsElasticIndicatorLimit = PagerTabStripOptions(.IsElasticIndicatorLimit)
40-
57+
public var isProgressiveIndicator: Bool {
58+
switch self {
59+
case .Common(_):
60+
return false
61+
case .Progressive(_, _):
62+
return true
63+
}
64+
}
65+
66+
public var isElasticIndicatorLimit: Bool {
67+
switch self {
68+
case .Common(_):
69+
return false
70+
case .Progressive(_, let elasticIndicatorLimit):
71+
return elasticIndicatorLimit
72+
}
73+
}
4174
}
75+
76+
77+
78+

Sources/PagerTabStripViewController.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public class PagerTabStripViewController: UIViewController, UIScrollViewDelegate
6262
public weak var delegate: PagerTabStripViewControllerDelegate?
6363
public weak var datasource: PagerTabStripViewControllerDataSource?
6464

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

6767
public private(set) var viewControllers = [UIViewController]()
6868
public private(set) var currentIndex = 0
@@ -131,7 +131,7 @@ public class PagerTabStripViewController: UIViewController, UIScrollViewDelegate
131131
currentIndex = index
132132
return
133133
}
134-
if animated && pagerOptions.contains(.SkipIntermediateViewControllers) && abs(currentIndex - index) > 1 {
134+
if animated && pagerBehaviour.skipIntermediateViewControllers && abs(currentIndex - index) > 1 {
135135
var tmpViewControllers = viewControllers
136136
let currentChildVC = viewControllers[currentIndex]
137137
let fromIndex = currentIndex < index ? index - 1 : index + 1
@@ -253,7 +253,7 @@ public class PagerTabStripViewController: UIViewController, UIScrollViewDelegate
253253
currentIndex = newCurrentIndex
254254
let changeCurrentIndex = newCurrentIndex != oldCurrentIndex
255255

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

258258
let (fromIndex, toIndex, scrollPercentage) = progressiveIndicatorData(virtualPage)
259259
try! progressiveDeledate.pagerTabStripViewController(self, updateIndicatorFromIndex: fromIndex, toIndex: toIndex, withProgressPercentage: scrollPercentage, indexWasChanged: changeCurrentIndex)
@@ -355,7 +355,7 @@ public class PagerTabStripViewController: UIViewController, UIScrollViewDelegate
355355
}
356356
}
357357
}
358-
let scrollPercentage = pagerOptions.contains(.IsElasticIndicatorLimit) ? self.scrollPercentage : ((toIndex < 0 || toIndex >= count) ? 0.0 : self.scrollPercentage)
358+
let scrollPercentage = pagerBehaviour.isElasticIndicatorLimit ? self.scrollPercentage : ((toIndex < 0 || toIndex >= count) ? 0.0 : self.scrollPercentage)
359359
return (fromIndex, toIndex, scrollPercentage)
360360
}
361361

0 commit comments

Comments
 (0)