Skip to content

Commit bb36cb9

Browse files
committed
fix(dialogs): ios fix for dialogs when app in background
1 parent fa0029f commit bb36cb9

File tree

1 file changed

+42
-54
lines changed

1 file changed

+42
-54
lines changed

src/dialogs/dialogs.ios.ts

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,46 @@ import {
1818
capitalizationType,
1919
fromObject,
2020
getCurrentPage,
21-
inputType,
21+
inputType
2222
} from '@nativescript/core';
2323
import { LoginOptions, MDCAlertControlerOptions, PromptOptions } from './dialogs';
2424
import { isDialogOptions } from './dialogs-common';
2525

2626
export { capitalizationType, inputType };
2727

28-
const UIViewAutoSizeUIViewAutoSize = (UIView as any).extend({
28+
@NativeClass
29+
class UIViewAutoSizeUIViewAutoSize extends UIView {
30+
_view: View;
2931
systemLayoutSizeFittingSizeWithHorizontalFittingPriorityVerticalFittingPriority(boundsSize: CGSize) {
32+
const view = this._view;
33+
if (!view) {
34+
return CGSizeZero;
35+
}
3036
const actualWidth = Math.min(boundsSize.width, Screen.mainScreen.widthPixels);
3137
const widthSpec = Utils.layout.makeMeasureSpec(Utils.layout.toDevicePixels(actualWidth), Utils.layout.EXACTLY);
3238
const heighthSpec = Utils.layout.makeMeasureSpec(Utils.layout.toDevicePixels(boundsSize.height), Utils.layout.UNSPECIFIED);
33-
const view = this._view as View;
3439
const measuredSize = View.measureChild(null, view, widthSpec, heighthSpec);
3540
const newWidth = Utils.layout.toDevicePixels(actualWidth);
3641
view.setMeasuredDimension(newWidth, measuredSize.measuredHeight);
3742
const size = CGSizeMake(Utils.layout.toDeviceIndependentPixels(measuredSize.measuredWidth), Utils.layout.toDeviceIndependentPixels(measuredSize.measuredHeight));
3843
return size;
39-
},
44+
}
4045
layoutSubviews() {
41-
const view = this._view as View;
46+
const view = this._view;
47+
if (!view) {
48+
return;
49+
}
4250
const size = this.frame.size;
4351
View.layoutChild(null, view, 0, 0, Utils.layout.toDevicePixels(size.width), Utils.layout.toDevicePixels(size.height));
44-
},
45-
});
52+
}
53+
}
4654

4755
function createUIViewAutoSizeUIViewAutoSize(view: View) {
48-
const self = UIViewAutoSizeUIViewAutoSize.new() as UIView;
56+
const self = UIViewAutoSizeUIViewAutoSize.new() as UIViewAutoSizeUIViewAutoSize;
4957
view._setupAsRootView({});
5058
view._isAddedToNativeVisualTree = true;
5159
view.callLoaded();
52-
(self as any)._view = view;
60+
self._view = view;
5361
self.addSubview(view.nativeViewProtected);
5462
(view.nativeViewProtected as UIView).autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight;
5563
return self;
@@ -71,55 +79,36 @@ class MDCDialogPresentationControllerDelegateImpl extends NSObject {
7179
}
7280
}
7381
}
74-
declare class IMDCAlertControllerImpl extends MDCAlertController {
82+
@NativeClass
83+
class MDCAlertControllerImpl extends MDCAlertController {
7584
autoFocusTextField?: TextField;
76-
customContentView?: View;
77-
// _customContentViewContext?: any;
7885
_resolveFunction?: Function;
79-
actions: NSArray<any>;
8086
_disableContentInsets: boolean;
81-
}
82-
const MDCAlertControllerImpl = (MDCAlertController as any).extend({
83-
viewDidAppear() {
87+
viewDidAppear(animated: boolean) {
88+
super.viewDidAppear(animated);
8489
if (this.autoFocusTextField) {
8590
this.autoFocusTextField.requestFocus();
8691
this.view.setNeedsLayout();
8792
this.autoFocusTextField = null;
8893
}
89-
},
90-
traitCollectionDidChange(previousTraitCollection: UITraitCollection): void {
91-
try {
92-
this.super.traitCollectionDidChange(previousTraitCollection);
93-
} catch (err) {
94-
console.error('error', err);
95-
}
96-
},
97-
viewDidDisappear(animated: boolean) {
98-
this.super.viewDidDisappear(animated);
99-
if (this.customContentView) {
100-
this.customContentView.callUnloaded();
101-
this.customContentView._tearDownUI(true);
102-
this.customContentView._isAddedToNativeVisualTree = false;
103-
this.customContentView = null;
104-
}
105-
},
94+
}
10695
viewDidLoad() {
107-
this.super.viewDidLoad();
96+
super.viewDidLoad();
10897
if (this._disableContentInsets) {
109-
console.log('removing contentInsets');
11098
(this.view as MDCAlertControllerView).contentInsets = UIEdgeInsetsZero;
11199
}
112-
},
100+
}
113101
viewDidUnload() {
114-
this.super.viewDidUnload();
115-
if (this.customContentView) {
116-
this.customContentView.callUnloaded();
117-
this.customContentView._tearDownUI(true);
118-
this.customContentView._isAddedToNativeVisualTree = false;
119-
this.customContentView = null;
102+
super.viewDidUnload();
103+
if (this.accessoryView instanceof UIViewAutoSizeUIViewAutoSize) {
104+
const view = this.accessoryView._view;
105+
view.callUnloaded();
106+
view._tearDownUI(true);
107+
view._isAddedToNativeVisualTree = false;
108+
this.accessoryView._view = null;
120109
}
121-
},
122-
});
110+
}
111+
}
123112

124113
function addButtonsToAlertController(alertController: MDCAlertController, options: ConfirmOptions & MDCAlertControlerOptions, callback?: Function, validationArgs?: (r) => any): void {
125114
if (!options) {
@@ -165,7 +154,7 @@ function addButtonsToAlertController(alertController: MDCAlertController, option
165154
}
166155

167156
function createAlertController(options: DialogOptions & MDCAlertControlerOptions, resolve?: Function) {
168-
const alertController = MDCAlertControllerImpl.alloc().init() as IMDCAlertControllerImpl;
157+
const alertController = MDCAlertControllerImpl.alloc().init() as MDCAlertControllerImpl;
169158
alertController.mdc_adjustsFontForContentSizeCategory = true;
170159

171160
if (options.title) {
@@ -234,14 +223,13 @@ function createAlertController(options: DialogOptions & MDCAlertControlerOptions
234223
options.view instanceof View
235224
? options.view
236225
: Builder.createViewFromEntry({
237-
moduleName: options.view as string,
226+
moduleName: options.view as string
238227
});
239228

240229
view.cssClasses.add(CSSUtils.MODAL_ROOT_VIEW_CSS_CLASS);
241230
const modalRootViewCssClasses = CSSUtils.getSystemCssClasses();
242231
modalRootViewCssClasses.forEach((c) => view.cssClasses.add(c));
243232

244-
alertController.customContentView = view;
245233
alertController._resolveFunction = resolve;
246234
const context = options.context || {};
247235
context.closeCallback = function (...originalArgs) {
@@ -286,7 +274,7 @@ export function alert(arg: any): Promise<void> {
286274
try {
287275
const defaultOptions = {
288276
// title: ALERT,
289-
okButtonText: DialogStrings.OK,
277+
okButtonText: DialogStrings.OK
290278
};
291279
const options = !isDialogOptions(arg) ? Object.assign(defaultOptions, { message: arg + '' }) : Object.assign(defaultOptions, arg);
292280
const alertController = createAlertController(options, resolve);
@@ -328,11 +316,11 @@ export function confirm(arg: any): Promise<boolean> {
328316
const defaultOptions = {
329317
// title: CONFIRM,
330318
okButtonText: DialogStrings.OK,
331-
cancelButtonText: DialogStrings.CANCEL,
319+
cancelButtonText: DialogStrings.CANCEL
332320
};
333321
const options = !isDialogOptions(arg)
334322
? Object.assign(defaultOptions, {
335-
message: arg + '',
323+
message: arg + ''
336324
})
337325
: Object.assign(defaultOptions, arg);
338326
const alertController = createAlertController(options, resolve);
@@ -355,7 +343,7 @@ export function prompt(arg: any): Promise<PromptResult> {
355343
// title: PROMPT,
356344
okButtonText: DialogStrings.OK,
357345
cancelButtonText: DialogStrings.CANCEL,
358-
inputType: inputType.text,
346+
inputType: inputType.text
359347
};
360348

361349
if (arguments.length === 1) {
@@ -444,7 +432,7 @@ export function login(arg: any): Promise<LoginResult> {
444432

445433
const defaultOptions = {
446434
okButtonText: DialogStrings.OK,
447-
cancelButtonText: DialogStrings.CANCEL,
435+
cancelButtonText: DialogStrings.CANCEL
448436
};
449437

450438
if (arguments.length === 1) {
@@ -500,7 +488,7 @@ export function login(arg: any): Promise<LoginResult> {
500488
resolve({
501489
result: r,
502490
userName: userNameTextField.text,
503-
password: passwordTextField.text,
491+
password: passwordTextField.text
504492
});
505493
},
506494
(r) => ({ result: r, userName: userNameTextField.text, password: passwordTextField.text })
@@ -557,7 +545,7 @@ export function action(): Promise<string> {
557545

558546
const defaultOptions = {
559547
// title: null,
560-
cancelButtonText: DialogStrings.CANCEL,
548+
cancelButtonText: DialogStrings.CANCEL
561549
};
562550

563551
if (arguments.length === 1) {

0 commit comments

Comments
 (0)