/
TSMessage.m
executable file
路422 lines (355 loc) 路 15.4 KB
/
TSMessage.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
//
// TSMessage.m
// Felix Krause
//
// Created by Felix Krause on 24.08.12.
// Copyright (c) 2012 Felix Krause. All rights reserved.
//
#import "TSMessage.h"
#import "TSMessageView.h"
#define kTSMessageDisplayTime 1.5
#define kTSMessageExtraDisplayTimePerPixel 0.04
#define kTSMessageAnimationDuration 0.3
@interface TSMessage ()
/** The queued messages (TSMessageView objects) */
@property (nonatomic, strong) NSMutableArray *messages;
- (void)fadeInCurrentNotification;
- (void)fadeOutNotification:(TSMessageView *)currentView;
@end
@implementation TSMessage
static TSMessage *sharedMessage;
static BOOL notificationActive;
static BOOL _useiOS7Style;
__weak static UIViewController *_defaultViewController;
+ (TSMessage *)sharedMessage
{
if (!sharedMessage)
{
sharedMessage = [[[self class] alloc] init];
}
return sharedMessage;
}
#pragma mark Public methods for setting up the notification
+ (void)showNotificationWithTitle:(NSString *)title
type:(TSMessageNotificationType)type
{
[self showNotificationWithTitle:title
subtitle:nil
type:type];
}
+ (void)showNotificationWithTitle:(NSString *)title
subtitle:(NSString *)subtitle
type:(TSMessageNotificationType)type
{
[self showNotificationInViewController:[self defaultViewController]
title:title
subtitle:subtitle
type:type];
}
+ (void)showNotificationInViewController:(UIViewController *)viewController
title:(NSString *)title
subtitle:(NSString *)subtitle
type:(TSMessageNotificationType)type
duration:(NSTimeInterval)duration
{
[self showNotificationInViewController:viewController
title:title
subtitle:subtitle
image:nil
type:type
duration:duration
callback:nil
buttonTitle:nil
buttonCallback:nil
atPosition:TSMessageNotificationPositionTop
canBeDismissedByUser:YES];
}
+ (void)showNotificationInViewController:(UIViewController *)viewController
title:(NSString *)title
subtitle:(NSString *)subtitle
type:(TSMessageNotificationType)type
duration:(NSTimeInterval)duration
canBeDismissedByUser:(BOOL)dismissingEnabled
{
[self showNotificationInViewController:viewController
title:title
subtitle:subtitle
image:nil
type:type
duration:duration
callback:nil
buttonTitle:nil
buttonCallback:nil
atPosition:TSMessageNotificationPositionTop
canBeDismissedByUser:dismissingEnabled];
}
+ (void)showNotificationInViewController:(UIViewController *)viewController
title:(NSString *)title
subtitle:(NSString *)subtitle
type:(TSMessageNotificationType)type
{
[self showNotificationInViewController:viewController
title:title
subtitle:subtitle
image:nil
type:type
duration:TSMessageNotificationDurationAutomatic
callback:nil
buttonTitle:nil
buttonCallback:nil
atPosition:TSMessageNotificationPositionTop
canBeDismissedByUser:YES];
}
+ (void)showNotificationInViewController:(UIViewController *)viewController
title:(NSString *)title
subtitle:(NSString *)subtitle
image:(UIImage *)image
type:(TSMessageNotificationType)type
duration:(NSTimeInterval)duration
callback:(void (^)())callback
buttonTitle:(NSString *)buttonTitle
buttonCallback:(void (^)())buttonCallback
atPosition:(TSMessageNotificationPosition)messagePosition
canBeDismissedByUser:(BOOL)dismissingEnabled
{
// Create the TSMessageView
TSMessageView *v = [[TSMessageView alloc] initWithTitle:title
subtitle:subtitle
image:image
type:type
duration:duration
inViewController:viewController
callback:callback
buttonTitle:buttonTitle
buttonCallback:buttonCallback
atPosition:messagePosition
canBeDismissedByUser:dismissingEnabled];
[self prepareNotificationToBeShown:v];
}
+ (void)prepareNotificationToBeShown:(TSMessageView *)messageView
{
NSString *title = messageView.title;
NSString *subtitle = messageView.subtitle;
for (TSMessageView *n in [TSMessage sharedMessage].messages)
{
if (([n.title isEqualToString:title] || (!n.title && !title)) && ([n.subtitle isEqualToString:subtitle] || (!n.subtitle && !subtitle)))
{
return; // avoid showing the same messages twice in a row
}
}
[[TSMessage sharedMessage].messages addObject:messageView];
if (!notificationActive)
{
[[TSMessage sharedMessage] fadeInCurrentNotification];
}
}
#pragma mark Fading in/out the message view
- (id)init
{
if ((self = [super init]))
{
_messages = [[NSMutableArray alloc] init];
}
return self;
}
- (void)fadeInCurrentNotification
{
if ([self.messages count] == 0) return;
notificationActive = YES;
TSMessageView *currentView = [self.messages objectAtIndex:0];
__block CGFloat verticalOffset = 0.0f;
void (^addStatusBarHeightToVerticalOffset)() = ^void() {
BOOL isPortrait = UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation]);
CGSize statusBarSize = [UIApplication sharedApplication].statusBarFrame.size;
CGFloat offset = isPortrait ? statusBarSize.height : statusBarSize.width;
verticalOffset += offset;
};
if ([currentView.viewController isKindOfClass:[UINavigationController class]] || [currentView.viewController.parentViewController isKindOfClass:[UINavigationController class]])
{
UINavigationController *currentNavigationController;
if([currentView.viewController isKindOfClass:[UINavigationController class]])
currentNavigationController = (UINavigationController *)currentView.viewController;
else
currentNavigationController = (UINavigationController *)currentView.viewController.parentViewController;
BOOL isViewIsUnderStatusBar = [[[currentNavigationController childViewControllers] firstObject] wantsFullScreenLayout];
if (!isViewIsUnderStatusBar && currentNavigationController.parentViewController == nil) {
isViewIsUnderStatusBar = ![TSMessage isNavigationBarInNavigationControllerHidden:currentNavigationController]; // strange but true
}
if (![TSMessage isNavigationBarInNavigationControllerHidden:currentNavigationController])
{
[currentNavigationController.view insertSubview:currentView
belowSubview:[currentNavigationController navigationBar]];
verticalOffset = [currentNavigationController navigationBar].bounds.size.height;
if ([TSMessage iOS7StyleEnabled] || isViewIsUnderStatusBar) {
addStatusBarHeightToVerticalOffset();
}
}
else
{
[currentView.viewController.view addSubview:currentView];
if ([TSMessage iOS7StyleEnabled] || isViewIsUnderStatusBar) {
addStatusBarHeightToVerticalOffset();
}
}
}
else
{
[currentView.viewController.view addSubview:currentView];
if ([TSMessage iOS7StyleEnabled]) {
addStatusBarHeightToVerticalOffset();
}
}
CGPoint toPoint;
if (currentView.messagePosition == TSMessageNotificationPositionTop)
{
CGFloat navigationbarBottomOfViewController = 0;
if (currentView.delegate && [currentView.delegate respondsToSelector:@selector(navigationbarBottomOfViewController:)])
navigationbarBottomOfViewController = [currentView.delegate navigationbarBottomOfViewController:currentView.viewController];
toPoint = CGPointMake(currentView.center.x,
navigationbarBottomOfViewController + verticalOffset + CGRectGetHeight(currentView.frame) / 2.0);
}
else
{
CGFloat y = currentView.viewController.view.bounds.size.height - CGRectGetHeight(currentView.frame) / 2.0;
if (!currentView.viewController.navigationController.isToolbarHidden) {
y -= CGRectGetHeight(currentView.viewController.navigationController.toolbar.bounds);
}
toPoint = CGPointMake(currentView.center.x, y);
}
dispatch_block_t animationBlock = ^{
currentView.center = toPoint;
if (![TSMessage iOS7StyleEnabled]) {
currentView.alpha = TSMessageViewAlpha;
}
};
void(^completionBlock)(BOOL) = ^(BOOL finished) {
currentView.messageIsFullyDisplayed = YES;
};
if (![TSMessage iOS7StyleEnabled]) {
[UIView animateWithDuration:kTSMessageAnimationDuration
delay:0.0
options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction
animations:animationBlock
completion:completionBlock];
} else {
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
[UIView animateWithDuration:kTSMessageAnimationDuration + 0.1
delay:0
usingSpringWithDamping:0.8
initialSpringVelocity:0.f
options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction
animations:animationBlock
completion:completionBlock];
#endif
}
if (currentView.duration == TSMessageNotificationDurationAutomatic)
{
currentView.duration = kTSMessageAnimationDuration + kTSMessageDisplayTime + currentView.frame.size.height * kTSMessageExtraDisplayTimePerPixel;
}
if (currentView.duration != TSMessageNotificationDurationEndless)
{
dispatch_async(dispatch_get_main_queue(), ^
{
[self performSelector:@selector(fadeOutNotification:)
withObject:currentView
afterDelay:currentView.duration];
});
}
}
+ (BOOL)isNavigationBarInNavigationControllerHidden:(UINavigationController *)navController
{
if([navController isNavigationBarHidden]) {
return YES;
} else if ([[navController navigationBar] isHidden]) {
return YES;
} else {
return NO;
}
}
- (void)fadeOutNotification:(TSMessageView *)currentView
{
currentView.messageIsFullyDisplayed = NO;
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(fadeOutNotification:)
object:currentView];
CGPoint fadeOutToPoint;
if (currentView.messagePosition == TSMessageNotificationPositionTop)
{
fadeOutToPoint = CGPointMake(currentView.center.x, -CGRectGetHeight(currentView.frame)/2.f);
}
else
{
fadeOutToPoint = CGPointMake(currentView.center.x,
currentView.viewController.view.bounds.size.height + CGRectGetHeight(currentView.frame)/2.f);
}
[UIView animateWithDuration:kTSMessageAnimationDuration animations:^
{
currentView.center = fadeOutToPoint;
if (![TSMessage iOS7StyleEnabled]) {
currentView.alpha = 0.f;
}
} completion:^(BOOL finished)
{
[currentView removeFromSuperview];
if ([self.messages count] > 0)
{
[self.messages removeObjectAtIndex:0];
}
notificationActive = NO;
if ([self.messages count] > 0)
{
[self fadeInCurrentNotification];
}
}];
}
+ (BOOL)dismissActiveNotification
{
if ([[TSMessage sharedMessage].messages count] == 0) return NO;
dispatch_async(dispatch_get_main_queue(), ^
{
if ([[TSMessage sharedMessage].messages count] == 0) return;
TSMessageView *currentMessage = [[TSMessage sharedMessage].messages objectAtIndex:0];
if (currentMessage.messageIsFullyDisplayed)
{
[[TSMessage sharedMessage] fadeOutNotification:currentMessage];
}
});
return YES;
}
#pragma mark Customizing TSMessages
+ (void)setDefaultViewController:(UIViewController *)defaultViewController
{
_defaultViewController = defaultViewController;
}
+ (void)addCustomDesignFromFileWithName:(NSString *)fileName
{
[TSMessageView addNotificationDesignFromFile:fileName];
}
#pragma mark Other methods
+ (BOOL)isNotificationActive
{
return notificationActive;
}
+ (UIViewController *)defaultViewController
{
__strong UIViewController *defaultViewController = _defaultViewController;
if (!defaultViewController) {
NSLog(@"TSMessages: It is recommended to set a custom defaultViewController that is used to display the notifications");
defaultViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
}
return defaultViewController;
}
+ (BOOL)iOS7StyleEnabled
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
// Decide wheter to use iOS 7 style or not based on the running device and the base sdk
BOOL iOS7SDK = NO;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
iOS7SDK = YES;
#endif
_useiOS7Style = ! (TS_SYSTEM_VERSION_LESS_THAN(@"7.0") || !iOS7SDK);
});
return _useiOS7Style;
}
@end