Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add epoxy model layout view #872

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.airbnb.epoxy;

import android.view.View;

import androidx.annotation.LayoutRes;

/**
* An {@link EpoxyModel} extension to abstract setup and usage of layout resource and span size.
*
* @see HiddenEpoxyModel
* @see SimpleEpoxyModel
*/
public class EpoxyModelWithLayoutView<T extends View> extends EpoxyModel<T> {

@LayoutRes protected final int layoutRes;
protected int spanCount;

public EpoxyModelWithLayoutView(@LayoutRes int layoutRes, int spanCount) {
this.layoutRes = layoutRes;
this.spanCount = spanCount;
}

@Override
protected int getDefaultLayout() {
return layoutRes;
}

@Override
public int getSpanSize(int totalSpanCount, int position, int itemCount) {
return spanCount;
}
}
12 changes: 4 additions & 8 deletions epoxy-adapter/src/main/java/com/airbnb/epoxy/HiddenEpoxyModel.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@
* Using a zero height and width {@link Space} view, as well as 0 span size, to exclude itself from
* view.
*/
class HiddenEpoxyModel extends EpoxyModel<Space> {
@Override
public int getDefaultLayout() {
return R.layout.view_holder_empty_view;
}
class HiddenEpoxyModel extends EpoxyModelWithLayoutView<Space> {

@Override
public int getSpanSize(int spanCount, int position, int itemCount) {
return 0;
HiddenEpoxyModel() {
super(R.layout.view_holder_empty_view, 0 /*spanCount*/);
}

}
16 changes: 2 additions & 14 deletions epoxy-adapter/src/main/java/com/airbnb/epoxy/SimpleEpoxyModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@
* subclass. This is useful for static layouts. You can also specify an onClick listener and the
* span size.
*/
public class SimpleEpoxyModel extends EpoxyModel<View> {
@LayoutRes private final int layoutRes;
public class SimpleEpoxyModel extends EpoxyModelWithLayoutView<View> {
private View.OnClickListener onClickListener;
private int spanCount = 1;

public SimpleEpoxyModel(@LayoutRes int layoutRes) {
this.layoutRes = layoutRes;
super(layoutRes, 1 /*spanCount*/);
}

public SimpleEpoxyModel onClick(View.OnClickListener listener) {
Expand Down Expand Up @@ -46,16 +44,6 @@ public void unbind(@NonNull View view) {
view.setOnClickListener(null);
}

@Override
protected int getDefaultLayout() {
return layoutRes;
}

@Override
public int getSpanSize(int totalSpanCount, int position, int itemCount) {
return spanCount;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.airbnb.epoxy

import android.widget.Space
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4

@RunWith(JUnit4::class)
class EpoxyModelWithLayoutViewTest {

@Test
fun `EpoxyModelWithLayoutView relays specified values from constructor`() {
val layoutRes = 12
val spanCount = 4
val model = EpoxyModelWithLayoutView<Space>(layoutRes, spanCount)

assert(model.defaultLayout == layoutRes)
assert(model.getSpanSize(100, 0, 10) == spanCount)
}
}