Add custom filtering functionality
This commit is contained in:
parent
116b257b80
commit
1948f6964b
|
@ -11,7 +11,7 @@
|
|||
<key>com.apple.XCTPerformanceMetric_WallClockTime</key>
|
||||
<dict>
|
||||
<key>baselineAverage</key>
|
||||
<real>0.391</real>
|
||||
<real>0.556</real>
|
||||
<key>baselineIntegrationDisplayName</key>
|
||||
<string>Local Baseline</string>
|
||||
</dict>
|
||||
|
|
|
@ -10,10 +10,19 @@ import UIKit
|
|||
|
||||
class HomeViewController: UITableViewController {
|
||||
lazy var playData = PlayData()
|
||||
lazy var words = playData.filteredWords
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Lifecycle
|
||||
|
||||
extension HomeViewController {
|
||||
|
||||
override func viewDidLoad() {
|
||||
super.viewDidLoad()
|
||||
// Do any additional setup after loading the view, typically from a nib.
|
||||
|
||||
playData.applyCustomFilter({ $0.lowercased().contains("swift") })
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -22,12 +31,12 @@ class HomeViewController: UITableViewController {
|
|||
|
||||
extension HomeViewController {
|
||||
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
|
||||
return playData.allWords.count
|
||||
return words.count
|
||||
}
|
||||
|
||||
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
|
||||
let cell = tableView.dequeueReusableCell(withIdentifier: StoryboardID.TableView.cell, for: indexPath)
|
||||
let word = playData.allWords[indexPath.row]
|
||||
let word = words[indexPath.row]
|
||||
|
||||
cell.textLabel?.text = word
|
||||
cell.detailTextLabel?.text = "\(playData.wordCounts.count(for: word)) counts"
|
||||
|
|
|
@ -10,21 +10,41 @@ import Foundation
|
|||
|
||||
class PlayData {
|
||||
private lazy var pathToWords = Bundle.main.path(forResource: "plays", ofType: "txt")
|
||||
|
||||
var wordCounts: NSCountedSet!
|
||||
|
||||
|
||||
var allWords: [String] = []
|
||||
var wordsSortedByCount: [String] = []
|
||||
private(set) var filteredWords: [String] = []
|
||||
|
||||
var wordCounts: NSCountedSet! {
|
||||
didSet {
|
||||
wordCountsChanged()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
init() {
|
||||
loadWords()
|
||||
filteredWords = allWords
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Core Methods
|
||||
|
||||
extension PlayData {
|
||||
var allWords: [String] {
|
||||
return wordCounts.allObjects as! [String]
|
||||
func applyCustomFilter(_ predicate: (_ word: String) -> Bool) {
|
||||
filteredWords = allWords.filter(predicate)
|
||||
}
|
||||
|
||||
func setCountThreshold(_ minimumCount: Int) {
|
||||
filteredWords = allWords.filter { wordCounts.count(for: $0) >= minimumCount }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// MARK: - Private Helper Methods
|
||||
|
||||
private extension PlayData {
|
||||
func loadWords() {
|
||||
guard let pathToWords = pathToWords else {
|
||||
|
@ -44,5 +64,13 @@ private extension PlayData {
|
|||
print("Error while parsing plays file:\n\n\(error.localizedDescription)")
|
||||
}
|
||||
}
|
||||
|
||||
func wordCountsChanged() {
|
||||
allWords = wordCounts.allObjects as! [String]
|
||||
|
||||
wordsSortedByCount = allWords.sorted(by: {
|
||||
wordCounts.count(for: $0) > wordCounts.count(for: $1)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -43,6 +43,23 @@ class iOS_Unit_TestingTests: XCTestCase {
|
|||
XCTAssertEqual(actual, expected, "\"\(word)\" did not have a count of \(count)")
|
||||
}
|
||||
}
|
||||
|
||||
func testFilteredWords() {
|
||||
let playData = PlayData()
|
||||
|
||||
playData.applyCustomFilter({ $0.count >= 10 })
|
||||
|
||||
var expected = 2047
|
||||
var actual = playData.filteredWords.count
|
||||
|
||||
XCTAssertEqual(actual, expected, "`filteredWords` did not have expeted count of \(expected)")
|
||||
|
||||
playData.applyCustomFilter { $0.lowercased().contains("swift") }
|
||||
|
||||
expected = 7
|
||||
actual = playData.filteredWords.count
|
||||
XCTAssertEqual(actual, expected, "`filteredWords` did not have expeted count of \(expected)")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue