Skip to content

Commit 19a099e

Browse files
ajsecordrandallli
authored andcommitted
Fix SwiftLint warnings (#1149)
* Exclude catalog/third_party from SwiftLint. * Added line length to .swiftlint.yml * Fixed SwiftLint warnings with some minor refactors.
1 parent d121f77 commit 19a099e

File tree

5 files changed

+160
-109
lines changed

5 files changed

+160
-109
lines changed

.swiftlint.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ excluded:
22
- Carthage
33
- Pods
44
- third_party
5+
- catalog/third_party
56

67
disabled_rules:
78
- cyclomatic_complexity
9+
- todo
10+
11+
line_length: 100
812

913
force_cast: warning
1014
force_try: warning

catalog/MDCCatalog/AppDelegate.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
2323

2424
var window: UIWindow?
2525

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

2930
let tree = CBCCreateNavigationTree()

catalog/MDCCatalog/MDCCatalogComponentsController.swift

Lines changed: 61 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ class MDCCatalogComponentsController: UICollectionViewController, MDCInkTouchCon
4444

4545
let layout = UICollectionViewFlowLayout()
4646
let sectionInset: CGFloat = spacing
47-
layout.sectionInset = UIEdgeInsets(top: sectionInset, left: sectionInset, bottom: sectionInset, right: sectionInset)
47+
layout.sectionInset = UIEdgeInsets(top: sectionInset,
48+
left: sectionInset,
49+
bottom: sectionInset,
50+
right: sectionInset)
4851
layout.minimumInteritemSpacing = spacing
4952
layout.minimumLineSpacing = spacing
5053

@@ -102,41 +105,10 @@ class MDCCatalogComponentsController: UICollectionViewController, MDCInkTouchCon
102105
titleLabel.translatesAutoresizingMaskIntoConstraints = false
103106

104107
containerView.addSubview(titleLabel)
105-
_ = NSLayoutConstraint(
106-
item: titleLabel,
107-
attribute: .leading,
108-
relatedBy: .equal,
109-
toItem: containerView,
110-
attribute: .leading,
111-
multiplier: 1.0,
112-
constant: titleInsets.left).isActive = true
113-
114-
_ = NSLayoutConstraint(
115-
item: titleLabel,
116-
attribute: .trailing,
117-
relatedBy: .equal,
118-
toItem: containerView,
119-
attribute: .trailing,
120-
multiplier: 1.0,
121-
constant: 0).isActive = true
122-
123-
_ = NSLayoutConstraint(
124-
item: titleLabel,
125-
attribute: .bottom,
126-
relatedBy: .equal,
127-
toItem: containerView,
128-
attribute: .bottom,
129-
multiplier: 1.0,
130-
constant: -titleInsets.bottom).isActive = true
131-
132-
_ = NSLayoutConstraint(
133-
item: titleLabel,
134-
attribute: .height,
135-
relatedBy: .equal,
136-
toItem: nil,
137-
attribute: .notAnAttribute,
138-
multiplier: 1.0,
139-
constant: titleSize.height).isActive = true
108+
constrainLabel(label: titleLabel,
109+
containerView: containerView,
110+
insets: titleInsets,
111+
height: titleSize.height)
140112

141113
self.headerViewController.headerView.addSubview(containerView)
142114

@@ -183,11 +155,13 @@ class MDCCatalogComponentsController: UICollectionViewController, MDCInkTouchCon
183155

184156
// MARK: MDCInkTouchControllerDelegate
185157

186-
func inkTouchController(_ inkTouchController: MDCInkTouchController, shouldProcessInkTouchesAtTouchLocation location: CGPoint) -> Bool {
158+
func inkTouchController(_ inkTouchController: MDCInkTouchController,
159+
shouldProcessInkTouchesAtTouchLocation location: CGPoint) -> Bool {
187160
return self.collectionView!.indexPathForItem(at: location) != nil
188161
}
189162

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

198172
// MARK: UICollectionViewDelegate
199173

200-
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
201-
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MDCCatalogCollectionViewCell",
202-
for: indexPath)
174+
override func collectionView(_ collectionView: UICollectionView,
175+
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
176+
let cell =
177+
collectionView.dequeueReusableCell(withReuseIdentifier: "MDCCatalogCollectionViewCell",
178+
for: indexPath)
203179
cell.backgroundColor = UIColor.white
204180

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

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

215+
// MARK: Private
216+
func constrainLabel(label: UILabel,
217+
containerView: UIView,
218+
insets: UIEdgeInsets,
219+
height: CGFloat) {
220+
_ = NSLayoutConstraint(
221+
item: label,
222+
attribute: .leading,
223+
relatedBy: .equal,
224+
toItem: containerView,
225+
attribute: .leading,
226+
multiplier: 1.0,
227+
constant: insets.left).isActive = true
228+
229+
_ = NSLayoutConstraint(
230+
item: label,
231+
attribute: .trailing,
232+
relatedBy: .equal,
233+
toItem: containerView,
234+
attribute: .trailing,
235+
multiplier: 1.0,
236+
constant: 0).isActive = true
237+
238+
_ = NSLayoutConstraint(
239+
item: label,
240+
attribute: .bottom,
241+
relatedBy: .equal,
242+
toItem: containerView,
243+
attribute: .bottom,
244+
multiplier: 1.0,
245+
constant: -insets.bottom).isActive = true
246+
247+
_ = NSLayoutConstraint(
248+
item: label,
249+
attribute: .height,
250+
relatedBy: .equal,
251+
toItem: nil,
252+
attribute: .notAnAttribute,
253+
multiplier: 1.0,
254+
constant: height).isActive = true
255+
}
237256
}
238257

239258
// UIScrollViewDelegate

catalog/MDCCatalog/MDCCatalogTileView.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class MDCCatalogTileView: UIView {
5252
let pixelSize = CGSize(width: frame.width * scale, height: frame.height * scale)
5353
let cachedPixelSize = CGSize(width: cachedImage.size.width * cachedImage.scale,
5454
height: cachedImage.size.height * cachedImage.scale)
55-
if (cachedPixelSize != pixelSize) {
55+
if cachedPixelSize != pixelSize {
5656
return createImage()
5757
}
5858
return cachedImage
@@ -61,6 +61,9 @@ class MDCCatalogTileView: UIView {
6161
}
6262
}
6363

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

@@ -122,5 +125,6 @@ class MDCCatalogTileView: UIView {
122125
imageCache.setObject(newImage, forKey: componentNameString as AnyObject)
123126
return newImage
124127
}
128+
// swiftlint:enable function_body_length
125129

126130
}

0 commit comments

Comments
 (0)