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

timob-12176: fix list view issues #3972

Merged
merged 8 commits into from
Mar 20, 2013
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

package ti.modules.titanium.ui.widget.listview;

import java.lang.ref.WeakReference;

import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.proxy.TiViewProxy;
Expand All @@ -17,10 +19,27 @@

@Kroll.proxy(creatableInModule = UIModule.class)
public class ListItemProxy extends TiViewProxy {

protected WeakReference<TiViewProxy> listProxy;

public TiUIView createView(Activity activity) {
return new TiListItem(this);
}

public void setListProxy(TiViewProxy list) {
listProxy = new WeakReference<TiViewProxy>(list);
}

public TiViewProxy getListProxy() {
if (listProxy != null) {
return listProxy.get();
}
return null;
}

public void release() {
super.release();
if (listProxy != null) {
listProxy = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public class ListSectionProxy extends ViewProxy{
private static final int MSG_DELETE_ITEMS_AT = MSG_FIRST_ID + 703;
private static final int MSG_GET_ITEM_AT = MSG_FIRST_ID + 704;
private static final int MSG_REPLACE_ITEMS_AT = MSG_FIRST_ID + 705;
private static final int MSG_UPDATE_ITEM_AT = MSG_FIRST_ID + 706;
private static final int MSG_GET_ITEMS = MSG_FIRST_ID + 707;






Expand Down Expand Up @@ -91,6 +96,9 @@ public void handleCreationDict(KrollDict dict) {
if (dict.containsKey(TiC.PROPERTY_FOOTER_TITLE)) {
footerTitle = TiConvert.toString(dict, TiC.PROPERTY_FOOTER_TITLE);
}
if (dict.containsKey(TiC.PROPERTY_ITEMS)) {
handleSetItems(dict.get(TiC.PROPERTY_ITEMS));
}
}

public void setAdapter(TiBaseAdapter a) {
Expand Down Expand Up @@ -143,6 +151,12 @@ public boolean handleMessage(Message msg)
result.setResult(null);
return true;
}

case MSG_GET_ITEMS: {
AsyncResult result = (AsyncResult) msg.obj;
result.setResult(itemProperties.toArray());
return true;
}

case MSG_APPEND_ITEMS: {
AsyncResult result = (AsyncResult) msg.obj;
Expand Down Expand Up @@ -186,14 +200,23 @@ public boolean handleMessage(Message msg)
result.setResult(item);
return true;
}

case MSG_UPDATE_ITEM_AT: {
AsyncResult result = (AsyncResult) msg.obj;
KrollDict data = (KrollDict) result.getArg();
int index = data.getInt("index");
Copy link
Contributor

Choose a reason for hiding this comment

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

Use TiC constant.

handleUpdateItemAt(index, data.get("data"));
Copy link
Contributor

Choose a reason for hiding this comment

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

TiC constant

result.setResult(null);
return true;
}

default : {
return super.handleMessage(msg);
}

}
}

@Kroll.method
public KrollDict getItemAt(int index) {
if (TiApplication.isUIThread()) {
Expand All @@ -210,14 +233,22 @@ private KrollDict handleGetItemAt(int index) {
return null;
}

@Kroll.method
@Kroll.method @Kroll.setProperty
public void setItems(Object data) {
if (TiApplication.isUIThread()) {
handleSetItems(data);
} else {
TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_SET_ITEMS), data);
}

}

@Kroll.method @Kroll.getProperty
public Object[] getItems() {
if (TiApplication.isUIThread()) {
return itemProperties.toArray();
} else {
return (Object[]) TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_GET_ITEMS));
}
}

@Kroll.method
Expand Down Expand Up @@ -282,12 +313,33 @@ public void replaceItemsAt(int index, int count, Object data) {
}
}

@Kroll.method
public void updateItemAt(int index, Object data) {
if (!isIndexValid(index) || !(data instanceof HashMap)) {
return;
}

if (TiApplication.isUIThread()) {
handleUpdateItemAt(index, new Object[]{data});
} else {
KrollDict d = new KrollDict();
d.put("index", index);
d.put("data", new Object[]{data});
TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_UPDATE_ITEM_AT), d);
}
}


public void processPreloadData() {
if (itemProperties != null && preload) {
handleSetItems(itemProperties.toArray());
preload = false;
}
}

public void refreshItems() {
handleSetItems(itemProperties.toArray());
}

private void processData(Object[] items, int offset) {
if (listItemData == null) {
Expand Down Expand Up @@ -399,17 +451,20 @@ private void handleInsertItemsAt(int index, Object data) {
}
}

private void deleteItems(int index, int count) {
private boolean deleteItems(int index, int count) {
boolean delete = false;
while (count > 0) {
if (index < itemProperties.size()) {
itemProperties.remove(index);
itemCount--;
delete = true;
}
if (index < listItemData.size()) {
listItemData.remove(index);
}
count--;
}
return delete;
}

private void handleDeleteItemsAt(int index, int count) {
Expand All @@ -420,8 +475,14 @@ private void handleDeleteItemsAt(int index, int count) {
}

private void handleReplaceItemsAt(int index, int count, Object data) {
deleteItems(index, count);
handleInsertItemsAt(index, data);
if (deleteItems(index, count)) {
handleInsertItemsAt(index, data);
}
}

private void handleUpdateItemAt(int index, Object data) {
handleReplaceItemsAt(index, 1, data);
setProperty(TiC.PROPERTY_ITEMS, itemProperties.toArray());
}

private TiListViewTemplate processTemplate(KrollDict itemData, int index) {
Expand Down Expand Up @@ -487,7 +548,7 @@ private TiListViewTemplate processDefaultTemplate(KrollDict data, int index) {
* @param index Entry's index relative to its section
* @return
*/
public void generateCellContent(int index, KrollDict data, TiListViewTemplate template, TiBaseListViewItem itemContent, int itemPosition, View item_layout) {
public void generateCellContent(int sectionIndex, KrollDict data, TiListViewTemplate template, TiBaseListViewItem itemContent, int itemPosition, View item_layout) {
//Here we create an item content and populate it with data
//Get item proxy
TiViewProxy itemProxy = template.getRootItem().getViewProxy();
Expand All @@ -498,7 +559,7 @@ public void generateCellContent(int index, KrollDict data, TiListViewTemplate te

if (data != null && template != null) {
generateChildContentViews(template.getRootItem(), null, itemContent, true);
populateViews(data, itemContent, template, itemPosition, index, item_layout);
populateViews(data, itemContent, template, itemPosition, sectionIndex, item_layout);
}
}

Expand All @@ -524,25 +585,24 @@ public void generateChildContentViews(DataItem item, TiUIView parentContent, TiB
}
}

public void appendExtraEventData(TiUIView view, int itemPosition, int sectionIndex, String bindId) {
public void appendExtraEventData(TiUIView view, int itemIndex, int sectionIndex, String bindId) {
KrollDict existingData = view.getAdditionalEventData();
if (existingData == null) {
existingData = new KrollDict();
view.setAdditionalEventData(existingData);
}

if (headerTitle != null) {
itemPosition -= 1;
sectionIndex -= 1;
itemIndex -= 1;
}

existingData.put(TiC.PROPERTY_SECTION, this);
existingData.put(TiC.PROPERTY_SECTION_INDEX, sectionIndex);
existingData.put(TiC.PROPERTY_BIND_ID, bindId);
existingData.put(TiC.PROPERTY_ITEM_INDEX, itemPosition);
existingData.put(TiC.PROPERTY_ITEM_INDEX, itemIndex);
}

public void populateViews(KrollDict data, TiBaseListViewItem cellContent, TiListViewTemplate template, int itemPosition, int sectionIndex, View item_layout) {
public void populateViews(KrollDict data, TiBaseListViewItem cellContent, TiListViewTemplate template, int itemIndex, int sectionIndex, View item_layout) {
Object cell = cellContent.getTag();

HashMap<String, ViewItem> views = (HashMap<String, ViewItem>) cellContent.getViewsMap();
Expand All @@ -552,7 +612,7 @@ public void populateViews(KrollDict data, TiBaseListViewItem cellContent, TiList
ViewItem viewItem = views.get(binding);
TiUIView view = viewItem.getView();
if (view != null) {
appendExtraEventData(view, itemPosition, sectionIndex, binding);
appendExtraEventData(view, itemIndex, sectionIndex, binding);
}
//if binding is contain in data given to us, process that data, otherwise
//apply default properties.
Expand Down Expand Up @@ -581,7 +641,7 @@ public void populateViews(KrollDict data, TiBaseListViewItem cellContent, TiList
}

TiListItem listItem = (TiListItem) cell;
appendExtraEventData(listItem, itemPosition, sectionIndex, TiC.PROPERTY_PROPERTIES);
appendExtraEventData(listItem, itemIndex, sectionIndex, TiC.PROPERTY_PROPERTIES);
if (data.containsKey(TiC.PROPERTY_PROPERTIES)) {
KrollDict properties = new KrollDict((HashMap)data.get(TiC.PROPERTY_PROPERTIES));
KrollDict diffProperties = cellContent.getViewItem().generateDiffProperties(properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public TiUIView createView(Activity activity) {

public void handleCreationArgs(KrollModule createdInModule, Object[] args) {
preloadSections = new ArrayList<ListSectionProxy>();
defaultValues.put(TiC.PROPERTY_DEFAULT_ITEM_TEMPLATE, UIModule.LIST_ITEM_TEMPLATE_DEFAULT);
super.handleCreationArgs(createdInModule, args);

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ public void generateDefaultProps(Activity activity) {
labelProxy.setActivity(activity);
//Generate properties
defaultLabelProperties.put(TiC.PROPERTY_LEFT, "2%");
defaultLabelProperties.put(TiC.PROPERTY_WIDTH, "80%");
defaultLabelProperties.put(TiC.PROPERTY_TEXT, "label");
defaultLabelProperties.put(TiC.PROPERTY_WORD_WRAP, true);
//bind the proxy and default propertiess
DataItem labelItem = new DataItem(labelProxy, TiC.PROPERTY_TITLE, rootItem);
dataItems.put(TiC.PROPERTY_TITLE, labelItem);
Expand All @@ -57,8 +57,7 @@ public void generateDefaultProps(Activity activity) {
imageProxy.setActivity(activity);
//Generate properties
defaultImageProperties.put(TiC.PROPERTY_RIGHT, "0");
defaultImageProperties.put(TiC.PROPERTY_HEIGHT, "60sp");
defaultImageProperties.put(TiC.PROPERTY_WIDTH, "60sp");
defaultImageProperties.put(TiC.PROPERTY_WIDTH, "15%");
//bind the proxy and default properties
DataItem imageItem = new DataItem (imageProxy, TiC.PROPERTY_IMAGE, rootItem);
dataItems.put(TiC.PROPERTY_IMAGE, imageItem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ protected void setOnClickListener(View view)
public void onClick(View view)
{
KrollDict data = dictFromEvent(lastUpEvent);
TiViewProxy listViewProxy = proxy.getParent();
TiViewProxy listViewProxy = ((ListItemProxy)proxy).getListProxy();
if (listViewProxy != null) {
TiUIView listView = listViewProxy.peekView();
if (listView != null) {
Expand Down