Skip to content

Commit 46705ee

Browse files
Martin GuillonMartoYankov
authored andcommitted
refactor(core-modules): implement createNativeView and initNativeView for all components
refactor(core-modules): implement createNativeView and initNativeView for all components
1 parent 7110753 commit 46705ee

48 files changed

Lines changed: 630 additions & 519 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

tests/app/ui/view/view-tests-common.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -345,19 +345,8 @@ class TestView extends LayoutBase {
345345
(<any>this.style).customShortHand = value;
346346
}
347347

348-
private _nativeView;
349348
constructor(public name: string) {
350349
super();
351-
this._nativeView = this.nativeViewProtected;
352-
this.nativeViewProtected = undefined;
353-
}
354-
355-
public createNativeView() {
356-
if (isIOS) {
357-
return this._nativeView;
358-
}
359-
360-
return super.createNativeView();
361350
}
362351

363352
public toString() {

tns-core-modules/application/application.ios.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,10 +226,8 @@ class IOSApplication implements IOSApplicationDefinition {
226226
// if we already have a root view, we reset it.
227227
this._rootView._onRootViewReset();
228228
}
229-
230229
const rootView = createRootView(view);
231230
this._rootView = rootView;
232-
const controller = getViewController(rootView);
233231

234232
if (createRootFrame.value) {
235233
// Don't setup as styleScopeHost
@@ -238,7 +236,7 @@ class IOSApplication implements IOSApplicationDefinition {
238236
// setup view as styleScopeHost
239237
rootView._setupAsRootView({});
240238
}
241-
239+
const controller = getViewController(rootView);
242240
const haveController = this._window.rootViewController !== null;
243241
this._window.rootViewController = controller;
244242
if (!haveController) {

tns-core-modules/ui/action-bar/action-bar.android.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,16 @@ export class ActionBar extends ActionBarBase {
131131
}
132132

133133
public createNativeView() {
134-
initializeMenuItemClickListener();
135-
const toolbar = new android.support.v7.widget.Toolbar(this._context);
136-
const menuItemClickListener = new MenuItemClickListener(this);
137-
toolbar.setOnMenuItemClickListener(menuItemClickListener);
138-
(<any>toolbar).menuItemClickListener = menuItemClickListener;
139-
return toolbar;
134+
return new android.support.v7.widget.Toolbar(this._context);
140135
}
141136

142137
public initNativeView(): void {
143138
super.initNativeView();
144-
(<any>this.nativeViewProtected).menuItemClickListener.owner = this;
139+
const nativeView = this.nativeViewProtected;
140+
initializeMenuItemClickListener();
141+
const menuItemClickListener = new MenuItemClickListener(this);
142+
nativeView.setOnMenuItemClickListener(menuItemClickListener);
143+
(<any>nativeView).menuItemClickListener = menuItemClickListener;
145144
}
146145

147146
public disposeNativeView() {

tns-core-modules/ui/activity-indicator/activity-indicator.ios.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ export * from "./activity-indicator-common";
44

55
export class ActivityIndicator extends ActivityIndicatorBase {
66
nativeViewProtected: UIActivityIndicatorView;
7-
8-
constructor() {
9-
super();
10-
this.nativeViewProtected = UIActivityIndicatorView.alloc().initWithActivityIndicatorStyle(UIActivityIndicatorViewStyle.Gray);
11-
this.nativeViewProtected.hidesWhenStopped = true;
7+
8+
createNativeView() {
9+
const view = UIActivityIndicatorView.alloc().initWithActivityIndicatorStyle(UIActivityIndicatorViewStyle.Gray);
10+
view.hidesWhenStopped = true;
11+
return view;
1212
}
1313

1414
get ios(): UIActivityIndicatorView {

tns-core-modules/ui/button/button.android.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,42 @@ function initializeClickListener(): void {
3737
}
3838

3939
ClickListener = ClickListenerImpl;
40-
APILEVEL = android.os.Build.VERSION.SDK_INT;
41-
AndroidButton = android.widget.Button;
4240
}
4341

4442
export class Button extends ButtonBase {
4543
nativeViewProtected: android.widget.Button;
4644

45+
constructor() {
46+
super();
47+
if (!APILEVEL) {
48+
APILEVEL = android.os.Build.VERSION.SDK_INT;
49+
}
50+
}
51+
4752
private _stateListAnimator: any;
4853
private _highlightedHandler: (args: TouchGestureEventData) => void;
4954

5055
@profile
5156
public createNativeView() {
52-
initializeClickListener();
53-
const button = new AndroidButton(this._context);
54-
const clickListener = new ClickListener(this);
55-
button.setOnClickListener(clickListener);
56-
(<any>button).clickListener = clickListener;
57-
return button;
57+
if (!AndroidButton) {
58+
AndroidButton = android.widget.Button;
59+
}
60+
return new AndroidButton(this._context);
5861
}
5962

6063
public initNativeView(): void {
61-
const nativeView = this.nativeViewProtected;
62-
(<any>nativeView).clickListener.owner = this;
6364
super.initNativeView();
65+
const nativeView = this.nativeViewProtected;
66+
initializeClickListener();
67+
const clickListener = new ClickListener(this);
68+
nativeView.setOnClickListener(clickListener);
69+
(<any>nativeView).clickListener = clickListener;
6470
}
6571

6672
public disposeNativeView() {
67-
(<any>this.nativeViewProtected).clickListener.owner = null;
73+
if (this.nativeViewProtected) {
74+
(<any>this.nativeViewProtected).clickListener.owner = null;
75+
}
6876
super.disposeNativeView();
6977
}
7078

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,20 @@ export class Button extends ButtonBase {
1414
private _tapHandler: NSObject;
1515
private _stateChangedHandler: ControlStateChangeListener;
1616

17-
constructor() {
18-
super();
19-
this.nativeViewProtected = UIButton.buttonWithType(UIButtonType.System);
17+
createNativeView() {
18+
return UIButton.buttonWithType(UIButtonType.System);
19+
}
2020

21+
public initNativeView(): void {
22+
super.initNativeView();
23+
const nativeView = this.nativeViewProtected;
2124
this._tapHandler = TapHandlerImpl.initWithOwner(new WeakRef(this));
22-
this.nativeViewProtected.addTargetActionForControlEvents(this._tapHandler, "tap", UIControlEvents.TouchUpInside);
25+
nativeView.addTargetActionForControlEvents(this._tapHandler, "tap", UIControlEvents.TouchUpInside);
26+
}
27+
28+
public disposeNativeView(): void {
29+
this._tapHandler = null;
30+
super.disposeNativeView();
2331
}
2432

2533
get ios() {

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

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -687,17 +687,22 @@ export abstract class ViewBase extends Observable implements ViewBaseDefinition
687687
}
688688

689689
this._context = context;
690-
let nativeView;
691-
if (isAndroid) {
692-
// const recycle = this.recycleNativeView;
693-
// if (recycle === "always" || (recycle === "auto" && !this._disableNativeViewRecycling)) {
694-
// nativeView = <android.view.View>getNativeView(context, this.typeName);
695-
// }
696690

697-
if (!nativeView) {
698-
nativeView = this.createNativeView();
699-
}
691+
// This will account for nativeView that is created in createNativeView, recycled
692+
// or for backward compatability - set before _setupUI in iOS contructor.
693+
let nativeView = this.nativeViewProtected;
700694

695+
// if (isAndroid) {
696+
// const recycle = this.recycleNativeView;
697+
// if (recycle === "always" || (recycle === "auto" && !this._disableNativeViewRecycling)) {
698+
// nativeView = <android.view.View>getNativeView(context, this.typeName);
699+
// }
700+
// }
701+
if (!nativeView) {
702+
nativeView = this.createNativeView();
703+
}
704+
705+
if (isAndroid) {
701706
this._androidView = nativeView;
702707
if (nativeView) {
703708
if (this._isPaddingRelative === undefined) {
@@ -730,14 +735,10 @@ export abstract class ViewBase extends Observable implements ViewBaseDefinition
730735
}
731736
}
732737
} else {
733-
// TODO: Implement _createNativeView for iOS
734-
nativeView = this.createNativeView();
735-
this._iosView = nativeView || this.nativeViewProtected;
738+
this._iosView = nativeView;
736739
}
737740

738-
// This will account for nativeView that is created in createNativeView, recycled
739-
// or for backward compatability - set before _setupUI in iOS contructor.
740-
this.setNativeView(nativeView || this.nativeViewProtected);
741+
this.setNativeView(nativeView);
741742

742743
if (this.parent) {
743744
const nativeIndex = this.parent._childIndexToNativeChildIndex(atIndex);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
7474
private _measuredWidth: number;
7575
private _measuredHeight: number;
7676

77-
private _isLayoutValid: boolean;
77+
protected _isLayoutValid: boolean;
7878
private _cssType: string;
7979

8080
private _localAnimations: Set<am.Animation>;

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

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,14 @@ export class View extends ViewCommon {
169169
}
170170
}
171171

172+
get isLayoutValid(): boolean {
173+
if (this.nativeViewProtected) {
174+
return this._isLayoutValid;
175+
}
176+
177+
return false;
178+
}
179+
172180
public layoutNativeView(left: number, top: number, right: number, bottom: number): void {
173181
if (!this.nativeViewProtected) {
174182
return;
@@ -323,6 +331,8 @@ export class View extends ViewCommon {
323331
return;
324332
}
325333

334+
this._setupAsRootView({});
335+
326336
super._showNativeModalView(parentWithController, context, closeCallback, fullscreen, stretched);
327337
let controller = this.viewController;
328338
if (!controller) {
@@ -336,8 +346,6 @@ export class View extends ViewCommon {
336346
this.viewController = controller;
337347
}
338348

339-
this._setupAsRootView({});
340-
341349
if (fullscreen) {
342350
controller.modalPresentationStyle = UIModalPresentationStyle.FullScreen;
343351
} else {
@@ -536,9 +544,8 @@ export class CustomLayoutView extends View {
536544

537545
nativeViewProtected: UIView;
538546

539-
constructor() {
540-
super();
541-
this.nativeViewProtected = UIView.alloc().initWithFrame(iosUtils.getter(UIScreen, UIScreen.mainScreen).bounds);
547+
createNativeView() {
548+
return UIView.alloc().initWithFrame(iosUtils.getter(UIScreen, UIScreen.mainScreen).bounds);
542549
}
543550

544551
get ios(): UIView {

tns-core-modules/ui/date-picker/date-picker.android.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,18 @@ export class DatePicker extends DatePickerBase {
5454
nativeViewProtected: android.widget.DatePicker;
5555

5656
public createNativeView() {
57-
initializeDateChangedListener();
5857
const picker = new android.widget.DatePicker(this._context);
5958
picker.setCalendarViewShown(false);
60-
const listener = new DateChangedListener(this);
61-
62-
picker.init(this.year, this.month - 1, this.day, listener);
63-
(<any>picker).listener = listener;
6459
return picker;
6560
}
6661

6762
public initNativeView(): void {
6863
super.initNativeView();
69-
(<any>this.nativeViewProtected).listener.owner = this;
64+
initializeDateChangedListener();
65+
const nativeView = this.nativeViewProtected;
66+
const listener = new DateChangedListener(this);
67+
nativeView.init(this.year, this.month - 1, this.day, listener);
68+
(<any>nativeView).listener = listener;
7069
}
7170

7271
public disposeNativeView() {

0 commit comments

Comments
 (0)