-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathAGPushNoteView.m
More file actions
213 lines (175 loc) · 6.88 KB
/
Copy pathAGPushNoteView.m
File metadata and controls
213 lines (175 loc) · 6.88 KB
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
//
// IAAPushNoteView.m
// TLV Airport
//
// Created by Aviel Gross on 1/29/14.
// Copyright (c) 2014 NGSoft. All rights reserved.
//
#import "AGPushNoteView.h"
#define APP [UIApplication sharedApplication].delegate
#define isIOS7 (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_6_1)
#define PUSH_VIEW [AGPushNoteView sharedPushView]
#define CLOSE_PUSH_SEC 5
#define SHOW_ANIM_DUR 0.5
#define HIDE_ANIM_DUR 0.35
@interface AGPushNoteView()
@property (weak, nonatomic) IBOutlet UILabel *messageLabel;
@property (weak, nonatomic) IBOutlet UIView *containerView;
@property (strong, nonatomic) NSTimer *closeTimer;
@property (strong, nonatomic) NSString *currentMessage;
@property (strong, nonatomic) NSMutableArray *pendingPushArr;
@property (strong, nonatomic) void (^messageTapActionBlock)(NSString *message);
@end
@implementation AGPushNoteView
//Singleton instance
static AGPushNoteView *_sharedPushView;
+ (instancetype)sharedPushView
{
@synchronized([self class])
{
if (!_sharedPushView){
NSArray *nibArr = [[NSBundle mainBundle] loadNibNamed: @"AGPushNoteView" owner:self options:nil];
for (id currentObject in nibArr)
{
if ([currentObject isKindOfClass:[AGPushNoteView class]])
{
_sharedPushView = (AGPushNoteView *)currentObject;
break;
}
}
[_sharedPushView setUpUI];
}
return _sharedPushView;
}
// to avoid compiler warning
return nil;
}
+ (void)setDelegateForPushNote:(id<AGPushNoteViewDelegate>)delegate {
[PUSH_VIEW setPushNoteDelegate:delegate];
}
#pragma mark - Lifecycle (of sort)
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
CGRect f = self.frame;
CGFloat width = [UIApplication sharedApplication].keyWindow.bounds.size.width;
self.frame = CGRectMake(f.origin.x, f.origin.y, width, f.size.height);
}
return self;
}
- (void)setUpUI {
CGRect f = self.frame;
CGFloat width = [UIApplication sharedApplication].keyWindow.bounds.size.width;
CGFloat height = isIOS7? 54: f.size.height;
self.frame = CGRectMake(f.origin.x, -height, width, height);
CGRect cvF = self.containerView.frame;
self.containerView.frame = CGRectMake(cvF.origin.x, cvF.origin.y, self.frame.size.width, cvF.size.height);
//OS Specific:
if (isIOS7) {
self.barTintColor = nil;
self.translucent = YES;
self.barStyle = UIBarStyleBlack;
} else {
[self setTintColor:[UIColor colorWithRed:5 green:31 blue:75 alpha:1]];
[self.messageLabel setTextAlignment:NSTextAlignmentCenter];
self.messageLabel.shadowColor = [UIColor blackColor];
}
self.layer.zPosition = MAXFLOAT;
self.backgroundColor = [UIColor clearColor];
self.multipleTouchEnabled = NO;
self.exclusiveTouch = YES;
UITapGestureRecognizer *msgTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(messageTapAction)];
self.messageLabel.userInteractionEnabled = YES;
[self.messageLabel addGestureRecognizer:msgTap];
//:::[For debugging]:::
// self.containerView.backgroundColor = [UIColor yellowColor];
// self.closeButton.backgroundColor = [UIColor redColor];
// self.messageLabel.backgroundColor = [UIColor greenColor];
[APP.window addSubview:PUSH_VIEW];
}
+ (void)awake {
if (PUSH_VIEW.frame.origin.y == 0) {
[APP.window addSubview:PUSH_VIEW];
}
}
+ (void)showWithNotificationMessage:(NSString *)message {
[AGPushNoteView showWithNotificationMessage:message completion:^{
//Nothing.
}];
}
+ (void)showWithNotificationMessage:(NSString *)message completion:(void (^)(void))completion {
PUSH_VIEW.currentMessage = message;
if (message) {
[PUSH_VIEW.pendingPushArr addObject:message];
PUSH_VIEW.messageLabel.text = message;
APP.window.windowLevel = UIWindowLevelStatusBar;
CGRect f = PUSH_VIEW.frame;
PUSH_VIEW.frame = CGRectMake(f.origin.x, -f.size.height, f.size.width, f.size.height);
[APP.window addSubview:PUSH_VIEW];
//Show
[UIView animateWithDuration:SHOW_ANIM_DUR delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
CGRect f = PUSH_VIEW.frame;
PUSH_VIEW.frame = CGRectMake(f.origin.x, 0, f.size.width, f.size.height);
} completion:^(BOOL finished) {
completion();
if ([PUSH_VIEW.pushNoteDelegate respondsToSelector:@selector(pushNoteDidAppear)]) {
[PUSH_VIEW.pushNoteDelegate pushNoteDidAppear];
}
}];
//Start timer (Currently not used to make sure user see & read the push...)
// PUSH_VIEW.closeTimer = [NSTimer scheduledTimerWithTimeInterval:CLOSE_PUSH_SEC target:[IAAPushNoteView class] selector:@selector(close) userInfo:nil repeats:NO];
}
}
+ (void)closeWitCompletion:(void (^)(void))completion {
if ([PUSH_VIEW.pushNoteDelegate respondsToSelector:@selector(pushNoteWillDisappear)]) {
[PUSH_VIEW.pushNoteDelegate pushNoteWillDisappear];
}
[PUSH_VIEW.closeTimer invalidate];
[UIView animateWithDuration:HIDE_ANIM_DUR delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
CGRect f = PUSH_VIEW.frame;
PUSH_VIEW.frame = CGRectMake(f.origin.x, -f.size.height, f.size.width, f.size.height);
} completion:^(BOOL finished) {
[PUSH_VIEW handlePendingPushJumpWitCompletion:completion];
}];
}
+ (void)close {
[AGPushNoteView closeWitCompletion:^{
//Nothing.
}];
}
#pragma mark - Pending push managment
- (void)handlePendingPushJumpWitCompletion:(void (^)(void))completion {
id lastObj = [self.pendingPushArr lastObject]; //Get myself
if (lastObj) {
[self.pendingPushArr removeObject:lastObj]; //Remove me from arr
NSString *messagePendingPush = [self.pendingPushArr lastObject]; //Maybe get pending push
if (messagePendingPush) { //If got something - remove from arr, - than show it.
[self.pendingPushArr removeObject:messagePendingPush];
[AGPushNoteView showWithNotificationMessage:messagePendingPush completion:completion];
} else {
APP.window.windowLevel = UIWindowLevelNormal;
}
}
}
- (NSMutableArray *)pendingPushArr {
if (!_pendingPushArr) {
_pendingPushArr = [[NSMutableArray alloc] init];
}
return _pendingPushArr;
}
#pragma mark - Actions
+ (void)setMessageAction:(void (^)(NSString *message))action {
PUSH_VIEW.messageTapActionBlock = action;
}
- (void)messageTapAction {
if (self.messageTapActionBlock) {
self.messageTapActionBlock(self.currentMessage);
[AGPushNoteView close];
}
}
- (IBAction)closeActionItem:(UIBarButtonItem *)sender {
[AGPushNoteView close];
}
@end