Skip to content

Commit

Permalink
Fixed SwiftLint warnings with some minor refactors.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajsecord committed Jan 10, 2017
1 parent 12a6781 commit 2c19802
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 109 deletions.
1 change: 1 addition & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ excluded:

disabled_rules:
- cyclomatic_complexity
- todo

line_length: 100

Expand Down
3 changes: 2 additions & 1 deletion catalog/MDCCatalog/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
func application(_ application: UIApplication, didFinishLaunchingWithOptions
launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
self.window = MDCCatalogWindow(frame: UIScreen.main.bounds)

let tree = CBCCreateNavigationTree()
Expand Down
103 changes: 61 additions & 42 deletions catalog/MDCCatalog/MDCCatalogComponentsController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ class MDCCatalogComponentsController: UICollectionViewController, MDCInkTouchCon

let layout = UICollectionViewFlowLayout()
let sectionInset: CGFloat = spacing
layout.sectionInset = UIEdgeInsets(top: sectionInset, left: sectionInset, bottom: sectionInset, right: sectionInset)
layout.sectionInset = UIEdgeInsets(top: sectionInset,
left: sectionInset,
bottom: sectionInset,
right: sectionInset)
layout.minimumInteritemSpacing = spacing
layout.minimumLineSpacing = spacing

Expand Down Expand Up @@ -102,41 +105,10 @@ class MDCCatalogComponentsController: UICollectionViewController, MDCInkTouchCon
titleLabel.translatesAutoresizingMaskIntoConstraints = false

containerView.addSubview(titleLabel)
_ = NSLayoutConstraint(
item: titleLabel,
attribute: .leading,
relatedBy: .equal,
toItem: containerView,
attribute: .leading,
multiplier: 1.0,
constant: titleInsets.left).isActive = true

_ = NSLayoutConstraint(
item: titleLabel,
attribute: .trailing,
relatedBy: .equal,
toItem: containerView,
attribute: .trailing,
multiplier: 1.0,
constant: 0).isActive = true

_ = NSLayoutConstraint(
item: titleLabel,
attribute: .bottom,
relatedBy: .equal,
toItem: containerView,
attribute: .bottom,
multiplier: 1.0,
constant: -titleInsets.bottom).isActive = true

_ = NSLayoutConstraint(
item: titleLabel,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1.0,
constant: titleSize.height).isActive = true
constrainLabel(label: titleLabel,
containerView: containerView,
insets: titleInsets,
height: titleSize.height)

self.headerViewController.headerView.addSubview(containerView)

Expand Down Expand Up @@ -183,11 +155,13 @@ class MDCCatalogComponentsController: UICollectionViewController, MDCInkTouchCon

// MARK: MDCInkTouchControllerDelegate

func inkTouchController(_ inkTouchController: MDCInkTouchController, shouldProcessInkTouchesAtTouchLocation location: CGPoint) -> Bool {
func inkTouchController(_ inkTouchController: MDCInkTouchController,
shouldProcessInkTouchesAtTouchLocation location: CGPoint) -> Bool {
return self.collectionView!.indexPathForItem(at: location) != nil
}

func inkTouchController(_ inkTouchController: MDCInkTouchController, inkViewAtTouchLocation location: CGPoint) -> MDCInkView {
func inkTouchController(_ inkTouchController: MDCInkTouchController,
inkViewAtTouchLocation location: CGPoint) -> MDCInkView {
if let indexPath = self.collectionView!.indexPathForItem(at: location) {
let cell = self.collectionView!.cellForItem(at: indexPath)
return self.inkViewForView(cell!)
Expand All @@ -197,9 +171,11 @@ class MDCCatalogComponentsController: UICollectionViewController, MDCInkTouchCon

// MARK: UICollectionViewDelegate

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MDCCatalogCollectionViewCell",
for: indexPath)
override func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell =
collectionView.dequeueReusableCell(withReuseIdentifier: "MDCCatalogCollectionViewCell",
for: indexPath)
cell.backgroundColor = UIColor.white

let componentName = self.node.children[indexPath.row].title
Expand All @@ -213,7 +189,9 @@ class MDCCatalogComponentsController: UICollectionViewController, MDCInkTouchCon
return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
let pad = CGFloat(1)
var cellWidth = (self.view.frame.size.width - 3 * pad) / 2
if self.view.frame.size.width > self.view.frame.size.height {
Expand All @@ -234,6 +212,47 @@ class MDCCatalogComponentsController: UICollectionViewController, MDCInkTouchCon
self.navigationController?.pushViewController(vc, animated: true)
}

// MARK: Private
func constrainLabel(label: UILabel,
containerView: UIView,
insets: UIEdgeInsets,
height: CGFloat) {
_ = NSLayoutConstraint(
item: label,
attribute: .leading,
relatedBy: .equal,
toItem: containerView,
attribute: .leading,
multiplier: 1.0,
constant: insets.left).isActive = true

_ = NSLayoutConstraint(
item: label,
attribute: .trailing,
relatedBy: .equal,
toItem: containerView,
attribute: .trailing,
multiplier: 1.0,
constant: 0).isActive = true

_ = NSLayoutConstraint(
item: label,
attribute: .bottom,
relatedBy: .equal,
toItem: containerView,
attribute: .bottom,
multiplier: 1.0,
constant: -insets.bottom).isActive = true

_ = NSLayoutConstraint(
item: label,
attribute: .height,
relatedBy: .equal,
toItem: nil,
attribute: .notAnAttribute,
multiplier: 1.0,
constant: height).isActive = true
}
}

// UIScrollViewDelegate
Expand Down
6 changes: 5 additions & 1 deletion catalog/MDCCatalog/MDCCatalogTileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class MDCCatalogTileView: UIView {
let pixelSize = CGSize(width: frame.width * scale, height: frame.height * scale)
let cachedPixelSize = CGSize(width: cachedImage.size.width * cachedImage.scale,
height: cachedImage.size.height * cachedImage.scale)
if (cachedPixelSize != pixelSize) {
if cachedPixelSize != pixelSize {
return createImage()
}
return cachedImage
Expand All @@ -61,6 +61,9 @@ class MDCCatalogTileView: UIView {
}
}

// This function is long but simple. The name-to-drawing map would be better replaced by a real
// dictionary, but Swift's dictionaries can't seem to handle C function pointers.
// swiftlint:disable function_body_length
func createImage() -> UIImage {
var newImage = UIImage()

Expand Down Expand Up @@ -122,5 +125,6 @@ class MDCCatalogTileView: UIView {
imageCache.setObject(newImage, forKey: componentNameString as AnyObject)
return newImage
}
// swiftlint:enable function_body_length

}
Loading

0 comments on commit 2c19802

Please sign in to comment.