fix(ios): dialog handling with top view controller (#8609)

This commit is contained in:
Nathan Walker
2020-06-03 12:42:10 -07:00
committed by GitHub
parent 446163d3f8
commit b015eeed51
5 changed files with 85 additions and 46 deletions

View File

@ -1,5 +1,6 @@
import { Page, ShownModallyData } from "tns-core-modules/ui/page";
import { EventData, fromObject } from "tns-core-modules/data/observable";
import { alert } from "tns-core-modules/ui/dialogs";
export function onShowingModally(args: ShownModallyData) {
console.log("login-page.onShowingModally, context: " + args.context);
@ -12,7 +13,19 @@ export function onShowingModally(args: ShownModallyData) {
onLoginButtonTap: function () {
console.log("login-page.onLoginButtonTap");
args.closeCallback(this.username, this.password);
}
},
showAlert: function () {
alert("showing alert!");
args.closeCallback();
},
openNestedModal: function () {
page.showModal("modal-view/nested-modal", {
context: "First",
closeCallback: () => {
console.log("login-page.openNestedModal");
}
});
}
});
}

View File

@ -7,5 +7,7 @@
<TextField hint="username" text="{{ username }}"/>
<TextField hint="password" text="{{ password }}" secure="true"/>
<Button text="Login" tap="{{ onLoginButtonTap }}"/>
<Button text="Show Alert" tap="{{ showAlert }}"/>
<Button text="Open Nested Modal" tap="{{ openNestedModal }}"/>
</StackLayout>
</Page>

View File

@ -0,0 +1,27 @@
import { Page, ShownModallyData } from "tns-core-modules/ui/page";
import { EventData, fromObject } from "tns-core-modules/data/observable";
import { alert } from "tns-core-modules/ui/dialogs";
export function onShowingModally(args: ShownModallyData) {
console.log("nested-modal.onShowingModally, context: " + args.context);
const page = <Page>args.object;
page.bindingContext = fromObject({
context: args.context,
onTap: function () {
alert("it works!");
}
});
}
export function onShownModally(args: ShownModallyData) {
console.log("nested-modal.onShownModally, context: " + args.context);
}
export function onLoaded(args: EventData) {
console.log("nested-modal.onLoaded");
}
export function onUnloaded() {
console.log("nested-modal.onUnloaded");
}

View File

@ -0,0 +1,9 @@
<Page xmlns="http://schemas.nativescript.org/tns.xsd"
showingModally="onShowingModally"
shownModally="onShownModally"
loaded="onLoaded" unloaded="onUnloaded" backgroundColor="Red">
<StackLayout backgroundColor="PaleGreen" margin="10">
<Label text="{{ context }}"/>
<Button text="Show Alert" tap="{{ onTap }}"/>
</StackLayout>
</Page>

View File

@ -1,11 +1,11 @@
/**
* iOS specific dialogs functions implementation.
*/
import { ios as iosView } from "../core/view";
import { ios as iosView, traceCategories, traceWrite, traceMessageType } from "../core/view";
import { ConfirmOptions, PromptOptions, PromptResult, LoginOptions, LoginResult, ActionOptions } from ".";
import { getCurrentPage, getLabelColor, getButtonColors, getTextFieldColor, isDialogOptions, inputType, capitalizationType, ALERT, OK, CONFIRM, CANCEL, PROMPT, parseLoginOptions } from "./dialogs-common";
import { isString, isDefined, isFunction } from "../../utils/types";
import { getRootView } from "../../application";
import { getRootView, ios } from "../../application";
export * from "./dialogs-common";
@ -200,54 +200,42 @@ export function login(...args: any[]): Promise<LoginResult> {
}
function showUIAlertController(alertController: UIAlertController) {
let currentView = getCurrentPage() || getRootView();
let viewController = ios.rootController;
while (viewController && viewController.presentedViewController) {
viewController = viewController.presentedViewController;
}
if (currentView) {
currentView = currentView.modal || currentView;
if (!viewController) {
traceWrite(`No root controller found to open dialog.`, traceCategories.Error, traceMessageType.warn);
return;
}
//get to the top most view controller on the stack
while (currentView && currentView.modal) {
currentView = currentView.modal;
if (alertController.popoverPresentationController) {
alertController.popoverPresentationController.sourceView = viewController.view;
alertController.popoverPresentationController.sourceRect = CGRectMake(viewController.view.bounds.size.width / 2.0, viewController.view.bounds.size.height / 2.0, 1.0, 1.0);
alertController.popoverPresentationController.permittedArrowDirections = 0;
}
let color = getButtonColors().color;
if (color) {
alertController.view.tintColor = color.ios;
}
let lblColor = getLabelColor();
if (lblColor) {
if (alertController.title) {
let title = NSAttributedString.alloc().initWithStringAttributes(alertController.title, <any>{ [NSForegroundColorAttributeName]: lblColor.ios });
alertController.setValueForKey(title, "attributedTitle");
}
let viewController: UIViewController = currentView.ios;
if (viewController.presentedViewController) {
viewController = viewController.presentedViewController;
}
if (!(currentView.ios instanceof UIViewController)) {
const parentWithController = iosView.getParentWithViewController(currentView);
viewController = parentWithController ? parentWithController.viewController : undefined;
}
if (viewController) {
if (alertController.popoverPresentationController) {
alertController.popoverPresentationController.sourceView = viewController.view;
alertController.popoverPresentationController.sourceRect = CGRectMake(viewController.view.bounds.size.width / 2.0, viewController.view.bounds.size.height / 2.0, 1.0, 1.0);
alertController.popoverPresentationController.permittedArrowDirections = 0;
}
let color = getButtonColors().color;
if (color) {
alertController.view.tintColor = color.ios;
}
let lblColor = getLabelColor();
if (lblColor) {
if (alertController.title) {
let title = NSAttributedString.alloc().initWithStringAttributes(alertController.title, <any>{ [NSForegroundColorAttributeName]: lblColor.ios });
alertController.setValueForKey(title, "attributedTitle");
}
if (alertController.message) {
let message = NSAttributedString.alloc().initWithStringAttributes(alertController.message, <any>{ [NSForegroundColorAttributeName]: lblColor.ios });
alertController.setValueForKey(message, "attributedMessage");
}
}
viewController.presentModalViewControllerAnimated(alertController, true);
if (alertController.message) {
let message = NSAttributedString.alloc().initWithStringAttributes(alertController.message, <any>{ [NSForegroundColorAttributeName]: lblColor.ios });
alertController.setValueForKey(message, "attributedMessage");
}
}
viewController.presentModalViewControllerAnimated(alertController, true);
}
export function action(): Promise<string> {