Skip to content

Commit

Permalink
feat(adapter): add kotlin adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxime Jallu committed Sep 29, 2017
1 parent 4ab328a commit 1050159
Show file tree
Hide file tree
Showing 14 changed files with 273 additions and 304 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.android.jmaxime.factory.ViewHolderFactory;
import com.android.jmaxime.interfaces.ViewCheckableCallback;
import com.android.jmaxime.viewholder.JRecyclerViewHolder;
import com.android.jmaxime.viewholder.RecyclerViewHolder;

import java.util.HashMap;
import java.util.List;
Expand All @@ -18,7 +18,7 @@ public class CheckableAdapter<T> extends RecyclerAdapter<T> implements ViewCheck

private HashMap<String, Boolean> mCheckedMap = new HashMap<>();

public CheckableAdapter(Class<? extends JRecyclerViewHolder<T>> viewHolderType) {
public CheckableAdapter(Class<? extends RecyclerViewHolder<T>> viewHolderType) {
super(viewHolderType);
setCommunication(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package com.android.jmaxime.adapters;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

@SuppressWarnings("WeakerAccess")
public class DividerItemDecoration extends RecyclerView.ItemDecoration {

private static final String INVALID = "invalid orientation";
private static final int[] ATTRS = new int[]{
android.R.attr.listDivider
};

public static final int LIST_HORIZONTAL = LinearLayoutManager.HORIZONTAL;
public static final int LIST_VERTICAL = LinearLayoutManager.VERTICAL;
public static final int GRID_STROKE = 3;
public static final int GRID_FILL = 4;
private static Drawable mDivider;
private int mOrientation;

/**
* Custom constructor
*
* @param context
* @param orientation
*/
public DividerItemDecoration(final Context context, final int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
if (mDivider == null){
mDivider = a.getDrawable(0);
}
a.recycle();
setOrientation(orientation);
}

/**
* Set the orientation of the underlying grid / list
*
* @param orientation must be
* {@link com.android.jmaxime.adapters.DividerItemDecoration#LIST_HORIZONTAL}
* {@link com.android.jmaxime.adapters.DividerItemDecoration#LIST_VERTICAL}
* {@link com.android.jmaxime.adapters.DividerItemDecoration#GRID_STROKE}
* {@link com.android.jmaxime.adapters.DividerItemDecoration#GRID_FILL}
*/
public void setOrientation(final int orientation) {
if (orientation != LIST_HORIZONTAL &&
orientation != LIST_VERTICAL &&
orientation != GRID_STROKE &&
orientation != GRID_FILL) {
throw new IllegalArgumentException(INVALID);
}
mOrientation = orientation;
}

@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
if (mDivider != null) {
switch (mOrientation) {
case LIST_VERTICAL:
drawVertical(c, parent);
break;
case LIST_HORIZONTAL:
drawHorizontal(c, parent);
break;
case GRID_FILL:
drawGridFill(c, parent);
break;
case GRID_STROKE:
drawGridStroke(c, parent);
break;
default:
throw new IllegalArgumentException(INVALID);
}
}
}

/**
* Draw vertical divider in parent
*
* @param c canvas
* @param parent recycler view
*/
public void drawVertical(final Canvas c, final RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();

final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}

/**
* Draw horizontal divider in parent
*
* @param c Canvas
* @param parent Recycler view
*/
public void drawHorizontal(final Canvas c, final RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();

final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getRight() + params.rightMargin;
final int right = left + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}

/**
* Remove both horizontal and vertical divider in parent
*
* @param c Canvas
* @param parent Recycler view
*/
public void drawGridStroke(final Canvas c, final RecyclerView parent) {
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getLeft() - params.leftMargin;
final int right = child.getRight() + params.rightMargin;
final int top = child.getTop() - params.topMargin;
final int bottom = child.getBottom() + params.bottomMargin;
mDivider.setBounds(left, top, left + mDivider.getIntrinsicHeight(), bottom);
mDivider.draw(c);
mDivider.setBounds(right - mDivider.getIntrinsicHeight(), top, right, bottom);
mDivider.draw(c);
mDivider.setBounds(left, top, right, top + mDivider.getIntrinsicHeight());
mDivider.draw(c);
mDivider.setBounds(left, bottom - mDivider.getIntrinsicHeight(), right, bottom);
mDivider.draw(c);
mDivider.draw(c);
}
}

/**
* Draw both horizontal and vertical divider in parent
*
* @param c Canvas
* @param parent Recler view
*/
public void drawGridFill(final Canvas c, final RecyclerView parent) {
final int childCount = parent.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = parent.getChildAt(i);
final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
.getLayoutParams();
final int left = child.getLeft() - params.leftMargin;
final int right = child.getRight() + params.rightMargin;
final int top = child.getTop() - params.topMargin;
final int bottom = child.getBottom() + params.bottomMargin;
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
super.getItemOffsets(outRect, view, parent, state);
if (mDivider != null) {
switch (mOrientation) {
case LIST_VERTICAL:
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
break;
case LIST_HORIZONTAL:
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
break;
case GRID_FILL:
outRect.set(mDivider.getIntrinsicWidth(), mDivider.getIntrinsicWidth(), mDivider.getIntrinsicWidth(), mDivider.getIntrinsicWidth());
break;
case GRID_STROKE:
outRect.set(0, 0, 0, 0);
break;
default:
throw new IllegalArgumentException(INVALID);
}
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import com.android.jmaxime.factory.ViewHolderFactory;
import com.android.jmaxime.interfaces.IBaseCommunication;
import com.android.jmaxime.viewholder.JRecyclerViewHolder;
import com.android.jmaxime.viewholder.RecyclerViewHolder;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -23,21 +23,21 @@ public class EasyPagerAdapter<T> extends PagerAdapter {
private final List<T> mItems;
private final ViewHolderFactory<T> mFactory;

public EasyPagerAdapter(Class<? extends JRecyclerViewHolder<T>> viewHolder) {
public EasyPagerAdapter(Class<? extends RecyclerViewHolder<T>> viewHolder) {
this(new ArrayList<>(), viewHolder);
}

public EasyPagerAdapter(List<T> items, Class<? extends JRecyclerViewHolder<T>> viewHolder) {
public EasyPagerAdapter(List<T> items, Class<? extends RecyclerViewHolder<T>> viewHolder) {
this(items, viewHolder, null);
}

public EasyPagerAdapter(List<T> items, Class<? extends JRecyclerViewHolder<T>> viewHolder, IBaseCommunication<T> callback) {
public EasyPagerAdapter(List<T> items, Class<? extends RecyclerViewHolder<T>> viewHolder, IBaseCommunication<T> callback) {
mItems = items;
mFactory = new ViewHolderFactory<>(viewHolder, callback);
}

@Override public Object instantiateItem(ViewGroup container, int position) {
JRecyclerViewHolder<T> vh = mFactory.createVH(container, 0);
RecyclerViewHolder<T> vh = mFactory.createVH(container, 0);
vh.bind(mItems.get(position));
container.addView(vh.itemView);
return vh.itemView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import com.android.jmaxime.interfaces.IAdapterChanged;
import com.android.jmaxime.interfaces.IBaseCommunication;
import com.android.jmaxime.interfaces.IViewType;
import com.android.jmaxime.viewholder.JRecyclerViewHolder;
import com.android.jmaxime.viewholder.RecyclerViewHolder;

import java.security.AccessControlException;
import java.util.ArrayList;
Expand All @@ -24,7 +24,7 @@
* Use this Class for : <br/>
* ... {DOCUMENTATION}
*/
public class RecyclerAdapter<T> extends RecyclerView.Adapter<JRecyclerViewHolder<T>> {
public class RecyclerAdapter<T> extends RecyclerView.Adapter<RecyclerViewHolder<T>> {

private List<T> mTList;
private ViewHolderFactory<T> mFactory;
Expand All @@ -38,19 +38,19 @@ public RecyclerAdapter(ViewHolderFactory<T> factory) {
this(new ArrayList<>(), factory);
}

public RecyclerAdapter(Class<? extends JRecyclerViewHolder<T>> viewHolderType) {
public RecyclerAdapter(Class<? extends RecyclerViewHolder<T>> viewHolderType) {
this(new ArrayList<>(), viewHolderType, null);
}

public RecyclerAdapter(Class<? extends JRecyclerViewHolder<T>> viewHolderType, @Nullable IBaseCommunication callback) {
public RecyclerAdapter(Class<? extends RecyclerViewHolder<T>> viewHolderType, @Nullable IBaseCommunication callback) {
this(new ArrayList<>(), viewHolderType, callback);
}

public RecyclerAdapter(List<T> TList, Class<? extends JRecyclerViewHolder<T>> viewHolderType) {
public RecyclerAdapter(List<T> TList, Class<? extends RecyclerViewHolder<T>> viewHolderType) {
this(TList, viewHolderType, null);
}

public RecyclerAdapter(List<T> TList, Class<? extends JRecyclerViewHolder<T>> viewHolderType, @Nullable IBaseCommunication callback) {
public RecyclerAdapter(List<T> TList, Class<? extends RecyclerViewHolder<T>> viewHolderType, @Nullable IBaseCommunication callback) {
this(TList, new ViewHolderFactory<>(viewHolderType, callback));
}

Expand All @@ -65,7 +65,7 @@ public void setFactory(ViewHolderFactory<T> factory) {
}

@Override
public JRecyclerViewHolder<T> onCreateViewHolder(ViewGroup parent, int viewType) {
public RecyclerViewHolder<T> onCreateViewHolder(ViewGroup parent, int viewType) {
if (mFactory == null) {
throw new AccessControlException("mFactory is not instancied. thanks use setFactory() method.");
}
Expand All @@ -75,7 +75,7 @@ public JRecyclerViewHolder<T> onCreateViewHolder(ViewGroup parent, int viewType)


@Override
public void onBindViewHolder(JRecyclerViewHolder<T> holder, int position) {
public void onBindViewHolder(RecyclerViewHolder<T> holder, int position) {
holder.setItem(getItem(position));
holder.setBound(false);
holder.bind(holder.getItem());
Expand Down Expand Up @@ -105,7 +105,7 @@ public int getItemViewType(int position) {
return super.getItemViewType(position);
}

public void putViewType(int viewType, Class<? extends JRecyclerViewHolder<T>> viewHolder){
public void putViewType(int viewType, Class<? extends RecyclerViewHolder<T>> viewHolder){
mFactory.putViewType(viewType, viewHolder);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.android.jmaxime.interfaces;

/**
* @author Maxime Jallu
* @since 29/09/2017
* <p>
* Use this Class for : <br/>
* {DOCUMENTATION}
*/
public interface IAdapterChanged {
void onItemCountChange(int count);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.android.jmaxime.interfaces;


public interface IBaseCommunication<T> {

default void onDeleteClicked(int position, T item) {
/*nothing*/
}

default void onEditClicked(int position, T item) {
/*nothing*/
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.android.jmaxime.interfaces;

/**
* @author Maxime Jallu
* @since 29/09/2017
* <p>
* Use this Class for : <br/>
* {DOCUMENTATION}
*/
public interface IViewType {
int getItemViewType();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.android.jmaxime.interfaces;

import android.annotation.SuppressLint;

/**
* @author Maxime Jallu
* @since 29/09/2017
* <p>
* Use this Class for : <br/>
* {DOCUMENTATION}
*/
@SuppressLint("NewApi")
public interface ViewCheckableCallback<T> extends IBaseCommunication<T> {
boolean isChecked(T item);
default boolean isCheckable(T item) {
return true;
}
void put(String key, boolean value);
}
Loading

0 comments on commit 1050159

Please sign in to comment.