forked from mxcl/PromiseKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPromiseKit+UIKit.m
103 lines (75 loc) · 2.51 KB
/
PromiseKit+UIKit.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
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
#import "Private/macros.m"
#import "PromiseKit/Deferred.h"
#import "PromiseKit/Promise.h"
#import "PromiseKit+UIKit.h"
@import UIKit.UINavigationController;
@interface PMKMFDeferred : Deferred
@end
@implementation PMKMFDeferred
- (void)mailComposeController:(id)controller didFinishWithResult:(int)result error:(NSError *)error {
if (error)
[self reject:error];
else
[self resolve:@(result)];
__anti_arc_release(self);
}
@end
@implementation UIViewController (PromiseKit)
- (Promise *)promiseViewController:(UIViewController *)vc animated:(BOOL)animated completion:(void(^)(void))block
{
[self presentViewController:vc animated:animated completion:block];
Deferred *d = [Deferred new];
if ([vc isKindOfClass:NSClassFromString(@"MFMailComposeViewController")]) {
d = [PMKMFDeferred new];
__anti_arc_retain(d);
SEL selector = NSSelectorFromString(@"setMailComposeDelegate:");
IMP imp = [vc methodForSelector:selector];
void (*func)(id, SEL, id) = (void *)imp;
func(vc, selector, d);
} else if ([vc isKindOfClass:[UINavigationController class]]) {
vc = [(id)vc viewControllers].firstObject;
} else
[vc viewWillDefer:d];
return d.promise.then(^(id o){
[self dismissViewControllerAnimated:animated completion:nil];
return o;
});
}
- (void)viewWillDefer:(Deferred *)deferred {
NSLog(@"Base implementation of viewWillDefer: called, you probably want to override this.");
}
@end
@interface PMKAlertViewDelegate : Deferred <UIAlertViewDelegate>
@end
@implementation PMKAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
[self resolve:@(buttonIndex)];
__anti_arc_release(self);
}
@end
@implementation UIAlertView (PromiseKit)
- (Promise *)promise {
PMKAlertViewDelegate *d = [PMKAlertViewDelegate new];
__anti_arc_retain(d);
self.delegate = d;
[self show];
return d.promise;
}
@end
@interface PMKActionSheetDelegate : Deferred <UIActionSheetDelegate>
@end
@implementation PMKActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
[self resolve:@(buttonIndex)];
__anti_arc_release(self);
}
@end
@implementation UIActionSheet (PromiseKit)
- (Promise *)promiseInView:(UIView *)view {
PMKActionSheetDelegate *d = [PMKActionSheetDelegate new];
__anti_arc_retain(d);
self.delegate = d;
[self showInView:view];
return d.promise;
}
@end