Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Table view header views now work. Started adding text fields.

  • Loading branch information...
commit 22cfe2f6c0b0ee97aa0527646290205f6a8bfb97 1 parent 613d85a
@mschulkind authored
View
2  coffee/example/main.coffee
@@ -51,3 +51,5 @@ onTNReady = ->
TN.glueViews(view, tableView)
))
navController.open()
+
+ navController.push(App.createTwitterDemoWindow())
View
50 coffee/example/search_box.coffee
@@ -3,22 +3,10 @@ App.createSearchBoxHeaderView = (options) ->
onChange = options?.onChange
onDone = options?.onDone
- textField = new TN.UI.TextField(
- hint: hint
- borderWidth: 1
- borderColor: 'black'
- borderRadius: 15
- backgroundColor: 'white'
- leftView: new TN.UI.View(width: 10)
- )
-
- textField.addEventListener('change', (e) -> onChange?(e))
- textField.addEventListener('done', (e) -> onDone?(e))
-
headerCell = new TN.GridCell(
padding: 10
inheritViewSizeMode: 'width'
- fixedHeight: 50
+ layoutMode: 'vertical'
view: new TN.UI.View(
backgroundColor: 'gray'
)
@@ -26,10 +14,38 @@ App.createSearchBoxHeaderView = (options) ->
# Wait until the parent tableview resizes us before adding the textField.
headerCell.view.addEventOnceListener('resize', ->
- headerCell.add(new TN.GridCell(
- growMode: 'both'
- view: textField
- ))
+ #textField = null
+
+ headerCell.batchUpdates(->
+ textFieldFrame = new TN.GridCell(
+ growMode: 'horizontal'
+ view: new TN.UI.View(
+ borderWidth: 1
+ borderColor: 'black'
+ borderRadius: 15
+ backgroundColor: 'white'
+ )
+ )
+ headerCell.add(textFieldFrame)
+
+ #textField = new TN.UI.TextField(
+ #hint: hint
+ #backgroundColor: "red"
+ #)
+
+ #textField.addEventListener('change', (e) -> onChange?(e))
+ #textField.addEventListener('done', (e) -> onDone?(e))
+
+ #textFieldFrame.add(new TN.GridCell(
+ #growMode: 'horizontal'
+ #inheritViewSizeMode: 'height'
+ #view: textField
+ #))
+ )
+
+ #textField.sizeToFit(->
+ #headerCell.layout()
+ #)
)
headerCell.view
View
16 coffee/example/twitter_demo.coffee
@@ -8,8 +8,8 @@ App.createTwitterDemoWindow = ->
rowHeight: 86
# Provide a fetcher so the table is pull-to-refreshable.
- refreshCallback: (onDone) ->
- updateResults(onDone)
+ #refreshCallback: (onDone) ->
+ #updateResults(onDone)
)
updateResults = (onDone) ->
@@ -74,12 +74,12 @@ App.createTwitterDemoWindow = ->
padding: 5
)
gridCell.add(imageAndTextCell)
- row.userData.imageView = imageView = new TN.UI.ImageView(
- width: 48
- height: 48
- backgroundColor: '#bbb'
- )
- imageAndTextCell.add(imageView)
+ #row.userData.imageView = imageView = new TN.UI.ImageView(
+ #width: 48
+ #height: 48
+ #backgroundColor: '#bbb'
+ #)
+ #imageAndTextCell.add(imageView)
imageAndTextCell.add(new TN.UI.View(width: 5))
View
1  res/xml/plugins.xml
@@ -24,6 +24,7 @@
<plugin name="cordovatruenative.navigationcontroller" value="org.apache.cordova.plugins.truenative.NavigationControllerPlugin"/>
<plugin name="cordovatruenative.tableview" value="org.apache.cordova.plugins.truenative.TableViewPlugin"/>
<plugin name="cordovatruenative.tableviewrow" value="org.apache.cordova.plugins.truenative.TableViewRowPlugin"/>
+ <plugin name="cordovatruenative.textfield" value="org.apache.cordova.plugins.truenative.TextFieldPlugin"/>
<plugin name="cordovatruenative.view" value="org.apache.cordova.plugins.truenative.ViewPlugin"/>
<plugin name="cordovatruenative.window" value="org.apache.cordova.plugins.truenative.WindowPlugin"/>
</plugins>
View
92 src/org/apache/cordova/plugins/truenative/TableViewPlugin.java
@@ -22,38 +22,85 @@
protected class TableViewData extends ViewData {
}
- private class TableViewAdapter extends ArrayAdapter<JSONObject> {
+ private class TableViewAdapter extends ArrayAdapter<Object> {
private TableViewPlugin mPlugin;
private ListView mTableView;
+ private View mHeaderView;
public TableViewAdapter(ListView tableView, TableViewPlugin plugin) {
- super(tableView.getContext(), 0, new ArrayList<JSONObject>());
+ super(tableView.getContext(), 0, new ArrayList<Object>());
mPlugin = plugin;
mTableView = tableView;
}
+ // The header view counts as a row, so compensate accordingly.
+ private int getAdjustedPosition(int position) {
+ return (mHeaderView == null) ? position : position - 1;
+ }
+
+ public void setHeaderView(View view) {
+ if (mHeaderView != null) {
+ remove(mHeaderView);
+ }
+
+
+ if (view != null) {
+ insert(view, 0);
+ }
+
+ mHeaderView = view;
+
+ notifyDataSetChanged();
+ }
+
+ @Override
+ public void clear() {
+ super.clear();
+
+ if (mHeaderView != null) {
+ // Add one back for the header view.
+ add(mHeaderView);
+ }
+ }
+
+ @Override
+ public boolean isEnabled(int position) {
+ // All are enabled except the header view.
+ return getAdjustedPosition(position) != -1;
+ }
+
+ @Override
public View getView(int position, View convertView, ViewGroup parent) {
- View view = convertView;
- if (view == null) {
- JSONObject viewOptions;
- try {
- viewOptions = new JSONObject(
- mPlugin.writeJavascriptForComponent(mTableView, "createRow()"));
- view = (View)mPlugin.createComponent(getContext(), viewOptions);
-
- mPlugin.writeJavascriptForComponent(
- mTableView,
- "constructRow(-1, "+position+", "
- +mPlugin.getComponentID(view)+")");
- } catch(JSONException e) {
- e.printStackTrace();
- fail();
+ int adjustedPosition = getAdjustedPosition(position);
+
+ View view;
+ if (adjustedPosition == -1) {
+ assertNotNull(mHeaderView);
+ return mHeaderView;
+ } else {
+ view = convertView;
+ if (view == null) {
+ JSONObject viewOptions;
+ try {
+ viewOptions = new JSONObject(
+ mPlugin.writeJavascriptForComponent(mTableView, "createRow()"));
+ view = (View)mPlugin.createComponent(getContext(), viewOptions);
+
+ mPlugin.writeJavascriptForComponent(
+ mTableView,
+ "constructRow(-1, "+adjustedPosition+", "
+ +mPlugin.getComponentID(view)+")");
+ } catch(JSONException e) {
+ e.printStackTrace();
+ fail();
+ }
}
- }
- mPlugin.writeJavascriptForComponent(
- mTableView,
- "reuseRow(-1, "+position+", "+mPlugin.getComponentID(view)+")");
+ mPlugin.writeJavascriptForComponent(
+ mTableView,
+ "reuseRow(-1, "+adjustedPosition+", "
+ +mPlugin.getComponentID(view)+")");
+ }
return view;
}
@@ -116,6 +163,9 @@ public void setComponentProperty(Object component, String key, Object value) {
assertNotNull(entry);
adapter.add(entry);
}
+ } else if (key.equals("headerView")) {
+ ((TableViewAdapter)tableView.getAdapter()).setHeaderView(
+ (View)createComponent(tableView.getContext(), (JSONObject)value));
} else {
super.setComponentProperty(component, key, value);
}
View
65 src/org/apache/cordova/plugins/truenative/TextFieldPlugin.java
@@ -0,0 +1,65 @@
+package org.apache.cordova.plugins.truenative;
+
+import android.text.InputType;
+import android.view.View;
+import android.widget.EditText;
+
+import android.content.Context;
+import org.apache.cordova.api.Plugin;
+import org.apache.cordova.api.PluginResult;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import static junit.framework.Assert.*;
+
+public class TextFieldPlugin extends ViewPlugin {
+ protected class TextFieldData extends ViewData {
+ }
+
+ @Override
+ protected Object newComponentInstance(Context context) {
+ return new TextFieldSubclass(context, this);
+ }
+
+ @Override
+ protected ComponentData newComponentDataInstance() {
+ return new TextFieldData();
+ }
+
+ @Override
+ protected void setupComponent(Object component, JSONObject options) {
+ super.setupComponent(component, options);
+
+ setComponentProperties(
+ component, options,
+ "hint");
+
+ EditText textField = (EditText)component;
+ textField.setSingleLine(true);
+ }
+
+ @Override
+ public Object getComponentProperty(Object component, String key) {
+ EditText textField = (EditText)component;
+ TextFieldData data = (TextFieldData)getComponentData(component);
+
+ if (key.equals("")) {
+ return null;
+ } else {
+ return super.getComponentProperty(component, key);
+ }
+ }
+
+ @Override
+ public void setComponentProperty(Object component, String key, Object value) {
+ EditText textField = (EditText)component;
+ TextFieldData data = (TextFieldData)getComponentData(component);
+
+ if (key.equals("hint")) {
+ textField.setHint((String)value);
+ } else {
+ super.setComponentProperty(component, key, value);
+ }
+ }
+}
View
18 src/org/apache/cordova/plugins/truenative/TextFieldSubclass.java
@@ -0,0 +1,18 @@
+package org.apache.cordova.plugins.truenative;
+
+import android.content.Context;
+import android.widget.EditText;
+
+public class TextFieldSubclass extends EditText {
+ private TextFieldPlugin mPlugin;
+
+ public TextFieldSubclass(Context context, TextFieldPlugin plugin) {
+ super(context);
+ mPlugin = plugin;
+ }
+
+ @Override
+ protected void onSizeChanged(int w, int h, int oldw, int oldh) {
+ mPlugin.onViewSizeChanged(this, w, h, oldw, oldh);
+ }
+}
View
27 src/org/apache/cordova/plugins/truenative/ViewPlugin.java
@@ -58,6 +58,18 @@ public void onClick(View v) {
fireEvent(v, "click", null);
}
});
+
+ try {
+ JSONArray children = options.optJSONArray("children");
+ if (children != null) {
+ for (int i = 0; i < children.length(); ++i) {
+ addChildView((ViewSubclass)view, children.getJSONObject(i));
+ }
+ }
+ } catch(JSONException e) {
+ e.printStackTrace();
+ fail();
+ }
}
@Override
@@ -212,18 +224,21 @@ public void setComponentProperty(Object component, String key, Object value) {
}
}
+ private void addChildView(ViewSubclass parent, JSONObject childOptions) {
+ View child = (View)createComponent(parent.getContext(), childOptions);
+ ViewData childData = (ViewData)getComponentData(child);
+ childData.parent = parent;
+ parent.addView(child, childData.layoutParams);
+ }
+
public void add(final JSONObject options) {
ctx.runOnUiThread(new Runnable() {
public void run() {
try {
String parentID = options.getString("parentID");
- JSONObject childOptions = options.getJSONObject("child");
-
ViewSubclass parent = (ViewSubclass)getComponent(parentID);
- View child = (View)createComponent(parent.getContext(), childOptions);
- ViewData childData = (ViewData)getComponentData(child);
- childData.parent = parent;
- parent.addView(child, childData.layoutParams);
+ JSONObject childOptions = options.getJSONObject("child");
+ addChildView(parent, childOptions);
} catch(JSONException e) {
e.printStackTrace();
fail();
Please sign in to comment.
Something went wrong with that request. Please try again.