1 | import { getLabelColor, getButtonColors, isDialogOptions, inputType, capitalizationType, DialogStrings, parseLoginOptions } from './dialogs-common';
|
2 | import { ad } from '../../utils/native-helper';
|
3 | export * from './dialogs-common';
|
4 | function isString(value) {
|
5 | return typeof value === 'string';
|
6 | }
|
7 | function createAlertDialog(options) {
|
8 | const alert = new android.app.AlertDialog.Builder(ad.getCurrentActivity(), options.theme ? options.theme : -1);
|
9 | alert.setTitle(options && isString(options.title) ? options.title : '');
|
10 | alert.setMessage(options && isString(options.message) ? options.message : '');
|
11 | if (options && options.cancelable === false) {
|
12 | alert.setCancelable(false);
|
13 | }
|
14 | return alert;
|
15 | }
|
16 | function showDialog(builder) {
|
17 | const dlg = builder.show();
|
18 | const labelColor = getLabelColor();
|
19 | if (labelColor) {
|
20 | const textViewId = dlg.getContext().getResources().getIdentifier('android:id/alertTitle', null, null);
|
21 | if (textViewId) {
|
22 | const tv = dlg.findViewById(textViewId);
|
23 | if (tv) {
|
24 | tv.setTextColor(labelColor.android);
|
25 | }
|
26 | }
|
27 | const messageTextViewId = dlg.getContext().getResources().getIdentifier('android:id/message', null, null);
|
28 | if (messageTextViewId) {
|
29 | const messageTextView = dlg.findViewById(messageTextViewId);
|
30 | if (messageTextView) {
|
31 | messageTextView.setTextColor(labelColor.android);
|
32 | }
|
33 | }
|
34 | }
|
35 | const { color, backgroundColor } = getButtonColors();
|
36 | if (color) {
|
37 | const buttons = [];
|
38 | for (let i = 0; i < 3; i++) {
|
39 | const id = dlg
|
40 | .getContext()
|
41 | .getResources()
|
42 | .getIdentifier('android:id/button' + i, null, null);
|
43 | buttons[i] = dlg.findViewById(id);
|
44 | }
|
45 | buttons.forEach((button) => {
|
46 | if (button) {
|
47 | if (color) {
|
48 | button.setTextColor(color.android);
|
49 | }
|
50 | if (backgroundColor) {
|
51 | button.setBackgroundColor(backgroundColor.android);
|
52 | }
|
53 | }
|
54 | });
|
55 | }
|
56 | }
|
57 | function addButtonsToAlertDialog(alert, options, callback) {
|
58 | if (!options) {
|
59 | return;
|
60 | }
|
61 | if (options.okButtonText) {
|
62 | alert.setPositiveButton(options.okButtonText, new android.content.DialogInterface.OnClickListener({
|
63 | onClick: function (dialog, id) {
|
64 | dialog.cancel();
|
65 | callback(true);
|
66 | },
|
67 | }));
|
68 | }
|
69 | if (options.cancelButtonText) {
|
70 | alert.setNegativeButton(options.cancelButtonText, new android.content.DialogInterface.OnClickListener({
|
71 | onClick: function (dialog, id) {
|
72 | dialog.cancel();
|
73 | callback(false);
|
74 | },
|
75 | }));
|
76 | }
|
77 | if (options.neutralButtonText) {
|
78 | alert.setNeutralButton(options.neutralButtonText, new android.content.DialogInterface.OnClickListener({
|
79 | onClick: function (dialog, id) {
|
80 | dialog.cancel();
|
81 | callback(undefined);
|
82 | },
|
83 | }));
|
84 | }
|
85 | alert.setOnDismissListener(new android.content.DialogInterface.OnDismissListener({
|
86 | onDismiss: function () {
|
87 | callback(false);
|
88 | },
|
89 | }));
|
90 | }
|
91 | export function alert(arg) {
|
92 | return new Promise((resolve, reject) => {
|
93 | try {
|
94 | const options = !isDialogOptions(arg) ? { title: DialogStrings.ALERT, okButtonText: DialogStrings.OK, message: arg + '' } : arg;
|
95 | const alert = createAlertDialog(options);
|
96 | alert.setPositiveButton(options.okButtonText, new android.content.DialogInterface.OnClickListener({
|
97 | onClick: function (dialog, id) {
|
98 | dialog.cancel();
|
99 | resolve();
|
100 | },
|
101 | }));
|
102 | alert.setOnDismissListener(new android.content.DialogInterface.OnDismissListener({
|
103 | onDismiss: function () {
|
104 | resolve();
|
105 | },
|
106 | }));
|
107 | showDialog(alert);
|
108 | }
|
109 | catch (ex) {
|
110 | reject(ex);
|
111 | }
|
112 | });
|
113 | }
|
114 | export function confirm(arg) {
|
115 | return new Promise((resolve, reject) => {
|
116 | try {
|
117 | const options = !isDialogOptions(arg)
|
118 | ? {
|
119 | title: DialogStrings.CONFIRM,
|
120 | okButtonText: DialogStrings.OK,
|
121 | cancelButtonText: DialogStrings.CANCEL,
|
122 | message: arg + '',
|
123 | }
|
124 | : arg;
|
125 | const alert = createAlertDialog(options);
|
126 | addButtonsToAlertDialog(alert, options, function (result) {
|
127 | resolve(result);
|
128 | });
|
129 | showDialog(alert);
|
130 | }
|
131 | catch (ex) {
|
132 | reject(ex);
|
133 | }
|
134 | });
|
135 | }
|
136 | export function prompt(...args) {
|
137 | let options;
|
138 | const defaultOptions = {
|
139 | title: DialogStrings.PROMPT,
|
140 | okButtonText: DialogStrings.OK,
|
141 | cancelButtonText: DialogStrings.CANCEL,
|
142 | inputType: inputType.text,
|
143 | };
|
144 | const arg = args[0];
|
145 | if (args.length === 1) {
|
146 | if (isString(arg)) {
|
147 | options = defaultOptions;
|
148 | options.message = arg;
|
149 | }
|
150 | else {
|
151 | options = arg;
|
152 | }
|
153 | }
|
154 | else if (args.length === 2) {
|
155 | if (isString(arg) && isString(args[1])) {
|
156 | options = defaultOptions;
|
157 | options.message = arg;
|
158 | options.defaultText = args[1];
|
159 | }
|
160 | }
|
161 | return new Promise((resolve, reject) => {
|
162 | try {
|
163 | const alert = createAlertDialog(options);
|
164 | const input = new android.widget.EditText(ad.getCurrentActivity());
|
165 | if (options) {
|
166 | if (options.inputType === inputType.password) {
|
167 | input.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD);
|
168 | }
|
169 | else if (options.inputType === inputType.email) {
|
170 | input.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
|
171 | }
|
172 | else if (options.inputType === inputType.number) {
|
173 | input.setInputType(android.text.InputType.TYPE_CLASS_NUMBER);
|
174 | }
|
175 | else if (options.inputType === inputType.decimal) {
|
176 | input.setInputType(android.text.InputType.TYPE_CLASS_NUMBER | android.text.InputType.TYPE_NUMBER_FLAG_DECIMAL);
|
177 | }
|
178 | else if (options.inputType === inputType.phone) {
|
179 | input.setInputType(android.text.InputType.TYPE_CLASS_PHONE);
|
180 | }
|
181 | switch (options.capitalizationType) {
|
182 | case capitalizationType.all: {
|
183 | input.setInputType(input.getInputType() | android.text.InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
|
184 | break;
|
185 | }
|
186 | case capitalizationType.sentences: {
|
187 | input.setInputType(input.getInputType() | android.text.InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
|
188 | break;
|
189 | }
|
190 | case capitalizationType.words: {
|
191 | input.setInputType(input.getInputType() | android.text.InputType.TYPE_TEXT_FLAG_CAP_WORDS);
|
192 | break;
|
193 | }
|
194 | }
|
195 | }
|
196 | input.setText((options && options.defaultText) || '');
|
197 | alert.setView(input);
|
198 | const getText = function () {
|
199 | return input.getText().toString();
|
200 | };
|
201 | addButtonsToAlertDialog(alert, options, function (r) {
|
202 | resolve({ result: r, text: getText() });
|
203 | });
|
204 | showDialog(alert);
|
205 | }
|
206 | catch (ex) {
|
207 | reject(ex);
|
208 | }
|
209 | });
|
210 | }
|
211 | export function login(...args) {
|
212 | const options = parseLoginOptions(args);
|
213 | return new Promise((resolve, reject) => {
|
214 | try {
|
215 | const alert = createAlertDialog(options);
|
216 | const userNameInput = new android.widget.EditText(ad.getApplicationContext());
|
217 | userNameInput.setHint(options.userNameHint ? options.userNameHint : '');
|
218 | userNameInput.setText(options.userName ? options.userName : '');
|
219 | const passwordInput = new android.widget.EditText(ad.getApplicationContext());
|
220 | passwordInput.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD);
|
221 | passwordInput.setTypeface(android.graphics.Typeface.DEFAULT);
|
222 | passwordInput.setHint(options.passwordHint ? options.passwordHint : '');
|
223 | passwordInput.setText(options.password ? options.password : '');
|
224 | const layout = new android.widget.LinearLayout(ad.getApplicationContext());
|
225 | layout.setOrientation(1);
|
226 | layout.addView(userNameInput);
|
227 | layout.addView(passwordInput);
|
228 | alert.setView(layout);
|
229 | addButtonsToAlertDialog(alert, options, function (r) {
|
230 | resolve({
|
231 | result: r,
|
232 | userName: userNameInput.getText().toString(),
|
233 | password: passwordInput.getText().toString(),
|
234 | });
|
235 | });
|
236 | showDialog(alert);
|
237 | }
|
238 | catch (ex) {
|
239 | reject(ex);
|
240 | }
|
241 | });
|
242 | }
|
243 | export function action(...args) {
|
244 | let options;
|
245 | const defaultOptions = { title: null, cancelButtonText: DialogStrings.CANCEL };
|
246 | if (args.length === 1) {
|
247 | if (isString(args[0])) {
|
248 | options = defaultOptions;
|
249 | options.message = args[0];
|
250 | }
|
251 | else {
|
252 | options = args[0];
|
253 | }
|
254 | }
|
255 | else if (args.length === 2) {
|
256 | if (isString(args[0]) && isString(args[1])) {
|
257 | options = defaultOptions;
|
258 | options.message = args[0];
|
259 | options.cancelButtonText = args[1];
|
260 | }
|
261 | }
|
262 | else if (args.length === 3) {
|
263 | if (isString(args[0]) && isString(args[1]) && typeof args[2] !== 'undefined') {
|
264 | options = defaultOptions;
|
265 | options.message = args[0];
|
266 | options.cancelButtonText = args[1];
|
267 | options.actions = args[2];
|
268 | }
|
269 | }
|
270 | return new Promise((resolve, reject) => {
|
271 | try {
|
272 | const alert = new android.app.AlertDialog.Builder(ad.getCurrentActivity(), options.theme ? options.theme : -1);
|
273 | const message = options && isString(options.message) ? options.message : '';
|
274 | const title = options && isString(options.title) ? options.title : '';
|
275 | if (options && options.cancelable === false) {
|
276 | alert.setCancelable(false);
|
277 | }
|
278 | if (title) {
|
279 | alert.setTitle(title);
|
280 | if (!options.actions) {
|
281 | alert.setMessage(message);
|
282 | }
|
283 | }
|
284 | else {
|
285 | alert.setTitle(message);
|
286 | }
|
287 | if (options.actions) {
|
288 | alert.setItems(options.actions, new android.content.DialogInterface.OnClickListener({
|
289 | onClick: function (dialog, which) {
|
290 | resolve(options.actions[which]);
|
291 | },
|
292 | }));
|
293 | }
|
294 | if (isString(options.cancelButtonText)) {
|
295 | alert.setNegativeButton(options.cancelButtonText, new android.content.DialogInterface.OnClickListener({
|
296 | onClick: function (dialog, id) {
|
297 | dialog.cancel();
|
298 | resolve(options.cancelButtonText);
|
299 | },
|
300 | }));
|
301 | }
|
302 | alert.setOnDismissListener(new android.content.DialogInterface.OnDismissListener({
|
303 | onDismiss: function () {
|
304 | if (isString(options.cancelButtonText)) {
|
305 | resolve(options.cancelButtonText);
|
306 | }
|
307 | else {
|
308 | resolve('');
|
309 | }
|
310 | },
|
311 | }));
|
312 | showDialog(alert);
|
313 | }
|
314 | catch (ex) {
|
315 | reject(ex);
|
316 | }
|
317 | });
|
318 | }
|
319 |
|
320 |
|
321 |
|
322 | export const Dialogs = {
|
323 | alert,
|
324 | confirm,
|
325 | prompt,
|
326 | login,
|
327 | action,
|
328 | };
|
329 |
|
\ | No newline at end of file |