Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# OSX
#
.DS_Store

# Xcode
#
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata
*.xccheckout
*.moved-aside
DerivedData
*.hmap
*.ipa
*.xcuserstate
project.xcworkspace

# Android/IJ
#
*.iml
.idea
.gradle
local.properties

# node.js
#
node_modules/
npm-debug.log

# BUCK
buck-out/
\.buckd/
android/app/libs
android/keystores/debug.keystore

# npmignore
sample
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# react-native-simple-toast
React Native Toast component for both Android and iOS. It just let iOS have the same toast performance with Android. Using [scalessec/Toast](https://github.com/scalessec/Toast) for iOS;

## Install
You can use [rnpm](https://github.com/rnpm/rnpm) to install native component easily;

```bash
npm install react-native-simple-toast --save
npm link
```

## Usage

It's just the same as [ToastAndroid](http://facebook.github.io/react-native/docs/toastandroid.html)

```javascript
import Toast from 'react-native-simple-toast';

Toast.show('This is a toast.');
Toast.show('This is a long toast.',Toast.LONG);
```
11 changes: 0 additions & 11 deletions index.android.js

This file was deleted.

13 changes: 0 additions & 13 deletions index.ios.js

This file was deleted.

31 changes: 31 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {NativeModules,ToastAndroid,Platform} from 'react-native';

var RCTToastAndroid = Platform.OS === 'android' ? ToastAndroid : NativeModules.LRDRCTSimpleToast;

var SimpleToast = {
// Toast duration constants
SHORT: RCTToastAndroid.SHORT,
LONG: RCTToastAndroid.LONG,

// Toast gravity constants
TOP: RCTToastAndroid.TOP,
BOTTOM: RCTToastAndroid.BOTTOM,
CENTER: RCTToastAndroid.CENTER,

show: function (
message,
duration
) {
RCTToastAndroid.show(message, duration === undefined ? this.SHORT : duration);
},

showWithGravity: function (
message,
duration,
gravity,
) {
RCTToastAndroid.showWithGravity(message, duration === undefined ? this.SHORT : duration, gravity);
},
};

export default SimpleToast;
Binary file not shown.
68 changes: 60 additions & 8 deletions ios/LRDRCTSimpleToast/LRDRCTSimpleToast.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,94 @@
#import "RCTBridgeModule.h"
#import "UIView+Toast.h"

NSInteger const LRDRCTSimpleToastBottomOffset = 28;
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
@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]
@"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];
[self _show:msg duration:duration gravity:LRDRCTSimpleToastGravityBottom];
});

- (void)_show:(NSString *)msg duration:(NSTimeInterval)duration {
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;
if (bound.size.height > LRDRCTSimpleToastBottomOffset) {
bound.size.height -= LRDRCTSimpleToastBottomOffset;
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:[CSToastManager defaultPosition]
position:position
title:nil
image:nil
style:nil
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "react-native-simple-toast",
"version": "0.0.1",
"description": "simple toast for react-native. In Android it's just native toast, in iOS it's https://github.com/scalessec/Toast",
"version": "0.0.5",
"description": "Simple Toast for react-native. In Android it's just native toast, in iOS it's https://github.com/scalessec/Toast",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand All @@ -10,10 +10,10 @@
"react-native",
"toast"
],
"author": "lrdcq",
"license": "ISC",
"dependencies": {
"react": "15.1.0",
"react-native": "^0.28.0"
}
"repository": {
"type": "git",
"url": "git+https://github.com/xgfe/react-native-simple-toast.git"
},
"author": "xgfe",
"license": "MIT"
}
42 changes: 22 additions & 20 deletions sample/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.samples"
android:versionCode="1"
android:versionName="1.0">
package="com.samples"
android:versionCode="1"
android:versionName="1.0">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="22" />
android:minSdkVersion="16"
android:targetSdkVersion="22"/>

<application
android:allowBackup="true"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
android:name=".MainApplication"
android:allowBackup="true"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity"/>
</application>


</manifest>
20 changes: 0 additions & 20 deletions sample/android/app/src/main/java/com/samples/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,4 @@ public class MainActivity extends ReactActivity {
protected String getMainComponentName() {
return "samples";
}

/**
* Returns whether dev mode should be enabled.
* This enables e.g. the dev menu.
*/
@Override
protected boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}

/**
* A list of packages used by the app. If the app uses additional views
* or modules besides the default ones, add more packages here.
*/
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage()
);
}
}
34 changes: 34 additions & 0 deletions sample/android/app/src/main/java/com/samples/MainApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.samples;

import com.facebook.react.ReactApplication;
import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage;

import android.app.Application;

import java.util.Arrays;
import java.util.List;


public class MainApplication extends Application implements ReactApplication {

private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
@Override
protected boolean getUseDeveloperSupport() {
return BuildConfig.DEBUG;
}

@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(new MainReactPackage());
}
};

@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}


}
2 changes: 1 addition & 1 deletion sample/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.1'
classpath 'com.android.tools.build:gradle:2.1.2'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
3 changes: 2 additions & 1 deletion sample/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Tue Oct 11 11:35:38 CST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
Loading