forked from cruffenach/CRToast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainViewController.m
executable file
·245 lines (192 loc) · 11 KB
/
MainViewController.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
//
// MainViewController.m
// CRNotificationDemo
//
//
#import "MainViewController.h"
#import "CRToast.h"
@interface MainViewController ()<UITextFieldDelegate>
@property (weak, readonly) NSDictionary *options;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIView *contentView;
@property (weak, nonatomic) IBOutlet UIToolbar *toolbar;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segFromDirection;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segToDirection;
@property (weak, nonatomic) IBOutlet UISegmentedControl *inAnimationTypeSegmentedControl;
@property (weak, nonatomic) IBOutlet UISegmentedControl *outAnimationTypeSegmentedControl;
@property (weak, nonatomic) IBOutlet UISlider *sliderDuration;
@property (weak, nonatomic) IBOutlet UILabel *lblDuration;
@property (weak, nonatomic) IBOutlet UISwitch *showImageSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *coverNavBarSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *slideOverSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *slideUnderSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *dismissibleWithTapSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *statusBarSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *navigationBarSwitch;
@property (weak, nonatomic) IBOutlet UISwitch *stickySwitch;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segAlignment;
@property (weak, nonatomic) IBOutlet UISegmentedControl *segSubtitleAlignment;
@property (weak, nonatomic) IBOutlet UITextField *txtNotificationMessage;
@property (weak, nonatomic) IBOutlet UITextField *txtSubtitleMessage;
@property (assign, nonatomic) NSTextAlignment textAlignment;
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.automaticallyAdjustsScrollViewInsets = NO;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
self.title = @"CRToast";
[self updateDurationLabel];
UIFont *font = [UIFont boldSystemFontOfSize:10];
[self.segFromDirection setTitleTextAttributes:@{NSFontAttributeName : font}
forState:UIControlStateNormal];
[self.segToDirection setTitleTextAttributes:@{NSFontAttributeName : font}
forState:UIControlStateNormal];
[self.inAnimationTypeSegmentedControl setTitleTextAttributes:@{NSFontAttributeName : font}
forState:UIControlStateNormal];
[self.outAnimationTypeSegmentedControl setTitleTextAttributes:@{NSFontAttributeName : font}
forState:UIControlStateNormal];
self.txtNotificationMessage.delegate = self;
self.txtSubtitleMessage.delegate = self;
UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(scrollViewTapped:)];
[_scrollView addGestureRecognizer:tapGestureRecognizer];
if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeAll;
}
- (void)layoutSubviews {
self.scrollView.contentInset = UIEdgeInsetsMake([self.topLayoutGuide length],
0,
[self.bottomLayoutGuide length],
0);
}
- (void)viewWillLayoutSubviews {
[super viewWillLayoutSubviews];
[self layoutSubviews];
}
- (BOOL)prefersStatusBarHidden {
return self.statusBarSwitch ? !self.statusBarSwitch.on : NO;
}
- (void)updateDurationLabel {
self.lblDuration.text = [NSString stringWithFormat:@"%f seconds", self.sliderDuration.value];
}
- (IBAction)sliderDurationChanged:(UISlider *)sender {
[self updateDurationLabel];
}
- (IBAction)statusBarChanged:(UISwitch *)sender {
[self setNeedsStatusBarAppearanceUpdate];
}
- (IBAction)navigationBarChanged:(UISwitch *)sender {
[[self navigationController] setNavigationBarHidden:!sender.on animated:YES];
}
# pragma mark - Show Notification
- (IBAction)btnShowNotificationPressed:(UIButton *)sender {
[CRToastManager showNotificationWithOptions:[self options]
apperanceBlock:^(void) {
NSLog(@"Appeared");
}
completionBlock:^(void) {
NSLog(@"Completed");
}];
}
- (IBAction)btnDismissNotificationPressed:(UIButton *)sender {
[CRToastManager dismissNotification:YES];
}
#pragma mark - Notifications
- (void)keyboardWillShow:(NSNotification*)notification {
self.scrollView.contentInset = UIEdgeInsetsMake([self.topLayoutGuide length],
0,
CGRectGetHeight([notification.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]),
0);
self.scrollView.scrollIndicatorInsets = self.scrollView.contentInset;
}
- (void)keyboardWillHide:(NSNotification*)notification {
self.scrollView.contentInset = UIEdgeInsetsMake([self.topLayoutGuide length],
0,
[self.bottomLayoutGuide length],
0);
self.scrollView.scrollIndicatorInsets = self.scrollView.contentInset;
}
- (void)orientationChanged:(NSNotification*)notification {
[self layoutSubviews];
}
#pragma mark - Overrides
CRToastAnimationType CRToastAnimationTypeFromSegmentedControl(UISegmentedControl *segmentedControl) {
return segmentedControl.selectedSegmentIndex == 0 ? CRToastAnimationTypeLinear :
segmentedControl.selectedSegmentIndex == 1 ? CRToastAnimationTypeSpring :
CRToastAnimationTypeGravity;
}
- (NSDictionary*)options {
NSMutableDictionary *options = [@{kCRToastNotificationTypeKey : self.coverNavBarSwitch.on ? @(CRToastTypeNavigationBar) : @(CRToastTypeStatusBar),
kCRToastNotificationPresentationTypeKey : self.slideOverSwitch.on ? @(CRToastPresentationTypeCover) : @(CRToastPresentationTypePush),
kCRToastUnderStatusBarKey : @(self.slideUnderSwitch.on),
kCRToastTextKey : self.txtNotificationMessage.text,
kCRToastTextAlignmentKey : @(self.textAlignment),
kCRToastTimeIntervalKey : @(self.sliderDuration.value),
kCRToastAnimationInTypeKey : @(CRToastAnimationTypeFromSegmentedControl(_inAnimationTypeSegmentedControl)),
kCRToastAnimationOutTypeKey : @(CRToastAnimationTypeFromSegmentedControl(_outAnimationTypeSegmentedControl)),
kCRToastAnimationInDirectionKey : @(self.segFromDirection.selectedSegmentIndex),
kCRToastAnimationOutDirectionKey : @(self.segToDirection.selectedSegmentIndex),
} mutableCopy];
if (self.showImageSwitch.on) {
options[kCRToastImageKey] = [UIImage imageNamed:@"alert_icon.png"];
}
if (![self.txtSubtitleMessage.text isEqualToString:@""]) {
options[kCRToastSubtitleTextKey] = self.txtSubtitleMessage.text;
options[kCRToastSubtitleTextAlignmentKey] = @(self.subtitleAlignment);
}
if (_dismissibleWithTapSwitch.on) {
options[kCRToastInteractionRespondersKey] = @[[CRToastInteractionResponder interactionResponderWithInteractionType:CRToastInteractionTypeTap
automaticallyDismiss:YES
block:^(CRToastInteractionType interactionType){
NSLog(@"Dismissed with %@ interaction", NSStringFromCRToastInteractionType(interactionType));
}]];
}
if (self.stickySwitch.on) {
options[kCRToastTimeIntervalKey] = @(DBL_MAX);
CRToastContentViewConfigurationBlock block = ^(UIView *view) {
// view.backgroundColor = [UIColor redColor];
UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[indicator startAnimating];
[view addSubview:indicator];
indicator.frame = CGRectOffset(indicator.frame,
CGRectGetWidth(view.frame) - CGRectGetWidth(indicator.frame),
CGRectGetHeight(view.frame) / 2 - CGRectGetHeight(indicator.frame) / 2);
};
options[kCRToastContentViewConfigurationBlockKey] = block;
}
return [NSDictionary dictionaryWithDictionary:options];
}
- (NSTextAlignment)textAlignment {
NSInteger selectedSegment = self.segAlignment.selectedSegmentIndex;
return selectedSegment == 0 ? NSTextAlignmentLeft :
selectedSegment == 1 ? NSTextAlignmentCenter :
NSTextAlignmentRight;
}
- (NSTextAlignment)subtitleAlignment {
NSInteger selectedSegment = self.segSubtitleAlignment.selectedSegmentIndex;
return selectedSegment == 0 ? NSTextAlignmentLeft :
selectedSegment == 1 ? NSTextAlignmentCenter :
NSTextAlignmentRight;
}
#pragma mark - Gesture Recognizer Selectors
- (void)scrollViewTapped:(UITapGestureRecognizer*)tapGestureRecognizer {
[_txtNotificationMessage resignFirstResponder];
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
// close the keyboard
[textField resignFirstResponder];
return YES;
}
@end