Last active
September 11, 2019 13:19
-
-
Save mjm918/4dadbd1d1bad14e33390bf6993ce9983 to your computer and use it in GitHub Desktop.
React Native Banner View (Android & iOS native module)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<set xmlns:android="http://schemas.android.com/apk/res/android"> | |
<scale | |
android:duration="400" | |
android:fromXScale="1" | |
android:fromYScale="1" | |
android:pivotX="50%" | |
android:pivotY="50%" | |
android:repeatCount="infinite" | |
android:repeatMode="reverse" | |
android:toXScale="0.75" | |
android:toYScale="0.75" | |
android:interpolator="@android:interpolator/bounce" /> | |
<scale | |
android:duration="200" | |
android:fromXScale="1" | |
android:fromYScale="1" | |
android:pivotX="50%" | |
android:pivotY="50%" | |
android:repeatCount="infinite" | |
android:repeatMode="reverse" | |
android:toXScale="1.25" | |
android:toYScale="1.25" | |
android:interpolator="@android:interpolator/bounce" /> | |
</set> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> | |
<item> | |
<shape android:shape="rectangle"> | |
<solid android:color="#BDBDBD"/> | |
<corners android:radius="5dp"/> | |
</shape> | |
</item> | |
<item | |
android:left="0dp" | |
android:right="0dp" | |
android:top="0dp" | |
android:bottom="2dp"> | |
<shape android:shape="rectangle"> | |
<solid android:color="#ffffff"/> | |
<corners android:radius="5dp"/> | |
</shape> | |
</item> | |
</layer-list> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:background="@drawable/shadow"> | |
<RelativeLayout | |
android:padding="10dp" | |
android:id="@+id/toast" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<ImageView | |
android:id="@+id/logo" | |
android:layout_width="30dp" | |
android:layout_height="30dp" | |
android:src="@mipmap/ic_launcher"/> | |
<TextView | |
android:id="@+id/tv_message" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentEnd="true" | |
android:layout_marginStart="20dp" | |
android:layout_marginEnd="0dp" | |
android:textSize="15sp" | |
android:layout_centerVertical="true" | |
android:layout_toEndOf="@+id/logo" | |
android:textColor="#fff" | |
android:text="Hello World" | |
android:layout_alignParentRight="true" | |
android:layout_marginLeft="20dp" | |
android:layout_marginRight="0dp" | |
android:layout_toRightOf="@+id/logo" /> | |
</RelativeLayout> | |
</RelativeLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MessageBox.h | |
// | |
// Created by Mohammad Julfikar on 08/09/2019. | |
// Copyright © 2019 Facebook. All rights reserved. | |
// | |
#import <UIKit/UIKit.h> | |
#import <React/RCTBridgeModule.h> | |
#import "Banner.h" | |
NS_ASSUME_NONNULL_BEGIN | |
@interface MessageBox : NSObject<RCTBridgeModule> | |
@end | |
NS_ASSUME_NONNULL_END |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MessageBox extends ReactContextBaseJavaModule { | |
private Context context; | |
private LayoutInflater inflater; | |
public MessageBox(@Nonnull ReactApplicationContext reactContext, LayoutInflater inflater) { | |
super(reactContext); | |
this.context = reactContext; | |
this.inflater = inflater; | |
} | |
@ReactMethod | |
public void show(String message, String messageType, final Callback callback){ | |
final View toastView = inflater.inflate(R.layout.toast_view, null); | |
int color = R.color.toastPrimary; | |
if(messageType.equals("warning")){ | |
color = R.color.toastWarning; | |
} | |
if(messageType.equals("success")){ | |
color = R.color.toastSuccess; | |
} | |
if(messageType.equals("danger")){ | |
color = R.color.toastDanger; | |
} | |
RelativeLayout background = toastView.findViewById(R.id.toast); | |
background.setBackgroundResource(color); | |
ImageView imageView = toastView.findViewById(R.id.logo); | |
imageView.startAnimation(AnimationUtils.loadAnimation(context, R.anim.pulse)); | |
TextView textView = toastView.findViewById(R.id.tv_message); | |
textView.setText(message); | |
final Toast toast = new Toast(getCurrentActivity()); | |
toast.setView(toastView); | |
toast.setDuration(Toast.LENGTH_SHORT); | |
toast.setGravity(Gravity.TOP | Gravity.FILL_HORIZONTAL, 0,0); | |
toast.show(); | |
new Handler().postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
callback.invoke(1); | |
} | |
}, 2000); | |
} | |
@Nonnull | |
@Override | |
public String getName() { | |
return "MessageBox"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {NativeModules} from 'react-native'; | |
module.exports = NativeModules.MessageBox; | |
/* | |
Usage ---- | |
import MessageBox from './MessageBox'; | |
MessageBox.show("Your message","danger",()=>{ | |
// triggers when message box is dismissed | |
}); | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// MessageBox.m | |
// | |
// Created by Mohammad Julfikar on 08/09/2019. | |
// Copyright © 2019 Facebook. All rights reserved. | |
// | |
#import "MessageBox.h" | |
@implementation MessageBox | |
- (dispatch_queue_t)methodQueue | |
{ | |
return dispatch_get_main_queue(); | |
} | |
RCT_EXPORT_MODULE(); | |
RCT_EXPORT_METHOD(show:(NSString *)messageTxt type:(NSString *)messageType onClose:(RCTResponseSenderBlock)callback) | |
{ | |
Banner *banner = [[Banner alloc]init]; | |
[banner show:messageTxt type:messageType onComplete:^(int done){ | |
callback(@[]); | |
}]; | |
NSLog(@"From Native View %@",messageTxt); | |
} | |
@end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RNMessageBox implements ReactPackage { | |
@Nonnull | |
@Override | |
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) { | |
List<NativeModule> modules = new ArrayList<>(); | |
modules.add(new MessageBox(reactContext, LayoutInflater.from(reactContext))); | |
return modules; | |
} | |
@Nonnull | |
@Override | |
public List<ViewManager> createViewManagers(@Nonnull ReactApplicationContext reactContext) { | |
return Collections.emptyList(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment