-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathLRDRCTSimpleToast.m
More file actions
115 lines (97 loc) · 3.88 KB
/
Copy pathLRDRCTSimpleToast.m
File metadata and controls
115 lines (97 loc) · 3.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
//
// LRDRCTSimpleToast.m
// LRDRCTSimpleToast
//
// Created by luoruidong on 16/6/30.
// Copyright © 2016年 luoruidong. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "RCTBridgeModule.h"
#import "UIView+Toast.h"
NSInteger const LRDRCTSimpleToastBottomOffset = 40;
double const LRDRCTSimpleToastShortDuration = 3.0;
double const LRDRCTSimpleToastLongDuration = 5.0;
NSInteger const LRDRCTSimpleToastGravityBottom = 1;
NSInteger const LRDRCTSimpleToastGravityCenter = 2;
NSInteger const LRDRCTSimpleToastGravityTop = 3;
@interface LRDRCTSimpleToast : NSObject <RCTBridgeModule>
@end
@implementation LRDRCTSimpleToast {
CGFloat _keyOffset;
}
- (instancetype)init {
if (self = [super init]) {
_keyOffset = 0;
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWasShown:)
name:UIKeyboardDidShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHiden:)
name:UIKeyboardWillHideNotification
object:nil];
}
return self;
}
- (void)keyboardWasShown:(NSNotification *)notification {
CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
int height = MIN(keyboardSize.height,keyboardSize.width);
int width = MAX(keyboardSize.height,keyboardSize.width);
_keyOffset = height;
}
- (void)keyboardWillHiden:(NSNotification *)notification {
_keyOffset = 0;
}
RCT_EXPORT_MODULE()
- (NSDictionary *)constantsToExport {
return @{
@"SHORT": [NSNumber numberWithDouble:LRDRCTSimpleToastShortDuration],
@"LONG": [NSNumber numberWithDouble:LRDRCTSimpleToastLongDuration],
@"BOTTOM": [NSNumber numberWithInteger:LRDRCTSimpleToastGravityBottom],
@"CENTER": [NSNumber numberWithInteger:LRDRCTSimpleToastGravityCenter],
@"TOP": [NSNumber numberWithInteger:LRDRCTSimpleToastGravityTop]
};
}
RCT_EXPORT_METHOD(show:(NSString *)msg duration:(double)duration {
[self _show:msg duration:duration gravity:LRDRCTSimpleToastGravityBottom];
});
RCT_EXPORT_METHOD(showWithGravity:(NSString *)msg duration:(double)duration gravity:(nonnull NSNumber *)gravity{
[self _show:msg duration:duration gravity:gravity.intValue];
});
- (void)_show:(NSString *)msg duration:(NSTimeInterval)duration gravity:(NSInteger)gravity {
dispatch_async(dispatch_get_main_queue(), ^{
UIView *root = [[[[[UIApplication sharedApplication] delegate] window] rootViewController] view];
CGRect bound = root.bounds;
bound.size.height -= _keyOffset;
if (bound.size.height > LRDRCTSimpleToastBottomOffset*2) {
bound.origin.y += LRDRCTSimpleToastBottomOffset;
bound.size.height -= LRDRCTSimpleToastBottomOffset*2;
}
UIView *view = [[UIView alloc] initWithFrame:bound];
view.userInteractionEnabled = NO;
[root addSubview:view];
UIView __weak *blockView = view;
id position;
if (gravity == LRDRCTSimpleToastGravityTop) {
position = CSToastPositionTop;
} else if (gravity == LRDRCTSimpleToastGravityCenter) {
position = CSToastPositionCenter;
} else {
position = CSToastPositionBottom;
}
[view makeToast:msg
duration:duration
position:position
title:nil
image:nil
style:nil
completion:^(BOOL didTap) {
[blockView removeFromSuperview];
}];
});
}
+ (BOOL)requiresMainQueueSetup
{
return YES;
}
@end