Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(core-modules): implement createNativeView and initNativeView for all components #6102

Merged
merged 42 commits into from Sep 26, 2018
Merged
Show file tree
Hide file tree
Changes from 34 commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
ac28aed
define and use nativeTextViewProtected for all text configuration rel…
farfromrefug Jul 20, 2018
bb67da7
separate createNativeView from nativeview delegate creations
farfromrefug Jul 20, 2018
194231e
Merge branch 'master' of github.com:NativeScript/NativeScript into mo…
farfromrefug Jul 21, 2018
5839cb5
fixed lint error
farfromrefug Jul 21, 2018
5bad5f6
button refactoring
farfromrefug Jul 21, 2018
db752b1
better that way?
farfromrefug Jul 21, 2018
24b8f38
fixed ios view creation as createNativeView is not fully supported on…
farfromrefug Jul 24, 2018
0fb47fa
Merge branch 'master' of github.com:NativeScript/NativeScript into mo…
farfromrefug Jul 24, 2018
58fc92c
prevent undefined error
farfromrefug Jul 24, 2018
a2f914a
update based on comments
farfromrefug Jul 30, 2018
1dbc942
Merge branch 'master' of github.com:NativeScript/NativeScript into mo…
farfromrefug Jul 30, 2018
48f7b66
fixed missing delegate call on ios
farfromrefug Jul 31, 2018
2ae1133
refactored all UI widgets for better use of createNativeView/initNati…
farfromrefug Aug 11, 2018
e20a8c5
Merge branch 'master' into module_refactoring
farfromrefug Aug 16, 2018
5a5b1c6
cleanup after comments
farfromrefug Aug 21, 2018
a3d1411
Merge branch 'module_refactoring' of github.com:Akylas/NativeScript i…
farfromrefug Aug 21, 2018
39a57c1
fixed webview to use nativeviewprotected like other widgets
farfromrefug Aug 21, 2018
29dc093
cleanup after comment
farfromrefug Aug 21, 2018
d3b1f8d
Merge branch 'master' into module_refactoring
MartoYankov Aug 21, 2018
1295fa3
fix tslint errors from merge conflict
MartoYankov Aug 21, 2018
7c17708
test fixes
farfromrefug Aug 22, 2018
5b08639
fix android slider native view owner assignment
MartoYankov Aug 22, 2018
2038a10
ensure nativeViewProtected is created
farfromrefug Aug 22, 2018
e68a22d
Merge branch 'module_refactoring' of github.com:Akylas/NativeScript i…
farfromrefug Aug 22, 2018
4fabdac
fixed _map being created too late
farfromrefug Aug 22, 2018
9908b4b
call _addView first so that nativeViewProtected is created
farfromrefug Aug 23, 2018
4baadce
Merge branch 'master' into module_refactoring
farfromrefug Sep 11, 2018
e80f1cd
make sure nativeViewProtected is never “cleared”. That way it is setu…
farfromrefug Sep 12, 2018
5165432
fixed tests for iOS now that we use createNativeView
farfromrefug Sep 12, 2018
8f59cb8
now that we use createNativeView the order is important and setupUI m…
farfromrefug Sep 12, 2018
dfbc3d1
refactoring. Better that way?
farfromrefug Sep 12, 2018
aef4b30
isLayoutValid for iOS now that we use createNativeView
farfromrefug Sep 12, 2018
439e825
better that way
farfromrefug Sep 12, 2018
81dded9
updateScrollBarVisibility fix
farfromrefug Sep 12, 2018
4ff80b9
Merge branch 'master' into module_refactoring
MartoYankov Sep 20, 2018
9dfafff
better to call this in initNativeView
farfromrefug Sep 20, 2018
9048879
oops!
farfromrefug Sep 20, 2018
ce76f94
refactoring
farfromrefug Sep 21, 2018
e549485
not needed as we have ```createNativeView```
farfromrefug Sep 21, 2018
b8113d8
refactor: call setupUI on components created with constructor
MartoYankov Sep 25, 2018
af042ab
call setupUI on dialog components for iOS only
MartoYankov Sep 25, 2018
8d39e65
move setupAsRootView before the modal is shown
MartoYankov Sep 26, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 0 additions & 11 deletions tests/app/ui/view/view-tests-common.ts
Expand Up @@ -345,19 +345,8 @@ class TestView extends LayoutBase {
(<any>this.style).customShortHand = value;
}

private _nativeView;
constructor(public name: string) {
super();
this._nativeView = this.nativeViewProtected;
this.nativeViewProtected = undefined;
}

public createNativeView() {
if (isIOS) {
return this._nativeView;
}

return super.createNativeView();
}

public toString() {
Expand Down
4 changes: 1 addition & 3 deletions tns-core-modules/application/application.ios.ts
Expand Up @@ -226,10 +226,8 @@ class IOSApplication implements IOSApplicationDefinition {
// if we already have a root view, we reset it.
this._rootView._onRootViewReset();
}

const rootView = createRootView(view);
this._rootView = rootView;
const controller = getViewController(rootView);

if (createRootFrame.value) {
// Don't setup as styleScopeHost
Expand All @@ -238,7 +236,7 @@ class IOSApplication implements IOSApplicationDefinition {
// setup view as styleScopeHost
rootView._setupAsRootView({});
}

const controller = getViewController(rootView);
const haveController = this._window.rootViewController !== null;
this._window.rootViewController = controller;
if (!haveController) {
Expand Down
13 changes: 6 additions & 7 deletions tns-core-modules/ui/action-bar/action-bar.android.ts
Expand Up @@ -131,17 +131,16 @@ export class ActionBar extends ActionBarBase {
}

public createNativeView() {
initializeMenuItemClickListener();
const toolbar = new android.support.v7.widget.Toolbar(this._context);
const menuItemClickListener = new MenuItemClickListener(this);
toolbar.setOnMenuItemClickListener(menuItemClickListener);
(<any>toolbar).menuItemClickListener = menuItemClickListener;
return toolbar;
return new android.support.v7.widget.Toolbar(this._context);
}

public initNativeView(): void {
super.initNativeView();
(<any>this.nativeViewProtected).menuItemClickListener.owner = this;
const nativeView = this.nativeViewProtected;
initializeMenuItemClickListener();
const menuItemClickListener = new MenuItemClickListener(this);
nativeView.setOnMenuItemClickListener(menuItemClickListener);
(<any>nativeView).menuItemClickListener = menuItemClickListener;
}

public disposeNativeView() {
Expand Down
10 changes: 5 additions & 5 deletions tns-core-modules/ui/activity-indicator/activity-indicator.ios.ts
Expand Up @@ -4,11 +4,11 @@ export * from "./activity-indicator-common";

export class ActivityIndicator extends ActivityIndicatorBase {
nativeViewProtected: UIActivityIndicatorView;

constructor() {
super();
this.nativeViewProtected = UIActivityIndicatorView.alloc().initWithActivityIndicatorStyle(UIActivityIndicatorViewStyle.Gray);
this.nativeViewProtected.hidesWhenStopped = true;
createNativeView() {
const view = UIActivityIndicatorView.alloc().initWithActivityIndicatorStyle(UIActivityIndicatorViewStyle.Gray);
view.hidesWhenStopped = true;
return view;
}

get ios(): UIActivityIndicatorView {
Expand Down
30 changes: 19 additions & 11 deletions tns-core-modules/ui/button/button.android.ts
Expand Up @@ -37,34 +37,42 @@ function initializeClickListener(): void {
}

ClickListener = ClickListenerImpl;
APILEVEL = android.os.Build.VERSION.SDK_INT;
AndroidButton = android.widget.Button;
}

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

constructor() {
super();
if (!APILEVEL) {
APILEVEL = android.os.Build.VERSION.SDK_INT;
}
}

private _stateListAnimator: any;
private _highlightedHandler: (args: TouchGestureEventData) => void;

@profile
public createNativeView() {
initializeClickListener();
const button = new AndroidButton(this._context);
const clickListener = new ClickListener(this);
button.setOnClickListener(clickListener);
(<any>button).clickListener = clickListener;
return button;
if (!AndroidButton) {
AndroidButton = android.widget.Button;
}
return new AndroidButton(this._context);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AndroidButton type is assigned inside initializeClickListener(), so after we moved it to initNativeView(), this is not declared. I think it will be safe to use the android.widget.Button constructor directly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will actually define AndroidButton. I think the point is not to always have to go through android.widget.Button?
Might be faster?

}

public initNativeView(): void {
const nativeView = this.nativeViewProtected;
(<any>nativeView).clickListener.owner = this;
super.initNativeView();
const nativeView = this.nativeViewProtected;
initializeClickListener();
const clickListener = new ClickListener(this);
nativeView.setOnClickListener(clickListener);
(<any>nativeView).clickListener = clickListener;
}

public disposeNativeView() {
(<any>this.nativeViewProtected).clickListener.owner = null;
if (this.nativeViewProtected) {
(<any>this.nativeViewProtected).clickListener.owner = null;
}
super.disposeNativeView();
}

Expand Down
16 changes: 12 additions & 4 deletions tns-core-modules/ui/button/button.ios.ts
Expand Up @@ -14,12 +14,20 @@ export class Button extends ButtonBase {
private _tapHandler: NSObject;
private _stateChangedHandler: ControlStateChangeListener;

constructor() {
super();
this.nativeViewProtected = UIButton.buttonWithType(UIButtonType.System);
createNativeView() {
return UIButton.buttonWithType(UIButtonType.System);
}

public initNativeView(): void {
super.initNativeView();
const nativeView = this.nativeViewProtected;
this._tapHandler = TapHandlerImpl.initWithOwner(new WeakRef(this));
this.nativeViewProtected.addTargetActionForControlEvents(this._tapHandler, "tap", UIControlEvents.TouchUpInside);
nativeView.addTargetActionForControlEvents(this._tapHandler, "tap", UIControlEvents.TouchUpInside);
}

public disposeNativeView(): void {
this._tapHandler = null;
super.disposeNativeView();
}

get ios() {
Expand Down
31 changes: 16 additions & 15 deletions tns-core-modules/ui/core/view-base/view-base.ts
Expand Up @@ -687,17 +687,22 @@ export abstract class ViewBase extends Observable implements ViewBaseDefinition
}

this._context = context;
let nativeView;
if (isAndroid) {
// const recycle = this.recycleNativeView;
// if (recycle === "always" || (recycle === "auto" && !this._disableNativeViewRecycling)) {
// nativeView = <android.view.View>getNativeView(context, this.typeName);
// }

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

// if (isAndroid) {
// const recycle = this.recycleNativeView;
// if (recycle === "always" || (recycle === "auto" && !this._disableNativeViewRecycling)) {
// nativeView = <android.view.View>getNativeView(context, this.typeName);
// }
// }
if (!nativeView) {
nativeView = this.createNativeView();
}

if (isAndroid) {
this._androidView = nativeView;
if (nativeView) {
if (this._isPaddingRelative === undefined) {
Expand Down Expand Up @@ -730,14 +735,10 @@ export abstract class ViewBase extends Observable implements ViewBaseDefinition
}
}
} else {
// TODO: Implement _createNativeView for iOS
nativeView = this.createNativeView();
this._iosView = nativeView || this.nativeViewProtected;
this._iosView = nativeView;
}

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

if (this.parent) {
const nativeIndex = this.parent._childIndexToNativeChildIndex(atIndex);
Expand Down
2 changes: 1 addition & 1 deletion tns-core-modules/ui/core/view/view-common.ts
Expand Up @@ -74,7 +74,7 @@ export abstract class ViewCommon extends ViewBase implements ViewDefinition {
private _measuredWidth: number;
private _measuredHeight: number;

private _isLayoutValid: boolean;
protected _isLayoutValid: boolean;
private _cssType: string;

private _localAnimations: Set<am.Animation>;
Expand Down
13 changes: 10 additions & 3 deletions tns-core-modules/ui/core/view/view.ios.ts
Expand Up @@ -169,6 +169,14 @@ export class View extends ViewCommon {
}
}

get isLayoutValid(): boolean {
if (this.nativeViewProtected) {
return this._isLayoutValid;
}

return false;
}

public layoutNativeView(left: number, top: number, right: number, bottom: number): void {
if (!this.nativeViewProtected) {
return;
Expand Down Expand Up @@ -536,9 +544,8 @@ export class CustomLayoutView extends View {

nativeViewProtected: UIView;

constructor() {
super();
this.nativeViewProtected = UIView.alloc().initWithFrame(iosUtils.getter(UIScreen, UIScreen.mainScreen).bounds);
createNativeView() {
return UIView.alloc().initWithFrame(iosUtils.getter(UIScreen, UIScreen.mainScreen).bounds);
}

get ios(): UIView {
Expand Down
11 changes: 5 additions & 6 deletions tns-core-modules/ui/date-picker/date-picker.android.ts
Expand Up @@ -54,19 +54,18 @@ export class DatePicker extends DatePickerBase {
nativeViewProtected: android.widget.DatePicker;

public createNativeView() {
initializeDateChangedListener();
const picker = new android.widget.DatePicker(this._context);
picker.setCalendarViewShown(false);
const listener = new DateChangedListener(this);

picker.init(this.year, this.month - 1, this.day, listener);
(<any>picker).listener = listener;
return picker;
}

public initNativeView(): void {
super.initNativeView();
(<any>this.nativeViewProtected).listener.owner = this;
initializeDateChangedListener();
const nativeView = this.nativeViewProtected;
const listener = new DateChangedListener(this);
nativeView.init(this.year, this.month - 1, this.day, listener);
(<any>nativeView).listener = listener;
}

public disposeNativeView() {
Expand Down
20 changes: 14 additions & 6 deletions tns-core-modules/ui/date-picker/date-picker.ios.ts
Expand Up @@ -11,14 +11,22 @@ export class DatePicker extends DatePickerBase {
private _changeHandler: NSObject;
public nativeViewProtected: UIDatePicker;

constructor() {
super();

this.nativeViewProtected = UIDatePicker.new();
this.nativeViewProtected.datePickerMode = UIDatePickerMode.Date;
public createNativeView() {
const picker = UIDatePicker.new();
picker.datePickerMode = UIDatePickerMode.Date;
return picker;
}

public initNativeView(): void {
super.initNativeView();
const nativeView = this.nativeViewProtected;
this._changeHandler = UIDatePickerChangeHandlerImpl.initWithOwner(new WeakRef(this));
this.nativeViewProtected.addTargetActionForControlEvents(this._changeHandler, "valueChanged", UIControlEvents.ValueChanged);
nativeView.addTargetActionForControlEvents(this._changeHandler, "valueChanged", UIControlEvents.ValueChanged);
}

public disposeNativeView() {
this._changeHandler = null;
super.disposeNativeView();
}

get ios(): UIDatePicker {
Expand Down