-
Notifications
You must be signed in to change notification settings - Fork 0
/
Report.swift
334 lines (306 loc) · 15.5 KB
/
Report.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// QAReport.swift
//
// Created by Quinn McHenry on 6/1/20.
import PDFKit
import UIKit
class Report {
let elements: [Element]
let findings: [Finding]
let context: LintingContext
let pdfTitle = "UILint Report"
let padding = CGFloat(10)
let paddingLarge = CGFloat(20)
var currentY = CGFloat(0)
var screenshot: UIImage? { context.screenshot }
public init(elements: [Element], findings: [Finding], details: LintingContext ) {
self.elements = elements
self.findings = findings
self.context = details
}
var pdfMetadata: [AnyHashable: Any] {
[kCGPDFContextCreator: "UILint", kCGPDFContextTitle: pdfTitle]
}
public func makePDF() -> Data {
let pdfData = NSMutableData()
UIGraphicsBeginPDFContextToData(pdfData, CGRect.zero, pdfMetadata)
drawFirstPage()
newPage("Findings")
if findings.isEmpty {
draw("No findings", attributes: body)
}
findings.forEach { finding in
let height = draw(finding, draw: false)
drawRule()
if currentY + height + 5 > pageSize.height {
newPage("Findings (continued)")
}
currentY += 5
draw(finding)
}
newPage("Elements")
elements.sorted().forEach { element in
let height = draw(element, draw: false)
if height > 0 {
if currentY + height + 5 > pageSize.height {
newPage("Elements (continued)")
}
currentY += 5
draw(element)
}
}
newPage("View Hierarchy")
currentY += 5
elements.forEach { element in
let height = draw(heirarchyElement: element, draw: false)
if currentY + height + 5 > pageSize.height {
newPage("View Hierarchy (continued)")
currentY += 5
}
draw(heirarchyElement: element)
}
UIGraphicsEndPDFContext()
return pdfData as Data
}
var pageSize: CGSize { UIGraphicsGetPDFContextBounds().size }
}
extension Report {
func newPage() {
UIGraphicsBeginPDFPage()
currentY = padding
}
func newPage(_ string: String) {
newPage()
draw(string, attributes: title2, xPosition: padding)
drawRule()
}
func drawCentered(_ string: NSAttributedString, rect: CGRect) {
let drawingOptions: NSStringDrawingOptions = [.usesLineFragmentOrigin, .usesFontLeading]
let stringSize = string.boundingRect(with: rect.size, options: drawingOptions, context: nil)
string.draw(in: CGRect(x: rect.origin.x + (rect.width - stringSize.width) / 2,
y: rect.origin.y + (rect.height - stringSize.height) / 2,
width: stringSize.width,
height: stringSize.height))
}
@discardableResult func draw(_ string: String, attributes: [NSAttributedString.Key: Any], xPosition: CGFloat? = nil,
width: CGFloat? = nil, updateHeight: Bool = true, draw: Bool = true) -> CGSize {
let xPosition = xPosition ?? padding
let actualWidth = width ?? (pageSize.width - 2 * padding)
let attributedString = NSAttributedString(string: string, attributes: attributes)
let drawingOptions: NSStringDrawingOptions = [.usesLineFragmentOrigin, .usesFontLeading]
let boundingSize = CGSize(width: actualWidth, height: .greatestFiniteMagnitude)
let stringSize = attributedString.boundingRect(with: boundingSize, options: drawingOptions, context: nil)
if draw && stringSize.height + currentY > pageSize.height {
newPage()
}
let stringRect = CGRect(x: xPosition, y: currentY, width: actualWidth, height: stringSize.height)
if draw {
attributedString.draw(in: stringRect)
if updateHeight {
currentY += stringRect.height + padding
}
}
return stringSize.size
}
@discardableResult func draw(_ image: UIImage?, outline: Bool = true, xPosition: CGFloat? = nil,
width: CGFloat? = nil, updateHeight: Bool = true, draw: Bool = true) -> CGSize {
guard let image = image else { return .zero }
let xPosition = xPosition ?? padding
let actualWidth = width ?? (pageSize.width - xPosition - 2 * padding)
let scaleW = min(image.size.width, actualWidth) / image.size.width
let scaleH = min(image.size.height, pageSize.height - currentY - padding) / image.size.height
let scale = min(scaleW, scaleH)
let drawRect = CGRect(x: xPosition,
y: currentY,
width: image.size.width * scale,
height: image.size.height * scale)
if draw {
image.draw(in: drawRect)
if outline {
UIColor.black.set()
UIRectFrame(drawRect)
}
if updateHeight {
currentY += drawRect.height + padding
}
}
return drawRect.size
}
@discardableResult func draw(_ color: UIColor, xPosition: CGFloat? = nil,
updateHeight: Bool = true, draw performDraw: Bool = true) -> CGSize {
let colorWidth = CGFloat(20)
let textWidth = CGFloat(100)
let xPosition = xPosition ?? padding
let colorRect = CGRect(x: xPosition, y: currentY, width: colorWidth, height: colorWidth)
if performDraw {
color.set()
UIRectFill(colorRect)
UIColor.black.set()
UIRectFrame(colorRect)
}
let textSize = draw(color.hex, attributes: unispacedBody, xPosition: xPosition + colorRect.width + padding,
width: textWidth, updateHeight: updateHeight, draw: performDraw)
let height = max(colorRect.height, textSize.height)
if performDraw && updateHeight {
currentY += height + padding
}
return CGSize(width: padding + textWidth, height: height)
}
@discardableResult func draw(_ cgColor: CGColor?, xPosition: CGFloat? = nil,
updateHeight: Bool = true, draw performDraw: Bool = true) -> CGSize {
let color = cgColor != nil ? UIColor(cgColor: cgColor!) : .clear
return draw(color, xPosition: xPosition, updateHeight: updateHeight, draw: performDraw)
}
@discardableResult func draw(_ finding: Finding, draw performDraw: Bool = true) -> CGFloat {
let severityWidth = CGFloat(75)
let severityHeight = CGFloat(40)
let remainingWidth = pageSize.width - 4 * padding - severityWidth
let messageWidth = remainingWidth * 0.6
let screenshotWidth = remainingWidth - messageWidth
var xPosition = padding
if performDraw {
color(severity: finding.severity).set()
UIRectFill(CGRect(x: xPosition, y: currentY, width: severityWidth, height: severityHeight))
drawCentered(NSAttributedString(string: finding.severity.rawValue,
attributes: style(severity: finding.severity)),
rect: CGRect(x: xPosition, y: currentY, width: severityWidth, height: 40))
}
xPosition += severityWidth + padding
let size0 = draw(finding.description, attributes: body, xPosition: xPosition, width: messageWidth,
updateHeight: false, draw: performDraw)
let messageX = xPosition
xPosition += messageWidth + padding
let size1 = draw(finding.screenshot, xPosition: xPosition, width: screenshotWidth,
updateHeight: false, draw: performDraw)
if performDraw {
currentY += size0.height + padding / 2
}
let size2 = draw(finding.explanation, attributes: detail, xPosition: messageX, width: messageWidth,
updateHeight: false, draw: performDraw)
let rowHeight = max(severityHeight, size0.height + size2.height + padding, size1.height)
if performDraw {
currentY += rowHeight - size0.height
}
return rowHeight
}
@discardableResult func draw(_ element: Element, draw performDraw: Bool = true) -> CGFloat {
switch element {
case .label: return drawLabel(element, draw: performDraw)
case .image: return drawImage(element, draw: performDraw)
default: return 0
}
}
@discardableResult func drawLabel(_ element: Element, draw performDraw: Bool = true) -> CGFloat {
guard case let Element.label(font, maxLines, text, textColor, measuredTextColor, measuredBackgroundColor, base)
= element else { return 0 }
var xPosition = padding
let size0 = draw("\(element.base.depth) Label: \(font.pointSize)pt", attributes: body,
xPosition: xPosition, width: 140, updateHeight: false, draw: performDraw)
xPosition += 140 + padding
let size1 = draw("\(font.fontName)", attributes: body, xPosition: xPosition, width: 180,
updateHeight: false, draw: performDraw)
xPosition += 180 + padding
let numberOfLines = element.numberOfLines(text: text, font: font, frame: base.windowFrame)
let size2 = draw("\(numberOfLines) / \(maxLines) lines", attributes: body, xPosition: xPosition,
width: 100, updateHeight: false, draw: performDraw)
xPosition += 100 + padding
let bgColor = measuredBackgroundColor?.cgColor ?? base.effectiveBackgroundColor ?? UIColor.clear.cgColor
let contrast = textColor.cgColor.contrastRatio(with: bgColor)
draw("\(String(format: "%.2f", contrast ?? 0)):1", attributes: body, xPosition: xPosition,
width: 100, updateHeight: false, draw: performDraw)
xPosition = padding
if performDraw { currentY += max(size0.height, size1.height, size2.height) + padding }
let colorSize = draw(textColor, xPosition: xPosition, updateHeight: false, draw: false)
xPosition += draw(textColor, xPosition: xPosition, updateHeight: false, draw: performDraw).width
draw(measuredTextColor ?? .clear, xPosition: xPosition, updateHeight: false, draw: performDraw)
xPosition += colorSize.width
draw(base.backgroundColor ?? .clear, xPosition: xPosition, updateHeight: false, draw: performDraw)
xPosition += colorSize.width
draw(measuredBackgroundColor ?? .clear, xPosition: xPosition, updateHeight: false, draw: performDraw)
xPosition += colorSize.width
draw(base.effectiveBackgroundColor, xPosition: xPosition, updateHeight: false, draw: performDraw)
xPosition += colorSize.width
if performDraw { currentY += colorSize.height + padding }
let sizeText = draw(text, attributes: detail, xPosition: 40, draw: performDraw)
let height = colorSize.height + padding + sizeText.height
return height
}
@discardableResult func drawImage(_ element: Element, draw performDraw: Bool = true) -> CGFloat {
guard case let Element.image(image, imageAccessibilityLabel, base) = element else { return 0 }
var xPosition = padding
let size0 = draw("\(element.base.depth) \(base.className):", attributes: body,
xPosition: xPosition, width: 140, updateHeight: false, draw: performDraw)
xPosition += 140 + padding
let size1 = draw(imageAccessibilityLabel ?? "{no accessibility label}", attributes: body,
xPosition: xPosition, width: 240, updateHeight: false, draw: performDraw)
xPosition += 240 + padding
let size2 = draw(image, xPosition: xPosition, width: pageSize.width - xPosition - padding,
updateHeight: false, draw: performDraw)
let rowHeight = max(size0.height, size1.height, size2.height) + padding
if performDraw {
currentY += rowHeight
}
return rowHeight
}
@discardableResult func draw(heirarchyElement element: Element, draw performDraw: Bool = true) -> CGFloat {
let spacer = "\(String(format: "%4d ", element.depth)) "
+ String(repeating: "- ", count: element.base.level)
let string = "\(element)"
let size = draw(spacer, attributes: unispacedBody, updateHeight: false, draw: performDraw)
let rowSize = draw(string, attributes: unispacedBody, xPosition: size.width + padding, draw: performDraw)
return rowSize.height
}
func drawRule(color: UIColor = .gray, height: CGFloat = 1) {
color.set()
UIRectFrame(CGRect(x: padding, y: currentY, width: pageSize.width - 2 * padding, height: height))
currentY += height
}
func drawSystemSummary(xPosition: CGFloat? = nil) {
let xPosition = xPosition ?? padding
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "HH:mm:ss E MMM d, yyyy"
let style = unispacedBody
draw("Bundle ID: \(Bundle.main.bundleIdentifier ?? "")", attributes: style, xPosition: xPosition)
draw("Date: \(dateFormatter.string(from: Date()))", attributes: style, xPosition: xPosition)
draw("iOS: \(UIDevice.current.systemVersion)", attributes: style, xPosition: xPosition)
draw("Device model: \(UIDevice.current.model)", attributes: style, xPosition: xPosition)
draw("Device name: \(UIDevice.current.name)", attributes: style, xPosition: xPosition)
draw("Locale: \(Locale.current.languageCode ?? "nil")", attributes: style, xPosition: xPosition)
if #available(iOS 12.0, *) {
let mode = context.traitCollection.userInterfaceStyle == .light ? "Light mode" : "Dark mode"
draw("Mode: \(mode)", attributes: style, xPosition: xPosition)
}
draw("Display gamut: \(context.traitCollection.displayGamut.name)", attributes: style, xPosition: xPosition)
draw("Global configuration", attributes: title2, xPosition: xPosition)
draw("Excluded checks", attributes: body, xPosition: xPosition)
if UILintConfig.shared.excludedChecks.isEmpty {
draw("No excluded checks", attributes: style, xPosition: xPosition + padding)
}
UILintConfig.shared.excludedChecks.forEach { check in
draw("\(check.self)", attributes: style, xPosition: xPosition + padding)
}
draw("Expected fonts", attributes: body, xPosition: xPosition)
if UILintConfig.shared.expectedFontNames.isEmpty {
draw("All fonts are expected", attributes: style, xPosition: xPosition + padding)
}
UILintConfig.shared.expectedFontNames.forEach { fontName in
draw("\(fontName)", attributes: style, xPosition: xPosition + padding)
}
currentY += padding
}
func drawFirstPage() {
newPage()
draw(pdfTitle, attributes: title1)
let screenshotSize = draw(screenshot, draw: false)
if screenshotSize.height / screenshotSize.width > 1.3 {
draw("Screenshot", attributes: title2, updateHeight: false)
draw("System Details", attributes: title2, xPosition: pageSize.width / 2 + padding)
draw(screenshot, width: pageSize.width / 2 - padding, updateHeight: false)
drawSystemSummary(xPosition: pageSize.width / 2 + padding)
} else {
draw("System Details", attributes: title2)
drawSystemSummary()
draw("Screenshot", attributes: title2)
draw(screenshot)
}
}
}