Skip to content

Commit 9c63c5d

Browse files
committed
fix(dialogs): ios correctly release everyting on close.
support for the PR NativeScript/NativeScript#10448
1 parent 164ab08 commit 9c63c5d

File tree

1 file changed

+50
-21
lines changed

1 file changed

+50
-21
lines changed

src/dialogs/dialogs.ios.ts

Lines changed: 50 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class MDCDialogPresentationControllerDelegateImpl extends NSObject {
7474
return delegate;
7575
}
7676
dialogPresentationControllerDidDismiss(controller: MDCDialogPresentationController) {
77+
// WARNING: only called when clicked on background!
7778
const callback = this._callback;
7879
if (callback) {
7980
callback();
@@ -83,6 +84,7 @@ class MDCDialogPresentationControllerDelegateImpl extends NSObject {
8384
@NativeClass
8485
class MDCAlertControllerImpl extends MDCAlertController {
8586
autoFocusTextField?: TextField;
87+
clear: Function;
8688
_resolveFunction?: Function;
8789
_disableContentInsets: boolean;
8890
viewDidAppear(animated: boolean) {
@@ -99,20 +101,27 @@ class MDCAlertControllerImpl extends MDCAlertController {
99101
(this.view as MDCAlertControllerView).contentInsets = UIEdgeInsetsZero;
100102
}
101103
}
102-
viewDidUnload() {
103-
super.viewDidUnload();
104-
if (this.accessoryView instanceof UIViewAutoSizeUIViewAutoSize) {
105-
const view = this.accessoryView._view;
106-
view.callUnloaded();
107-
view._tearDownUI(true);
108-
view.parent = null;
109-
view._isAddedToNativeVisualTree = false;
110-
this.accessoryView._view = null;
111-
}
112-
}
104+
viewDidLayoutSubviews() {
105+
// we enforce the bounds first
106+
// when showing a modal on top of us and then hiding the modal, our size gets messed up
107+
this.view.bounds = CGRectMake(0, 0, this.preferredContentSize.width, this.preferredContentSize.height);
108+
super.viewDidLayoutSubviews();
109+
}
110+
// viewDidUnload is not called anymore
111+
// viewDidUnload() {
112+
// super.viewDidUnload();
113+
// if (this.accessoryView instanceof UIViewAutoSizeUIViewAutoSize) {
114+
// const view = this.accessoryView._view;
115+
// view.callUnloaded();
116+
// view._tearDownUI(true);
117+
// view.parent = null;
118+
// view._isAddedToNativeVisualTree = false;
119+
// this.accessoryView._view = null;
120+
// }
121+
// }
113122
}
114123

115-
function addButtonsToAlertController(alertController: MDCAlertController, options: ConfirmOptions & MDCAlertControlerOptions, callback?: Function, validationArgs?: (r) => any): void {
124+
function addButtonsToAlertController(alertController: MDCAlertControllerImpl, options: ConfirmOptions & MDCAlertControlerOptions, callback?: Function, validationArgs?: (r) => any): void {
116125
if (!options) {
117126
return;
118127
}
@@ -128,7 +137,9 @@ function addButtonsToAlertController(alertController: MDCAlertController, option
128137
if (Utils.isFunction(callback)) {
129138
callback(result);
130139
}
131-
alertController.dismissModalViewControllerAnimated(true);
140+
alertController.dismissViewControllerAnimatedCompletion(true, () => {
141+
alertController.clear();
142+
});
132143
}
133144
let buttonsFont;
134145

@@ -159,6 +170,7 @@ function addButtonsToAlertController(alertController: MDCAlertController, option
159170

160171
function createAlertController(options: DialogOptions & MDCAlertControlerOptions, resolve?: Function) {
161172
const alertController = MDCAlertControllerImpl.alloc().init() as MDCAlertControllerImpl;
173+
alertController['isAlertController'] = true;
162174
alertController.mdc_adjustsFontForContentSizeCategory = true;
163175

164176
if (options.title) {
@@ -167,6 +179,20 @@ function createAlertController(options: DialogOptions & MDCAlertControlerOptions
167179
if (options.message) {
168180
alertController.message = options.message;
169181
}
182+
183+
const clear = (alertController.clear = function clear() {
184+
alertController._resolveFunction = null;
185+
if (alertController.accessoryView instanceof UIViewAutoSizeUIViewAutoSize) {
186+
const view = alertController.accessoryView._view;
187+
view.callUnloaded();
188+
view._tearDownUI(true);
189+
view.parent = null;
190+
view._isAddedToNativeVisualTree = false;
191+
alertController.accessoryView._view = null;
192+
}
193+
alertController._resolveFunction = null;
194+
alertController.mdc_dialogPresentationController.delegate = null;
195+
});
170196
// if (options.buttonFont) {
171197
// alertController.buttonFont = options.buttonFont.getUIFont(alertController.buttonFont);
172198
// }
@@ -186,11 +212,13 @@ function createAlertController(options: DialogOptions & MDCAlertControlerOptions
186212
alertController._resolveFunction = resolve;
187213
const context = options.context || {};
188214
context.closeCallback = function (...originalArgs) {
189-
alertController.dismissModalViewControllerAnimated(true);
190215
if (alertController._resolveFunction && resolve) {
191216
alertController._resolveFunction = null;
192217
resolve.apply(this, originalArgs);
193218
}
219+
alertController.dismissViewControllerAnimatedCompletion(true, () => {
220+
clear();
221+
});
194222
};
195223
view.bindingContext = fromObject(context);
196224
alertController.accessoryView = createUIViewAutoSizeUIViewAutoSize(view);
@@ -206,9 +234,8 @@ function createAlertController(options: DialogOptions & MDCAlertControlerOptions
206234
view.viewController = alertController; // needed to prevent a crash in layoutChild
207235
}
208236
const dialogPresentationControllerDelegate = MDCDialogPresentationControllerDelegateImpl.initWithCallback(() => {
209-
resolve && resolve();
210-
alertController._resolveFunction = null;
211-
alertController.mdc_dialogPresentationController.delegate = null;
237+
resolve?.();
238+
clear();
212239
});
213240
alertController.mdc_dialogPresentationController.dialogPresentationControllerDelegate = dialogPresentationControllerDelegate;
214241

@@ -236,7 +263,6 @@ export function alert(arg: any): Promise<any> {
236263
const alertController = createAlertController(options, resolve);
237264

238265
addButtonsToAlertController(alertController, options, (result) => {
239-
alertController._resolveFunction = null;
240266
resolve(result);
241267
});
242268

@@ -248,7 +274,7 @@ export function alert(arg: any): Promise<any> {
248274
}
249275

250276
export class AlertDialog {
251-
alertController: MDCAlertController;
277+
alertController: MDCAlertControllerImpl;
252278
presentingController: UIViewController;
253279
constructor(private options: any) {}
254280

@@ -261,7 +287,10 @@ export class AlertDialog {
261287
async hide() {
262288
if (this.presentingController) {
263289
return new Promise<void>((resolve) => {
264-
this.presentingController.dismissViewControllerAnimatedCompletion(true, resolve);
290+
this.presentingController.dismissViewControllerAnimatedCompletion(true, () => {
291+
resolve?.();
292+
this.alertController.clear();
293+
});
265294
this.presentingController = null;
266295
this.alertController = null;
267296
});
@@ -534,7 +563,7 @@ function showUIAlertController(alertController: MDCAlertController, options: Dia
534563

535564
let viewController = Application.ios.rootController;
536565

537-
while (viewController && viewController.presentedViewController && !(viewController.presentedViewController instanceof MDCAlertControllerImpl)) {
566+
while (viewController && viewController.presentedViewController) {
538567
viewController = viewController.presentedViewController;
539568
}
540569

0 commit comments

Comments
 (0)