Skip to content

Commit

Permalink
Merge pull request #9017 from fmerzadyan/6_1_0-timob-24181
Browse files Browse the repository at this point in the history
[6_1_0][TIMOB-24181] Android: fix ListItem template property inconsistencies
  • Loading branch information
ssjsamir committed May 17, 2017
2 parents 051f3e6 + 4c71e2b commit 50a0b80
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ private void processData(Object[] items, int offset) {
for (int i = 0; i < items.length; i++) {
Object itemData = items[i];
if (itemData instanceof HashMap) {
KrollDict d = new KrollDict((HashMap)itemData);
KrollDict d = new KrollDict((HashMap) itemData);
TiListViewTemplate template = processTemplate(d, i + offset);
template.updateOrMergeWithDefaultProperties(d, true);
temps[i] = template;
Expand All @@ -464,7 +464,7 @@ private void processData(Object[] items, int offset) {
for (int i = 0; i < items.length; i++) {
Object itemData = items[i];
if (itemData instanceof HashMap) {
KrollDict d = new KrollDict((HashMap)itemData);
KrollDict d = new KrollDict((HashMap) itemData);
TiListViewTemplate template = temps[i];
if (template != null) {
template.updateOrMergeWithDefaultProperties(d, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ public void handleCreationDict(KrollDict options) {
addPreloadSections((Object[]) obj, -1, true);
}
}
if (options.containsKey(TiC.PROPERTY_DEFAULT_ITEM_TEMPLATE)) {
setProperty(TiC.PROPERTY_DEFAULT_ITEM_TEMPLATE, options.get(TiC.PROPERTY_DEFAULT_ITEM_TEMPLATE));
}
}

public void clearPreloadSections() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ public void processProperties(KrollDict d) {
if (d.containsKey(TiC.PROPERTY_TEMPLATES)) {
Object templates = d.get(TiC.PROPERTY_TEMPLATES);
if (templates != null) {
processTemplates(new KrollDict((HashMap)templates));
processTemplates(new KrollDict((HashMap) templates));
}
}

Expand Down Expand Up @@ -815,7 +815,7 @@ private void refreshItems() {
protected void processTemplates(KrollDict templates) {
for (String key : templates.keySet()) {
//Here we bind each template with a key so we can use it to look up later
KrollDict properties = new KrollDict((HashMap)templates.get(key));
KrollDict properties = new KrollDict((HashMap) templates.get(key));
TiListViewTemplate template = new TiListViewTemplate(key, properties);
//Set type to template, for recycling purposes.
template.setType(getItemType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.appcelerator.kroll.KrollDict;
Expand Down Expand Up @@ -222,26 +223,35 @@ public DataItem getRootItem() {
return rootItem;
}

/**
*
* @param data dictionary holding properties for template
* @param update if true update entry else copy non-null values into data parameter
*/
public void updateOrMergeWithDefaultProperties(KrollDict data, boolean update) {
for (String binding: data.keySet()) {
DataItem dataItem = dataItems.get(binding);
if (dataItem == null) continue;

KrollDict defaultProps = dataItem.getDefaultProperties();
KrollDict props = new KrollDict((HashMap)data.get(binding));
KrollDict newProps = new KrollDict((HashMap) data.get(binding));
if (defaultProps != null) {
if (update) {
//update default properties
Set<String> existingKeys = defaultProps.keySet();
for (String key: props.keySet()) {
if (!existingKeys.contains(key)) {
Set<String> defaultPropsKeys = defaultProps.keySet();
for (String key: newProps.keySet()) {
if (!defaultPropsKeys.contains(key)) {
defaultProps.put(key, null);
}
}
} else {
//merge default properties with new properties and update data
HashMap<String, Object> newData = ((HashMap<String, Object>)defaultProps.clone());
newData.putAll(props);
HashMap<String, Object> newData = ((HashMap<String, Object>) defaultProps.clone());
for (Map.Entry<String, Object> entry : newProps.entrySet()) {
if (entry.getValue() != null) {
newData.put(entry.getKey(), entry.getValue());
}
}
data.put(binding, newData);
}
}
Expand Down

0 comments on commit 50a0b80

Please sign in to comment.