-
Notifications
You must be signed in to change notification settings - Fork 728
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added OnModelCheckedChangeListener with tests * created ListenerUtils and moved common functions in it.
- Loading branch information
1 parent
688bddb
commit b0aa309
Showing
16 changed files
with
585 additions
and
45 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
epoxy-adapter/src/main/java/com/airbnb/epoxy/ListenersUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.airbnb.epoxy; | ||
|
||
import android.view.View; | ||
import android.view.ViewParent; | ||
|
||
import androidx.annotation.Nullable; | ||
import androidx.recyclerview.widget.RecyclerView; | ||
import androidx.recyclerview.widget.RecyclerView.ViewHolder; | ||
|
||
public class ListenersUtils { | ||
|
||
@Nullable | ||
static EpoxyViewHolder getEpoxyHolderForChildView(View v) { | ||
RecyclerView recyclerView = findParentRecyclerView(v); | ||
if (recyclerView == null) { | ||
return null; | ||
} | ||
|
||
ViewHolder viewHolder = recyclerView.findContainingViewHolder(v); | ||
if (viewHolder == null) { | ||
return null; | ||
} | ||
|
||
if (!(viewHolder instanceof EpoxyViewHolder)) { | ||
return null; | ||
} | ||
|
||
return (EpoxyViewHolder) viewHolder; | ||
} | ||
|
||
@Nullable | ||
private static RecyclerView findParentRecyclerView(@Nullable View v) { | ||
if (v == null) { | ||
return null; | ||
} | ||
|
||
ViewParent parent = v.getParent(); | ||
if (parent instanceof RecyclerView) { | ||
return (RecyclerView) parent; | ||
} | ||
|
||
if (parent instanceof View) { | ||
return findParentRecyclerView((View) parent); | ||
} | ||
|
||
return null; | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
epoxy-adapter/src/main/java/com/airbnb/epoxy/OnModelCheckedChangeListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.airbnb.epoxy; | ||
|
||
import android.widget.CompoundButton; | ||
|
||
public interface OnModelCheckedChangeListener<T extends EpoxyModel<?>, V> { | ||
/** | ||
* Called when the view bound to the model is checked. | ||
* | ||
* @param model The model that the view is bound to. | ||
* @param parentView The view bound to the model which received the click. | ||
* @param clickedView The view that received the click. This is either a child of the parentView | ||
* or the parentView itself | ||
* @param isChecked The new value for isChecked property. | ||
* @param position The position of the model in the adapter. | ||
*/ | ||
void onChecked(T model, V parentView, | ||
CompoundButton checkedView, boolean isChecked, int position); | ||
} |
61 changes: 61 additions & 0 deletions
61
epoxy-adapter/src/main/java/com/airbnb/epoxy/WrappedEpoxyModelCheckedChangeListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.airbnb.epoxy; | ||
|
||
import android.widget.CompoundButton; | ||
import android.widget.CompoundButton.OnCheckedChangeListener; | ||
|
||
import androidx.recyclerview.widget.RecyclerView; | ||
|
||
/** | ||
* Used in the generated models to transform normal checked change listener to model | ||
* checked change. | ||
*/ | ||
public class WrappedEpoxyModelCheckedChangeListener<T extends EpoxyModel<?>, V> | ||
implements OnCheckedChangeListener { | ||
|
||
private final OnModelCheckedChangeListener<T, V> originalCheckedChangeListener; | ||
|
||
public WrappedEpoxyModelCheckedChangeListener( | ||
OnModelCheckedChangeListener<T, V> checkedListener | ||
) { | ||
if (checkedListener == null) { | ||
throw new IllegalArgumentException("Checked change listener cannot be null"); | ||
} | ||
|
||
this.originalCheckedChangeListener = checkedListener; | ||
} | ||
|
||
@Override | ||
public void onCheckedChanged(CompoundButton button, boolean isChecked) { | ||
EpoxyViewHolder epoxyHolder = ListenersUtils.getEpoxyHolderForChildView(button); | ||
if (epoxyHolder == null) { | ||
throw new IllegalStateException("Could not find RecyclerView holder for clicked view"); | ||
} | ||
|
||
final int adapterPosition = epoxyHolder.getAdapterPosition(); | ||
if (adapterPosition != RecyclerView.NO_POSITION) { | ||
originalCheckedChangeListener | ||
.onChecked((T) epoxyHolder.getModel(), (V) epoxyHolder.objectToBind(), button, | ||
isChecked, adapterPosition); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (!(o instanceof WrappedEpoxyModelCheckedChangeListener)) { | ||
return false; | ||
} | ||
|
||
WrappedEpoxyModelCheckedChangeListener<?, ?> | ||
that = (WrappedEpoxyModelCheckedChangeListener<?, ?>) o; | ||
|
||
return originalCheckedChangeListener.equals(that.originalCheckedChangeListener); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return originalCheckedChangeListener.hashCode(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...ontest/src/main/java/com/airbnb/epoxy/integrationtest/ModelWithCheckedChangeListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.airbnb.epoxy.integrationtest; | ||
|
||
import android.view.View; | ||
import android.widget.CompoundButton; | ||
import android.widget.CompoundButton.OnCheckedChangeListener; | ||
|
||
import com.airbnb.epoxy.EpoxyAttribute; | ||
import com.airbnb.epoxy.EpoxyModel; | ||
|
||
import androidx.annotation.NonNull; | ||
|
||
public class ModelWithCheckedChangeListener extends EpoxyModel<View> { | ||
|
||
@EpoxyAttribute OnCheckedChangeListener checkedChangeListener; | ||
|
||
@Override | ||
protected int getDefaultLayout() { | ||
return R.layout.model_with_checked_change; | ||
} | ||
|
||
@Override | ||
public void bind(@NonNull View view) { | ||
if (view instanceof CompoundButton) { | ||
((CompoundButton) view).setOnCheckedChangeListener(checkedChangeListener); | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
epoxy-integrationtest/src/main/res/layout/model_with_checked_change.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<CheckBox xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
</CheckBox> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.