Skip to content

Commit

Permalink
Merge pull request #4 from FabianTerhorst/ft/betterConverters
Browse files Browse the repository at this point in the history
Improve Layout Converters
  • Loading branch information
FabianTerhorst committed May 20, 2016
2 parents c82ad4b + 66a4975 commit d892357
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 39 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ buildscript {
}
dependencies {
...
classpath 'io.fabianterhorst:fastlayout-gradle-plugin:0.0.1-alpha17'
classpath 'io.fabianterhorst:fastlayout-gradle-plugin:0.0.2-alpha2'
...
}
}
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,18 @@
android:id="@+id/textView2"
android:text="Hello Linear Layout"
android:layout_weight="1"
android:background="@mipmap/ic_launcher"
android:layout_width="wrap_content"
android:drawableTop="@mipmap/ic_launcher"
android:drawableTintMode="screen"
android:textColor="@color/accent"
android:drawableStart="@mipmap/ic_launcher"
android:elevation="10dp"
android:nextFocusLeft="@id/linearLayout1"
android:layout_marginStart="10dp"
android:drawablePadding="10dp"
android:layout_height="wrap_content"/>
<!--android:drawableTint="@color/accent"-->

</LinearLayout>
<FrameLayout
Expand All @@ -56,13 +66,15 @@
android:layout_width="match_parent"
android:layout_marginEnd="10dp"
android:paddingEnd="10dp"
android:addStatesFromChildren="false"
android:paddingStart="10dp"
android:layout_toEndOf="@id/linearLayout1"
android:layout_height="wrap_content">

<TextView
android:id="@+id/textView3"
android:gravity="center"
android:fitsSystemWindows="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,30 @@ public class DefaultAttributesConverter extends LayoutConverter {
public LayoutAttribute onConvertLayoutAttributeValue(Object attributeValue, String attributeName) {
switch (attributeName) {
case "android:orientation":
return super.onConvertLayoutAttribute("LinearLayout." + String.valueOf(attributeValue).toUpperCase(), attributeName, false);
return super.onConvertLayoutAttribute(String.valueOf(attributeValue), "LinearLayout." + String.valueOf(attributeValue).toUpperCase(), attributeName, false);
case "android:gravity":
return super.onConvertLayoutAttribute("Gravity." + String.valueOf(attributeValue).toUpperCase(), attributeName, false);
return super.onConvertLayoutAttribute(String.valueOf(attributeValue), "Gravity." + String.valueOf(attributeValue).toUpperCase(), attributeName, false);
case "android:layout_gravity":
return onConvertLayoutAttribute("Gravity." + String.valueOf(attributeValue).toUpperCase(), attributeName, false);
case "android:background":
String value = String.valueOf(attributeValue);
if (value.startsWith("?attr/")) {
return super.onConvertLayoutAttribute("LayoutUtils.getAttrDrawable(getContext(), R.attr." + value.replace("?attr/", "") + ")", attributeName, false);
}
return onConvertLayoutAttribute(String.valueOf(attributeValue), "Gravity." + String.valueOf(attributeValue).toUpperCase(), attributeName, false);
}
return null;
return super.onConvertLayoutAttributeValue(attributeValue, attributeName);
}

@Override
public LayoutAttribute onConvertLayoutAttribute(Object attributeValue, String attributeName, boolean isString) {
public LayoutAttribute onConvertLayoutAttribute(String attributeStartValue, Object attributeValue, String attributeName, boolean isString) {
switch (attributeName) {
case "android:layout_gravity":
return new LayoutAttribute(LayoutAttribute.Type.PARAM, attribute(attributeName.replace("android:layout_", ""), attributeValue));
case "android:background":
if (String.valueOf(attributeStartValue).startsWith("R.")) {
return new LayoutAttribute(setter("BackgroundResource", attributeStartValue, false));
}
break;
}
//Todo : list with all
if(attributeName.startsWith("android:nextFocus")) {
return new LayoutAttribute(setter(attributeToName(attributeName) + "Id", attributeStartValue, false));
}
return super.onConvertLayoutAttribute(attributeValue, attributeName, isString);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,40 @@ public List<LayoutAttribute> finish() {

public LayoutAttribute onConvertLayoutAttributeValue(Object attributeValue, String attributeName) {
String attribute = String.valueOf(attributeValue);
LayoutAttribute layoutAttribute = new LayoutAttribute(attributeValue);
if (attribute.startsWith("@+id/") || attribute.startsWith("@id/")) {
return onConvertLayoutAttribute("R.id" + attribute.replace("@+id/", ".").replace("@id/", "."), attributeName, false);
} else if (attribute.startsWith("@dimen/")) {
return onConvertLayoutAttribute("(int) getContext().getResources().getDimension(R.dimen." + attribute.replace("@dimen/", "") + ")", attributeName, false);
} else if (attribute.startsWith("@string/")) {
return onConvertLayoutAttribute("getContext().getString(R.string." + attribute.replace("@string/", "") + ")", attributeName, false);
boolean isString = true;

if ((attribute.startsWith("@") || attribute.startsWith("?")) && attribute.contains("/")) {
String[] attributeSplit = attribute.split("/");
String type = attributeSplit[0].replace("@+", "").replace("@", "").replace("?", "");
attribute = "R." + type + "." + attributeSplit[1];
isString = false;
}

if (attribute.startsWith("R.dimen.")) {
return onConvertLayoutAttribute(attribute, "(int) getContext().getResources().getDimension(" + attribute + ")", attributeName, false);
} else if (attribute.startsWith("R.string.")) {
return onConvertLayoutAttribute(attribute, "getContext().getString(" + attribute + ")", attributeName, false);
} else if (attribute.startsWith("R.color.")) {
return onConvertLayoutAttribute(attribute, "getContext().getColor(" + attribute + ")", attributeName, false);
} else if (attribute.startsWith("R.drawable.") || attribute.startsWith("R.mipmap.") || attribute.startsWith("R.attr.")) {
return onConvertLayoutAttribute(attribute, "LayoutUtils.getAttrDrawable(getContext(), " + attribute + ")", attributeName, false);
} else if (attribute.endsWith("dp") && isNumber(attribute.replace("dp", ""))) {
return onConvertLayoutAttribute("(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, " + attribute.replace("dp", "") + ", getResources().getDisplayMetrics())", attributeName, false);
} /*else if (attribute.startsWith("?attr/") && attributeName != null && attributeName.equals("Background")) {
layoutAttribute = new LayoutAttribute("LayoutUtils.getAttrDrawable(getContext(), R.attr." + attribute.replace("?attr/", "") + ")", false);
} else if (attribute.startsWith("?attr/")) {
layoutAttribute = new LayoutAttribute("LayoutUtils.getAttrInt(getContext(), R.attr." + attribute.replace("?attr/", "") + ")", false);
} */ else if (attribute.equals("false") || attribute.equals("true")) {
return onConvertLayoutAttribute(attribute, "(int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, " + attribute.replace("dp", "") + ", getResources().getDisplayMetrics())", attributeName, false);
} else if (attribute.equals("false") || attribute.equals("true")) {
return onConvertLayoutAttribute(attribute, attributeName, false);
} else if (attribute.endsWith("sp") && isNumber(attribute.replace("sp", ""))) {
return onConvertLayoutAttribute("(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, " + attribute.replace("sp", "") + ", Resources.getSystem().getDisplayMetrics())", attributeName, false);
return onConvertLayoutAttribute(attribute, "(int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, " + attribute.replace("sp", "") + ", getResources().getDisplayMetrics())", attributeName, false);
} else if (isNumber(attribute)) {
return onConvertLayoutAttribute(attributeValue, attributeName, false);
}
return onConvertLayoutAttribute(layoutAttribute.getValue(), attributeName, true);
return onConvertLayoutAttribute(attribute, attributeName, isString);
}

public LayoutAttribute onConvertLayoutAttribute(Object attributeValue, String attributeName, boolean isString) {
/*attributeName = attributeName.split(":")[1];
String[] split = attributeName.split("_");
attributeName = "";
for (String refactor : split) {
attributeName += capitalize(refactor);
}*/
return onConvertLayoutAttribute(String.valueOf(attributeValue), attributeValue, attributeName, isString);
}

public LayoutAttribute onConvertLayoutAttribute(String attributeStartValue, Object attributeValue, String attributeName, boolean isString) {
attributeName = attributeToName(attributeName);
return new LayoutAttribute(setter(attributeName, attributeValue, isString));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class MarginConverter extends LayoutConverter {
private Object margin[] = new Object[]{null, null, null, null};//left,top,right,bottom

@Override
public LayoutAttribute onConvertLayoutAttribute(Object attributeValue, String attributeName, boolean isString) {
public LayoutAttribute onConvertLayoutAttribute(String attributeStartValue, Object attributeValue, String attributeName, boolean isString) {
switch (attributeName) {
case "android:layout_marginLeft":
margin = new Object[]{attributeValue, margin[1], margin[2], margin[3]};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public class PaddingConverter extends LayoutConverter {

private Object padding[] = new Object[]{null, null, null, null};//left,top,right,bottom

private Object paddingRelative[] = new Object[]{null, null};//left,top,right,bottom
private Object paddingRelative[] = new Object[]{null, null};//start,end

@Override
public LayoutAttribute onConvertLayoutAttribute(Object attributeValue, String attributeName, boolean isString) {
public LayoutAttribute onConvertLayoutAttribute(String attributeStartValue, Object attributeValue, String attributeName, boolean isString) {
switch (attributeName) {
case "android:paddingLeft":
padding = new Object[]{attributeValue, padding[1], padding[2], padding[3]};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class RelativeLayoutConverter extends LayoutConverter {
}};

@Override
public LayoutAttribute onConvertLayoutAttribute(Object attributeValue, String attributeName, boolean isString) {
public LayoutAttribute onConvertLayoutAttribute(String attributeStartValue, Object attributeValue, String attributeName, boolean isString) {
if(relativeParameters.contains(attributeName)) {
return new LayoutAttribute(LayoutAttribute.Type.PARAM, "addRule(" + "RelativeLayout." + stringToConstant(attributeToName(attributeName.replace("_to", "_").replace("layout_", ""))).toUpperCase() + (!attributeValue.equals("true") && !attributeValue.equals("false") ? ", " + attributeValue : ", " + "RelativeLayout." + String.valueOf(attributeValue).toUpperCase()) + ")");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
public class SizeConverter extends LayoutConverter {

@Override
public LayoutAttribute onConvertLayoutAttribute(Object attributeValue, String attributeName, boolean isString) {
public LayoutAttribute onConvertLayoutAttribute(String attributeStartValue, Object attributeValue, String attributeName, boolean isString) {
switch (attributeName) {
case "android:layout_width":
if(isString) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package io.fabianterhorst.fastlayout.converters;

import java.util.ArrayList;
import java.util.List;

/**
* Created by fabianterhorst on 20.05.16.
*/
public class TextViewLayoutConverter extends LayoutConverter {

private Object drawable[] = new Object[]{null, null, null, null};//left,top,right,bottom

private Object drawableRelative[] = new Object[]{null, null};//start,end

@Override
public LayoutAttribute onConvertLayoutAttribute(String attributeStartValue, Object attributeValue, String attributeName, boolean isString) {
switch (attributeName) {
case "android:drawablePadding":
return new LayoutAttribute(setter("CompoundDrawablePadding", attributeValue, false));
case "android:drawableTintMode":
return new LayoutAttribute(setter("CompoundDrawableTintMode", "android.graphics.PorterDuff.Mode." + String.valueOf(attributeValue).toUpperCase(), false));
case "android:drawableTint":
return new LayoutAttribute(setter("CompoundDrawableTintList", attributeValue, false));
case "android:drawableLeft":
drawable = new Object[]{attributeValue, drawable[1], drawable[2], drawable[3]};
return new LayoutAttribute();
case "android:drawableTop":
drawable = new Object[]{drawable[0], attributeValue, drawable[2], drawable[3]};
return new LayoutAttribute();
case "android:drawableRight":
drawable = new Object[]{drawable[0], drawable[1], attributeValue, drawable[3]};
return new LayoutAttribute();
case "android:drawableBottom":
drawable = new Object[]{drawable[0], drawable[1], drawable[2], attributeValue};
return new LayoutAttribute();
case "android:drawableStart":
drawableRelative = new Object[]{attributeValue, drawable[1], drawable[2], drawableRelative[1] != null ? drawableRelative[1] : drawable[3]};
return new LayoutAttribute();
case "android:drawableEnd":
drawableRelative = new Object[]{drawableRelative[0] != null ? drawableRelative[0] : drawable[0], drawable[1], attributeValue, drawable[3]};
return new LayoutAttribute();
}
return null;
}

@Override
public List<LayoutAttribute> onFinish() {
List<LayoutAttribute> finished = new ArrayList<>();
if (drawable[0] != null || drawable[1] != null || drawable[2] != null || drawable[3] != null) {
finished.add(new LayoutAttribute("setCompoundDrawables(" + drawable[0] + "," + drawable[1] + "," + drawable[2] + "," + drawable[3] + ")"));
}
if (drawableRelative[0] != null || drawableRelative[1] != null) {
finished.add(new LayoutAttribute("setCompoundDrawablesRelative(" + drawableRelative[0] + "," + drawable[1] + "," + drawable[2] + "," + drawableRelative[3] + ")"));
}
return finished;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import io.fabianterhorst.fastlayout.converters.PaddingConverter;
import io.fabianterhorst.fastlayout.converters.RelativeLayoutConverter;
import io.fabianterhorst.fastlayout.converters.SizeConverter;
import io.fabianterhorst.fastlayout.converters.TextViewLayoutConverter;

@SupportedAnnotationTypes({"io.fabianterhorst.fastlayout.annotations.Layouts", "io.fabianterhorst.fastlayout.annotations.Converter"})
public class LayoutProcessor extends AbstractProcessor {
Expand Down Expand Up @@ -246,6 +247,9 @@ private LayoutEntity createLayoutFromChild(Node node, String root) {
if (root.equals("RelativeLayout")) {
layoutConverters.add(new RelativeLayoutConverter());
}
if (node.getNodeName().equals("TextView")) {
layoutConverters.add(new TextViewLayoutConverter());
}
/*last*/
layoutConverters.add(new LayoutConverter());
converters.setAll(layoutConverters);
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.2-alpha1
0.0.2-alpha2

0 comments on commit d892357

Please sign in to comment.