-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCPTutorial.m
executable file
·250 lines (210 loc) · 7 KB
/
CPTutorial.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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//
// CPTutorial.m
//
// Created by Can Poyrazoğlu on 27.10.14.
//
#import "CPTutorial.h"
#import "CPTutorialInvisibleProxyView.h"
#import "CPTutorialTargetTouchIndicatorView.h"
#define CPTUTORIAL_NAME_FORMAT (@"_CPTutorial_%@")
#define CPTUTORIAL_KEY(x) ([NSString stringWithFormat:CPTUTORIAL_NAME_FORMAT, x])
#define CPTUTORIAL_NOOP (^{})
static CPTutorial *current;
static NSMutableDictionary *tutorials;
static NSMutableArray *tutorialsDisplayedThisSession;
@implementation CPTutorial{
UIView *windowOverlayView;
CPTutorialBalloon *lastAddedBalloon;
CPTutorialAction completionHandler;
}
-(instancetype)pause{
self.currentBalloon.shouldHoldAfterBeingDismissed = YES;
[self.currentBalloon dismiss];
_currentBalloon = self.currentBalloon.nextBalloon;
[self checkCompletion];
return self;
}
-(instancetype)resume{
[self.currentBalloon dismiss];
return self;
}
-(void)checkCompletion{
if(!self.currentBalloon){
[CPTutorial markTutorialAsCompleted:self.name];
if(completionHandler){
completionHandler();
}
}
}
-(instancetype)insert:(CPTutorialAction)actions{
CPTutorial *before = current;
current = self;
actions();
current = before;
return self;
}
-(instancetype)step{
_currentBalloon = self.currentBalloon.nextBalloon;
[self checkCompletion];
self.currentBalloon.shouldHoldAfterBeingDismissed = NO;
[self.currentBalloon signal];
[self refresh];
return self;
}
-(instancetype)cancel{
[self.currentBalloon dismiss];
_currentBalloon = nil;
[self checkCompletion];
return self;
}
+(CPTutorial*)tutorialNamed:(NSString*)tutorialName{
CPTutorial *tut = tutorials[tutorialName];
if(!tut){
tut = [[CPTutorial alloc] initWithName:tutorialName];
tutorials[tutorialName] = tut;
}
return tut;
}
+(CPTutorial*)currentTutorial{
return current;
}
-(instancetype)initWithName:(NSString*)tutorialName{
self = [super init];
_name = tutorialName;
return self;
}
-(instancetype)init{
NSAssert(NO, @"Use [CPTutorial initWithName:] to initialize CPTutorial. Pass in a unique name, doesn't matter what it is.");
return nil;
}
+(void)initialize{
[super initialize];
tutorialsDisplayedThisSession = [NSMutableArray array];
tutorials = [NSMutableDictionary dictionary];
}
-(BOOL)shouldDisplay{
return [CPTutorial shouldDisplayTutorialWithName:self.name];
}
+(CPTutorial*)displayWithName:(NSString*)tutorialName forceIfAlreadyCompleted:(BOOL)forceIfCompleted actions:(CPTutorialAction)actions completion:(CPTutorialCompletion)completion{
if(!forceIfCompleted && ![CPTutorial shouldDisplayTutorialWithName:tutorialName]){
if(completion){
completion(NO);
}
return nil;
}else{
[[CPTutorial beginStepsWithTutorialName:tutorialName] completeWith:^{
if(completion){
completion(YES);
}
}];
actions();
return [CPTutorial endSteps];
}
}
+(CPTutorial*)displayWithName:(NSString*)tutorialName actions:(CPTutorialAction)actions completion:(CPTutorialCompletion)completion{
return [self displayWithName:tutorialName forceIfAlreadyCompleted:NO actions:actions completion:completion];
}
+(UIView*)placeholderAt:(CGRect)frame{
CPTutorialInvisibleProxyView *proxy = [[CPTutorialInvisibleProxyView alloc] initWithFrame:frame];
return proxy;
}
+(CPTutorialDisplayable*)targetTouchIndicatorAt:(CGRect)frame{
return [self targetTouchIndicatorAt:frame withAnimationDelay:0];
}
+(CPTutorialDisplayable*)targetTouchIndicatorAt:(CGRect)frame withAnimationDelay:(NSTimeInterval)delay extraVisible:(BOOL)extraVisible{
CPTutorialTargetTouchIndicatorView *view = [[CPTutorialTargetTouchIndicatorView alloc] initWithFrame:frame];
[view beginAnimatingAfterDelay:delay extraVisible:extraVisible];
return view;
}
+(CPTutorialDisplayable*)targetTouchIndicatorAt:(CGRect)frame withAnimationDelay:(NSTimeInterval)delay{
return [self targetTouchIndicatorAt:frame withAnimationDelay:delay extraVisible:NO];
}
+(CPTutorial*)displayWithName:(NSString*)tutorialName forceIfAlreadyCompleted:(BOOL)forceIfCompleted actions:(CPTutorialAction)actions{
return [self displayWithName:tutorialName forceIfAlreadyCompleted:forceIfCompleted actions:actions completion:nil];
}
+(CPTutorial*)displayWithName:(NSString*)tutorialName actions:(CPTutorialAction)actions{
return [self displayWithName:tutorialName actions:actions completion:nil];
}
-(instancetype)completeWith:(CPTutorialAction)completion{
if(!completion){
completion = CPTUTORIAL_NOOP;
}
completionHandler = completion;
return self;
}
+(instancetype)pause:(NSString*)tutorialName{
return [[CPTutorial tutorialNamed:tutorialName] pause];
}
+(instancetype)resume:(NSString*)tutorialName{
return [[CPTutorial tutorialNamed:tutorialName] resume];
}
+(instancetype)cancel:(NSString*)tutorialName{
return [[CPTutorial tutorialNamed:tutorialName] cancel];
}
+(CPTutorial*)beginStepsWithTutorialName:(NSString*)tutorialName{
NSAssert(tutorialName.length, @"Tutorial must have a valid name!");
CPTutorial *tut = [CPTutorial tutorialNamed:tutorialName];
[tutorialsDisplayedThisSession addObject:tutorialName];
current = tut;
return tut;
}
-(void)addBalloon:(CPTutorialBalloon*)balloon{
if(!_firstBalloon){
_firstBalloon = balloon;
}
if(!_currentBalloon){
_currentBalloon = balloon;
}
lastAddedBalloon.nextBalloon = balloon;
lastAddedBalloon = balloon;
if(self.style){
balloon.style = self.style;
}
}
+(CPTutorial*)endSteps{
CPTutorial *tut = current;
current = nil;
CPTutorialBalloon *firstBalloon = tut.firstBalloon;
[firstBalloon signal];
return tut;
}
+(BOOL)shouldDisplayTutorialWithName:(NSString*)name{
if(!name){
return YES;
}
if([tutorialsDisplayedThisSession containsObject:name]){
return NO;
}
NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
NSString *targetKey = CPTUTORIAL_KEY(name);
id val = [settings valueForKey:targetKey];
if([val isEqualToString:@"done"]){
return NO;
}else{
return YES;
}
}
-(instancetype)pauseIfOn:(id)target{
if(self.currentBalloon.targetView == target || self.currentBalloon == target){
[self.currentBalloon hold];
}
return self;
}
-(instancetype)resumeIfOn:(id)target{
if(self.currentBalloon.targetView == target || self.currentBalloon == target){
[self.currentBalloon dismiss];
}
return self;
}
-(instancetype)refresh{
CGRect rect = [self.currentBalloon.targetView.superview convertRect:self.currentBalloon.targetView.frame toView:nil];
[self.currentBalloon attachedViewFrameDidChange:rect];
return self;
}
+(void)markTutorialAsCompleted:(NSString*)tipName{
NSUserDefaults *settings = [NSUserDefaults standardUserDefaults];
NSString *targetKey = CPTUTORIAL_KEY(tipName);
[settings setValue:@"done" forKey:targetKey];
[settings synchronize];
}
@end