Skip to content

Commit

Permalink
[ModelView] - Add code to support set layout params manually from init (
Browse files Browse the repository at this point in the history
#1114)

* [ModelView] - Add code to support set layout params manually from initialize of view

* [ModelView] - Remove redundant code

* [Ktlint] Fix fail ktlint check

* [ModelView] - Update code create, fix naming and add test for manual layout params model view

* [Docs+Test] - Clean docs and fix unit test for epoxy-processortest
  • Loading branch information
dinhlamvn authored Feb 25, 2022
1 parent e698c56 commit 7825b76
Show file tree
Hide file tree
Showing 7 changed files with 397 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@
/**
* Use with {@link #autoLayout()} to declare what layout parameters should be used to size your
* view when it is added to a RecyclerView. This maps to the LayoutParams options {@code
* layout_width} and {@code layout_height}.
* layout_width} and {@code layout_height}. If you want to set the LayoutParams manually, you can
* use {@link Size#MANUAL} and set the params in the View's constructor when it is initialized (a
* runtime exception will be thrown if layout params are not set during view instantiation when
* MANUAL is used).
*/
enum Size {
NONE,
MANUAL,
WRAP_WIDTH_WRAP_HEIGHT,
WRAP_WIDTH_MATCH_HEIGHT,
MATCH_WIDTH_WRAP_HEIGHT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,12 +472,19 @@ class GeneratedModelWriter(
modelInfo.modelType
)

val (layoutWidth, layoutHeight) = getLayoutDimensions(modelInfo)

addStatement(
"v.setLayoutParams(new \$T(\$L, \$L))",
ClassNames.ANDROID_MARGIN_LAYOUT_PARAMS, layoutWidth, layoutHeight
)
getLayoutDimensions(modelInfo)?.let { (layoutWidth, layoutHeight) ->
addStatement(
"v.setLayoutParams(new \$T(\$L, \$L))",
ClassNames.ANDROID_MARGIN_LAYOUT_PARAMS, layoutWidth, layoutHeight
)
} ?: run {
beginControlFlow("if (v.getLayoutParams() == null)")
.addStatement(
"throw new \$T(\"Layout params is required to be set for Size.MANUAL\")",
NullPointerException::class.java
)
.endControlFlow()
}

addStatement("return v")
}
Expand All @@ -486,12 +493,13 @@ class GeneratedModelWriter(
return methods
}

private fun getLayoutDimensions(modelInfo: GeneratedModelInfo): Pair<CodeBlock, CodeBlock> {
private fun getLayoutDimensions(modelInfo: GeneratedModelInfo): Pair<CodeBlock, CodeBlock>? {
val matchParent = CodeBlock.of("\$T.MATCH_PARENT", ClassNames.ANDROID_MARGIN_LAYOUT_PARAMS)
val wrapContent = CodeBlock.of("\$T.WRAP_CONTENT", ClassNames.ANDROID_MARGIN_LAYOUT_PARAMS)

// Returns a pair of width to height
return when (modelInfo.layoutParams) {
ModelView.Size.MANUAL -> null
ModelView.Size.WRAP_WIDTH_MATCH_HEIGHT -> wrapContent to matchParent
ModelView.Size.MATCH_WIDTH_MATCH_HEIGHT -> matchParent to matchParent
// This will be used for Styleable views as the default
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,14 @@ class ViewProcessorTest {
)
}

@Test
fun testAutoLayoutManualLayoutParams() {
assertGeneration(
"AutoLayoutModelViewManualLayoutParams.java",
"AutoLayoutModelViewManualLayoutParamsModel_.java"
)
}

@Test
fun testModelViewInheritsFromSuperClass() {
assertViewsHaveModelsGenerated(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.airbnb.epoxy;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup.LayoutParams;

import com.airbnb.epoxy.ModelView.Size;

@ModelView(autoLayout = Size.MANUAL)
public class AutoLayoutModelViewManualLayoutParams extends View {

public AutoLayoutModelViewManualLayoutParams(Context context) {
super(context);
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
}

@ModelProp
void setValue(int value) {

}
}
Loading

0 comments on commit 7825b76

Please sign in to comment.