forked from alejandrojapkin/CocoaLUT
/
LUTProcessor.m
69 lines (58 loc) · 1.5 KB
/
LUTProcessor.m
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
//
// LUTProcessor.m
// Pods
//
// Created by Wil Gieseler on 12/16/13.
//
//
#import "LUTProcessor.h"
@interface LUTProcessor (){
NSDate *_startTime;
BOOL _calledCancelHandler;
}
@end
@implementation LUTProcessor
+ (instancetype)processorForLUT:(LUT *)lut
completionHandler:(void(^)(LUT *reversedLUT))completionHandler
cancelHandler:(void(^)())cancelHandler {
LUTProcessor *r = [[self alloc] init];
[r setLut:lut];
[r setCancelHandler:cancelHandler];
[r setCompletionHandler:completionHandler];
return r;
}
- (void)process {
_startTime = [NSDate date];
}
- (void)cancel {
self.cancelled = YES;
self.progressDescription = @"Canceling...";
}
- (void)completedWithLUT:(LUT *)lut {
self.progress = 1;
NSLog(@"-> Processor finished in %fs", -[_startTime timeIntervalSinceNow]);
dispatch_async(dispatch_get_main_queue(), ^{
self.completionHandler(lut);
});
}
- (BOOL)checkCancellation {
if (self.cancelled) {
@synchronized(self) {
if (!_calledCancelHandler) {
[self didCancel];
_calledCancelHandler = YES;
}
}
return YES;
}
return NO;
}
- (void)didCancel {
dispatch_async(dispatch_get_main_queue(), ^{
self.cancelHandler();
});
}
- (void)setProgress:(float)progress section:(int)section of:(int)sectionCount {
self.progress = (progress / (float)sectionCount) + (((float)section - 1) / (float)sectionCount);
}
@end