-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #78 from DmIvanov/Sorting
Sorting by filename
- Loading branch information
Showing
6 changed files
with
316 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 = false { | ||
didSet { | ||
processData() | ||
} | ||
} | ||
|
||
var filter = "" { | ||
didSet { | ||
processData() | ||
} | ||
} | ||
|
||
var sortDescriptors = [NSSortDescriptor]() { | ||
didSet { | ||
processData() | ||
} | ||
} | ||
|
||
fileprivate var originalData = [CompileMeasure]() | ||
fileprivate 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()) | ||
} | ||
} |
Oops, something went wrong.