Add spec for DialogManagerAndroid (#24912)

Summary:
Part of #24875.

## Changelog

[General] [Added] - TM add spec for DialogManagerAndroid
Pull Request resolved: https://github.com/facebook/react-native/pull/24912

Reviewed By: fkgozali

Differential Revision: D15433854

Pulled By: RSNara

fbshipit-source-id: e7234debe16de5afbc770f8feee2471f41b54427
This commit is contained in:
Uilque Messias
2019-05-22 18:22:17 -07:00
committed by Facebook Github Bot
parent 116ac65fea
commit 32340d377b
4 changed files with 95 additions and 35 deletions
+33 -30
View File
@@ -10,9 +10,13 @@
'use strict';
const NativeModules = require('../BatchedBridge/NativeModules');
import NativeModules from '../BatchedBridge/NativeModules';
import Platform from '../Utilities/Platform';
import DialogManagerAndroid, {
type DialogOptions,
} from '../NativeModules/specs/NativeDialogManagerAndroid';
const RCTAlertManager = NativeModules.AlertManager;
const Platform = require('../Utilities/Platform');
export type Buttons = Array<{
text?: string,
@@ -53,14 +57,19 @@ class Alert {
if (Platform.OS === 'ios') {
Alert.prompt(title, message, buttons, 'default');
} else if (Platform.OS === 'android') {
let config = {
if (!DialogManagerAndroid) {
return;
}
const constants = DialogManagerAndroid.getConstants();
const config: DialogOptions = {
title: title || '',
message: message || '',
cancelable: false,
};
if (options) {
config = {...config, cancelable: options.cancelable};
if (options && options.cancelable) {
config.cancelable = options.cancelable;
}
// At most three buttons (neutral, negative, positive). Ignore rest.
// The text 'OK' should be probably localized. iOS Alert does that in native.
@@ -70,38 +79,32 @@ class Alert {
const buttonPositive = validButtons.pop();
const buttonNegative = validButtons.pop();
const buttonNeutral = validButtons.pop();
if (buttonNeutral) {
config = {...config, buttonNeutral: buttonNeutral.text || ''};
config.buttonNeutral = buttonNeutral.text || '';
}
if (buttonNegative) {
config = {...config, buttonNegative: buttonNegative.text || ''};
config.buttonNegative = buttonNegative.text || '';
}
if (buttonPositive) {
config = {...config, buttonPositive: buttonPositive.text || ''};
config.buttonPositive = buttonPositive.text || '';
}
NativeModules.DialogManagerAndroid.showAlert(
config,
errorMessage => console.warn(errorMessage),
(action, buttonKey) => {
if (action === NativeModules.DialogManagerAndroid.buttonClicked) {
if (
buttonKey === NativeModules.DialogManagerAndroid.buttonNeutral
) {
buttonNeutral.onPress && buttonNeutral.onPress();
} else if (
buttonKey === NativeModules.DialogManagerAndroid.buttonNegative
) {
buttonNegative.onPress && buttonNegative.onPress();
} else if (
buttonKey === NativeModules.DialogManagerAndroid.buttonPositive
) {
buttonPositive.onPress && buttonPositive.onPress();
}
} else if (action === NativeModules.DialogManagerAndroid.dismissed) {
options && options.onDismiss && options.onDismiss();
const onAction = (action, buttonKey) => {
if (action === constants.buttonClicked) {
if (buttonKey === constants.buttonNeutral) {
buttonNeutral.onPress && buttonNeutral.onPress();
} else if (buttonKey === constants.buttonNegative) {
buttonNegative.onPress && buttonNegative.onPress();
} else if (buttonKey === constants.buttonPositive) {
buttonPositive.onPress && buttonPositive.onPress();
}
},
);
} else if (action === constants.dismissed) {
options && options.onDismiss && options.onDismiss();
}
};
const onError = errorMessage => console.warn(errorMessage);
DialogManagerAndroid.showAlert(config, onError, onAction);
}
}
+6 -2
View File
@@ -9,14 +9,18 @@
'use strict';
const NativeModules = require('../BatchedBridge/NativeModules');
import NativeDialogManagerAndroid from '../NativeModules/specs/NativeDialogManagerAndroid';
function emptyCallback() {}
module.exports = {
alertWithArgs: function(args, callback) {
// TODO(5998984): Polyfill it correctly with DialogManagerAndroid
NativeModules.DialogManagerAndroid.showAlert(
if (!NativeDialogManagerAndroid) {
return;
}
NativeDialogManagerAndroid.showAlert(
args,
emptyCallback,
callback || emptyCallback,
@@ -0,0 +1,49 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow strict-local
* @format
*/
'use strict';
import type {TurboModule} from 'RCTExport';
import * as TurboModuleRegistry from 'TurboModuleRegistry';
/* 'buttonClicked' | 'dismissed' */
type DialogAction = string;
/*
buttonPositive = -1,
buttonNegative = -2,
buttonNeutral = -3
*/
type DialogButtonKey = number;
export type DialogOptions = {|
title?: string,
message?: string,
buttonPositive?: string,
buttonNegative?: string,
buttonNeutral?: string,
items?: Array<string>,
cancelable?: boolean,
|};
export interface Spec extends TurboModule {
+getConstants: () => {|
+buttonClicked: DialogAction,
+dismissed: DialogAction,
+buttonPositive: DialogButtonKey,
+buttonNegative: DialogButtonKey,
+buttonNeutral: DialogButtonKey,
|};
+showAlert: (
config: DialogOptions,
onError: (string) => void,
onAction: (action: DialogAction, buttonKey?: DialogButtonKey) => void,
) => void;
}
export default TurboModuleRegistry.get<Spec>('DialogManagerAndroid');
@@ -10,6 +10,7 @@
'use strict';
import NativeDialogManagerAndroid from '../NativeModules/specs/NativeDialogManagerAndroid';
const NativeModules = require('../BatchedBridge/NativeModules');
export type Rationale = {
@@ -134,10 +135,13 @@ class PermissionsAndroid {
permission,
);
if (shouldShowRationale) {
if (shouldShowRationale && !!NativeDialogManagerAndroid) {
return new Promise((resolve, reject) => {
NativeModules.DialogManagerAndroid.showAlert(
rationale,
const options = {
...rationale,
};
NativeDialogManagerAndroid.showAlert(
options,
() => reject(new Error('Error showing rationale')),
() =>
resolve(