Skip to content

Commit 32340d3

Browse files
uqmessiasfacebook-github-bot
authored andcommitted
Add spec for DialogManagerAndroid (facebook#24912)
Summary: Part of facebook#24875. ## Changelog [General] [Added] - TM add spec for DialogManagerAndroid Pull Request resolved: facebook#24912 Reviewed By: fkgozali Differential Revision: D15433854 Pulled By: RSNara fbshipit-source-id: e7234debe16de5afbc770f8feee2471f41b54427
1 parent 116ac65 commit 32340d3

File tree

4 files changed

+95
-35
lines changed

4 files changed

+95
-35
lines changed

Libraries/Alert/Alert.js

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@
1010

1111
'use strict';
1212

13-
const NativeModules = require('../BatchedBridge/NativeModules');
13+
import NativeModules from '../BatchedBridge/NativeModules';
14+
import Platform from '../Utilities/Platform';
15+
import DialogManagerAndroid, {
16+
type DialogOptions,
17+
} from '../NativeModules/specs/NativeDialogManagerAndroid';
18+
1419
const RCTAlertManager = NativeModules.AlertManager;
15-
const Platform = require('../Utilities/Platform');
1620

1721
export type Buttons = Array<{
1822
text?: string,
@@ -53,14 +57,19 @@ class Alert {
5357
if (Platform.OS === 'ios') {
5458
Alert.prompt(title, message, buttons, 'default');
5559
} else if (Platform.OS === 'android') {
56-
let config = {
60+
if (!DialogManagerAndroid) {
61+
return;
62+
}
63+
const constants = DialogManagerAndroid.getConstants();
64+
65+
const config: DialogOptions = {
5766
title: title || '',
5867
message: message || '',
5968
cancelable: false,
6069
};
6170

62-
if (options) {
63-
config = {...config, cancelable: options.cancelable};
71+
if (options && options.cancelable) {
72+
config.cancelable = options.cancelable;
6473
}
6574
// At most three buttons (neutral, negative, positive). Ignore rest.
6675
// The text 'OK' should be probably localized. iOS Alert does that in native.
@@ -70,38 +79,32 @@ class Alert {
7079
const buttonPositive = validButtons.pop();
7180
const buttonNegative = validButtons.pop();
7281
const buttonNeutral = validButtons.pop();
82+
7383
if (buttonNeutral) {
74-
config = {...config, buttonNeutral: buttonNeutral.text || ''};
84+
config.buttonNeutral = buttonNeutral.text || '';
7585
}
7686
if (buttonNegative) {
77-
config = {...config, buttonNegative: buttonNegative.text || ''};
87+
config.buttonNegative = buttonNegative.text || '';
7888
}
7989
if (buttonPositive) {
80-
config = {...config, buttonPositive: buttonPositive.text || ''};
90+
config.buttonPositive = buttonPositive.text || '';
8191
}
82-
NativeModules.DialogManagerAndroid.showAlert(
83-
config,
84-
errorMessage => console.warn(errorMessage),
85-
(action, buttonKey) => {
86-
if (action === NativeModules.DialogManagerAndroid.buttonClicked) {
87-
if (
88-
buttonKey === NativeModules.DialogManagerAndroid.buttonNeutral
89-
) {
90-
buttonNeutral.onPress && buttonNeutral.onPress();
91-
} else if (
92-
buttonKey === NativeModules.DialogManagerAndroid.buttonNegative
93-
) {
94-
buttonNegative.onPress && buttonNegative.onPress();
95-
} else if (
96-
buttonKey === NativeModules.DialogManagerAndroid.buttonPositive
97-
) {
98-
buttonPositive.onPress && buttonPositive.onPress();
99-
}
100-
} else if (action === NativeModules.DialogManagerAndroid.dismissed) {
101-
options && options.onDismiss && options.onDismiss();
92+
93+
const onAction = (action, buttonKey) => {
94+
if (action === constants.buttonClicked) {
95+
if (buttonKey === constants.buttonNeutral) {
96+
buttonNeutral.onPress && buttonNeutral.onPress();
97+
} else if (buttonKey === constants.buttonNegative) {
98+
buttonNegative.onPress && buttonNegative.onPress();
99+
} else if (buttonKey === constants.buttonPositive) {
100+
buttonPositive.onPress && buttonPositive.onPress();
102101
}
103-
},
104-
);
102+
} else if (action === constants.dismissed) {
103+
options && options.onDismiss && options.onDismiss();
104+
}
105+
};
106+
const onError = errorMessage => console.warn(errorMessage);
107+
DialogManagerAndroid.showAlert(config, onError, onAction);
105108
}
106109
}
107110

Libraries/Alert/RCTAlertManager.android.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@
99

1010
'use strict';
1111

12-
const NativeModules = require('../BatchedBridge/NativeModules');
12+
import NativeDialogManagerAndroid from '../NativeModules/specs/NativeDialogManagerAndroid';
1313

1414
function emptyCallback() {}
1515

1616
module.exports = {
1717
alertWithArgs: function(args, callback) {
1818
// TODO(5998984): Polyfill it correctly with DialogManagerAndroid
19-
NativeModules.DialogManagerAndroid.showAlert(
19+
if (!NativeDialogManagerAndroid) {
20+
return;
21+
}
22+
23+
NativeDialogManagerAndroid.showAlert(
2024
args,
2125
emptyCallback,
2226
callback || emptyCallback,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
*/
10+
11+
'use strict';
12+
13+
import type {TurboModule} from 'RCTExport';
14+
import * as TurboModuleRegistry from 'TurboModuleRegistry';
15+
16+
/* 'buttonClicked' | 'dismissed' */
17+
type DialogAction = string;
18+
/*
19+
buttonPositive = -1,
20+
buttonNegative = -2,
21+
buttonNeutral = -3
22+
*/
23+
type DialogButtonKey = number;
24+
export type DialogOptions = {|
25+
title?: string,
26+
message?: string,
27+
buttonPositive?: string,
28+
buttonNegative?: string,
29+
buttonNeutral?: string,
30+
items?: Array<string>,
31+
cancelable?: boolean,
32+
|};
33+
34+
export interface Spec extends TurboModule {
35+
+getConstants: () => {|
36+
+buttonClicked: DialogAction,
37+
+dismissed: DialogAction,
38+
+buttonPositive: DialogButtonKey,
39+
+buttonNegative: DialogButtonKey,
40+
+buttonNeutral: DialogButtonKey,
41+
|};
42+
+showAlert: (
43+
config: DialogOptions,
44+
onError: (string) => void,
45+
onAction: (action: DialogAction, buttonKey?: DialogButtonKey) => void,
46+
) => void;
47+
}
48+
49+
export default TurboModuleRegistry.get<Spec>('DialogManagerAndroid');

Libraries/PermissionsAndroid/PermissionsAndroid.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
'use strict';
1212

13+
import NativeDialogManagerAndroid from '../NativeModules/specs/NativeDialogManagerAndroid';
1314
const NativeModules = require('../BatchedBridge/NativeModules');
1415

1516
export type Rationale = {
@@ -134,10 +135,13 @@ class PermissionsAndroid {
134135
permission,
135136
);
136137

137-
if (shouldShowRationale) {
138+
if (shouldShowRationale && !!NativeDialogManagerAndroid) {
138139
return new Promise((resolve, reject) => {
139-
NativeModules.DialogManagerAndroid.showAlert(
140-
rationale,
140+
const options = {
141+
...rationale,
142+
};
143+
NativeDialogManagerAndroid.showAlert(
144+
options,
141145
() => reject(new Error('Error showing rationale')),
142146
() =>
143147
resolve(

0 commit comments

Comments
 (0)