Add swiftlint & fixes

This commit is contained in:
pera 2017-05-05 15:51:39 -03:00
parent a0c75eb245
commit 21ca6cc08a
32 changed files with 504 additions and 512 deletions

51
.swiftlint.yml Normal file
View File

@ -0,0 +1,51 @@
disabled_rules: # rule identifiers to exclude from running
# - colon
# - comma
# - control_statement
- line_length
- function_body_length
- identifier_name
- type_name
- large_tuple
opt_in_rules: # some rules are only opt-in
- empty_count
# Find all the available rules by running:
# swiftlint rules
excluded: # paths to ignore during linting. Takes precedence over `included`.
- Carthage
- Pods
- Source/ExcludedFolder
- Source/ExcludedFile.swift
# configurable rules can be customized from this configuration file
# binary rules can set their severity level
force_cast: warning # implicitly
force_try:
severity: warning # explicitly
# rules that have both warning and error levels, can set just the warning level
# implicitly
line_length: 200
# they can set both implicitly with an array
type_body_length:
- 300 # warning
- 400 # error
# or they can set both explicitly
file_length:
warning: 500
error: 1200
# naming rules can set warnings/errors for min_length and max_length
# additionally they can set excluded names
type_name:
min_length: 4 # only warning
max_length: # warning and error
warning: 40
error: 50
excluded: iPhone # excluded via string
identifier_name:
min_length: # only min_length
error: 4 # only error
excluded: # excluded via string array
- id
- URL
- GlobalAPIKey
reporter: "xcode" # reporter type (xcode, json, csv, checkstyle, junit, html, emoji)

View File

@ -12,14 +12,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UITabBar.appearance().tintColor = UIColor.init(red: 0.027, green: 0.725, blue: 0.608, alpha: 1)
UIApplication.shared.statusBarStyle = .lightContent
let _ = YoutubeExampleViewController(nibName: nil, bundle: nil)
_ = YoutubeExampleViewController(nibName: nil, bundle: nil)
return true
}
@ -46,6 +45,4 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}

View File

@ -55,10 +55,10 @@ class BarExampleViewController: BarPagerTabStripViewController {
}
var childViewControllers = [child_1, child_2, child_3, child_4]
for (index, _) in childViewControllers.enumerated(){
for index in childViewControllers.indices {
let nElements = childViewControllers.count - index
let n = (Int(arc4random()) % nElements) + index
if n != index{
if n != index {
swap(&childViewControllers[index], &childViewControllers[n])
}
}
@ -70,8 +70,7 @@ class BarExampleViewController: BarPagerTabStripViewController {
isReload = true
if arc4random() % 2 == 0 {
pagerBehaviour = .progressive(skipIntermediateViewControllers: arc4random() % 2 == 0, elasticIndicatorLimit: arc4random() % 2 == 0 )
}
else {
} else {
pagerBehaviour = .common(skipIntermediateViewControllers: arc4random() % 2 == 0)
}
super.reloadPagerTabStripView()

View File

@ -54,10 +54,10 @@ class ButtonBarExampleViewController: ButtonBarPagerTabStripViewController {
var childViewControllers = [child_1, child_2, child_3, child_4, child_6, child_7, child_8]
for (index, _) in childViewControllers.enumerated(){
for index in childViewControllers.indices {
let nElements = childViewControllers.count - index
let n = (Int(arc4random()) % nElements) + index
if n != index{
if n != index {
swap(&childViewControllers[index], &childViewControllers[n])
}
}
@ -69,8 +69,7 @@ class ButtonBarExampleViewController: ButtonBarPagerTabStripViewController {
isReload = true
if arc4random() % 2 == 0 {
pagerBehaviour = .progressive(skipIntermediateViewControllers: arc4random() % 2 == 0, elasticIndicatorLimit: arc4random() % 2 == 0 )
}
else {
} else {
pagerBehaviour = .common(skipIntermediateViewControllers: arc4random() % 2 == 0)
}
super.reloadPagerTabStripView()

View File

@ -43,7 +43,7 @@ class TableChildExampleViewController: UITableViewController, IndicatorInfoProvi
override func viewDidLoad() {
super.viewDidLoad()
tableView.register(UINib(nibName: "PostCell", bundle: Bundle.main), forCellReuseIdentifier: cellIdentifier)
tableView.estimatedRowHeight = 60.0;
tableView.estimatedRowHeight = 60.0
tableView.rowHeight = UITableViewAutomaticDimension
tableView.allowsSelection = false
if blackTheme {
@ -67,9 +67,9 @@ class TableChildExampleViewController: UITableViewController, IndicatorInfoProvi
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! PostCell
let data = DataProvider.sharedInstance.postsData.object(at: indexPath.row) as!
NSDictionary
guard let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as? PostCell,
let data = DataProvider.sharedInstance.postsData.object(at: indexPath.row) as? NSDictionary else { return PostCell() }
cell.configureWithData(data)
if blackTheme {
cell.changeStylToBlack()
@ -82,4 +82,5 @@ class TableChildExampleViewController: UITableViewController, IndicatorInfoProvi
func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo {
return itemInfo
}
}

File diff suppressed because one or more lines are too long

View File

@ -26,7 +26,6 @@ import UIKit
class PostCell: UITableViewCell {
@IBOutlet weak var userImage: UIImageView!
@IBOutlet weak var postName: UILabel!
@IBOutlet weak var postText: UILabel!
@ -36,8 +35,7 @@ class PostCell: UITableViewCell {
userImage.layer.cornerRadius = 10.0
}
func configureWithData(_ data: NSDictionary){
func configureWithData(_ data: NSDictionary) {
if let post = data["post"] as? NSDictionary, let user = post["user"] as? NSDictionary {
postName.text = user["name"] as? String
postText.text = post["text"] as? String
@ -45,8 +43,7 @@ class PostCell: UITableViewCell {
}
}
func changeStylToBlack(){
func changeStylToBlack() {
userImage?.layer.cornerRadius = 30.0
postText.text = nil
postName.font = UIFont(name: "HelveticaNeue-Light", size:18) ?? .systemFont(ofSize: 18)

View File

@ -65,8 +65,3 @@ class InstagramExampleViewController: ButtonBarPagerTabStripViewController {
dismiss(animated: true, completion: nil)
}
}

View File

@ -49,8 +49,7 @@ class NavButtonBarExampleViewController: ButtonBarPagerTabStripViewController {
newCell?.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
oldCell?.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
})
}
else {
} else {
newCell?.transform = CGAffineTransform(scaleX: 1.0, y: 1.0)
oldCell?.transform = CGAffineTransform(scaleX: 0.8, y: 0.8)
}
@ -75,10 +74,10 @@ class NavButtonBarExampleViewController: ButtonBarPagerTabStripViewController {
var childViewControllers = [child_1, child_2, child_3, child_4, child_6, child_7, child_8]
for (index, _) in childViewControllers.enumerated(){
for index in childViewControllers.indices {
let nElements = childViewControllers.count - index
let n = (Int(arc4random()) % nElements) + index
if n != index{
if n != index {
swap(&childViewControllers[index], &childViewControllers[n])
}
}
@ -90,8 +89,7 @@ class NavButtonBarExampleViewController: ButtonBarPagerTabStripViewController {
isReload = true
if arc4random() % 2 == 0 {
pagerBehaviour = .progressive(skipIntermediateViewControllers: arc4random() % 2 == 0, elasticIndicatorLimit: arc4random() % 2 == 0 )
}
else {
} else {
pagerBehaviour = .common(skipIntermediateViewControllers: arc4random() % 2 == 0)
}
super.reloadPagerTabStripView()

View File

@ -44,12 +44,12 @@ class ReloadExampleViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let _ = navigationController {
if navigationController != nil {
navigationItem.titleView = bigLabel
bigLabel.sizeToFit()
}
if let pagerViewController = childViewControllers.filter( { $0 is PagerTabStripViewController } ).first as? PagerTabStripViewController {
if let pagerViewController = childViewControllers.first as? PagerTabStripViewController {
updateTitle(of: pagerViewController)
}
}
@ -61,11 +61,10 @@ class ReloadExampleViewController: UIViewController {
}
child.reloadPagerTabStripView()
updateTitle(of: child)
break;
break
}
}
@IBAction func closeTapped(_ sender: UIButton) {
dismiss(animated: true, completion: nil)
}
@ -81,7 +80,7 @@ class ReloadExampleViewController: UIViewController {
navigationItem.titleView?.sizeToFit()
}
override var preferredStatusBarStyle : UIStatusBarStyle {
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
}

View File

@ -50,10 +50,10 @@ class SegmentedExampleViewController: SegmentedPagerTabStripViewController {
var childViewControllers = [child_1, child_2, child_3, child_4]
let count = childViewControllers.count
for (index, _) in childViewControllers.enumerated(){
for index in childViewControllers.indices {
let nElements = count - index
let n = (Int(arc4random()) % nElements) + index
if n != index{
if n != index {
swap(&childViewControllers[index], &childViewControllers[n])
}
}

View File

@ -70,8 +70,3 @@ class SpotifyExampleViewController: ButtonBarPagerTabStripViewController {
dismiss(animated: true, completion: nil)
}
}

View File

@ -45,10 +45,10 @@ class TwitterExampleViewController: TwitterPagerTabStripViewController {
var childViewControllers = [child_1, child_2, child_3, child_4, child_6, child_7, child_8]
for (index, _) in childViewControllers.enumerated(){
for index in childViewControllers.indices {
let nElements = childViewControllers.count - index
let n = (Int(arc4random()) % nElements) + index
if n != index{
if n != index {
swap(&childViewControllers[index], &childViewControllers[n])
}
}

View File

@ -30,7 +30,6 @@ class YoutubeExampleViewController: BaseButtonBarPagerTabStripViewController<You
let redColor = UIColor(red: 221/255.0, green: 0/255.0, blue: 19/255.0, alpha: 1.0)
let unselectedIconColor = UIColor(red: 73/255.0, green: 8/255.0, blue: 10/255.0, alpha: 1.0)
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
@ -38,7 +37,7 @@ class YoutubeExampleViewController: BaseButtonBarPagerTabStripViewController<You
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
buttonBarItemSpec = ButtonBarItemSpec.nibFile(nibName: "YoutubeIconCell", bundle: Bundle(for: YoutubeIconCell.self), width: { (cell: IndicatorInfo) -> CGFloat in
buttonBarItemSpec = ButtonBarItemSpec.nibFile(nibName: "YoutubeIconCell", bundle: Bundle(for: YoutubeIconCell.self), width: { _ in
return 55.0
})
}
@ -55,13 +54,10 @@ class YoutubeExampleViewController: BaseButtonBarPagerTabStripViewController<You
settings.style.buttonBarLeftContentInset = 0
settings.style.buttonBarRightContentInset = 0
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
newCell?.iconImage.tintColor = .white
}
super.viewDidLoad()
navigationController?.navigationBar.shadowImage = UIImage()
@ -77,7 +73,6 @@ class YoutubeExampleViewController: BaseButtonBarPagerTabStripViewController<You
return [child_1, child_2, child_3]
}
override func configure(cell: YoutubeIconCell, for indicatorInfo: IndicatorInfo) {
cell.iconImage.image = indicatorInfo.image?.withRenderingMode(.alwaysTemplate)
}
@ -85,7 +80,7 @@ class YoutubeExampleViewController: BaseButtonBarPagerTabStripViewController<You
override func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int, withProgressPercentage progressPercentage: CGFloat, indexWasChanged: Bool) {
super.updateIndicator(for: viewController, fromIndex: fromIndex, toIndex: toIndex, withProgressPercentage: progressPercentage, indexWasChanged: indexWasChanged)
if indexWasChanged && toIndex > -1 && toIndex < viewControllers.count {
let child = viewControllers[toIndex] as! IndicatorInfoProvider
let child = viewControllers[toIndex] as! IndicatorInfoProvider // swiftlint:disable:this force_cast
UIView.performWithoutAnimation({ [weak self] () -> Void in
guard let me = self else { return }
me.navigationItem.leftBarButtonItem?.title = child.indicatorInfo(for: me).title
@ -99,8 +94,3 @@ class YoutubeExampleViewController: BaseButtonBarPagerTabStripViewController<You
dismiss(animated: true, completion: nil)
}
}

View File

@ -25,8 +25,7 @@
import Foundation
import UIKit
class YoutubeIconCell : UICollectionViewCell {
class YoutubeIconCell: UICollectionViewCell {
@IBOutlet weak var iconImage: UIImageView!

View File

@ -10,4 +10,4 @@
import UIKit
import XLPagerTabStrip
var str = "Hello, playground"
var helloWorld = "Hello, playground"

View File

@ -80,7 +80,7 @@ open class BarPagerTabStripViewController: PagerTabStripViewController, PagerTab
open override func reloadPagerTabStripView() {
super.reloadPagerTabStripView()
barView.optionsCount = viewControllers.count
if isViewLoaded{
if isViewLoaded {
barView.moveTo(index: currentIndex, animated: false)
}
}

View File

@ -50,7 +50,6 @@ open class BarView: UIView {
addSubview(selectedBar)
}
// MARK: - Helpers
private func updateSelectedBarPosition(with animation: Bool) {
@ -61,8 +60,7 @@ open class BarView: UIView {
UIView.animate(withDuration: 0.3, animations: { [weak self] in
self?.selectedBar.frame = frame
})
}
else{
} else {
selectedBar.frame = frame
}
}

View File

@ -24,14 +24,13 @@
import Foundation
open class BaseButtonBarPagerTabStripViewController<ButtonBarCellType : UICollectionViewCell>: PagerTabStripViewController, PagerTabStripDataSource, PagerTabStripIsProgressiveDelegate, UICollectionViewDelegate, UICollectionViewDataSource {
open class BaseButtonBarPagerTabStripViewController<ButtonBarCellType: UICollectionViewCell>: PagerTabStripViewController, PagerTabStripDataSource, PagerTabStripIsProgressiveDelegate, UICollectionViewDelegate, UICollectionViewDataSource {
public var settings = ButtonBarPagerTabStripSettings()
public var buttonBarItemSpec: ButtonBarItemSpec<ButtonBarCellType>!
public var changeCurrentIndex: ((_ oldCell: ButtonBarCellType?, _ newCell: ButtonBarCellType?, _ animated: Bool) -> Void)?
public var changeCurrentIndexProgressive: ((_ oldCell: ButtonBarCellType?, _ newCell: ButtonBarCellType?, _ progressPercentage: CGFloat, _ changeCurrentIndex: Bool, _ animated: Bool) -> Void)?
@IBOutlet public weak var buttonBarView: ButtonBarView!
lazy private var cachedCellWidths: [CGFloat]? = { [unowned self] in
@ -55,7 +54,7 @@ open class BaseButtonBarPagerTabStripViewController<ButtonBarCellType : UICollec
let buttonBarViewAux = buttonBarView ?? {
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .horizontal
flowLayout.sectionInset = UIEdgeInsetsMake(0, settings.style.buttonBarLeftContentInset ?? 35, 0, settings.style.buttonBarRightContentInset ?? 35)
flowLayout.sectionInset = UIEdgeInsets(top: 0, left: settings.style.buttonBarLeftContentInset ?? 35, bottom: 0, right: settings.style.buttonBarRightContentInset ?? 35)
let buttonBarHeight = settings.style.buttonBarHeight ?? 44
let buttonBar = ButtonBarView(frame: CGRect(x: 0, y: 0, width: view.frame.size.width, height: buttonBarHeight), collectionViewLayout: flowLayout)
buttonBar.backgroundColor = .orange
@ -79,12 +78,12 @@ open class BaseButtonBarPagerTabStripViewController<ButtonBarCellType : UICollec
buttonBarView.dataSource = self
}
buttonBarView.scrollsToTop = false
let flowLayout = buttonBarView.collectionViewLayout as! UICollectionViewFlowLayout
let flowLayout = buttonBarView.collectionViewLayout as! UICollectionViewFlowLayout // swiftlint:disable:this force_cast
flowLayout.scrollDirection = .horizontal
flowLayout.minimumInteritemSpacing = settings.style.buttonBarMinimumInteritemSpacing ?? flowLayout.minimumInteritemSpacing
flowLayout.minimumLineSpacing = settings.style.buttonBarMinimumLineSpacing ?? flowLayout.minimumLineSpacing
let sectionInset = flowLayout.sectionInset
flowLayout.sectionInset = UIEdgeInsetsMake(sectionInset.top, settings.style.buttonBarLeftContentInset ?? sectionInset.left, sectionInset.bottom, settings.style.buttonBarRightContentInset ?? sectionInset.right)
flowLayout.sectionInset = UIEdgeInsets(top: sectionInset.top, left: settings.style.buttonBarLeftContentInset ?? sectionInset.left, bottom: sectionInset.bottom, right: settings.style.buttonBarRightContentInset ?? sectionInset.right)
buttonBarView.showsHorizontalScrollIndicator = false
buttonBarView.backgroundColor = settings.style.buttonBarBackgroundColor ?? buttonBarView.backgroundColor
buttonBarView.selectedBar.backgroundColor = settings.style.selectedBarBackgroundColor
@ -154,16 +153,14 @@ open class BaseButtonBarPagerTabStripViewController<ButtonBarCellType : UICollec
var numberOfLargeCells = 0
var totalWidthOfLargeCells: CGFloat = 0
for minimumCellWidthValue in minimumCellWidths {
if minimumCellWidthValue > suggestedStretchedCellWidth {
for minimumCellWidthValue in minimumCellWidths where minimumCellWidthValue > suggestedStretchedCellWidth {
totalWidthOfLargeCells += minimumCellWidthValue
numberOfLargeCells += 1
}
}
guard numberOfLargeCells > previousNumberOfLargeCells else { return suggestedStretchedCellWidth }
let flowLayout = buttonBarView.collectionViewLayout as! UICollectionViewFlowLayout
let flowLayout = buttonBarView.collectionViewLayout as! UICollectionViewFlowLayout // swiftlint:disable:this force_cast
let collectionViewAvailiableWidth = buttonBarView.frame.size.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right
let numberOfCells = minimumCellWidths.count
let cellSpacingTotal = CGFloat(numberOfCells - 1) * flowLayout.minimumLineSpacing
@ -216,8 +213,7 @@ open class BaseButtonBarPagerTabStripViewController<ButtonBarCellType : UICollec
if let changeCurrentIndexProgressive = changeCurrentIndexProgressive {
changeCurrentIndexProgressive(oldCell, newCell, 1, true, true)
}
}
else {
} else {
if let changeCurrentIndex = changeCurrentIndex {
changeCurrentIndex(oldCell, newCell, true)
}
@ -235,7 +231,7 @@ open class BaseButtonBarPagerTabStripViewController<ButtonBarCellType : UICollec
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) as? ButtonBarCellType else {
fatalError("UICollectionViewCell should be or extend from ButtonBarViewCell")
}
let childController = viewControllers[indexPath.item] as! IndicatorInfoProvider
let childController = viewControllers[indexPath.item] as! IndicatorInfoProvider // swiftlint:disable:this force_cast
let indicatorInfo = childController.indicatorInfo(for: self)
configure(cell: cell, for: indicatorInfo)
@ -244,8 +240,7 @@ open class BaseButtonBarPagerTabStripViewController<ButtonBarCellType : UICollec
if let changeCurrentIndexProgressive = changeCurrentIndexProgressive {
changeCurrentIndexProgressive(currentIndex == indexPath.item ? nil : cell, currentIndex == indexPath.item ? cell : nil, 1, true, false)
}
}
else {
} else {
if let changeCurrentIndex = changeCurrentIndex {
changeCurrentIndex(currentIndex == indexPath.item ? nil : cell, currentIndex == indexPath.item ? cell : nil, false)
}
@ -263,19 +258,19 @@ open class BaseButtonBarPagerTabStripViewController<ButtonBarCellType : UICollec
shouldUpdateButtonBarView = true
}
open func configure(cell: ButtonBarCellType, for indicatorInfo: IndicatorInfo){
open func configure(cell: ButtonBarCellType, for indicatorInfo: IndicatorInfo) {
fatalError("You must override this method to set up ButtonBarView cell accordingly")
}
private func calculateWidths() -> [CGFloat] {
let flowLayout = buttonBarView.collectionViewLayout as! UICollectionViewFlowLayout
let flowLayout = buttonBarView.collectionViewLayout as! UICollectionViewFlowLayout // swiftlint:disable:this force_cast
let numberOfCells = viewControllers.count
var minimumCellWidths = [CGFloat]()
var collectionViewContentWidth: CGFloat = 0
for viewController in viewControllers {
let childController = viewController as! IndicatorInfoProvider
let childController = viewController as! IndicatorInfoProvider // swiftlint:disable:this force_cast
let indicatorInfo = childController.indicatorInfo(for: self)
switch buttonBarItemSpec! {
case .cellClass(let widthCallback):
@ -296,8 +291,7 @@ open class BaseButtonBarPagerTabStripViewController<ButtonBarCellType : UICollec
if !settings.style.buttonBarItemsShouldFillAvailableWidth || collectionViewAvailableVisibleWidth < collectionViewContentWidth {
return minimumCellWidths
}
else {
} else {
let stretchedCellWidthIfAllEqual = (collectionViewAvailableVisibleWidth - cellSpacingTotal) / CGFloat(numberOfCells)
let generalMinimumCellWidth = calculateStretchedCellWidths(minimumCellWidths, suggestedStretchedCellWidth: stretchedCellWidthIfAllEqual, previousNumberOfLargeCells: 0)
var stretchedCellWidths = [CGFloat]()
@ -314,7 +308,6 @@ open class BaseButtonBarPagerTabStripViewController<ButtonBarCellType : UICollec
private var shouldUpdateButtonBarView = true
}
open class ExampleBaseButtonBarPagerTabStripViewController: BaseButtonBarPagerTabStripViewController<ButtonBarViewCell> {
public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
@ -327,8 +320,8 @@ open class ExampleBaseButtonBarPagerTabStripViewController: BaseButtonBarPagerTa
initialize()
}
open func initialize(){
buttonBarItemSpec = .nibFile(nibName: "ButtonCell", bundle: Bundle(for: ButtonBarViewCell.self), width:{ [weak self] (childItemInfo) -> CGFloat in
open func initialize() {
buttonBarItemSpec = .nibFile(nibName: "ButtonCell", bundle: Bundle(for: ButtonBarViewCell.self), width: { [weak self] (childItemInfo) -> CGFloat in
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = self?.settings.style.buttonBarItemFont ?? label.font
@ -338,7 +331,7 @@ open class ExampleBaseButtonBarPagerTabStripViewController: BaseButtonBarPagerTa
})
}
open override func configure(cell: ButtonBarViewCell, for indicatorInfo: IndicatorInfo){
open override func configure(cell: ButtonBarViewCell, for indicatorInfo: IndicatorInfo) {
cell.label.text = indicatorInfo.title
if let image = indicatorInfo.image {
cell.imageView.image = image

View File

@ -101,7 +101,7 @@ open class ButtonBarPagerTabStripViewController: PagerTabStripViewController, Pa
open override func viewDidLoad() {
super.viewDidLoad()
buttonBarItemSpec = .nibFile(nibName: "ButtonCell", bundle: Bundle(for: ButtonBarViewCell.self), width:{ [weak self] (childItemInfo) -> CGFloat in
buttonBarItemSpec = .nibFile(nibName: "ButtonCell", bundle: Bundle(for: ButtonBarViewCell.self), width: { [weak self] (childItemInfo) -> CGFloat in
let label = UILabel()
label.translatesAutoresizingMaskIntoConstraints = false
label.font = self?.settings.style.buttonBarItemFont
@ -110,7 +110,6 @@ open class ButtonBarPagerTabStripViewController: PagerTabStripViewController, Pa
return labelSize.width + (self?.settings.style.buttonBarItemLeftRightMargin ?? 8) * 2
})
let buttonBarViewAux = buttonBarView ?? {
let flowLayout = UICollectionViewFlowLayout()
flowLayout.scrollDirection = .horizontal
@ -137,12 +136,12 @@ open class ButtonBarPagerTabStripViewController: PagerTabStripViewController, Pa
buttonBarView.dataSource = self
}
buttonBarView.scrollsToTop = false
let flowLayout = buttonBarView.collectionViewLayout as! UICollectionViewFlowLayout
let flowLayout = buttonBarView.collectionViewLayout as! UICollectionViewFlowLayout // swiftlint:disable:this force_cast
flowLayout.scrollDirection = .horizontal
flowLayout.minimumInteritemSpacing = settings.style.buttonBarMinimumInteritemSpacing ?? flowLayout.minimumInteritemSpacing
flowLayout.minimumLineSpacing = settings.style.buttonBarMinimumLineSpacing ?? flowLayout.minimumLineSpacing
let sectionInset = flowLayout.sectionInset
flowLayout.sectionInset = UIEdgeInsetsMake(sectionInset.top, settings.style.buttonBarLeftContentInset ?? sectionInset.left, sectionInset.bottom, settings.style.buttonBarRightContentInset ?? sectionInset.right)
flowLayout.sectionInset = UIEdgeInsets(top: sectionInset.top, left: settings.style.buttonBarLeftContentInset ?? sectionInset.left, bottom: sectionInset.bottom, right: settings.style.buttonBarRightContentInset ?? sectionInset.right)
buttonBarView.showsHorizontalScrollIndicator = false
buttonBarView.backgroundColor = settings.style.buttonBarBackgroundColor ?? buttonBarView.backgroundColor
@ -210,7 +209,7 @@ open class ButtonBarPagerTabStripViewController: PagerTabStripViewController, Pa
guard numberOfLargeCells > previousNumberOfLargeCells else { return suggestedStretchedCellWidth }
let flowLayout = buttonBarView.collectionViewLayout as! UICollectionViewFlowLayout
let flowLayout = buttonBarView.collectionViewLayout as! UICollectionViewFlowLayout // swiftlint:disable:this force_cast
let collectionViewAvailiableWidth = buttonBarView.frame.size.width - flowLayout.sectionInset.left - flowLayout.sectionInset.right
let numberOfCells = minimumCellWidths.count
let cellSpacingTotal = CGFloat(numberOfCells - 1) * flowLayout.minimumLineSpacing
@ -266,7 +265,7 @@ open class ButtonBarPagerTabStripViewController: PagerTabStripViewController, Pa
return cells
}
// MARK: - UICollectionViewDelegateFlowLayout
// MARK: - UICollectionViewDelegateFlowLayut
open func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
guard let cellWidthValue = cachedCellWidths?[indexPath.row] else {
@ -290,8 +289,7 @@ open class ButtonBarPagerTabStripViewController: PagerTabStripViewController, Pa
if let changeCurrentIndexProgressive = changeCurrentIndexProgressive {
changeCurrentIndexProgressive(cells.first!, cells.last!, 1, true, true)
}
}
else {
} else {
if let changeCurrentIndex = changeCurrentIndex {
changeCurrentIndex(cells.first!, cells.last!, true)
}
@ -312,7 +310,7 @@ open class ButtonBarPagerTabStripViewController: PagerTabStripViewController, Pa
collectionViewDidLoad = true
let childController = viewControllers[indexPath.item] as! IndicatorInfoProvider
let childController = viewControllers[indexPath.item] as! IndicatorInfoProvider // swiftlint:disable:this force_cast
let indicatorInfo = childController.indicatorInfo(for: self)
cell.label.text = indicatorInfo.title
@ -333,8 +331,7 @@ open class ButtonBarPagerTabStripViewController: PagerTabStripViewController, Pa
if let changeCurrentIndexProgressive = changeCurrentIndexProgressive {
changeCurrentIndexProgressive(currentIndex == indexPath.item ? nil : cell, currentIndex == indexPath.item ? cell : nil, 1, true, false)
}
}
else {
} else {
if let changeCurrentIndex = changeCurrentIndex {
changeCurrentIndex(currentIndex == indexPath.item ? nil : cell, currentIndex == indexPath.item ? cell : nil, false)
}
@ -351,18 +348,18 @@ open class ButtonBarPagerTabStripViewController: PagerTabStripViewController, Pa
shouldUpdateButtonBarView = true
}
open func configureCell(_ cell: ButtonBarViewCell, indicatorInfo: IndicatorInfo){
open func configureCell(_ cell: ButtonBarViewCell, indicatorInfo: IndicatorInfo) {
}
private func calculateWidths() -> [CGFloat] {
let flowLayout = buttonBarView.collectionViewLayout as! UICollectionViewFlowLayout
let flowLayout = buttonBarView.collectionViewLayout as! UICollectionViewFlowLayout // swiftlint:disable:this force_cast
let numberOfCells = viewControllers.count
var minimumCellWidths = [CGFloat]()
var collectionViewContentWidth: CGFloat = 0
for viewController in viewControllers {
let childController = viewController as! IndicatorInfoProvider
let childController = viewController as! IndicatorInfoProvider // swiftlint:disable:this force_cast
let indicatorInfo = childController.indicatorInfo(for: self)
switch buttonBarItemSpec! {
case .cellClass(let widthCallback):
@ -383,8 +380,7 @@ open class ButtonBarPagerTabStripViewController: PagerTabStripViewController, Pa
if !settings.style.buttonBarItemsShouldFillAvailableWidth || collectionViewAvailableVisibleWidth < collectionViewContentWidth {
return minimumCellWidths
}
else {
} else {
let stretchedCellWidthIfAllEqual = (collectionViewAvailableVisibleWidth - cellSpacingTotal) / CGFloat(numberOfCells)
let generalMinimumCellWidth = calculateStretchedCellWidths(minimumCellWidths, suggestedStretchedCellWidth: stretchedCellWidthIfAllEqual, previousNumberOfLargeCells: 0)
var stretchedCellWidths = [CGFloat]()

View File

@ -75,7 +75,7 @@ open class ButtonBarView: UICollectionView {
updateSelectedBarPosition(animated, swipeDirection: swipeDirection, pagerScroll: pagerScroll)
}
open func move(fromIndex: Int, toIndex: Int, progressPercentage: CGFloat,pagerScroll: PagerScroll) {
open func move(fromIndex: Int, toIndex: Int, progressPercentage: CGFloat, pagerScroll: PagerScroll) {
selectedIndex = progressPercentage > 0.5 ? toIndex : fromIndex
let fromFrame = layoutAttributesForItem(at: IndexPath(item: fromIndex, section: 0))!.frame
@ -87,13 +87,11 @@ open class ButtonBarView: UICollectionView {
if toIndex < 0 {
let cellAtts = layoutAttributesForItem(at: IndexPath(item: 0, section: 0))
toFrame = cellAtts!.frame.offsetBy(dx: -cellAtts!.frame.size.width, dy: 0)
}
else {
} else {
let cellAtts = layoutAttributesForItem(at: IndexPath(item: (numberOfItems - 1), section: 0))
toFrame = cellAtts!.frame.offsetBy(dx: cellAtts!.frame.size.width, dy: 0)
}
}
else {
} else {
toFrame = layoutAttributesForItem(at: IndexPath(item: toIndex, section: 0))!.frame
}
@ -115,7 +113,7 @@ open class ButtonBarView: UICollectionView {
setContentOffset(CGPoint(x: targetContentOffset, y: 0), animated: false)
}
open func updateSelectedBarPosition(_ animated: Bool, swipeDirection: SwipeDirection, pagerScroll: PagerScroll) -> Void {
open func updateSelectedBarPosition(_ animated: Bool, swipeDirection: SwipeDirection, pagerScroll: PagerScroll) {
var selectedBarFrame = selectedBar.frame
let selectedCellIndexPath = IndexPath(item: selectedIndex, section: 0)
@ -131,22 +129,21 @@ open class ButtonBarView: UICollectionView {
UIView.animate(withDuration: 0.3, animations: { [weak self] in
self?.selectedBar.frame = selectedBarFrame
})
}
else {
} else {
selectedBar.frame = selectedBarFrame
}
}
// MARK: - Helpers
private func updateContentOffset(animated: Bool, pagerScroll: PagerScroll, toFrame: CGRect, toIndex: Int) -> Void {
private func updateContentOffset(animated: Bool, pagerScroll: PagerScroll, toFrame: CGRect, toIndex: Int) {
guard pagerScroll != .no || (pagerScroll != .scrollOnlyIfOutOfScreen && (toFrame.origin.x < contentOffset.x || toFrame.origin.x >= (contentOffset.x + frame.size.width - contentInset.left))) else { return }
let targetContentOffset = contentSize.width > frame.size.width ? contentOffsetForCell(withFrame: toFrame, andIndex: toIndex) : 0
setContentOffset(CGPoint(x: targetContentOffset, y: 0), animated: animated)
}
private func contentOffsetForCell(withFrame cellFrame: CGRect, andIndex index: Int) -> CGFloat {
let sectionInset = (collectionViewLayout as! UICollectionViewFlowLayout).sectionInset
let sectionInset = (collectionViewLayout as! UICollectionViewFlowLayout).sectionInset // swiftlint:disable:this force_cast
var alignmentOffset: CGFloat = 0.0
switch selectedBarAlignment {

View File

@ -47,18 +47,17 @@ public struct IndicatorInfo {
}
extension IndicatorInfo : ExpressibleByStringLiteral {
public init(stringLiteral value: String){
public init(stringLiteral value: String) {
title = value
}
public init(extendedGraphemeClusterLiteral value: String){
public init(extendedGraphemeClusterLiteral value: String) {
title = value
}
public init(unicodeScalarLiteral value: String){
public init(unicodeScalarLiteral value: String) {
title = value
}
}

View File

@ -38,7 +38,6 @@ public enum PagerTabStripBehaviour {
}
}
public var isProgressiveIndicator: Bool {
switch self {
case .common(_):
@ -57,7 +56,3 @@ public enum PagerTabStripBehaviour {
}
}
}

View File

@ -25,5 +25,7 @@
import Foundation
public enum PagerTabStripError: Error {
case viewControllerNotContainedInPagerTabStrip
case viewControllerOutOfBounds
}

View File

@ -22,15 +22,14 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import Foundation
// MARK: Protocols
public protocol IndicatorInfoProvider {
func indicatorInfo(for pagerTabStripController: PagerTabStripViewController) -> IndicatorInfo
}
public protocol PagerTabStripDelegate: class {
@ -38,7 +37,7 @@ public protocol PagerTabStripDelegate: class {
func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int)
}
public protocol PagerTabStripIsProgressiveDelegate : PagerTabStripDelegate {
public protocol PagerTabStripIsProgressiveDelegate: PagerTabStripDelegate {
func updateIndicator(for viewController: PagerTabStripViewController, fromIndex: Int, toIndex: Int, withProgressPercentage progressPercentage: CGFloat, indexWasChanged: Bool)
}
@ -48,8 +47,7 @@ public protocol PagerTabStripDataSource: class {
func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController]
}
//MARK: PagerTabStripViewController
// MARK: PagerTabStripViewController
open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate {
@ -79,8 +77,7 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate {
open var swipeDirection: SwipeDirection {
if containerView.contentOffset.x > lastContentOffset {
return .left
}
else if containerView.contentOffset.x < lastContentOffset {
} else if containerView.contentOffset.x < lastContentOffset {
return .right
}
return .none
@ -142,7 +139,7 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate {
childViewControllers.forEach { $0.endAppearanceTransition() }
}
override open func viewDidLayoutSubviews(){
override open func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
updateIfNeeded()
}
@ -168,8 +165,7 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate {
containerView.setContentOffset(CGPoint(x: pageOffsetForChild(at: fromIndex), y: 0), animated: false)
(navigationController?.view ?? view).isUserInteractionEnabled = !animated
containerView.setContentOffset(CGPoint(x: pageOffsetForChild(at: index), y: 0), animated: true)
}
else {
} else {
(navigationController?.view ?? view).isUserInteractionEnabled = !animated
containerView.setContentOffset(CGPoint(x: pageOffsetForChild(at: index), y: 0), animated: animated)
}
@ -179,17 +175,17 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate {
moveToViewController(at: viewControllers.index(of: viewController)!, animated: animated)
}
//MARK: - PagerTabStripDataSource
// MARK: - PagerTabStripDataSource
open func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
assertionFailure("Sub-class must implement the PagerTabStripDataSource viewControllers(for:) method")
return []
}
//MARK: - Helpers
// MARK: - Helpers
open func updateIfNeeded() {
if isViewLoaded && !lastSize.equalTo(containerView.bounds.size){
if isViewLoaded && !lastSize.equalTo(containerView.bounds.size) {
updateContent()
}
}
@ -202,13 +198,13 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate {
return CGFloat(index) * containerView.bounds.width
}
open func offsetForChild(at index: Int) -> CGFloat{
open func offsetForChild(at index: Int) -> CGFloat {
return (CGFloat(index) * containerView.bounds.width) + ((containerView.bounds.width - view.bounds.width) * 0.5)
}
open func offsetForChild(viewController: UIViewController) throws -> CGFloat{
open func offsetForChild(viewController: UIViewController) throws -> CGFloat {
guard let index = viewControllers.index(of: viewController) else {
throw PagerTabStripError.viewControllerNotContainedInPagerTabStrip
throw PagerTabStripError.viewControllerOutOfBounds
}
return offsetForChild(at: index)
}
@ -222,7 +218,7 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate {
return Int((contentOffset + 1.5 * pageWidth) / pageWidth) - 1
}
open func pageFor(virtualPage: Int) -> Int{
open func pageFor(virtualPage: Int) -> Int {
if virtualPage < 0 {
return 0
}
@ -245,11 +241,10 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate {
for (index, childController) in pagerViewControllers.enumerated() {
let pageOffsetForChild = self.pageOffsetForChild(at: index)
if fabs(containerView.contentOffset.x - pageOffsetForChild) < containerView.bounds.width {
if let _ = childController.parent {
if childController.parent != nil {
childController.view.frame = CGRect(x: offsetForChild(at: index), y: 0, width: view.bounds.width, height: containerView.bounds.height)
childController.view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
}
else {
} else {
childController.beginAppearanceTransition(true, animated: false)
addChildViewController(childController)
childController.view.frame = CGRect(x: offsetForChild(at: index), y: 0, width: view.bounds.width, height: containerView.bounds.height)
@ -258,9 +253,8 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate {
childController.didMove(toParentViewController: self)
childController.endAppearanceTransition()
}
}
else {
if let _ = childController.parent {
} else {
if childController.parent != nil {
childController.beginAppearanceTransition(false, animated: false)
childController.willMove(toParentViewController: nil)
childController.view.removeFromSuperview()
@ -281,23 +275,20 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate {
let (fromIndex, toIndex, scrollPercentage) = progressiveIndicatorData(virtualPage)
progressiveDeledate.updateIndicator(for: self, fromIndex: fromIndex, toIndex: toIndex, withProgressPercentage: scrollPercentage, indexWasChanged: changeCurrentIndex)
}
else{
} else {
delegate?.updateIndicator(for: self, fromIndex: min(oldCurrentIndex, pagerViewControllers.count - 1), toIndex: newCurrentIndex)
}
}
open func reloadPagerTabStripView() {
guard isViewLoaded else { return }
for childController in viewControllers {
if let _ = childController.parent {
for childController in viewControllers where childController.parent != nil {
childController.beginAppearanceTransition(false, animated: false)
childController.willMove(toParentViewController: nil)
childController.view.removeFromSuperview()
childController.removeFromParentViewController()
childController.endAppearanceTransition()
}
}
reloadViewControllers()
containerView.contentSize = CGSize(width: containerView.bounds.width * CGFloat(viewControllers.count), height: containerView.contentSize.height)
if currentIndex >= viewControllers.count {
@ -308,7 +299,7 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate {
updateContent()
}
//MARK: - UIScrollDelegate
// MARK: - UIScrollDelegate
open func scrollViewDidScroll(_ scrollView: UIScrollView) {
if containerView == scrollView {
@ -331,7 +322,7 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate {
}
}
//MARK: - Orientation
// MARK: - Orientation
open override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
@ -346,8 +337,7 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate {
}
}
//MARK: Private
// MARK: Private
private func progressiveIndicatorData(_ virtualPage: Int) -> (Int, Int, CGFloat) {
let count = viewControllers.count
@ -359,26 +349,21 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate {
if virtualPage > count - 1 {
fromIndex = count - 1
toIndex = count
}
else {
} else {
if self.scrollPercentage >= 0.5 {
fromIndex = max(toIndex - 1, 0)
}
else {
} else {
toIndex = fromIndex + 1
}
}
}
else if direction == .right {
} else if direction == .right {
if virtualPage < 0 {
fromIndex = 0
toIndex = -1
}
else {
} else {
if self.scrollPercentage > 0.5 {
fromIndex = min(toIndex + 1, count - 1)
}
else {
} else {
toIndex = fromIndex - 1
}
}
@ -387,20 +372,20 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate {
return (fromIndex, toIndex, scrollPercentage)
}
private func reloadViewControllers(){
private func reloadViewControllers() {
guard let dataSource = datasource else {
fatalError("dataSource must not be nil")
}
viewControllers = dataSource.viewControllers(for: self)
// viewControllers
guard viewControllers.count != 0 else {
guard !viewControllers.isEmpty else {
fatalError("viewControllers(for:) should provide at least one child view controller")
}
viewControllers.forEach { if !($0 is IndicatorInfoProvider) { fatalError("Every view controller provided by PagerTabStripDataSource's viewControllers(for:) method must conform to InfoProvider") }}
}
private var pagerTabStripChildViewControllersForScrolling : [UIViewController]?
private var pagerTabStripChildViewControllersForScrolling: [UIViewController]?
private var lastPageNumber = 0
private var lastContentOffset: CGFloat = 0.0
private var pageBeforeRotate = 0

View File

@ -33,7 +33,6 @@ public struct SegmentedPagerTabStripSettings {
public var style = Style()
}
open class SegmentedPagerTabStripViewController: PagerTabStripViewController, PagerTabStripDataSource, PagerTabStripDelegate {
@IBOutlet weak public var segmentedControl: UISegmentedControl!
@ -76,12 +75,11 @@ open class SegmentedPagerTabStripViewController: PagerTabStripViewController, Pa
func reloadSegmentedControl() {
segmentedControl.removeAllSegments()
for (index, item) in viewControllers.enumerated(){
let child = item as! IndicatorInfoProvider
for (index, item) in viewControllers.enumerated() {
let child = item as! IndicatorInfoProvider // swiftlint:disable:this force_cast
if let image = child.indicatorInfo(for: self).image {
segmentedControl.insertSegment(with: image, at: index, animated: false)
}
else {
} else {
segmentedControl.insertSegment(withTitle: child.indicatorInfo(for: self).title, at: index, animated: false)
}
}

View File

@ -91,11 +91,9 @@ open class TwitterPagerTabStripViewController: PagerTabStripViewController, Page
var xOffset: CGFloat = 0
if fromIndex < toIndex {
xOffset = distance * CGFloat(fromIndex) + distance * progressPercentage
}
else if fromIndex > toIndex {
} else if fromIndex > toIndex {
xOffset = distance * CGFloat(fromIndex) - distance * progressPercentage
}
else {
} else {
xOffset = distance * CGFloat(fromIndex)
}
@ -175,9 +173,9 @@ open class TwitterPagerTabStripViewController: PagerTabStripViewController, Page
childTitleLabels.forEach { $0.removeFromSuperview() }
childTitleLabels.removeAll()
for (index, item) in viewControllers.enumerated() {
let child = item as! IndicatorInfoProvider
let child = item as! IndicatorInfoProvider // swiftlint:disable:this force_cast
let indicatorInfo = child.indicatorInfo(for: self)
let navTitleLabel : UILabel = {
let navTitleLabel: UILabel = {
let label = UILabel()
label.text = indicatorInfo.title
label.font = UIApplication.shared.statusBarOrientation.isPortrait ? settings.style.portraitTitleFont : settings.style.landscapeTitleFont
@ -223,8 +221,7 @@ open class TwitterPagerTabStripViewController: PagerTabStripViewController, Page
label.alpha = {
if offset < distance * CGFloat(index) {
return (offset - distance * CGFloat(index - 1)) / distance
}
else {
} else {
return 1 - ((offset - distance * CGFloat(index)) / distance)
}
}()

View File

@ -192,6 +192,7 @@
28F828791C494B2C00330CF4 /* Frameworks */,
28F8287A1C494B2C00330CF4 /* Headers */,
28F8287B1C494B2C00330CF4 /* Resources */,
CB25B8F81EBCF0CE00FEB0A2 /* SwiftLint */,
);
buildRules = (
);
@ -275,6 +276,23 @@
};
/* End PBXResourcesBuildPhase section */
/* Begin PBXShellScriptBuildPhase section */
CB25B8F81EBCF0CE00FEB0A2 /* SwiftLint */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
);
name = SwiftLint;
outputPaths = (
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "if which swiftlint >/dev/null; then\n swiftlint\nelse\n echo \"warning: SwiftLint not installed, download from https://github.com/realm/SwiftLint\"\nfi";
};
/* End PBXShellScriptBuildPhase section */
/* Begin PBXSourcesBuildPhase section */
28F828781C494B2C00330CF4 /* Sources */ = {
isa = PBXSourcesBuildPhase;