-
Notifications
You must be signed in to change notification settings - Fork 325
/
MTStatusBarOverlay.m
executable file
·1762 lines (1477 loc) · 79.9 KB
/
MTStatusBarOverlay.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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// MTStatusBarOverlay.m
//
// Created by Matthias Tretter on 27.09.10.
// Copyright (c) 2009-2011 Matthias Tretter, @myell0w. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
// Credits go to:
// -------------------------------
// http://stackoverflow.com/questions/2833724/adding-view-on-statusbar-in-iphone
// http://www.cocoabyss.com/uikit/custom-status-bar-ios/
// @reederapp for inspiration
// -------------------------------
#import "MTStatusBarOverlay.h"
#import <QuartzCore/QuartzCore.h>
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Functions
////////////////////////////////////////////////////////////////////////
NSData* MTStatusBarBackgroundImageData(BOOL shrinked);
unsigned char* MTStatusBarBackgroundImageArray(BOOL shrinked);
unsigned int MTStatusBarBackgroundImageLength(BOOL shrinked);
void mt_dispatch_sync_on_main_thread(dispatch_block_t block);
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Defines
////////////////////////////////////////////////////////////////////////
// macro to check if running on at least iOS 8
#define kMTIsOperatingSystemAtLeast8 [[NSProcessInfo processInfo] respondsToSelector:@selector(isOperatingSystemAtLeastVersion:)] ? [[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:(NSOperatingSystemVersion){8.0, 0.0, 0.0}] : NO
// the height of the status bar
#define kStatusBarHeight 20.f
// width of the screen in present orientation
#define kScreenWidth ((kMTIsOperatingSystemAtLeast8) ? [UIScreen mainScreen].nativeBounds.size.width/[UIScreen mainScreen].nativeScale : [UIScreen mainScreen].bounds.size.width)
// height of the screen in present orientation
#define kScreenHeight ((kMTIsOperatingSystemAtLeast8) ? [UIScreen mainScreen].nativeBounds.size.height/[UIScreen mainScreen].nativeScale : [UIScreen mainScreen].bounds.size.height)
// macro for checking if we are on the iPad
#define IsIPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
// macro for checking if we are on the iPad in iPhone-Emulation mode
#define IsIPhoneEmulationMode (!IsIPad && \
MAX([UIApplication sharedApplication].statusBarFrame.size.width, [UIApplication sharedApplication].statusBarFrame.size.height) > 480.f)
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Customization
////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////
// Light Theme (for UIStatusBarStyleDefault)
///////////////////////////////////////////////////////
#define kLightThemeTextColor [UIColor blackColor]
#define kLightThemeErrorMessageTextColor [UIColor blackColor] // [UIColor colorWithRed:0.494898f green:0.330281f blue:0.314146f alpha:1.0f]
#define kLightThemeFinishedMessageTextColor [UIColor blackColor] // [UIColor colorWithRed:0.389487f green:0.484694f blue:0.38121f alpha:1.0f]
#define kLightThemeShadowColor [UIColor whiteColor]
#define kLightThemeErrorMessageShadowColor [UIColor whiteColor]
#define kLightThemeFinishedMessageShadowColor [UIColor whiteColor]
#define kLightThemeActivityIndicatorViewStyle UIActivityIndicatorViewStyleGray
#define kLightThemeDetailViewBackgroundColor [UIColor blackColor]
#define kLightThemeDetailViewBorderColor [UIColor darkGrayColor]
#define kLightThemeHistoryTextColor [UIColor colorWithRed:0.749f green:0.749f blue:0.749f alpha:1.0f]
///////////////////////////////////////////////////////
// Dark Theme (for UIStatusBarStyleBlackOpaque)
///////////////////////////////////////////////////////
#define kDarkThemeTextColor [UIColor colorWithRed:0.749f green:0.749f blue:0.749f alpha:1.0f]
#define kDarkThemeErrorMessageTextColor [UIColor colorWithRed:0.749f green:0.749f blue:0.749f alpha:1.0f] // [UIColor colorWithRed:0.918367f green:0.48385f blue:0.423895f alpha:1.0f]
#define kDarkThemeFinishedMessageTextColor [UIColor colorWithRed:0.749f green:0.749f blue:0.749f alpha:1.0f] // [UIColor colorWithRed:0.681767f green:0.918367f blue:0.726814f alpha:1.0f]
#define kDarkThemeActivityIndicatorViewStyle UIActivityIndicatorViewStyleWhite
#define kDarkThemeDetailViewBackgroundColor [UIColor colorWithRed:0.3f green:0.3f blue:0.3f alpha:1.0f]
#define kDarkThemeDetailViewBorderColor [UIColor whiteColor]
#define kDarkThemeHistoryTextColor [UIColor whiteColor]
///////////////////////////////////////////////////////
// Progress
///////////////////////////////////////////////////////
#define kProgressViewAlpha 0.4f
#define kProgressViewBackgroundColor [UIColor colorWithRed:0.0f green:0.0f blue:0.0f alpha:1.0f]
///////////////////////////////////////////////////////
// Animations
///////////////////////////////////////////////////////
// minimum time that a message is shown, when messages are queued
#define kMinimumMessageVisibleTime 0.4f
// duration of the animation to show next status message in seconds
#define kNextStatusAnimationDuration 0.6f
// duration the statusBarOverlay takes to appear when it was hidden
#define kAppearAnimationDuration 0.5f
// animation duration of animation mode shrink
#define kAnimationDurationShrink 0.3f
// animation duration of animation mode fallDown
#define kAnimationDurationFallDown 0.4f
// animation duration of change of progressView-size
#define kUpdateProgressViewDuration 0.2f
// delay after that the status bar gets visible again after rotation
#define kRotationAppearDelay [UIApplication sharedApplication].statusBarOrientationAnimationDuration
///////////////////////////////////////////////////////
// Text
///////////////////////////////////////////////////////
// Text that is displayed in the finished-Label when the finish was successful
#define kFinishedText @"✓"
#define kFinishedFontSize 22.f
// Text that is displayed when an error occured
#define kErrorText @"✗"
#define kErrorFontSize 19.f
///////////////////////////////////////////////////////
// Detail View
///////////////////////////////////////////////////////
#define kHistoryTableRowHeight 25.f
#define kMaxHistoryTableRowCount 5
#define kDetailViewAlpha 0.9f
#define kDetailViewWidth (IsIPad ? 400.f : 280.f)
// default frame of detail view when it is hidden
#define kDefaultDetailViewFrame CGRectMake((kScreenWidth - kDetailViewWidth)/2, -(kHistoryTableRowHeight*kMaxHistoryTableRowCount + kStatusBarHeight),\
kDetailViewWidth, kHistoryTableRowHeight*kMaxHistoryTableRowCount + kStatusBarHeight)
///////////////////////////////////////////////////////
// Size
///////////////////////////////////////////////////////
// Size of the text in the status labels
#define kStatusLabelSize 12.f
// default-width of the small-mode
#define kWidthSmall 26.f
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Class Extension
////////////////////////////////////////////////////////////////////////
@interface MTStatusBarOverlay ()
@property (nonatomic, strong) UIActivityIndicatorView *activityIndicator;
@property (nonatomic, strong) UIImageView *statusBarBackgroundImageView;
@property (nonatomic, strong) UILabel *statusLabel1;
@property (nonatomic, strong) UILabel *statusLabel2;
@property (nonatomic, unsafe_unretained) UILabel *hiddenStatusLabel;
@property (unsafe_unretained, nonatomic, readonly) UILabel *visibleStatusLabel;
@property (nonatomic, strong) UIImageView *progressView;
@property (nonatomic, assign) CGRect oldBackgroundViewFrame;
// overwrite property for read-write-access
@property (assign, getter=isHideInProgress) BOOL hideInProgress;
@property (assign, getter=isActive) BOOL active;
// read out hidden-state using alpha-value and hidden-property
@property (nonatomic, readonly, getter=isReallyHidden) BOOL reallyHidden;
@property (nonatomic, strong) UITextView *detailTextView;
@property (nonatomic, strong) NSMutableArray *messageQueue;
// overwrite property for read-write-access
@property (nonatomic, strong) NSMutableArray *messageHistory;
@property (nonatomic, strong) UITableView *historyTableView;
@property (nonatomic, assign) BOOL forcedToHide;
// intern method that posts a new entry to the message-queue
- (void)postMessage:(NSString *)message type:(MTMessageType)messageType duration:(NSTimeInterval)duration animated:(BOOL)animated immediate:(BOOL)immediate;
// intern method that clears the messageQueue and then posts a new entry to it
- (void)postImmediateMessage:(NSString *)message type:(MTMessageType)messageType duration:(NSTimeInterval)duration animated:(BOOL)animated;
// intern method that does all the work of showing the next message in the queue
- (void)showNextMessage;
// is called when the user touches the statusbar
- (void)contentViewClicked:(UIGestureRecognizer *)gestureRecognizer;
// is called when the user swipes down the statusbar
- (void)contentViewSwipedUp:(UIGestureRecognizer *)gestureRecognizer;
- (void)contentViewSwipedDown:(UIGestureRecognizer *)gestureRecognizer;
// updates the current status bar background image for the given style and current size
- (void)setStatusBarBackgroundForStyle:(UIStatusBarStyle)style;
// updates the text-colors of the labels for the given style and message type
- (void)setColorSchemeForStatusBarStyle:(UIStatusBarStyle)style messageType:(MTMessageType)messageType;
// updates the visiblity of the activity indicator and finished-label depending on the type
- (void)updateUIForMessageType:(MTMessageType)messageType duration:(NSTimeInterval)duration;
// updates the size of the progressView to always cover only the displayed text-frame
- (void)updateProgressViewSizeForLabel:(UILabel *)label;
// calls the delegate when a switch from one message to another one occured
- (void)callDelegateWithNewMessage:(NSString *)newMessage;
// update the height of the detail text view according to new text
- (void)updateDetailTextViewHeight;
// shrink/expand the overlay
- (void)setShrinked:(BOOL)shrinked animated:(BOOL)animated;
// set hidden-state using alpha-value instead of hidden-property
- (void)setHidden:(BOOL)hidden useAlpha:(BOOL)useAlpha;
// used for performSelector:withObject:
- (void)setHiddenUsingAlpha:(BOOL)hidden;
// set hidden-state of detailView
- (void)setDetailViewHidden:(BOOL)hidden animated:(BOOL)animated;
// History-tracking
- (void)addMessageToHistory:(NSString *)message;
- (void)clearHistory;
// selectors
- (void)rotateToStatusBarFrame:(NSValue *)statusBarFrameValue;
- (void)didChangeStatusBarFrame:(NSNotification *)notification;
// Fix to not overlay Notification Center
- (void)applicationDidBecomeActive:(NSNotification *)notifaction;
- (void)applicationWillResignActive:(NSNotification *)notifaction;
// returns the current frame for the detail view depending on the interface orientation
- (CGRect)backgroundViewFrameForStatusBarInterfaceOrientation;
@end
@implementation MTStatusBarOverlay
@synthesize backgroundView = backgroundView_;
@synthesize detailView = detailView_;
@synthesize statusBarBackgroundImageView = statusBarBackgroundImageView_;
@synthesize statusLabel1 = statusLabel1_;
@synthesize statusLabel2 = statusLabel2_;
@synthesize hiddenStatusLabel = hiddenStatusLabel_;
@synthesize progress = progress_;
@synthesize progressView = progressView_;
@synthesize activityIndicator = activityIndicator_;
@synthesize finishedLabel = finishedLabel_;
@synthesize hidesActivity = hidesActivity_;
@synthesize defaultStatusBarImage = defaultStatusBarImage_;
@synthesize defaultStatusBarImageShrinked = defaultStatusBarImageShrinked_;
@synthesize smallFrame = smallFrame_;
@synthesize oldBackgroundViewFrame = oldBackgroundViewFrame_;
@synthesize animation = animation_;
@synthesize hideInProgress = hideInProgress_;
@synthesize active = active_;
@synthesize messageQueue = messageQueue_;
@synthesize canRemoveImmediateMessagesFromQueue = canRemoveImmediateMessagesFromQueue_;
@synthesize detailViewMode = detailViewMode_;
@synthesize detailText = detailText_;
@synthesize detailTextView = detailTextView_;
@synthesize messageHistory = messageHistory_;
@synthesize historyTableView = historyTableView_;
@synthesize delegate = delegate_;
@synthesize forcedToHide = forcedToHide_;
@synthesize lastPostedMessage = lastPostedMessage_;
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Lifecycle
////////////////////////////////////////////////////////////////////////
- (id)initWithFrame:(CGRect)frame {
if ((self = [super initWithFrame:frame])) {
CGRect statusBarFrame = [UIApplication sharedApplication].statusBarFrame;
// only use height of 20px even is status bar is doubled
statusBarFrame.size.height = statusBarFrame.size.height == 2*kStatusBarHeight ? kStatusBarHeight : statusBarFrame.size.height;
// if we are on the iPad but in iPhone-Mode (non-universal-app) correct the width
if(IsIPhoneEmulationMode) {
statusBarFrame.size.width = 320.f;
}
// Place the window on the correct level and position
self.windowLevel = UIWindowLevelStatusBar+1.f;
self.frame = statusBarFrame;
self.alpha = 0.f;
self.hidden = NO;
// Default Small size: just show Activity Indicator
smallFrame_ = CGRectMake(statusBarFrame.size.width - kWidthSmall, 0.f, kWidthSmall, statusBarFrame.size.height);
// Default-values
animation_ = MTStatusBarOverlayAnimationNone;
active_ = NO;
hidesActivity_ = NO;
forcedToHide_ = NO;
// the detail view that is shown when the user touches the status bar in animation mode "FallDown"
detailView_ = [[UIView alloc] initWithFrame:kDefaultDetailViewFrame];
detailView_.backgroundColor = [UIColor blackColor];
detailView_.alpha = kDetailViewAlpha;
detailView_.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
detailViewMode_ = MTDetailViewModeCustom;
// add rounded corners to detail-view
detailView_.layer.masksToBounds = YES;
detailView_.layer.cornerRadius = 10.f;
detailView_.layer.borderWidth = 2.5f;
// add shadow
/*detailView_.layer.shadowColor = [UIColor blackColor].CGColor;
detailView_.layer.shadowOpacity = 1.0f;
detailView_.layer.shadowRadius = 6.0f;
detailView_.layer.shadowOffset = CGSizeMake(0, 3);*/
// Detail Text label
detailTextView_ = [[UITextView alloc] initWithFrame:CGRectMake(0, kStatusBarHeight,
kDefaultDetailViewFrame.size.width, kDefaultDetailViewFrame.size.height - kStatusBarHeight)];
detailTextView_.backgroundColor = [UIColor clearColor];
detailTextView_.userInteractionEnabled = NO;
detailTextView_.hidden = detailViewMode_ != MTDetailViewModeDetailText;
[detailView_ addSubview:detailTextView_];
// Message History
messageHistory_ = [[NSMutableArray alloc] init];
historyTableView_ = [[UITableView alloc] initWithFrame:CGRectMake(0, kStatusBarHeight,
kDefaultDetailViewFrame.size.width, kDefaultDetailViewFrame.size.height - kStatusBarHeight)];
historyTableView_.dataSource = self;
historyTableView_.delegate = nil;
historyTableView_.rowHeight = kHistoryTableRowHeight;
historyTableView_.separatorStyle = UITableViewCellSeparatorStyleNone;
// make table view-background transparent
historyTableView_.backgroundColor = [UIColor clearColor];
historyTableView_.opaque = NO;
historyTableView_.hidden = detailViewMode_ != MTDetailViewModeHistory;
historyTableView_.backgroundView = nil;
[detailView_ addSubview:historyTableView_];
[self addSubview:detailView_];
// Create view that stores all the content
CGRect backgroundFrame = [self backgroundViewFrameForStatusBarInterfaceOrientation];
backgroundView_ = [[UIView alloc] initWithFrame:backgroundFrame];
backgroundView_.clipsToBounds = YES;
backgroundView_.autoresizingMask = UIViewAutoresizingFlexibleWidth;
oldBackgroundViewFrame_ = backgroundView_.frame;
// Add gesture recognizers
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(contentViewClicked:)];
//UISwipeGestureRecognizer *upGestureRecognizer = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(contentViewSwipedUp:)] autorelease];
//UISwipeGestureRecognizer *downGestureRecognizer = [[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(contentViewSwipedDown:)] autorelease];
//upGestureRecognizer.direction = UISwipeGestureRecognizerDirectionUp;
//downGestureRecognizer.direction = UISwipeGestureRecognizerDirectionDown;
[backgroundView_ addGestureRecognizer:tapGestureRecognizer];
//[detailView_ addGestureRecognizer:upGestureRecognizer];
//[self addGestureRecognizer:downGestureRecognizer];
// Images used as background when status bar style is Default
defaultStatusBarImage_ = [UIImage imageWithData:MTStatusBarBackgroundImageData(NO)];
defaultStatusBarImageShrinked_ = [UIImage imageWithData:MTStatusBarBackgroundImageData(YES)];
// Background-Image of the Content View
statusBarBackgroundImageView_ = [[UIImageView alloc] initWithFrame:backgroundView_.frame];
statusBarBackgroundImageView_.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self addSubviewToBackgroundView:statusBarBackgroundImageView_];
// Activity Indicator
activityIndicator_ = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
activityIndicator_.frame = CGRectMake(6.f, 3.f, backgroundView_.frame.size.height - 6.f, backgroundView_.frame.size.height - 6.f);
activityIndicator_.hidesWhenStopped = YES;
// iOS 5 doesn't correctly resize the activityIndicator. Bug?
if ([activityIndicator_ respondsToSelector:@selector(setColor:)]) {
[activityIndicator_.layer setValue:[NSNumber numberWithFloat:0.75f] forKeyPath:@"transform.scale"];
}
[self addSubviewToBackgroundView:activityIndicator_];
// Finished-Label
finishedLabel_ = [[UILabel alloc] initWithFrame:CGRectMake(4.f,1.f,backgroundView_.frame.size.height, backgroundView_.frame.size.height-1.f)];
finishedLabel_.shadowOffset = CGSizeMake(0.f, 1.f);
finishedLabel_.backgroundColor = [UIColor clearColor];
finishedLabel_.hidden = YES;
finishedLabel_.text = kFinishedText;
#ifdef __IPHONE_6_0
finishedLabel_.textAlignment = NSTextAlignmentCenter;
#else
finishedLabel_.textAlignment = UITextAlignmentCenter;
#endif
finishedLabel_.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:kFinishedFontSize];
finishedLabel_.adjustsFontSizeToFitWidth = YES;
[self addSubviewToBackgroundView:finishedLabel_];
// Status Label 1 is first visible
statusLabel1_ = [[UILabel alloc] initWithFrame:CGRectMake(30.f, 0.f, backgroundView_.frame.size.width - 60.f,backgroundView_.frame.size.height-1.f)];
statusLabel1_.backgroundColor = [UIColor clearColor];
statusLabel1_.shadowOffset = CGSizeMake(0.f, 1.f);
statusLabel1_.font = [UIFont boldSystemFontOfSize:kStatusLabelSize];
statusLabel1_.numberOfLines = 1;
#ifdef __IPHONE_6_0
statusLabel1_.textAlignment = NSTextAlignmentCenter;
statusLabel1_.lineBreakMode = NSLineBreakByTruncatingTail;
#else
statusLabel1_.textAlignment = UITextAlignmentCenter;
statusLabel1_.lineBreakMode = UILineBreakModeTailTruncation;
#endif
statusLabel1_.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self addSubviewToBackgroundView:statusLabel1_];
// Status Label 2 is hidden
statusLabel2_ = [[UILabel alloc] initWithFrame:CGRectMake(30.f, backgroundView_.frame.size.height,backgroundView_.frame.size.width - 60.f , backgroundView_.frame.size.height-1.f)];
statusLabel2_.shadowOffset = CGSizeMake(0.f, 1.f);
statusLabel2_.backgroundColor = [UIColor clearColor];
statusLabel2_.font = [UIFont boldSystemFontOfSize:kStatusLabelSize];
statusLabel2_.numberOfLines = 1;
#ifdef __IPHONE_6_0
statusLabel2_.textAlignment = NSTextAlignmentCenter;
statusLabel2_.lineBreakMode = NSLineBreakByTruncatingTail;
#else
statusLabel2_.textAlignment = UITextAlignmentCenter;
statusLabel2_.lineBreakMode = UILineBreakModeTailTruncation;
#endif
statusLabel2_.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self addSubviewToBackgroundView:statusLabel2_];
// the hidden status label at the beginning
hiddenStatusLabel_ = statusLabel2_;
progress_ = 1.0;
progressView_ = [[UIImageView alloc] initWithFrame:statusBarBackgroundImageView_.frame];
progressView_.opaque = NO;
progressView_.hidden = YES;
progressView_.alpha = kProgressViewAlpha;
[self addSubviewToBackgroundView:progressView_];
messageQueue_ = [[NSMutableArray alloc] init];
canRemoveImmediateMessagesFromQueue_ = YES;
[self addSubview:backgroundView_];
// listen for changes of status bar frame
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didChangeStatusBarFrame:)
name:UIApplicationWillChangeStatusBarFrameNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification object:nil];
// initial rotation, fixes the issue with a wrong bar appearance in landscape only mode
[self rotateToStatusBarFrame:nil];
}
return self;
}
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
delegate_ = nil;
}
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark UIWindow
////////////////////////////////////////////////////////////////////////
- (UIViewController *)rootViewController {
return [UIApplication sharedApplication].delegate.window.rootViewController;
}
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Status Bar Appearance
////////////////////////////////////////////////////////////////////////
- (void)addSubviewToBackgroundView:(UIView *)view {
view.userInteractionEnabled = NO;
[self.backgroundView addSubview:view];
}
- (void)addSubviewToBackgroundView:(UIView *)view atIndex:(NSInteger)index {
view.userInteractionEnabled = NO;
[self.backgroundView insertSubview:view atIndex:index];
}
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Save/Restore current State
////////////////////////////////////////////////////////////////////////
- (void)saveState {
[self saveStateSynchronized:YES];
}
- (void)saveStateSynchronized:(BOOL)synchronizeAtEnd {
// TODO: save more state
[[NSUserDefaults standardUserDefaults] setBool:self.shrinked forKey:kMTStatusBarOverlayStateShrinked];
if (synchronizeAtEnd) {
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
- (void)restoreState {
// restore shrinked-state
[self setShrinked:[[NSUserDefaults standardUserDefaults] boolForKey:kMTStatusBarOverlayStateShrinked] animated:NO];
}
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Message Posting
////////////////////////////////////////////////////////////////////////
- (void)postMessage:(NSString *)message {
[self postMessage:message animated:YES];
}
- (void)postMessage:(NSString *)message animated:(BOOL)animated {
[self postMessage:message type:MTMessageTypeActivity duration:0 animated:animated immediate:NO];
}
- (void)postMessage:(NSString *)message duration:(NSTimeInterval)duration {
[self postMessage:message type:MTMessageTypeActivity duration:duration animated:YES immediate:NO];
}
- (void)postMessage:(NSString *)message duration:(NSTimeInterval)duration animated:(BOOL)animated {
[self postMessage:message type:MTMessageTypeActivity duration:duration animated:animated immediate:NO];
}
- (void)postImmediateMessage:(NSString *)message animated:(BOOL)animated {
[self postImmediateMessage:message type:MTMessageTypeActivity duration:0 animated:animated];
}
- (void)postImmediateMessage:(NSString *)message duration:(NSTimeInterval)duration {
[self postImmediateMessage:message type:MTMessageTypeActivity duration:duration animated:YES];
}
- (void)postImmediateMessage:(NSString *)message duration:(NSTimeInterval)duration animated:(BOOL)animated {
[self postImmediateMessage:message type:MTMessageTypeActivity duration:duration animated:animated];
}
- (void)postFinishMessage:(NSString *)message duration:(NSTimeInterval)duration {
[self postFinishMessage:message duration:duration animated:YES];
}
- (void)postFinishMessage:(NSString *)message duration:(NSTimeInterval)duration animated:(BOOL)animated {
[self postMessage:message type:MTMessageTypeFinish duration:duration animated:animated immediate:NO];
}
- (void)postImmediateFinishMessage:(NSString *)message duration:(NSTimeInterval)duration animated:(BOOL)animated {
[self postImmediateMessage:message type:MTMessageTypeFinish duration:duration animated:animated];
}
- (void)postErrorMessage:(NSString *)message duration:(NSTimeInterval)duration {
[self postErrorMessage:message duration:duration animated:YES];
}
- (void)postErrorMessage:(NSString *)message duration:(NSTimeInterval)duration animated:(BOOL)animated {
[self postMessage:message type:MTMessageTypeError duration:duration animated:animated immediate:NO];
}
- (void)postImmediateErrorMessage:(NSString *)message duration:(NSTimeInterval)duration animated:(BOOL)animated {
[self postImmediateMessage:message type:MTMessageTypeError duration:duration animated:animated];
}
- (void)postMessageDictionary:(NSDictionary *)messageDictionary {
[self postMessage:[messageDictionary valueForKey:kMTStatusBarOverlayMessageKey]
type:[[messageDictionary valueForKey:kMTStatusBarOverlayMessageTypeKey] intValue]
duration:[[messageDictionary valueForKey:kMTStatusBarOverlayDurationKey] doubleValue]
animated:[[messageDictionary valueForKey:kMTStatusBarOverlayAnimationKey] boolValue]
immediate:[[messageDictionary valueForKey:kMTStatusBarOverlayImmediateKey] boolValue]];
}
- (void)postMessage:(NSString *)message type:(MTMessageType)messageType duration:(NSTimeInterval)duration animated:(BOOL)animated immediate:(BOOL)immediate {
mt_dispatch_sync_on_main_thread(^{
// don't add to queue when message is empty
if (message.length == 0) {
return;
}
NSDictionary *messageDictionaryRepresentation = [NSDictionary dictionaryWithObjectsAndKeys:message, kMTStatusBarOverlayMessageKey,
[NSNumber numberWithInt:messageType], kMTStatusBarOverlayMessageTypeKey,
[NSNumber numberWithDouble:duration], kMTStatusBarOverlayDurationKey,
[NSNumber numberWithBool:animated], kMTStatusBarOverlayAnimationKey,
[NSNumber numberWithBool:immediate], kMTStatusBarOverlayImmediateKey, nil];
@synchronized (self.messageQueue) {
[self.messageQueue insertObject:messageDictionaryRepresentation atIndex:0];
}
// if the overlay is currently not active, begin with showing of messages
if (!self.active) {
[self showNextMessage];
}
});
}
- (void)postImmediateMessage:(NSString *)message type:(MTMessageType)messageType duration:(NSTimeInterval)duration animated:(BOOL)animated {
@synchronized(self.messageQueue) {
NSMutableArray *clearedMessages = [NSMutableArray array];
for (id messageDictionary in self.messageQueue) {
if (messageDictionary != [self.messageQueue lastObject] &&
(self.canRemoveImmediateMessagesFromQueue || [[messageDictionary valueForKey:kMTStatusBarOverlayImmediateKey] boolValue] == NO)) {
[clearedMessages addObject:messageDictionary];
}
}
[self.messageQueue removeObjectsInArray:clearedMessages];
// call delegate
if ([self.delegate respondsToSelector:@selector(statusBarOverlayDidClearMessageQueue:)] && clearedMessages.count > 0) {
[self.delegate statusBarOverlayDidClearMessageQueue:clearedMessages];
}
}
[self postMessage:message type:messageType duration:duration animated:animated immediate:YES];
}
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Showing Next Message
////////////////////////////////////////////////////////////////////////
- (void)showNextMessage {
if (self.forcedToHide) {
return;
}
// if there is no next message to show overlay is not active anymore
@synchronized(self.messageQueue) {
if([self.messageQueue count] < 1) {
self.active = NO;
return;
}
}
// there is a next message, overlay is active
self.active = YES;
NSDictionary *nextMessageDictionary = nil;
// read out next message
@synchronized(self.messageQueue) {
nextMessageDictionary = [self.messageQueue lastObject];
}
NSString *message = [nextMessageDictionary valueForKey:kMTStatusBarOverlayMessageKey];
MTMessageType messageType = (MTMessageType)[[nextMessageDictionary valueForKey:kMTStatusBarOverlayMessageTypeKey] intValue];
NSTimeInterval duration = (NSTimeInterval)[[nextMessageDictionary valueForKey:kMTStatusBarOverlayDurationKey] doubleValue];
BOOL animated = [[nextMessageDictionary valueForKey:kMTStatusBarOverlayAnimationKey] boolValue];
// don't show anything if status bar is hidden (queue gets cleared)
if([UIApplication sharedApplication].statusBarHidden) {
@synchronized(self.messageQueue) {
[self.messageQueue removeAllObjects];
}
self.active = NO;
return;
}
// don't duplicate animation if already displaying with text
if (!self.reallyHidden && [self.visibleStatusLabel.text isEqualToString:message]) {
// remove unneccesary message
@synchronized(self.messageQueue) {
if (self.messageQueue.count > 0)
[self.messageQueue removeLastObject];
}
// show the next message w/o delay
[self showNextMessage];
return;
}
// cancel previous hide- and clear requests
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hide) object:nil];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(clearHistory) object:nil];
// update UI depending on current status bar style
UIStatusBarStyle statusBarStyle = [UIApplication sharedApplication].statusBarStyle;
[self setStatusBarBackgroundForStyle:statusBarStyle];
[self setColorSchemeForStatusBarStyle:statusBarStyle messageType:messageType];
[self updateUIForMessageType:messageType duration:duration];
// if status bar is currently hidden, show it unless it is forced to hide
if (self.reallyHidden) {
// clear currently visible status label
self.visibleStatusLabel.text = @"";
// show status bar overlay with animation
[UIView animateWithDuration:self.shrinked ? 0 : kAppearAnimationDuration
animations:^{
[self setHidden:NO useAlpha:YES];
}];
}
if (animated) {
// set text of currently not visible label to new text
self.hiddenStatusLabel.text = message;
// update progressView to only cover displayed text
[self updateProgressViewSizeForLabel:self.hiddenStatusLabel];
// position hidden status label under visible status label
self.hiddenStatusLabel.frame = CGRectMake(self.hiddenStatusLabel.frame.origin.x,
kStatusBarHeight,
self.hiddenStatusLabel.frame.size.width,
self.hiddenStatusLabel.frame.size.height);
// animate hidden label into user view and visible status label out of view
[UIView animateWithDuration:kNextStatusAnimationDuration
delay:0
options:UIViewAnimationOptionCurveEaseInOut | UIViewAnimationOptionAllowUserInteraction
animations:^{
// move both status labels up
self.statusLabel1.frame = CGRectMake(self.statusLabel1.frame.origin.x,
self.statusLabel1.frame.origin.y - kStatusBarHeight,
self.statusLabel1.frame.size.width,
self.statusLabel1.frame.size.height);
self.statusLabel2.frame = CGRectMake(self.statusLabel2.frame.origin.x,
self.statusLabel2.frame.origin.y - kStatusBarHeight,
self.statusLabel2.frame.size.width,
self.statusLabel2.frame.size.height);
}
completion:^(BOOL finished) {
// add old message to history
[self addMessageToHistory:self.visibleStatusLabel.text];
// after animation, set new hidden status label indicator
if (self.hiddenStatusLabel == self.statusLabel1) {
self.hiddenStatusLabel = self.statusLabel2;
} else {
self.hiddenStatusLabel = self.statusLabel1;
}
// remove the message from the queue
@synchronized(self.messageQueue) {
if (self.messageQueue.count > 0)
[self.messageQueue removeLastObject];
}
// inform delegate about message-switch
[self callDelegateWithNewMessage:message];
// show the next message
[self performSelector:@selector(showNextMessage) withObject:nil afterDelay:kMinimumMessageVisibleTime];
}];
}
// w/o animation just save old text and set new one
else {
// add old message to history
[self addMessageToHistory:self.visibleStatusLabel.text];
// set new text
self.visibleStatusLabel.text = message;
// update progressView to only cover displayed text
[self updateProgressViewSizeForLabel:self.visibleStatusLabel];
// remove the message from the queue
@synchronized(self.messageQueue) {
if (self.messageQueue.count > 0)
[self.messageQueue removeLastObject];
}
// inform delegate about message-switch
[self callDelegateWithNewMessage:message];
// show next message
[self performSelector:@selector(showNextMessage) withObject:nil afterDelay:kMinimumMessageVisibleTime];
}
self.lastPostedMessage = message;
}
- (void)hide {
[self.activityIndicator stopAnimating];
self.statusLabel1.text = @"";
self.statusLabel2.text = @"";
self.hideInProgress = NO;
// cancel previous hide- and clear requests
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(hide) object:nil];
// hide detailView
[self setDetailViewHidden:YES animated:YES];
// hide status bar overlay with animation
[UIView animateWithDuration:self.shrinked ? 0. : kAppearAnimationDuration
delay:0
options:UIViewAnimationOptionAllowUserInteraction
animations:^{
[self setHidden:YES useAlpha:YES];
} completion:^(BOOL finished) {
// call delegate
if ([self.delegate respondsToSelector:@selector(statusBarOverlayDidHide)]) {
[self.delegate statusBarOverlayDidHide];
}
}];
}
- (void)hideTemporary {
self.forcedToHide = YES;
// hide status bar overlay with animation
[UIView animateWithDuration:self.shrinked ? 0. : kAppearAnimationDuration animations:^{
[self setHidden:YES useAlpha:YES];
}];
}
// this shows the status bar overlay, if there is text to show
- (void)show {
self.forcedToHide = NO;
if (self.reallyHidden) {
if (self.visibleStatusLabel.text.length > 0) {
// show status bar overlay with animation
[UIView animateWithDuration:self.shrinked ? 0. : kAppearAnimationDuration animations:^{
[self setHidden:NO useAlpha:YES];
}];
}
[self showNextMessage];
}
}
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Rotation
////////////////////////////////////////////////////////////////////////
- (void)didChangeStatusBarFrame:(NSNotification *)notification {
NSValue * statusBarFrameValue = [notification.userInfo valueForKey:UIApplicationStatusBarFrameUserInfoKey];
// TODO: react on changes of status bar height (e.g. incoming call, tethering, ...)
// NSLog(@"Status bar frame changed: %@", NSStringFromCGRect([statusBarFrameValue CGRectValue]));
// have to use performSelector to prohibit animation of rotation
[self performSelector:@selector(rotateToStatusBarFrame:) withObject:statusBarFrameValue afterDelay:0];
}
- (void)rotateToStatusBarFrame:(NSValue *)statusBarFrameValue {
// current interface orientation
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
// is the statusBar visible before rotation?
BOOL visibleBeforeTransformation = !self.reallyHidden;
// store a flag, if the StatusBar is currently shrinked
BOOL shrinkedBeforeTransformation = self.shrinked;
// hide and then unhide after rotation
if (visibleBeforeTransformation) {
[self setHidden:YES useAlpha:YES];
[self setDetailViewHidden:YES animated:NO];
}
CGFloat pi = (CGFloat)M_PI;
if (orientation == UIDeviceOrientationPortrait) {
self.transform = CGAffineTransformIdentity;
self.frame = CGRectMake(0.f,0.f,kScreenWidth,kStatusBarHeight);
self.smallFrame = CGRectMake(self.frame.size.width - kWidthSmall, 0.0f, kWidthSmall, self.frame.size.height);
}else if (orientation == UIDeviceOrientationLandscapeLeft) {
self.transform = CGAffineTransformMakeRotation(pi * (90.f) / 180.0f);
self.frame = CGRectMake(kScreenWidth - kStatusBarHeight,0, kStatusBarHeight, kScreenHeight);
self.smallFrame = CGRectMake(kScreenHeight-kWidthSmall,0,kWidthSmall,kStatusBarHeight);
} else if (orientation == UIDeviceOrientationLandscapeRight) {
self.transform = CGAffineTransformMakeRotation(pi * (-90.f) / 180.0f);
self.frame = CGRectMake(0.f,0.f, kStatusBarHeight, kScreenHeight);
self.smallFrame = CGRectMake(kScreenHeight-kWidthSmall,0.f, kWidthSmall, kStatusBarHeight);
} else if (orientation == UIDeviceOrientationPortraitUpsideDown) {
self.transform = CGAffineTransformMakeRotation(pi);
self.frame = CGRectMake(0.f,kScreenHeight - kStatusBarHeight,kScreenWidth,kStatusBarHeight);
self.smallFrame = CGRectMake(self.frame.size.width - kWidthSmall, 0.f, kWidthSmall, self.frame.size.height);
}
self.backgroundView.frame = [self backgroundViewFrameForStatusBarInterfaceOrientation];
// if the statusBar is currently shrinked, update the frames for the new rotation state
if (shrinkedBeforeTransformation) {
// the oldBackgroundViewFrame is the frame of the whole StatusBar
self.oldBackgroundViewFrame = CGRectMake(0.f,0.f,UIInterfaceOrientationIsPortrait(orientation) ? kScreenWidth : kScreenHeight,kStatusBarHeight);
// the backgroundView gets the newly computed smallFrame
self.backgroundView.frame = self.smallFrame;
}
// make visible after given time
if (visibleBeforeTransformation) {
// TODO:
// somehow this doesn't work anymore since rotation-method was changed from
// DeviceDidRotate-Notification to StatusBarFrameChanged-Notification
// therefore iplemented it with a UIView-Animation instead
//[self performSelector:@selector(setHiddenUsingAlpha:) withObject:[NSNumber numberWithBool:NO] afterDelay:kRotationAppearDelay];
[UIView animateWithDuration:kAppearAnimationDuration
delay:kRotationAppearDelay
options:UIViewAnimationOptionCurveEaseInOut
animations:^{
[self setHiddenUsingAlpha:NO];
}
completion:NULL];
}
}
////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Setter/Getter
////////////////////////////////////////////////////////////////////////
- (void)setProgress:(double)progress {
// bound progress to 0.0 - 1.0
progress = MAX(0.0, MIN(progress, 1.0));
// do not decrease progress if it is no reset
if (progress == 0.0 || progress > progress_) {
progress_ = progress;
}
// update UI on main thread
[self performSelectorOnMainThread:@selector(updateProgressViewSizeForLabel:) withObject:self.visibleStatusLabel waitUntilDone:NO];
}
- (void)setDetailText:(NSString *)detailText {
if (detailText_ != detailText) {
detailText_ = [detailText copy];
// update text in label
self.detailTextView.text = detailText;
// update height of detailText-View
[self updateDetailTextViewHeight];
// update height of detailView
[self setDetailViewHidden:self.detailViewHidden animated:YES];
}
}
- (void)setDetailViewMode:(MTDetailViewMode)detailViewMode {
detailViewMode_ = detailViewMode;
// update UI
self.historyTableView.hidden = detailViewMode != MTDetailViewModeHistory;
self.detailTextView.hidden = detailViewMode != MTDetailViewModeDetailText;
}
- (void)setAnimation:(MTStatusBarOverlayAnimation)animation {
animation_ = animation;
// update appearance according to new animation-mode
// if new animation mode is shrink or none, the detailView mustn't be visible
if (animation == MTStatusBarOverlayAnimationShrink || animation == MTStatusBarOverlayAnimationNone) {
// detailView currently visible -> hide it
if (!self.detailViewHidden) {
[self setDetailViewHidden:YES animated:YES];
}
}
// if new animation mode is fallDown, the overlay must be extended
if (animation == MTStatusBarOverlayAnimationFallDown) {
if (self.shrinked) {
[self setShrinked:NO animated:YES];
}
}
}
- (BOOL)isShrinked {
return self.backgroundView.frame.size.width == self.smallFrame.size.width;
}
- (void)setShrinked:(BOOL)shrinked animated:(BOOL)animated {
[UIView animateWithDuration:animated ? kAnimationDurationShrink : 0.
animations:^{
// shrink the overlay
if (shrinked) {
self.oldBackgroundViewFrame = self.backgroundView.frame;
self.backgroundView.frame = self.smallFrame;
self.statusLabel1.hidden = YES;
self.statusLabel2.hidden = YES;
}
// expand the overlay