-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathViewControllerDataSource.swift
More file actions
90 lines (73 loc) · 2.57 KB
/
ViewControllerDataSource.swift
File metadata and controls
90 lines (73 loc) · 2.57 KB
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
//
// ViewControllerDataSource.swift
// BuildTimeAnalyzer
//
// Created by Dmitrii on 02/12/2017.
// Copyright © 2017 Cane Media Ltd. All rights reserved.
//
import Foundation
class ViewControllerDataSource {
var aggregateByFile: Bool = false {
didSet {
processData()
}
}
var filter: String = "" {
didSet {
processData()
}
}
var sortDescriptors: [NSSortDescriptor] = [] {
didSet {
processData()
}
}
private var originalData: [CompileMeasure] = []
private var processedData: [CompileMeasure] = []
func resetSourceData(newSourceData: [CompileMeasure]) {
originalData = newSourceData
processData()
}
func isEmpty() -> Bool {
return processedData.isEmpty
}
func count() -> Int {
return processedData.count
}
func measure(index: Int) -> CompileMeasure? {
guard index < processedData.count && index >= 0 else { return nil }
return processedData[index]
}
// MARK: - Private methods
private func processData() {
var newProcessedData = aggregateIfNeeded(originalData)
newProcessedData = applySortingIfNeeded(newProcessedData)
newProcessedData = applyFilteringIfNeeded(newProcessedData)
processedData = newProcessedData
}
private func aggregateIfNeeded(_ input: [CompileMeasure]) -> [CompileMeasure] {
guard aggregateByFile else { return input }
var fileTimes: [String: CompileMeasure] = [:]
for measure in input {
if let fileMeasure = fileTimes[measure.path] {
fileMeasure.time += measure.time
fileTimes[measure.path] = fileMeasure
} else {
let newFileMeasure = CompileMeasure(rawPath: measure.path, time: measure.time)
fileTimes[measure.path] = newFileMeasure
}
}
return Array(fileTimes.values)
}
private func applySortingIfNeeded(_ input: [CompileMeasure]) -> [CompileMeasure] {
if sortDescriptors.isEmpty { return input }
return (input as NSArray).sortedArray(using: sortDescriptors) as! Array
}
private func applyFilteringIfNeeded(_ input: [CompileMeasure]) -> [CompileMeasure] {
guard !filter.isEmpty else { return input }
return input.filter{ textContains($0.code, pattern: filter) || textContains($0.filename, pattern: filter) }
}
private func textContains(_ text: String, pattern: String) -> Bool {
return text.lowercased().contains(pattern.lowercased())
}
}