Skip to content

Commit

Permalink
fix float and dimmen attr covert
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel163 committed Feb 9, 2017
1 parent 90c0564 commit 8fb3877
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.ebr163.attributesdispatcher.Attribute;
import com.ebr163.attributesdispatcher.CustomView;
import com.ebr163.attributesdispatcher.attr.ColorAttr;
import com.ebr163.attributesdispatcher.attr.DimenAttr;
import com.ebr163.attributesdispatcher.attr.FloatAttr;
import com.ebr163.attributesdispatcher.attr.StringAttr;


Expand All @@ -22,6 +24,8 @@ public class MyCustomView extends EditText {
public int color;
@StringAttr(value = "custom_text", defaultValue = "adasdasd")
protected String text;
@DimenAttr(value = "custom_float")
protected float cfloat;

public MyCustomView(Context context) {
super(context);
Expand All @@ -37,6 +41,10 @@ public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
MyCustomViewAttribute.init(this, attrs);
}

@Attribute
protected void setCustomFloat(@FloatAttr(value = "custom_float") float attr) {
}

@Attribute
protected void setCustomAttr(@ColorAttr(value = "custom_color", defaultValue = Color.GREEN) int color) {
this.setTextColor(color);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
<declare-styleable name="MyCustomView">
<attr name="custom_text" format="string" />
<attr name="custom_color" format="color" />
<attr name="custom_float" format="dimension" />
</declare-styleable>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ private void addInitTargetField(MethodSpec.Builder builder, CustomViewElement cu
checkAnnotationFields(field, ColorAttr.class);
String value = field.getAnnotation(ColorAttr.class).value();
int defValue = field.getAnnotation(ColorAttr.class).defaultValue();
builder.addStatement("$N." + field.getSimpleName().toString() + " = $N.getColor($L.R.styleable.$L_$L, -1)", "target", "typedArray",
customViewElement.appPackage, customViewElement.inputClassName, value);
builder.addStatement("$N." + field.getSimpleName().toString() + " = $N.getColor($L.R.styleable.$L_$L, $L)", "target", "typedArray",
customViewElement.appPackage, customViewElement.inputClassName, value, defValue);

}

Expand All @@ -146,15 +146,15 @@ private void addInitTargetField(MethodSpec.Builder builder, CustomViewElement cu
String value = field.getAnnotation(DimenAttr.class).value();
float defValue = field.getAnnotation(DimenAttr.class).defaultValue();
builder.addStatement("$N." + field.getSimpleName().toString() + " = $N.getDimension($L.R.styleable.$L_$L, $L)", "target", "typedArray",
customViewElement.appPackage, customViewElement.inputClassName, value, defValue);
customViewElement.appPackage, customViewElement.inputClassName, value, String.valueOf(defValue) + "f");
}

for (Element field : customViewElement.floatElements) {
checkAnnotationFields(field, FloatAttr.class);
String value = field.getAnnotation(FloatAttr.class).value();
float defValue = field.getAnnotation(FloatAttr.class).defaultValue();
builder.addStatement("$N." + field.getSimpleName().toString() + " = $N.getFloat($L.R.styleable.$L_$L, $L)", "target", "typedArray",
customViewElement.appPackage, customViewElement.inputClassName, value, defValue);
customViewElement.appPackage, customViewElement.inputClassName, value, String.valueOf(defValue) + "f");
}

for (Element field : customViewElement.integerElements) {
Expand Down Expand Up @@ -232,11 +232,11 @@ private void addAttrGenBody(MethodSpec.Builder builder, ExecutableElement method
} else if (annotation.toString().equals(DimenAttr.class.getCanonicalName())) {
String key = parameter.getAnnotation(DimenAttr.class).value();
float defValue = parameter.getAnnotation(DimenAttr.class).defaultValue();
builder.addStatement("$T $N = $N.getDimension($L.R.styleable.$L_$L, $L)", type, name, "typedArray", customViewElement.appPackage, customViewElement.inputClassName, key, defValue);
builder.addStatement("$T $N = $N.getDimension($L.R.styleable.$L_$L, $L)", type, name, "typedArray", customViewElement.appPackage, customViewElement.inputClassName, key, String.valueOf(defValue) + "f");
} else if (annotation.toString().equals(FloatAttr.class.getCanonicalName())) {
String key = parameter.getAnnotation(FloatAttr.class).value();
float defValue = parameter.getAnnotation(FloatAttr.class).defaultValue();
builder.addStatement("$T $N = $N.getFloat($L.R.styleable.$L_$L, $L)", type, name, "typedArray", customViewElement.appPackage, customViewElement.inputClassName, key, defValue);
builder.addStatement("$T $N = $N.getFloat($L.R.styleable.$L_$L, $L)", type, name, "typedArray", customViewElement.appPackage, customViewElement.inputClassName, key, String.valueOf(defValue) + "f");
} else if (annotation.toString().equals(StringAttr.class.getCanonicalName())) {
String key = parameter.getAnnotation(StringAttr.class).value();
String defValue = parameter.getAnnotation(StringAttr.class).defaultValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
public @interface DimenAttr {
String value();

float defaultValue() default 0f;
float defaultValue() default 0;
int maxSdkVersion() default 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
public @interface FloatAttr {
String value();

float defaultValue() default 0f;
float defaultValue() default 0;
int maxSdkVersion() default 0;
}

0 comments on commit 8fb3877

Please sign in to comment.