Skip to content

Commit aa44eb9

Browse files
author
Vladimir Amiorkov
authored
fix(ios-modal): closeCallback not being called with popover presentation style (#7189)
1 parent 77c45da commit aa44eb9

File tree

8 files changed

+52
-5
lines changed

8 files changed

+52
-5
lines changed

e2e/modal-navigation/app/home/home-page.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as application from "tns-core-modules/application";
22
import { StackLayout } from "tns-core-modules/ui/layouts/stack-layout";
33
import { NavigatedData, Page } from "tns-core-modules/ui/page";
4-
import { View, EventData } from "tns-core-modules/ui/core/view";
4+
import { View, EventData, ShowModalOptions } from "tns-core-modules/ui/core/view";
55
import { Frame } from "tns-core-modules/ui/frame";
66

77
export function onNavigatingTo(args: NavigatedData) {
@@ -29,6 +29,20 @@ export function onModalNoPage(args: EventData) {
2929
false);
3030
}
3131

32+
export function onPopoverModal(args: EventData) {
33+
const view = args.object as View;
34+
let options: ShowModalOptions = {
35+
context: "context",
36+
closeCallback: () => console.log("home-page modal popover frame closed"),
37+
animated: false,
38+
ios: {
39+
presentationStyle: UIModalPresentationStyle.Popover
40+
}
41+
}
42+
43+
view.showModal("modal-no-page/modal-no-page", options);
44+
}
45+
3246
export function onModalFrame(args: EventData) {
3347
const view = args.object as View;
3448

e2e/modal-navigation/app/home/home-page.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<Button text="Show Modal Without Page" tap="onModalNoPage" textAlignment="left" />
2121
<Button text="Show Modal Page With Frame" tap="onModalFrame" textAlignment="left" />
2222
<Button text="Show Modal Page" tap="onModalPage" textAlignment="left" />
23+
<Button text="Show 'popover' modal" tap="onPopoverModal" textAlignment="left" />
2324
<Button text="Show Modal Layout" tap="onModalLayout" textAlignment="left" />
2425
<Button text="Show Modal TabView" tap="onModalTabView" textAlignment="left" />
2526
<Button text="Android Back Btn Events" tap="onAndroidBackEvents" textAlignment="left" />

e2e/modal-navigation/app/modal-no-page/modal-no-page.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { NavigatedData, Page } from "tns-core-modules/ui/page";
33
import { View, EventData } from "tns-core-modules/ui/core/view";
44
import { Frame, ShownModallyData } from "tns-core-modules/ui/frame";
55
import { fromObject } from "tns-core-modules/data/observable";
6-
import { confirm } from "ui/dialogs";
6+
import { confirm } from "tns-core-modules/ui/dialogs";
77

88
export function onLoaded(args) {
99
console.log("modal-no-page loaded");

e2e/modal-navigation/app/modal/modal-page.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { NavigatedData, Page } from "tns-core-modules/ui/page";
33
import { View, EventData } from "tns-core-modules/ui/core/view";
44
import { Frame, ShownModallyData } from "tns-core-modules/ui/frame";
55
import { fromObject } from "tns-core-modules/data/observable";
6-
import { confirm } from "ui/dialogs";
6+
import { confirm } from "tns-core-modules/ui/dialogs";
77

88
export function onShowingModally(args: ShownModallyData) {
99
console.log("modal-page showingModally");

e2e/modal-navigation/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"nativescript-dev-appium": "next",
2626
"nativescript-dev-typescript": "next",
2727
"nativescript-dev-webpack": "next",
28+
"tns-platform-declarations": "next",
2829
"rimraf": "^2.6.2",
2930
"typescript": "^3.1.6"
3031
},

e2e/modal-navigation/references.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/// <reference path="./node_modules/tns-platform-declarations/ios.d.ts" />

tns-core-modules/ui/core/view/view-common.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
} from "../../gestures";
2323

2424
import { createViewFromEntry } from "../../builder";
25+
import { isAndroid } from "../../../platform";
2526
import { StyleScope } from "../../styling/style-scope";
2627
import { LinearGradient } from "../../styling/linear-gradient";
2728
import { BackgroundRepeat } from "../../styling/style-properties";
@@ -324,7 +325,11 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
324325
}
325326
}
326327

327-
that._hideNativeModalView(parent, whenClosedCallback);
328+
if (isAndroid || (parent.viewController && parent.viewController.presentedViewController)) {
329+
that._hideNativeModalView(parent, whenClosedCallback);
330+
} else {
331+
whenClosedCallback();
332+
}
328333
}
329334
};
330335
}

tns-core-modules/ui/core/view/view.ios.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const majorVersion = iosUtils.MajorVersion;
2929
export class View extends ViewCommon {
3030
nativeViewProtected: UIView;
3131
viewController: UIViewController;
32+
private _popoverPresentationDelegate: ios.UIPopoverPresentationControllerDelegateImp;
3233

3334
private _isLaidOut = false;
3435
private _hasTransfrom = false;
@@ -416,6 +417,8 @@ export class View extends ViewCommon {
416417

417418
if (presentationStyle === UIModalPresentationStyle.Popover) {
418419
const popoverPresentationController = controller.popoverPresentationController;
420+
this._popoverPresentationDelegate = ios.UIPopoverPresentationControllerDelegateImp.initWithOwnerAndCallback(new WeakRef(this), this._closeModalCallback);
421+
popoverPresentationController.delegate = this._popoverPresentationDelegate;
419422
const view = parent.nativeViewProtected;
420423
// Note: sourceView and sourceRect are needed to specify the anchor location for the popover.
421424
// Note: sourceView should be the button triggering the modal. If it the Page the popover might appear "behind" the page content
@@ -906,7 +909,7 @@ export namespace ios {
906909

907910
public viewDidLoad(): void {
908911
super.viewDidLoad();
909-
912+
910913
// Unify translucent and opaque bars layout
911914
// this.edgesForExtendedLayout = UIRectEdgeBottom;
912915
this.extendedLayoutIncludesOpaqueBars = true;
@@ -982,4 +985,26 @@ export namespace ios {
982985
}
983986
}
984987
}
988+
989+
export class UIPopoverPresentationControllerDelegateImp extends NSObject implements UIPopoverPresentationControllerDelegate {
990+
public static ObjCProtocols = [UIPopoverPresentationControllerDelegate];
991+
992+
private owner: WeakRef<View>;
993+
private closedCallback: Function;
994+
995+
public static initWithOwnerAndCallback(owner: WeakRef<View>, whenClosedCallback: Function): UIPopoverPresentationControllerDelegateImp {
996+
const instance = <UIPopoverPresentationControllerDelegateImp>super.new();
997+
instance.owner = owner;
998+
instance.closedCallback = whenClosedCallback;
999+
1000+
return instance;
1001+
}
1002+
1003+
public popoverPresentationControllerDidDismissPopover(popoverPresentationController: UIPopoverPresentationController) {
1004+
const owner = this.owner.get();
1005+
if (owner && typeof this.closedCallback === "function") {
1006+
this.closedCallback();
1007+
}
1008+
}
1009+
}
9851010
}

0 commit comments

Comments
 (0)