Show native dialog

This commit is contained in:
Pedro Pombeiro 2018-12-12 12:16:28 +01:00
parent 5f76085a3c
commit 183a4a2ffb
No known key found for this signature in database
GPG Key ID: A65DEB11E4BBC647
6 changed files with 96 additions and 1 deletions

View File

@ -9,6 +9,7 @@
import React, { Component } from 'react'
import { Platform, StyleSheet, Button, Text, View } from 'react-native'
import ToastExample from './ToastExample'
import CustomDialog from './CustomDialog'
const instructions = Platform.select({
ios: 'Press Cmd+R to reload,\n' + 'Cmd+D or shake for dev menu',
@ -18,7 +19,8 @@ const instructions = Platform.select({
});
function showNativeDialog() {
ToastExample.show('Example toast', ToastExample.LONG)
ToastExample.show('Showing native dialog', ToastExample.SHORT)
CustomDialog.show()
}
type Props = {};

10
StatusPoC/CustomDialog.js Normal file
View File

@ -0,0 +1,10 @@
/**
* This exposes the native ToastExample module as a JS module. This has a
* function 'show' which takes the following parameters:
*
* 1. String message: A string with the text to toast
* 2. int duration: The duration of the toast. May be ToastExample.SHORT or
* ToastExample.LONG
*/
import {NativeModules} from 'react-native';
module.exports = NativeModules.CustomDialog;

View File

@ -0,0 +1,24 @@
// CustomDialogModule.java
package com.statuspoc;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
public class CustomDialogModule extends ReactContextBaseJavaModule {
public CustomDialogModule(ReactApplicationContext reactContext) {
super(reactContext);
}
@Override
public String getName() {
return "CustomDialog";
}
@ReactMethod
public void show() {
new FireMissilesDialogFragment().show(getReactApplicationContext().getCurrentActivity().getFragmentManager(), "");
}
}

View File

@ -0,0 +1,30 @@
// CustomDialogPackage.java
package com.statuspoc;
import com.facebook.react.ReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CustomDialogPackage implements ReactPackage {
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
@Override
public List<NativeModule> createNativeModules(
ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new CustomDialogModule(reactContext));
return modules;
}
}

View File

@ -0,0 +1,28 @@
package com.statuspoc;
import android.app.AlertDialog;
import android.app.DialogFragment;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class FireMissilesDialogFragment extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Fire missiles?")
.setPositiveButton("Fire!", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}

View File

@ -23,6 +23,7 @@ public class MainApplication extends Application implements ReactApplication {
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new CustomDialogPackage(),
new CustomToastPackage()
);
}