-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainViewController.m
More file actions
151 lines (123 loc) · 4 KB
/
MainViewController.m
File metadata and controls
151 lines (123 loc) · 4 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
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
//
// MainViewController.m
// SpeedReader
//
// Created by Joel Green on 1/18/14.
// Copyright (c) 2014 Joel Green. All rights reserved.
//
#import "MainViewController.h"
@interface MainViewController ()
@property (weak, nonatomic) IBOutlet UISegmentedControl *topSegControl;
@property (weak, nonatomic) IBOutlet UISlider *bottomSlider;
@property (weak, nonatomic) IBOutlet UILabel *staticWordLabel;
@property (weak, nonatomic) IBOutlet UILabel *wpmLabel;
@property (weak, nonatomic) IBOutlet UILabel *numWordsLabel;
@property (strong, nonatomic) NSMutableString *clipboardContent;
@property (strong, nonatomic) NSMutableArray *timers;
@property (strong, nonatomic) NSMutableArray *wordChunks;
@property int wordChunckSize;
@property float delayConstant;
@property int previousIndex;
@property int wpm;
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSMutableArray *)wordChunks
{
if (!_wordChunks) {
_wordChunks = [[NSMutableArray alloc] init];
}
return _wordChunks;
}
- (NSMutableArray *)timers
{
if (!_timers) {
_timers = [[NSMutableArray alloc] init];
}
return _timers;
}
#pragma mark -
- (void)viewDidLoad
{
[super viewDidLoad];
self.wordChunckSize = self.topSegControl.selectedSegmentIndex + 1;
self.delayConstant = 0.8 - (float)self.bottomSlider.value;
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
self.clipboardContent = [[NSMutableString alloc] initWithString:[UIPasteboard generalPasteboard].string];
self.numWordsLabel.text = [NSString stringWithFormat:@"%d Words", (int)self.clipboardContent.length];
for (NSTimer *timer in self.timers){
[timer invalidate];
}
[self.timers removeAllObjects];
[self startReading];
}
- (IBAction)topSegChanged:(id)sender {
self.wordChunckSize = self.topSegControl.selectedSegmentIndex + 1;
[self updateWPM];
}
- (IBAction)bottomSliderChanged:(id)sender
{
self.delayConstant = 0.8 - (float)self.bottomSlider.value;
[self updateWPM];
}
- (void)updateWPM
{
float fwpm = (float)self.wordChunckSize * 60.0 / self.delayConstant;
self.wpm = (int)fwpm;
self.wpmLabel.text = [NSString stringWithFormat:@"%d WPM",self.wpm];
}
- (void)startReading
{
self.previousIndex = 0;
[self displayWords];
[self updateWPM];
}
- (void)displayWords
{
[self.timers addObject:[NSTimer scheduledTimerWithTimeInterval:self.delayConstant
target:self
selector:@selector(timerFired:)
userInfo:nil
repeats:NO]];
}
- (void)timerFired:(NSTimer *)timer
{
NSString *wordGroup;
int wordCount = 0;
for (int i = self.previousIndex; i < self.clipboardContent.length; i++)
{
unichar cr = [self.clipboardContent characterAtIndex:i];
if (cr == ' ') {
wordCount += 1;
}
if (wordCount == self.wordChunckSize) {
wordCount = 0;
wordGroup = [self.clipboardContent substringWithRange:NSMakeRange(self.previousIndex, i - self.previousIndex)];
self.previousIndex = i + 1;
self.staticWordLabel.text = wordGroup;
[self displayWords];
break;
}
if (i == self.clipboardContent.length - 1) {
wordGroup = [self.clipboardContent substringWithRange:NSMakeRange(self.previousIndex, self.clipboardContent.length - self.previousIndex)];
self.staticWordLabel.text = wordGroup;
[self displayWords];
break;
}
}
}
@end