-
Notifications
You must be signed in to change notification settings - Fork 0
/
Progress.swift
62 lines (48 loc) · 985 Bytes
/
Progress.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
//
// Progress.swift
// Reader
//
// Created by Niket Shah on 19/06/2015.
// Copyright (c) 2015 Niket Shah. All rights reserved.
//
import Foundation
class Progress {
let wordCountArray: [Int]
let total: Int
var index = 0
init() {
wordCountArray = []
total = 0
}
init(paragraphList: [String]) {
var count = 0
wordCountArray = paragraphList.map { (paragraph: String) -> Int in
count += paragraph.wordCount()
return count
}
total = count
}
func next() -> Double {
if index < wordCountArray.count - 1 {
index++
}
return percentage()
}
func previous() -> Double {
if index > 0 {
index--
}
return percentage()
}
func percentage() -> Double {
return Double(wordCountArray[index])/Double(total)
}
}
protocol WordCountable {
func wordCount() -> Int
}
extension String: WordCountable {
func wordCount() -> Int {
return self.componentsSeparatedByCharactersInSet(NSCharacterSet.whitespaceCharacterSet()).count
}
}