Files
Alexander Vakrilov cc97a16800 feat: Scoped Packages (#7911)
* chore: move tns-core-modules to nativescript-core

* chore: preparing compat generate script

* chore: add missing definitions

* chore: no need for http-request to be private

* chore: packages chore

* test: generate tests for tns-core-modules

* chore: add anroid module for consistency

* chore: add .npmignore

* chore: added privateModulesWhitelist

* chore(webpack): added bundle-entry-points

* chore: scripts

* chore: tests changed to use @ns/core

* test: add scoped-packages test project

* test: fix types

* test: update test project

* chore: build scripts

* chore: update build script

* chore: npm scripts cleanup

* chore: make the compat pgk work with old wp config

* test: generate diff friendly tests

* chore: create barrel exports

* chore: move files after rebase

* chore: typedoc config

* chore: compat mode

* chore: review of barrels

* chore: remove tns-core-modules import after rebase

* chore: dev workflow setup

* chore: update developer-workflow

* docs: experiment with API extractor

* chore: api-extractor and barrel exports

* chore: api-extractor configs

* chore: generate d.ts rollup with api-extractor

* refactor: move methods inside Frame

* chore: fic tests to use Frame static methods

* refactor: create Builder class

* refactor: use Builder class in tests

* refactor: include Style in ui barrel

* chore: separate compat build script

* chore: fix tslint errors

* chore: update NATIVESCRIPT_CORE_ARGS

* chore: fix compat pack

* chore: fix ui-test-app build with linked modules

* chore: Application, ApplicationSettings, Connectivity and Http

* chore: export Trace, Profiling and Utils

* refactor: Static create methods for ImageSource

* chore: fix deprecated usages of ImageSource

* chore: move Span and FormattedString to ui

* chore: add events-args and ImageSource to index files

* chore: check for CLI >= 6.2 when building for IOS

* chore: update travis build

* chore: copy Pod file to compat package

* chore: update error msg ui-tests-app

* refactor: Apply suggestions from code review

Co-Authored-By: Martin Yankov <m.i.yankov@gmail.com>

* chore: typings and refs

* chore: add missing d.ts files for public API

* chore: adress code review FB

* chore: update api-report

* chore: dev-workflow for other apps

* chore: api update

* chore: update api-report
2019-10-17 00:45:33 +03:00

353 lines
13 KiB
TypeScript

/**
* Android specific dialogs functions implementation.
*/
import { DialogOptions, ConfirmOptions, PromptOptions, PromptResult, LoginOptions, LoginResult, ActionOptions } from ".";
import { getLabelColor, getButtonColors, isDialogOptions, inputType, capitalizationType, ALERT, OK, CONFIRM, CANCEL, PROMPT, parseLoginOptions } from "./dialogs-common";
import { android as androidApp } from "../../application";
export * from "./dialogs-common";
function isString(value): value is string {
return typeof value === "string";
}
function createAlertDialog(options?: DialogOptions): android.app.AlertDialog.Builder {
const alert = new android.app.AlertDialog.Builder(androidApp.foregroundActivity);
alert.setTitle(options && isString(options.title) ? options.title : "");
alert.setMessage(options && isString(options.message) ? options.message : "");
if (options && options.cancelable === false) {
alert.setCancelable(false);
}
return alert;
}
function showDialog(builder: android.app.AlertDialog.Builder) {
const dlg = builder.show();
const labelColor = getLabelColor();
if (labelColor) {
const textViewId = dlg.getContext().getResources().getIdentifier("android:id/alertTitle", null, null);
if (textViewId) {
const tv = <android.widget.TextView>dlg.findViewById(textViewId);
if (tv) {
tv.setTextColor(labelColor.android);
}
}
const messageTextViewId = dlg.getContext().getResources().getIdentifier("android:id/message", null, null);
if (messageTextViewId) {
const messageTextView = <android.widget.TextView>dlg.findViewById(messageTextViewId);
if (messageTextView) {
messageTextView.setTextColor(labelColor.android);
}
}
}
let { color, backgroundColor } = getButtonColors();
if (color) {
let buttons: android.widget.Button[] = [];
for (let i = 0; i < 3; i++) {
let id = dlg.getContext().getResources().getIdentifier("android:id/button" + i, null, null);
buttons[i] = <android.widget.Button>dlg.findViewById(id);
}
buttons.forEach(button => {
if (button) {
if (color) {
button.setTextColor(color.android);
}
if (backgroundColor) {
button.setBackgroundColor(backgroundColor.android);
}
}
});
}
}
function addButtonsToAlertDialog(alert: android.app.AlertDialog.Builder, options: ConfirmOptions,
callback: Function): void {
if (!options) {
return;
}
if (options.okButtonText) {
alert.setPositiveButton(options.okButtonText, new android.content.DialogInterface.OnClickListener({
onClick: function (dialog: android.content.DialogInterface, id: number) {
dialog.cancel();
callback(true);
}
}));
}
if (options.cancelButtonText) {
alert.setNegativeButton(options.cancelButtonText, new android.content.DialogInterface.OnClickListener({
onClick: function (dialog: android.content.DialogInterface, id: number) {
dialog.cancel();
callback(false);
}
}));
}
if (options.neutralButtonText) {
alert.setNeutralButton(options.neutralButtonText, new android.content.DialogInterface.OnClickListener({
onClick: function (dialog: android.content.DialogInterface, id: number) {
dialog.cancel();
callback(undefined);
}
}));
}
alert.setOnDismissListener(new android.content.DialogInterface.OnDismissListener({
onDismiss: function () {
callback(false);
}
}));
}
export function alert(arg: any): Promise<void> {
return new Promise<void>((resolve, reject) => {
try {
const options = !isDialogOptions(arg) ? { title: ALERT, okButtonText: OK, message: arg + "" } : arg;
const alert = createAlertDialog(options);
alert.setPositiveButton(options.okButtonText, new android.content.DialogInterface.OnClickListener({
onClick: function (dialog: android.content.DialogInterface, id: number) {
dialog.cancel();
resolve();
}
}));
alert.setOnDismissListener(new android.content.DialogInterface.OnDismissListener({
onDismiss: function () {
resolve();
}
}));
showDialog(alert);
} catch (ex) {
reject(ex);
}
});
}
export function confirm(arg: any): Promise<boolean> {
return new Promise<boolean>((resolve, reject) => {
try {
const options = !isDialogOptions(arg) ? { title: CONFIRM, okButtonText: OK, cancelButtonText: CANCEL, message: arg + "" } : arg;
const alert = createAlertDialog(options);
addButtonsToAlertDialog(alert, options, function (result) { resolve(result); });
showDialog(alert);
} catch (ex) {
reject(ex);
}
});
}
export function prompt(arg: any): Promise<PromptResult> {
let options: PromptOptions;
const defaultOptions = {
title: PROMPT,
okButtonText: OK,
cancelButtonText: CANCEL,
inputType: inputType.text,
};
if (arguments.length === 1) {
if (isString(arg)) {
options = defaultOptions;
options.message = arg;
} else {
options = arg;
}
} else if (arguments.length === 2) {
if (isString(arguments[0]) && isString(arguments[1])) {
options = defaultOptions;
options.message = arguments[0];
options.defaultText = arguments[1];
}
}
return new Promise<PromptResult>((resolve, reject) => {
try {
const alert = createAlertDialog(options);
const input = new android.widget.EditText(androidApp.foregroundActivity);
if (options) {
if (options.inputType === inputType.password) {
input.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD);
} else if (options.inputType === inputType.email) {
input.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
} else if (options.inputType === inputType.number) {
input.setInputType(android.text.InputType.TYPE_CLASS_NUMBER);
} else if (options.inputType === inputType.decimal) {
input.setInputType(android.text.InputType.TYPE_CLASS_NUMBER | android.text.InputType.TYPE_NUMBER_FLAG_DECIMAL);
} else if (options.inputType === inputType.phone) {
input.setInputType(android.text.InputType.TYPE_CLASS_PHONE);
}
switch (options.capitalizationType) {
case capitalizationType.all: {
input.setInputType(input.getInputType() | android.text.InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS);
break;
}
case capitalizationType.sentences: {
input.setInputType(input.getInputType() | android.text.InputType.TYPE_TEXT_FLAG_CAP_SENTENCES);
break;
}
case capitalizationType.words: {
input.setInputType(input.getInputType() | android.text.InputType.TYPE_TEXT_FLAG_CAP_WORDS);
break;
}
}
}
input.setText(options && options.defaultText || "");
alert.setView(input);
const getText = function () { return input.getText().toString(); };
addButtonsToAlertDialog(alert, options, function (r) { resolve({ result: r, text: getText() }); });
showDialog(alert);
} catch (ex) {
reject(ex);
}
});
}
export function login(...args: any[]): Promise<LoginResult> {
let options: LoginOptions = parseLoginOptions(args);
return new Promise<LoginResult>((resolve, reject) => {
try {
const context = androidApp.foregroundActivity;
const alert = createAlertDialog(options);
const userNameInput = new android.widget.EditText(context);
userNameInput.setHint(options.userNameHint ? options.userNameHint : "");
userNameInput.setText(options.userName ? options.userName : "");
const passwordInput = new android.widget.EditText(context);
passwordInput.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD);
passwordInput.setTypeface(android.graphics.Typeface.DEFAULT);
passwordInput.setHint(options.passwordHint ? options.passwordHint : "");
passwordInput.setText(options.password ? options.password : "");
const layout = new android.widget.LinearLayout(context);
layout.setOrientation(1);
layout.addView(userNameInput);
layout.addView(passwordInput);
alert.setView(layout);
addButtonsToAlertDialog(alert, options, function (r) {
resolve({
result: r,
userName: userNameInput.getText().toString(),
password: passwordInput.getText().toString()
});
});
showDialog(alert);
} catch (ex) {
reject(ex);
}
});
}
export function action(arg: any): Promise<string> {
let options: ActionOptions;
const defaultOptions = { title: null, cancelButtonText: CANCEL };
if (arguments.length === 1) {
if (isString(arguments[0])) {
options = defaultOptions;
options.message = arguments[0];
} else {
options = arguments[0];
}
} else if (arguments.length === 2) {
if (isString(arguments[0]) && isString(arguments[1])) {
options = defaultOptions;
options.message = arguments[0];
options.cancelButtonText = arguments[1];
}
} else if (arguments.length === 3) {
if (isString(arguments[0]) && isString(arguments[1]) && typeof arguments[2] !== "undefined") {
options = defaultOptions;
options.message = arguments[0];
options.cancelButtonText = arguments[1];
options.actions = arguments[2];
}
}
return new Promise<string>((resolve, reject) => {
try {
const activity = androidApp.foregroundActivity || androidApp.startActivity;
const alert = new android.app.AlertDialog.Builder(activity);
const message = options && isString(options.message) ? options.message : "";
const title = options && isString(options.title) ? options.title : "";
if (options && options.cancelable === false) {
alert.setCancelable(false);
}
if (title) {
alert.setTitle(title);
if (!options.actions) {
alert.setMessage(message);
}
}
else {
alert.setTitle(message);
}
if (options.actions) {
alert.setItems(options.actions, new android.content.DialogInterface.OnClickListener({
onClick: function (dialog: android.content.DialogInterface, which: number) {
resolve(options.actions[which]);
}
}));
}
if (isString(options.cancelButtonText)) {
alert.setNegativeButton(options.cancelButtonText, new android.content.DialogInterface.OnClickListener({
onClick: function (dialog: android.content.DialogInterface, id: number) {
dialog.cancel();
resolve(options.cancelButtonText);
}
}));
}
alert.setOnDismissListener(new android.content.DialogInterface.OnDismissListener({
onDismiss: function () {
if (isString(options.cancelButtonText)) {
resolve(options.cancelButtonText);
} else {
resolve("");
}
}
}));
showDialog(alert);
} catch (ex) {
reject(ex);
}
});
}