Skip to content

Commit

Permalink
[TIMOB-18062] iOS and Android: AttributedString Parity
Browse files Browse the repository at this point in the history
-Added Attribute Range to Property Accessors instead of creating set and get methods
-Added attributeProxyFor method to make code easier to understand
-Using property accessors in Label Proxy for Attributed String
  • Loading branch information
Ashraf A. S committed Dec 10, 2014
1 parent 0e342e0 commit 839ec27
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 112 deletions.
Original file line number Diff line number Diff line change
@@ -1,60 +1,27 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2013 by Appcelerator, Inc. All Rights Reserved.
* Copyright (c) 2009-2014 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
package ti.modules.titanium.ui;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.util.TiConvert;

@Kroll.proxy(propertyAccessors = {
TiC.PROPERTY_ATTRIBUTE_TYPE,
TiC.PROPERTY_ATTRIBUTE_VALUE
TiC.PROPERTY_ATTRIBUTE_VALUE,
TiC.PROPERTY_ATTRIBUTE_RANGE,
})
public class AttributeProxy extends KrollProxy
{
private static final String TAG = "Attribute";

protected int[] range = null;

protected AttributeProxy()
{
}

@Kroll.method @Kroll.setProperty
public void setRange (int[] range)
{
this.range = range;
}

@Kroll.method @Kroll.getProperty
public int[] getRange()
{
return range;
}

@Override
public void handleCreationDict(KrollDict options) {
super.handleCreationDict(options);

// Support setting range at creation.
Object inRange = options.get(TiC.PROPERTY_ATTRIBUTE_RANGE);
if (inRange != null && inRange instanceof Object[]) {
try {
int[] rangeArr = TiConvert.toIntArray((Object[]) inRange);
setRange(rangeArr);

} catch (ClassCastException e) {
Log.e(TAG, "Invalid range array. Must only contain numbers.");
}
}
}

@Override
public String getApiName()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2013 by Appcelerator, Inc. All Rights Reserved.
* Copyright (c) 2009-2014 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
Expand Down Expand Up @@ -31,7 +31,9 @@ public AttributedStringProxy()
@Kroll.method
public void addAttribute(AttributeProxy attribute)
{
attributes.add(attribute);
if(attribute != null) {
attributes.add(attribute);
}
}

/**
Expand All @@ -58,35 +60,37 @@ public void handleCreationDict(KrollDict options) {
super.handleCreationDict(options);

// Support setting attributes at creation.
Object[] values = (Object[])options.get(TiC.PROPERTY_ATTRIBUTES);
if (values != null && values instanceof Object[]) {
Object obj = options.get(TiC.PROPERTY_ATTRIBUTES);
if(obj != null && obj instanceof Object[]){
Object[] values = (Object[])obj;
for(int i = 0; i < values.length; i++) {
if (values[i] == null) {
Log.e(TAG, "Unable to create attribute proxy for null object passed in.");
return;
}
AttributeProxy attributeProxy = null;
if (values[i] instanceof AttributeProxy) {
attributeProxy = (AttributeProxy) values[i];
}
else {
KrollDict attributeDict = null;
if (values[i] instanceof KrollDict) {
attributeDict = (KrollDict) values[i];
} else if (values[i] instanceof HashMap) {
attributeDict = new KrollDict((HashMap) values[i]);
}
if (attributeDict != null) {
attributeProxy = new AttributeProxy();
attributeProxy.setCreationUrl(getCreationUrl().getNormalizedUrl());
attributeProxy.handleCreationDict(attributeDict);
}
}
if (attributeProxy != null) {
addAttribute(attributeProxy);
}
addAttribute(attributeProxyFor(values[i]));
}
}
}

private AttributeProxy attributeProxyFor(Object obj){
if (obj instanceof AttributeProxy) {
return (AttributeProxy)obj;
} else {
AttributeProxy attributeProxy = null;
KrollDict attributeDict = null;
if (obj instanceof KrollDict) {
attributeDict = (KrollDict)obj;
} else if (obj instanceof HashMap) {
attributeDict = new KrollDict((HashMap)obj);
}

if (attributeDict != null) {
attributeProxy = new AttributeProxy();
attributeProxy.setCreationUrl(getCreationUrl().getNormalizedUrl());
attributeProxy.handleCreationDict(attributeDict);
}
return attributeProxy;
}
}

Expand Down
42 changes: 2 additions & 40 deletions android/modules/ui/src/java/ti/modules/titanium/ui/LabelProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

@Kroll.proxy(creatableInModule=UIModule.class, propertyAccessors = {
TiC.PROPERTY_AUTO_LINK,
TiC.PROPERTY_ATTRIBUTED_STRING,
TiC.PROPERTY_COLOR,
TiC.PROPERTY_ELLIPSIZE,
TiC.PROPERTY_FONT,
Expand All @@ -43,9 +44,7 @@ public class LabelProxy extends TiViewProxy
private static final int MSG_FIRST_ID = KrollProxy.MSG_LAST_ID + 1;
private static final int MSG_SET_ATTRIBUTED_STRING = MSG_FIRST_ID + 100;
protected static final int MSG_LAST_ID = MSG_FIRST_ID + 999;

private AttributedStringProxy attributedString = null;


public LabelProxy()
{
defaultValues.put(TiC.PROPERTY_TEXT, "");
Expand All @@ -70,43 +69,6 @@ public TiUIView createView(Activity activity)
return new TiUILabel(this);
}

@Kroll.method @Kroll.getProperty
public AttributedStringProxy getAttributedString()
{
return attributedString;
}

@Kroll.method @Kroll.setProperty
public void setAttributedString(AttributedStringProxy attrString)
{
attributedString = attrString;
if (TiApplication.isUIThread()) {
handleSetAttributedString(attributedString);
} else {
TiMessenger.sendBlockingMainMessage(getMainHandler().obtainMessage(MSG_SET_ATTRIBUTED_STRING), attributedString);
}
}

private void handleSetAttributedString(AttributedStringProxy attrString)
{
((TiUILabel) getOrCreateView()).setAttributedString(attrString);
}

// This handler callback is tied to the UI thread.
@SuppressWarnings({ "unchecked", "rawtypes" })
public boolean handleMessage(Message msg)
{
switch (msg.what) {
case MSG_SET_ATTRIBUTED_STRING: {
AsyncResult result = (AsyncResult) msg.obj;
handleSetAttributedString((AttributedStringProxy) result.getArg());
result.setResult(null);
return true;
}
}
return super.handleMessage(msg);
}

@Override
public String getApiName()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,11 @@ public void setAttributedString(AttributedStringProxy attrString)
for (AttributeProxy attr : attributes) {
if (attr.hasProperty(TiC.PROPERTY_TYPE)) {
Object type = attr.getProperty(TiC.PROPERTY_ATTRIBUTE_TYPE);
int[] range = attr.getRange();
int[] range = null;
Object inRange = attr.getProperty(TiC.PROPERTY_ATTRIBUTE_RANGE);
if (inRange != null && inRange instanceof Object[]) {
range = TiConvert.toIntArray((Object[])inRange);
}
Object attrValue = attr.getProperty(TiC.PROPERTY_ATTRIBUTE_VALUE);
switch (TiConvert.toInt(type)) {
case UIModule.ATTRIBUTE_FONT:
Expand Down
2 changes: 1 addition & 1 deletion apidoc/Titanium/UI/Label.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ properties:
If set, the label ignores the `text`, `color`, and `shadow` properties.
type: Titanium.UI.AttributedString
platforms: [iphone, ipad, android]
since: "3.6.0"
since: {android: "3.6.0", iphone: "3.2.0", ipad: "3.2.0"}

- name: autoLink
summary: Automatically convert certain text items in the label to clickable links.
Expand Down
7 changes: 2 additions & 5 deletions apidoc/Titanium/UI/iOS/Attribute.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
---
name: Attribute
deprecated:
since: "3.6.0"
notes: |
For new development, use <Titanium.UI.Attribute>
name: Titanium.UI.iOS.Attribute
summary: An abstract datatype for specifying an attributed string attribute.
description: |
Attributes are added to the <Titanium.UI.iOS.AttributedString> object to create
Expand All @@ -13,6 +9,7 @@ description: |
For examples of using Attributed Strings, see the
[Attributed Strings guide](http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.UI.iOS.AttributedString).
platforms: [iphone, ipad]
extends: Titanium.Proxy
since: "3.2.0"

properties:
Expand Down
4 changes: 0 additions & 4 deletions apidoc/Titanium/UI/iOS/AttributedString.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
---
name: Titanium.UI.iOS.AttributedString
deprecated:
since: "3.6.0"
notes: |
For new development, use <Titanium.UI.AttributedString>
summary: |
An attributed string proxy manages character strings and associated sets of attributes (for example,
font and kerning) that apply to individual characters or ranges of characters in the string.
Expand Down

0 comments on commit 839ec27

Please sign in to comment.