Skip to content

Commit 8d95162

Browse files
author
罗瑞东
committed
feat(toast): add position
1 parent add67ad commit 8d95162

File tree

11 files changed

+121
-61
lines changed

11 files changed

+121
-61
lines changed

index.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
import {NativeModules,ToastAndroid,Platform} from 'react-native';
22

3-
var RCTToastModule = Platform.OS === 'android' ? ToastAndroid : NativeModules.LRDRCTSimpleToast;
3+
var RCTToastAndroid = Platform.OS === 'android' ? ToastAndroid : NativeModules.LRDRCTSimpleToast;
44

55
var SimpleToast = {
6-
SHORT: RCTToastModule.SHORT,
7-
LONG: RCTToastModule.LONG,
8-
show: function (message:string, duration:number):void {
9-
RCTToastModule.show(message, duration === undefined ? this.SHORT : duration);
10-
}
6+
// Toast duration constants
7+
SHORT: RCTToastAndroid.SHORT,
8+
LONG: RCTToastAndroid.LONG,
9+
10+
// Toast gravity constants
11+
TOP: RCTToastAndroid.TOP,
12+
BOTTOM: RCTToastAndroid.BOTTOM,
13+
CENTER: RCTToastAndroid.CENTER,
14+
15+
show: function (
16+
message,
17+
duration
18+
) {
19+
RCTToastAndroid.show(message, duration === undefined ? this.SHORT : duration);
20+
},
21+
22+
showWithGravity: function (
23+
message,
24+
duration,
25+
gravity,
26+
) {
27+
RCTToastAndroid.showWithGravity(message, duration === undefined ? this.SHORT : duration, gravity);
28+
},
1129
};
1230

1331
export default SimpleToast;

ios/LRDRCTSimpleToast/LRDRCTSimpleToast.m

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@
99
#import "RCTBridgeModule.h"
1010
#import "UIView+Toast.h"
1111

12-
NSInteger const LRDRCTSimpleToastBottomOffset = 28;
12+
NSInteger const LRDRCTSimpleToastBottomOffset = 40;
1313
double const LRDRCTSimpleToastShortDuration = 3.0;
1414
double const LRDRCTSimpleToastLongDuration = 5.0;
15+
NSInteger const LRDRCTSimpleToastGravityBottom = 1;
16+
NSInteger const LRDRCTSimpleToastGravityCenter = 2;
17+
NSInteger const LRDRCTSimpleToastGravityTop = 3;
1518

1619
@interface LRDRCTSimpleToast : NSObject <RCTBridgeModule>
1720
@end
@@ -23,28 +26,44 @@ @implementation LRDRCTSimpleToast
2326
- (NSDictionary *)constantsToExport {
2427
return @{
2528
@"SHORT": [NSNumber numberWithDouble:LRDRCTSimpleToastShortDuration],
26-
@"LONG": [NSNumber numberWithDouble:LRDRCTSimpleToastLongDuration]
29+
@"LONG": [NSNumber numberWithDouble:LRDRCTSimpleToastLongDuration],
30+
@"BOTTOM": [NSNumber numberWithInteger:LRDRCTSimpleToastGravityBottom],
31+
@"CENTER": [NSNumber numberWithInteger:LRDRCTSimpleToastGravityCenter],
32+
@"TOP": [NSNumber numberWithInteger:LRDRCTSimpleToastGravityTop]
2733
};
2834
}
2935

3036
RCT_EXPORT_METHOD(show:(NSString *)msg duration:(double)duration {
31-
[self _show:msg duration:duration];
37+
[self _show:msg duration:duration gravity:LRDRCTSimpleToastGravityBottom];
3238
});
3339

34-
- (void)_show:(NSString *)msg duration:(NSTimeInterval)duration {
40+
RCT_EXPORT_METHOD(showWithGravity:(NSString *)msg duration:(double)duration gravity:(nonnull NSNumber *)gravity{
41+
[self _show:msg duration:duration gravity:gravity.intValue];
42+
});
43+
44+
- (void)_show:(NSString *)msg duration:(NSTimeInterval)duration gravity:(NSInteger)gravity {
3545
dispatch_async(dispatch_get_main_queue(), ^{
3646
UIView *root = [[[[[UIApplication sharedApplication] delegate] window] rootViewController] view];
3747
CGRect bound = root.bounds;
38-
if (bound.size.height > LRDRCTSimpleToastBottomOffset) {
39-
bound.size.height -= LRDRCTSimpleToastBottomOffset;
48+
if (bound.size.height > LRDRCTSimpleToastBottomOffset*2) {
49+
bound.origin.y += LRDRCTSimpleToastBottomOffset;
50+
bound.size.height -= LRDRCTSimpleToastBottomOffset*2;
4051
}
4152
UIView *view = [[UIView alloc] initWithFrame:bound];
4253
view.userInteractionEnabled = NO;
4354
[root addSubview:view];
4455
UIView __weak *blockView = view;
56+
id position;
57+
if (gravity == LRDRCTSimpleToastGravityTop) {
58+
position = CSToastPositionTop;
59+
} else if (gravity == LRDRCTSimpleToastGravityCenter) {
60+
position = CSToastPositionCenter;
61+
} else {
62+
position = CSToastPositionBottom;
63+
}
4564
[view makeToast:msg
4665
duration:duration
47-
position:[CSToastManager defaultPosition]
66+
position:position
4867
title:nil
4968
image:nil
5069
style:nil

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-simple-toast",
3-
"version": "0.0.3",
3+
"version": "0.0.4",
44
"description": "Simple Toast for react-native. In Android it's just native toast, in iOS it's https://github.com/scalessec/Toast",
55
"main": "index.js",
66
"scripts": {
Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2-
package="com.samples"
3-
android:versionCode="1"
4-
android:versionName="1.0">
2+
package="com.samples"
3+
android:versionCode="1"
4+
android:versionName="1.0">
55

6-
<uses-permission android:name="android.permission.INTERNET" />
6+
<uses-permission android:name="android.permission.INTERNET"/>
77
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
88

99
<uses-sdk
10-
android:minSdkVersion="16"
11-
android:targetSdkVersion="22" />
10+
android:minSdkVersion="16"
11+
android:targetSdkVersion="22"/>
1212

1313
<application
14-
android:allowBackup="true"
15-
android:label="@string/app_name"
16-
android:icon="@mipmap/ic_launcher"
17-
android:theme="@style/AppTheme">
18-
<activity
19-
android:name=".MainActivity"
20-
android:label="@string/app_name"
21-
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
22-
<intent-filter>
23-
<action android:name="android.intent.action.MAIN" />
24-
<category android:name="android.intent.category.LAUNCHER" />
25-
</intent-filter>
26-
</activity>
27-
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
14+
android:name=".MainApplication"
15+
android:allowBackup="true"
16+
android:label="@string/app_name"
17+
android:icon="@mipmap/ic_launcher"
18+
android:theme="@style/AppTheme">
19+
<activity
20+
android:name=".MainActivity"
21+
android:label="@string/app_name"
22+
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
23+
<intent-filter>
24+
<action android:name="android.intent.action.MAIN"/>
25+
<category android:name="android.intent.category.LAUNCHER"/>
26+
</intent-filter>
27+
</activity>
28+
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity"/>
2829
</application>
2930

31+
3032
</manifest>

sample/android/app/src/main/java/com/samples/MainActivity.java

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,4 @@ public class MainActivity extends ReactActivity {
1717
protected String getMainComponentName() {
1818
return "samples";
1919
}
20-
21-
/**
22-
* Returns whether dev mode should be enabled.
23-
* This enables e.g. the dev menu.
24-
*/
25-
@Override
26-
protected boolean getUseDeveloperSupport() {
27-
return BuildConfig.DEBUG;
28-
}
29-
30-
/**
31-
* A list of packages used by the app. If the app uses additional views
32-
* or modules besides the default ones, add more packages here.
33-
*/
34-
@Override
35-
protected List<ReactPackage> getPackages() {
36-
return Arrays.<ReactPackage>asList(
37-
new MainReactPackage()
38-
);
39-
}
4020
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.samples;
2+
3+
import com.facebook.react.ReactApplication;
4+
import com.facebook.react.ReactNativeHost;
5+
import com.facebook.react.ReactPackage;
6+
import com.facebook.react.shell.MainReactPackage;
7+
8+
import android.app.Application;
9+
10+
import java.util.Arrays;
11+
import java.util.List;
12+
13+
14+
public class MainApplication extends Application implements ReactApplication {
15+
16+
private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
17+
@Override
18+
protected boolean getUseDeveloperSupport() {
19+
return BuildConfig.DEBUG;
20+
}
21+
22+
@Override
23+
protected List<ReactPackage> getPackages() {
24+
return Arrays.<ReactPackage>asList(new MainReactPackage());
25+
}
26+
};
27+
28+
@Override
29+
public ReactNativeHost getReactNativeHost() {
30+
return mReactNativeHost;
31+
}
32+
33+
34+
}

sample/android/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:1.3.1'
8+
classpath 'com.android.tools.build:gradle:2.1.2'
99

1010
// NOTE: Do not place your application dependencies here; they belong
1111
// in the individual module build.gradle files
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#Tue Oct 11 11:35:38 CST 2016
12
distributionBase=GRADLE_USER_HOME
23
distributionPath=wrapper/dists
34
zipStoreBase=GRADLE_USER_HOME
45
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip

sample/index.android.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ class samples extends Component {
2626
</Text>
2727
<TouchableHighlight onPress={()=>{Toast.show("This is a Toast",Toast.LONG)}}>
2828
<Text>Tap to show Toast!</Text>
29-
</TouchableHighlight>
29+
</TouchableHighlight>
30+
<TouchableHighlight onPress={()=>{Toast.showWithGravity("This is a Toast Center",Toast.LONG,Toast.CENTER)}}>
31+
<Text>Tap to show Toast Center!</Text>
32+
</TouchableHighlight>
3033
</View>
3134
);
3235
}

sample/index.ios.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ class samples extends Component {
2626
</Text>
2727
<TouchableHighlight onPress={()=>{Toast.show("This is a Toast",Toast.LONG)}}>
2828
<Text>Tap to show Toast!</Text>
29-
</TouchableHighlight>
29+
</TouchableHighlight>
30+
<TouchableHighlight onPress={()=>{Toast.showWithGravity("This is a Toast Center",Toast.LONG,Toast.CENTER)}}>
31+
<Text>Tap to show Toast Center!</Text>
32+
</TouchableHighlight>
3033
</View>
3134
);
3235
}

0 commit comments

Comments
 (0)