-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadMe.m
More file actions
113 lines (96 loc) · 3.2 KB
/
ReadMe.m
File metadata and controls
113 lines (96 loc) · 3.2 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
//
// ReadMe.m
// ReadMe
//
// Created by Benoît Layer on 08/03/2014.
// Copyright (c) 2014 Benoît Layer. All rights reserved.
//
#import "ReadMe.h"
dispatch_source_t create_dispatch_timer(double interval, dispatch_queue_t queue, dispatch_block_t block)
{
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
if (timer)
{
dispatch_source_set_timer(timer, dispatch_time(DISPATCH_TIME_NOW, interval * NSEC_PER_SEC), interval * NSEC_PER_SEC, (1ull * NSEC_PER_SEC) / 10);
dispatch_source_set_event_handler(timer, block);
dispatch_resume(timer);
}
return timer;
}
// Aproximative, based on constatations...
static float ORP = 0.434;
@interface ReadMe () {
dispatch_source_t _timer;
NSMutableArray *_splitedText;
}
@end
@implementation ReadMe
+ (instancetype)sharedInstance
{
static dispatch_once_t once;
static id sharedInstance;
dispatch_once(&once, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
- (id)init
{
self = [super init];
if (self) {
_wordsPerMinute = 300;
}
return self;
}
- (void)readText:(NSString *)text
{
// Split text into array by space character
_splitedText = [NSMutableArray arrayWithArray:[text componentsSeparatedByString:@" "]];
// Launch a timer to send words (timer on background thread).
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
double secondsToFire = (double)60.f/_wordsPerMinute;
_timer = create_dispatch_timer(secondsToFire, queue, ^{
if (_splitedText != nil && _splitedText.count > 0)
{
NSString *word = _splitedText[0];
// Lot of rules missing here, parse number ? etc...
NSString *cleanedWord = [word stringByReplacingOccurrencesOfString:@".,?!" withString:@""];
NSUInteger wordLength = cleanedWord.length;
int orp = MAX(0, round(wordLength*ORP)-1);
if (_delegate != nil && [_delegate respondsToSelector:@selector(readMe:didReadWord:withORP:)])
{
// Call delegate on main thread.
dispatch_async(dispatch_get_main_queue(), ^{
[_delegate readMe:self didReadWord:word withORP:orp];
});
}
[_splitedText removeObjectAtIndex:0];
}
else
{
if (_delegate != nil && [_delegate respondsToSelector:@selector(readMeDidFinishReading:)])
{
// Call delegate on main thread.
dispatch_async(dispatch_get_main_queue(), ^{
[_delegate readMeDidFinishReading:self];
});
}
}
});
}
- (void)stopReading
{
if (_timer) {
dispatch_source_cancel(_timer);
// Remove this if you are on a Deployment Target of iOS6 or OSX 10.8 and above
_timer = nil;
}
if (_delegate != nil && [_delegate respondsToSelector:@selector(readMeDidFinishReading:)])
{
// Call delegate on main thread.
dispatch_async(dispatch_get_main_queue(), ^{
[_delegate readMeDidFinishReading:self];
});
}
}
@end