forked from ryanbriones/focus-timer
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFTTimerFormatter.m
More file actions
127 lines (96 loc) · 3.63 KB
/
FTTimerFormatter.m
File metadata and controls
127 lines (96 loc) · 3.63 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
#import "FTTimerFormatter.h"
@implementation FTTimerFormatter
- (id) init {
[super init];
return self;
}
- (NSString *) stringForObjectValue: (id) timeAsString {
int totalTimeInSeconds = [timeAsString integerValue];
int remainder, hours, minutes, seconds;
remainder = totalTimeInSeconds;
hours = floor(remainder / 3600);
remainder -= hours * 3600;
minutes = floor(remainder / 60);
remainder -= minutes * 60;
seconds = remainder;
if(hours > 0) {
return [NSString stringWithFormat: @"%02d:%02d:%02d", hours, minutes, seconds];
} else {
return [NSString stringWithFormat: @"%02d:%02d", minutes, seconds];
}
}
- (BOOL) getObjectValue: (id *) anObject forString: (NSString *) string errorDescription: (NSString **) error {
if([string length] < 3) {
*anObject = [NSNumber numberWithInt: [string intValue]];
return YES;
} else {
int timerSeconds, minutes, seconds;
NSArray *timerParts = [string componentsSeparatedByString: @":"];
if([timerParts count] == 2) {
timerSeconds = 0;
NSMutableString *minutesString = [[NSMutableString alloc] init];
[minutesString setString: [timerParts objectAtIndex: 0]];
NSString *secondsString = [timerParts objectAtIndex: 1];
if([secondsString length] == 3) {
seconds = [[secondsString substringWithRange: NSMakeRange(1,2)] intValue];
[minutesString appendString: [secondsString substringWithRange: NSMakeRange(0, 1)]];
minutes = [[minutesString substringWithRange: NSMakeRange(1,2)] intValue];
}
if([secondsString length] == 1) {
seconds = [secondsString intValue] * 10;
minutes = [minutesString intValue];
}
if([secondsString length] == 2) {
seconds = [secondsString intValue];
minutes = [minutesString intValue];
}
timerSeconds += minutes * 60;
timerSeconds += seconds;
*anObject = [NSNumber numberWithInt: timerSeconds];
return YES;
} else if ([timerParts count] == 1) {
timerSeconds = 0;
minutes = [[timerParts objectAtIndex: 0] intValue] / 100;
timerSeconds += minutes * 60;
seconds = [[timerParts objectAtIndex: 0] intValue] % 100;
timerSeconds += seconds;
*anObject = [NSNumber numberWithInt: timerSeconds];
return YES;
}
}
return NO;
}
- (BOOL)isPartialStringValid:(NSString *)partialString newEditingString:(NSString **)newString errorDescription:(NSString **)error {
if(NSEqualRanges(NSMakeRange(2,1), [partialString rangeOfString: @":"])) {
NSArray *timerParts = [partialString componentsSeparatedByString: @":"];
int minutes, seconds;
NSMutableString *minutesString = [[NSMutableString alloc] init];
[minutesString setString: [timerParts objectAtIndex: 0]];
NSString *secondsString = [timerParts objectAtIndex: 1];
if([secondsString length] == 3) {
seconds = [[secondsString substringWithRange: NSMakeRange(1,2)] intValue];
[minutesString appendString: [secondsString substringWithRange: NSMakeRange(0, 1)]];
minutes = [[minutesString substringWithRange: NSMakeRange(1,2)] intValue];
} else if([minutesString length] == 1) {
}
if([secondsString length] == 1) {
seconds = [secondsString intValue] * 10;
minutes = [minutesString intValue];
}
if([secondsString length] == 2) {
seconds = [secondsString intValue];
minutes = [minutesString intValue];
}
if(seconds > 59 || minutes > 59) {
*newString = nil;
return NO;
}
return YES;
} else if(NSEqualRanges(NSMakeRange(3,1), [partialString rangeOfString: @":"])) {
*newString = nil;
*error = @"test";
return NO;
}
return YES;
}
@end