Skip to content

Commit

Permalink
Merge pull request #5 from MaximeJallu/add_adapter
Browse files Browse the repository at this point in the history
feat(adapter): add kotlin adapter
  • Loading branch information
MaximeJallu authored Sep 29, 2017
2 parents 078c705 + b0280f5 commit ee553c2
Show file tree
Hide file tree
Showing 15 changed files with 542 additions and 42 deletions.
29 changes: 18 additions & 11 deletions adapter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
}

dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.4-3"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.1.50"
classpath 'me.tatarka:gradle-retrolambda:3.7.0'
}
}
Expand All @@ -16,10 +16,11 @@ apply plugin: 'kotlin-android'

android {
compileSdkVersion 26
buildToolsVersion "26.0.0"
buildToolsVersion "26.0.1"


defaultConfig {
// applicationId "com.android.jmaxime.com.android.jmaxime.adapter"
minSdkVersion 23
targetSdkVersion 26
versionCode 1
Expand All @@ -37,24 +38,30 @@ android {
}

compileOptions {
targetCompatibility 1.8
sourceCompatibility 1.8
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}

def androidSupport = '26.1.0'

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:26.0.2'
// implementation "com.android.support:appcompat-v7:$androidSupport"
testImplementation 'junit:junit:4.12'
androidTestImplementation('com.android.support.test.espresso:espresso-core:3.0.1', {
exclude group: 'com.android.support', module: 'support-annotations'
})

compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.4-3"
compile "com.android.support:appcompat-v7:26.0.2"
compile "com.android.support:design:26.0.2"
compile "org.jetbrains.kotlin:kotlin-stdlib:1.1.50"
compile "com.android.support:appcompat-v7:$androidSupport"
compile "com.android.support:design:$androidSupport"
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'
compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
compile 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
compile 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.android.jmaxime.adapters;

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

import java.util.HashMap;
import java.util.List;

/**
* @author Maxime Jallu
* @since 26/09/2017
* <p>
* Use this Class for : <br/>
* {DOCUMENTATION}
*/
public class CheckableAdapter<T> extends RecyclerAdapter<T> implements ViewCheckableCallback<T> {

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

public CheckableAdapter(Class<? extends JRecyclerViewHolder<T>> viewHolderType) {
super(viewHolderType);
setCommunication(this);
}

public CheckableAdapter(List<T> TList, ViewHolderFactory<T> factory) {
super(TList, factory);
setCommunication(this);
}

/**
* Clear map and add item to selected
* add selected position to sparseArray
*
* @param position
*/
public void setSelected(int position) {
/*si plusieurs cellules sont checked alors on fera un notify complet*/
mCheckedMap.clear();
put(String.valueOf(getItem(position).hashCode()), true);
notifyDataSetChanged();
}

@Override public boolean isChecked(T item) {
String hashString = String.valueOf(item.hashCode());
return mCheckedMap.containsKey(hashString) ? mCheckedMap.get(hashString) : false;
}

@Override public void put(String key, boolean value) {
mCheckedMap.put(key, value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.android.jmaxime.adapters;

import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;

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

import java.util.ArrayList;
import java.util.List;

/**
* @author Maxime Jallu
* @since 26/09/2017
* <p>
* Use this Class for : <br/>
* {DOCUMENTATION}
*/
public class EasyPagerAdapter<T> extends PagerAdapter {

private final List<T> mItems;
private final ViewHolderFactory<T> mFactory;

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

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

public EasyPagerAdapter(List<T> items, Class<? extends JRecyclerViewHolder<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);
vh.bind(mItems.get(position));
container.addView(vh.itemView);
return vh.itemView;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}

@Override public int getCount() {
return mItems.size();
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}

public void addAll(List<T> medias) {
mItems.addAll(medias);
notifyDataSetChanged();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
package com.android.jmaxime.adapters;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.android.jmaxime.factory.ViewHolderFactory;
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 java.security.AccessControlException;
import java.util.ArrayList;
import java.util.List;

/**
* @author Maxime Jallu
* @since 03/05/2017
* <p>
* Create for CubeInStore - Android (Decathlon)
* <p>
* Use this Class for : <br/>
* ... {DOCUMENTATION}
*/
public class RecyclerAdapter<T> extends RecyclerView.Adapter<JRecyclerViewHolder<T>> {

private List<T> mTList;
private ViewHolderFactory<T> mFactory;
private IAdapterChanged mAdapterChanged;

public RecyclerAdapter() {
mTList = new ArrayList<>();
}

public RecyclerAdapter(ViewHolderFactory<T> factory) {
this(new ArrayList<>(), factory);
}

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

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

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

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


public RecyclerAdapter(List<T> TList, ViewHolderFactory<T> factory) {
mTList = TList;
mFactory = factory;
}

public void setFactory(ViewHolderFactory<T> factory) {
mFactory = factory;
}

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

return mFactory.createVH(parent, viewType);
}


@Override
public void onBindViewHolder(JRecyclerViewHolder<T> holder, int position) {
holder.setItem(getItem(position));
holder.setBound(false);
holder.bind(holder.getItem());
holder.setBound(true);
}

@Override
public int getItemCount() {
return mTList != null ? mTList.size() : 0;
}

/**
* Get Item
*
* @param position position founded
* @return instance to position
*/
public T getItem(int position) {
return mTList.get(position);
}

@Override
public int getItemViewType(int position) {
if (getItem(position) != null && getItem(position) instanceof IViewType) {
return ((IViewType) getItem(position)).getItemViewType();
}
return super.getItemViewType(position);
}

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

public boolean contains(final T obj) {
return mTList.contains(obj);
}

protected void callChangedListener() {
if (mAdapterChanged != null) {
mAdapterChanged.onItemCountChange(getItemCount());
}
}

public void setOnAdapterChangedListener(IAdapterChanged adapterChanged) {
mAdapterChanged = adapterChanged;
}

public void setCommunication(IBaseCommunication communication) {
mFactory.setCommunication(communication);
notifyDataSetChanged();
}

/**
* Inserts the specified element at the specified position in this list (optional operation).
* Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
*
* @param item element to be inserted
*/
public void addItem(T item) {
if (mTList != null) {
mTList.add(item);
notifyItemInserted(mTList.size());
}
}

/**
* Inserts the specified element at the specified position in this list (optional operation).
* Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
*
* @param item element to be inserted
* @param position index at which the specified element is to be inserted
*/
public void addItem(T item, int position) {
if (mTList != null) {
position = Math.min(position, mTList.size());
mTList.add(position, item);
notifyItemInserted(position);
}
}

/**
* Inserts the specified element at the specified position in this list (optional operation).
* Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
*
* @param collection elements to be inserted
*/
public void addAll(List<T> collection) {
if (mTList != null) {
mTList.addAll(collection);
int start = Math.max(0, (mTList.size() - collection.size()) - 1);
notifyItemRangeInserted(start, collection.size());
}
}

/**
* Inserts the specified element at the specified position in this list (optional operation).
* Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
*
* @param item the element to be removed
*/
public void removeItem(T item) {
removeItem(getTList().indexOf(item));
}

/**
* Inserts the specified element at the specified position in this list (optional operation).
* Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
*
* @param position the index of the element to be removed
*/
public void removeItem(int position) {
if (mTList != null && position > -1 && position < mTList.size()) {
mTList.remove(position);
notifyItemRemoved(position);
}
}

/**
* Set new list items and notifyDataSetChanged()
*
* @param list new instance items list for bind views
* @link notifyDataSetChanged
*/
public void updateItems(@NonNull List<T> list) {
mTList = list;
notifyDataSetChanged();
}

/**
* @return instance items list
*/
public List<T> getTList() {
return mTList;
}

public boolean isEmpty() {
return mTList == null || mTList.isEmpty();
}
}
Loading

0 comments on commit ee553c2

Please sign in to comment.