Skip to content

Commit 7f381c4

Browse files
committed
Added text-field.ios implementation
1 parent 6c60c83 commit 7f381c4

File tree

7 files changed

+143
-44
lines changed

7 files changed

+143
-44
lines changed

ui/core/view.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import application = require("application");
44
export class View extends proxy.ProxyObject {
55
private _parent: Panel;
66

7-
public onInitialized(content: android.content.Context) {
7+
public onInitialized(context: android.content.Context) {
88
// TODO: This is used by Android, rethink this routine
99
}
1010

1111
public addToParent(native: any) {
1212
// TODO: Temporary
1313
if (application.ios && this.ios) {
1414
native.addSubview(this.ios);
15+
this.ios.sizeToFit();
1516
} else if (application.android && this.android) {
1617
native.addView(this.android);
1718
}
@@ -62,6 +63,16 @@ export class Panel extends View {
6263
this._children = new Array<View>();
6364
}
6465

66+
public onInitialized(context: android.content.Context) {
67+
super.onInitialized(context);
68+
69+
// delegate the initialized event to the children
70+
var i;
71+
for (i = 0; i < this._children.length; i++) {
72+
this._children[i].onInitialized(context);
73+
}
74+
}
75+
6576
public addChild(child: View) {
6677
// Validate child is not parented
6778
if (child.parent) {
@@ -96,4 +107,8 @@ export class Panel extends View {
96107
this._children.splice(index, 1);
97108
child.onRemovedFromParent();
98109
}
110+
}
111+
112+
export class StackPanel extends Panel {
113+
99114
}

ui/frame/frame.android.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import application = require("application");
55

66
export class Frame extends frameCommon.Frame {
77
public navigateCore(context: any) {
8-
if (this.backStack.length === 0) {
8+
if (!this.currentPage) {
99
// When navigating for the very first time we do not want to start an activity
1010
// TODO: Revisit/polish this behavior
1111
return;

ui/pages/page.android.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ class AndroidPage implements definition.AndroidPage {
4141
}
4242

4343
public getActivityExtends(): any {
44-
if (!this._body) {
45-
this.rebuildBody();
44+
if (!this._activityExtends) {
45+
this.rebuildExtends();
4646
}
4747

4848
return this._activityExtends;
@@ -53,26 +53,29 @@ class AndroidPage implements definition.AndroidPage {
5353
this._activityExtends = null;
5454
}
5555

56-
private rebuildBody() {
57-
var that = this;
58-
this._body = {
59-
onCreate: function () {
60-
that._activity = this;
61-
this.super.onCreate(null);
62-
63-
var view = that._ownerPage.contentView;
64-
if (view) {
65-
// TODO: Notify the entire visual tree for being initialized
66-
view.onInitialized(that._activity);
67-
that._activity.setContentView(view.android);
56+
private rebuildExtends() {
57+
// we may have a body set externally
58+
if (!this._body) {
59+
var that = this;
60+
this._body = {
61+
onCreate: function () {
62+
that._activity = this;
63+
this.super.onCreate(null);
64+
65+
var view = that._ownerPage.contentView;
66+
if (view) {
67+
// TODO: Notify the entire visual tree for being initialized
68+
view.onInitialized(that._activity);
69+
that._activity.setContentView(view.android);
70+
}
6871
}
6972
}
70-
}
7173

72-
if (this._ownerPage.onLoaded) {
73-
this._body.onStart = function () {
74-
this.super.onStart();
75-
that._ownerPage.onLoaded();
74+
if (this._ownerPage.onLoaded) {
75+
this._body.onStart = function () {
76+
this.super.onStart();
77+
that._ownerPage.onLoaded();
78+
}
7679
}
7780
}
7881

ui/text-field/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
declare var module, require;
2-
module.exports = require("ui/text-input/text-input");
2+
module.exports = require("ui/text-field/text-field");

ui/text-field/text-field.android.ts

Lines changed: 43 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,70 @@
11
import observable = require("ui/core/observable");
22
import view = require("ui/core/view");
33
import application = require("application");
4+
import definition = require("ui/text-field");
45

5-
export class TextInput extends view.View {
6+
var TEXT = "text";
7+
// this is the name of the property to store text locally until attached to a valid Context
8+
var TEXTPRIVATE = "_text";
9+
10+
export class TextField extends view.View implements definition.TextField {
611
private _android: android.widget.EditText;
7-
private static textProperty = "text";
812

913
constructor() {
1014
super();
15+
}
1116

12-
var context = application.android.currentContext;
13-
this._android = new android.widget.EditText(context);
14-
15-
// TODO: This code is same for Label, extract base class or derive from Label
16-
var that = this;
17-
var textWatcher = new android.text.TextWatcher({
18-
beforeTextChanged: function (text: string, start: number, count: number, after: number) {
19-
},
20-
onTextChanged: function (text: string, start: number, before: number, count: number) {
21-
},
22-
afterTextChanged: function (editable: android.text.IEditable) {
23-
that.updateTwoWayBinding(TextInput.textProperty, editable.toString());
24-
}
25-
});
26-
this._android.addTextChangedListener(textWatcher);
17+
public onInitialized(context: android.content.Context) {
18+
if (!this._android) {
19+
// TODO: We need to decide whether we will support context switching and if yes - to implement it.
20+
this.createUI(context);
21+
}
2722
}
2823

2924
get android(): android.widget.EditText {
3025
return this._android;
3126
}
3227

3328
get text(): string {
29+
if (!this._android) {
30+
return this[TEXTPRIVATE];
31+
}
3432
return this._android.getText().toString();
3533
}
3634
set text(value: string) {
37-
this.setProperty(TextInput.textProperty, value);
35+
this.setProperty(TEXT, value);
3836
}
3937

4038
public setNativeProperty(data: observable.PropertyChangeData) {
4139
// TODO: Will this be a gigantic if-else switch?
42-
if (data.propertyName === TextInput.textProperty) {
43-
this._android.setText(data.value, android.widget.TextView.BufferType.EDITABLE);
40+
if (data.propertyName === TEXT) {
41+
if (this._android) {
42+
this._android.setText(data.value, android.widget.TextView.BufferType.EDITABLE);
43+
} else {
44+
this[TEXTPRIVATE] = data.value;
45+
}
4446
} else if (true) {
4547
}
4648
}
49+
50+
private createUI(context: android.content.Context) {
51+
this._android = new android.widget.EditText(context);
52+
if (this[TEXTPRIVATE]) {
53+
this._android.setText(this[TEXTPRIVATE]);
54+
delete this[TEXTPRIVATE];
55+
}
56+
57+
// TODO: This code is same for Label, extract base class or derive from Label
58+
var that = this;
59+
var textWatcher = new android.text.TextWatcher({
60+
beforeTextChanged: function (text: string, start: number, count: number, after: number) {
61+
},
62+
onTextChanged: function (text: string, start: number, before: number, count: number) {
63+
},
64+
afterTextChanged: function (editable: android.text.IEditable) {
65+
that.updateTwoWayBinding(TEXT, editable.toString());
66+
}
67+
});
68+
this._android.addTextChangedListener(textWatcher);
69+
}
4770
}

ui/text-field/text-field.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1-

1+
declare module "ui/text-field" {
2+
import view = require("ui/core/view");
3+
4+
export class TextField extends view.View {
5+
public text: string;
6+
}
7+
}

ui/text-field/text-field.ios.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,53 @@
1-

1+
import observable = require("ui/core/observable");
2+
import view = require("ui/core/view");
3+
import application = require("application");
4+
import definition = require("ui/text-field");
5+
6+
var TEXT = "text";
7+
8+
export class TextField extends view.View implements definition.TextField {
9+
private _ios: UIKit.UITextField;
10+
private _delegate: any;
11+
12+
constructor() {
13+
super();
14+
15+
this._ios = new UIKit.UITextField();
16+
17+
var that = this;
18+
var protocolImplementation = Foundation.NSObject.extends({}, {}).implements({
19+
protocol: "UITextFieldDelegate",
20+
implementation: {
21+
textFieldDidEndEditing: function (field: UIKit.UITextField) {
22+
that.updateTwoWayBinding(TEXT, field.text);
23+
console.log("TextField end edit");
24+
}
25+
}
26+
});
27+
28+
this._delegate = new protocolImplementation();
29+
this._ios.delegate = this._delegate;
30+
31+
var frame = CoreGraphics.CGRectMake(100, 100, 50, 30);
32+
}
33+
34+
get ios(): UIKit.UITextField {
35+
return this._ios;
36+
}
37+
38+
get text(): string {
39+
return this.ios.text;
40+
}
41+
set text(value: string) {
42+
this.setProperty(TEXT, value);
43+
}
44+
45+
public setNativeProperty(data: observable.PropertyChangeData) {
46+
// TODO: Will this be a gigantic if-else switch?
47+
if (data.propertyName === TEXT) {
48+
this._ios.text = data.value;
49+
this._ios.sizeToFit();
50+
} else if (true) {
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)