anvil icon indicating copy to clipboard operation
anvil copied to clipboard

How to use anvil with ConstraintLayout?

Open amoikevin opened this issue 8 years ago • 2 comments

I wrote a helper class:

package trikita.anvil.constraint;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.ConstraintSet;
import android.support.constraint.Guideline;
import android.view.View;

import com.android.Action;
import com.android.widget.ConstraintLayoutUtils;

import trikita.anvil.Anvil;
import trikita.anvil.Anvil.AttributeSetter;
import trikita.anvil.Anvil.Renderable;
import trikita.anvil.BaseDSL.ViewClassResult;

import static trikita.anvil.BaseDSL.attr;
import static trikita.anvil.BaseDSL.v;

/**
 * Created by JKL on 2017/3/24.
 */
public final class ConstraintDSL implements AttributeSetter {

    static {
        Anvil.registerAttributeSetter(new ConstraintDSL());
    }

    public static Void constraintBaselineToBaseline(final int value) {
        return attr("constraintBaselineToBaseline", value);
    }

    public static Void constraintBottomToBottom(final int value) {
        return attr("constraintBottomToBottom", value);
    }

    public static Void constraintBottomToTop(final int value) {
        return attr("constraintBottomToTop", value);
    }

    public static Void constraintDimensionRatio(final String value) {
        return attr("constraintDimensionRatio", value);
    }

    public static Void constraintEditorAbsoluteX(final int value) {
        return attr("constraintEditorAbsoluteX", value);
    }

    public static Void constraintEditorAbsoluteY(final int value) {
        return attr("constraintEditorAbsoluteY", value);
    }

    public static Void constraintEndToEnd(final int value) {
        return attr("constraintEndToEnd", value);
    }

    public static Void constraintEndToStart(final int value) {
        return attr("constraintEndToStart", value);
    }

    public static Void constraintGoneBottomMargin(final int value) {
        return attr("constraintGoneBottomMargin", value);
    }

    public static Void constraintGoneEndMargin(final int value) {
        return attr("constraintGoneEndMargin", value);
    }

    public static Void constraintGoneLeftMargin(final int value) {
        return attr("constraintGoneLeftMargin", value);
    }

    public static Void constraintGoneRightMargin(final int value) {
        return attr("constraintGoneRightMargin", value);
    }

    public static Void constraintGoneStartMargin(final int value) {
        return attr("constraintGoneStartMargin", value);
    }

    public static Void constraintGoneTopMargin(final int value) {
        return attr("constraintGoneTopMargin", value);
    }

    public static Void constraintGuideBegin(final int value) {
        return attr("constraintGuideBegin", value);
    }

    public static Void constraintGuideEnd(final int value) {
        return attr("constraintGuideEnd", value);
    }

    public static Void constraintGuidePercent(final float value) {
        return attr("constraintGuidePercent", value);
    }

    public static Void constraintHorizontalBias(final float bias) {
        return attr("constraintVerticalBias", bias);
    }

    public static Void constraintHorizontalChainStyle(final int style) {
        return attr("constraintHorizontalChainStyle", style);
    }

    public static Void constraintHorizontalWeight(final float weight) {
        return attr("constraintHorizontalWeight", weight);
    }

    public static ViewClassResult constraintLayout() {
        return v(ConstraintLayout.class);
    }

    public static Void constraintLayout(@NonNull final Renderable render) {
        return v(ConstraintLayout.class, render);
    }

    public static Void constraintLeftToLeft(final int value) {
        return attr("constraintLeftToLeft", value);
    }

    public static Void constraintLeftToRight(final int value) {
        return attr("constraintLeftToRight", value);
    }

    public static Void constraintMatchConstraintDefaultHeight(final int value) {
        return attr("constraintMatchConstraintDefaultHeight", value);
    }

    public static Void constraintMatchConstraintDefaultWidth(final int value) {
        return attr("constraintMatchConstraintDefaultWidth", value);
    }

    public static Void constraintMatchConstraintMaxHeight(final int value) {
        return attr("constraintMatchConstraintMaxHeight", value);
    }

    public static Void constraintMatchConstraintMaxWidth(final int value) {
        return attr("constraintMatchConstraintMaxWidth", value);
    }

    public static Void constraintMatchConstraintMinHeight(final int value) {
        return attr("constraintMatchConstraintMinHeight", value);
    }

    public static Void constraintMatchConstraintMinWidth(final int value) {
        return attr("constraintMatchConstraintMinWidth", value);
    }

    public static Void constraintOrientation(final int value) {
        return attr("constraintOrientation", value);
    }

    public static Void constraintRightToLeft(final int value) {
        return attr("constraintRightToLeft", value);
    }

    public static Void constraintRightToRight(final int value) {
        return attr("constraintRightToRight", value);
    }

    public static Void constraintStartToEnd(final int value) {
        return attr("constraintStartToEnd", value);
    }

    public static Void constraintStartToStart(final int value) {
        return attr("constraintStartToStart", value);
    }

    public static Void constraintTopToBottom(final int value) {
        return attr("constraintTopToBottom", value);
    }

    public static Void constraintTopToTop(final int value) {
        return attr("constraintTopToTop", value);
    }

    public static Void constraintVerticalBias(final float bias) {
        return attr("constraintVerticalBias", bias);
    }

    public static Void constraintVerticalChainStyle(final int style) {
        return attr("constraintVerticalChainStyle", style);
    }

    public static Void constraintVerticalWeight(final float weight) {
        return attr("constraintVerticalWeight", weight);
    }

    public static ViewClassResult guideline() {
        return v(Guideline.class);
    }

    public static Void guideline(@NonNull final Renderable render) {
        return v(Guideline.class, render);
    }

    public static void apply(@NonNull final Action<ConstraintSet> applyer) {
        final ConstraintLayout parent = Anvil.currentView();
        if (parent == null) return;
        ConstraintLayoutUtils.apply(parent, applyer);
    }

    @Override
    public boolean set(@NonNull View v, @NonNull String name, @Nullable Object value, @Nullable Object prevValue) {
        switch (name) {
            case "constraintHorizontalWeight":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Float) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.horizontalWeight = (float) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintVerticalWeight":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Float) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.verticalWeight = (float) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintHorizontalBias":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Float) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.horizontalBias = (float) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintVerticalBias":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Float) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.verticalBias = (float) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintHorizontalChainStyle":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.horizontalChainStyle = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintVerticalChainStyle":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.verticalChainStyle = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintGuideBegin":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Float) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.guideBegin = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintGuideEnd":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Float) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.guideEnd = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintGuidePercent":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Float) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.guidePercent = (float) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintLeftToLeft":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.leftToLeft = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintLeftToRight":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.leftToRight = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintRightToLeft":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.rightToLeft = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintRightToRight":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.rightToRight = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintTopToTop":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.topToTop = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintTopToBottom":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.topToBottom = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintBottomToTop":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.bottomToTop = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintBottomToBottom":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.bottomToBottom = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintBaselineToBaseline":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.baselineToBaseline = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintStartToEnd":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.startToEnd = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintStartToStart":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.startToStart = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintEndToStart":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.endToStart = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintEndToEnd":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.endToEnd = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintGoneLeftMargin":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.goneLeftMargin = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintGoneTopMargin":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.goneTopMargin = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintGoneRightMargin":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.goneRightMargin = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintGoneBottomMargin":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.goneBottomMargin = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintGoneStartMargin":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.goneStartMargin = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintGoneEndMargin":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.goneEndMargin = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintDimensionRatio":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof String) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.dimensionRatio = (String) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintMatchConstraintDefaultWidth":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.matchConstraintDefaultWidth = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintMatchConstraintDefaultHeight":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.matchConstraintDefaultHeight = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintMatchConstraintMinWidth":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.matchConstraintMinWidth = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintMatchConstraintMaxWidth":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.matchConstraintMaxWidth = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintMatchConstraintMinHeight":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.matchConstraintMinHeight = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintMatchConstraintMaxHeight":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.matchConstraintMaxHeight = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintEditorAbsoluteX":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.editorAbsoluteX = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintEditorAbsoluteY":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.editorAbsoluteY = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
            case "constraintOrientation":
                if (v.getLayoutParams() instanceof ConstraintLayout.LayoutParams && value != null && value instanceof Integer) {
                    ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) v.getLayoutParams();
                    params.orientation = (int) value;
                    v.setLayoutParams(params);
                    return true;
                }
                break;
        }
        return false;
    }
}

But it did not work Any idea?

amoikevin avatar Jun 13 '17 05:06 amoikevin

Is this project still active? I, too, would like to use Anvil with ConstraintLayout and the latest versions of other support libraries.

pardom avatar Apr 16 '18 23:04 pardom

Experimental DSL support for CL is done in https://github.com/anvil-ui/anvil/pull/132

sgrekov avatar Sep 13 '19 09:09 sgrekov