Skip to content

Commit

Permalink
Merge pull request #14 from FarshidRoohi/develop
Browse files Browse the repository at this point in the history
Release new version
  • Loading branch information
FarshidRoohi authored Oct 8, 2022
2 parents 1d5bfb2 + 91d2262 commit 1810a4c
Show file tree
Hide file tree
Showing 25 changed files with 602 additions and 142 deletions.
5 changes: 2 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.5.31'
ext.kotlin_version = '1.7.10'

repositories {
google()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.3'
classpath 'com.android.tools.build:gradle:7.3.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "com.vanniktech:gradle-maven-publish-plugin:0.17.0"
}
Expand Down
16 changes: 12 additions & 4 deletions customAdapterRecycleView/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ apply plugin: 'maven-publish'
apply plugin: 'com.vanniktech.maven.publish'

android {
compileSdkVersion 30
compileSdkVersion 33

defaultConfig {
minSdkVersion 14
targetSdkVersion 30
targetSdkVersion 33
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

}
Expand All @@ -20,6 +20,14 @@ android {
}
}

compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
buildFeatures {
viewBinding = true
}

}

publishing {
Expand All @@ -37,8 +45,8 @@ publishing {

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.core:core-ktx:1.9.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation "androidx.core:core-ktx:1.6.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.farshidroohi

import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
Expand All @@ -23,10 +24,11 @@ abstract class AdapterRecyclerView<T>(
@IdRes val retryButtonId: Int
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {


companion object {
private const val ITEM_VIEW = 0
private const val ITEM_LOADING = 1
private const val ITEM_FAILED = 2
const val ITEM_VIEW = 0
const val ITEM_LOADING = 1
const val ITEM_FAILED = 2
}

val items: MutableList<T?> = arrayListOf()
Expand All @@ -39,18 +41,26 @@ abstract class AdapterRecyclerView<T>(


abstract fun onBindView(
viewHolder: ItemViewHolder,
viewHolder: RecyclerView.ViewHolder,
position: Int,
context: Context,
element: T?
)

open fun onCreateViewHolder(
layoutInflater: LayoutInflater,
viewGroup: ViewGroup,
viewType: Int
): RecyclerView.ViewHolder? {
return null
}

override fun onCreateViewHolder(viewGroup: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

val inflater = LayoutInflater.from(viewGroup.context)

return when (viewType) {

ITEM_VIEW -> {
val view = inflater.inflate(itemViewLayout, viewGroup, false)
ItemViewHolder(view)
Expand All @@ -65,11 +75,7 @@ abstract class AdapterRecyclerView<T>(
val view = inflater.inflate(itemFailedLayout, viewGroup, false)
FailedViewHolder(view)
}

else -> {
val view = inflater.inflate(itemViewLayout, viewGroup, false)
ItemViewHolder(view)
}
else -> onCreateViewHolder(inflater, viewGroup, viewType)!!
}

}
Expand All @@ -78,10 +84,6 @@ abstract class AdapterRecyclerView<T>(

when (viewHolder) {

is ItemViewHolder -> {
onBindView(viewHolder, position, viewHolder.itemView.context, getItem(position))
}

is FailedViewHolder -> {

handleSingleRow(viewHolder, position)
Expand All @@ -90,12 +92,14 @@ abstract class AdapterRecyclerView<T>(
view?.setOnClickListener {
onRetryClicked()
}

}

is LoadingViewHolder -> {
handleSingleRow(viewHolder, position)
}
else -> {
onBindView(viewHolder, position, viewHolder.itemView.context, getItem(position))
}

}

Expand Down Expand Up @@ -125,6 +129,7 @@ abstract class AdapterRecyclerView<T>(
}


@SuppressLint("NotifyDataSetChanged")
fun removeAll() {
if (items.isEmpty()) {
return
Expand All @@ -142,11 +147,12 @@ abstract class AdapterRecyclerView<T>(
notifyItemRangeChanged(position, itemCount)
}

@SuppressLint("NotifyDataSetChanged")
fun remove(vararg item: T) {
if (this.items.isEmpty()) {
return
}
this.items.removeAll(item)
this.items.removeAll(item.toSet())
notifyDataSetChanged()
}

Expand All @@ -155,6 +161,7 @@ abstract class AdapterRecyclerView<T>(
}


@SuppressLint("NotifyDataSetChanged")
fun loadedState(newItems: List<T?>?) {

if (newItems == null) {
Expand Down Expand Up @@ -264,5 +271,4 @@ abstract class AdapterRecyclerView<T>(
class LoadingViewHolder(view: View) : RecyclerView.ViewHolder(view)

class FailedViewHolder(view: View) : RecyclerView.ViewHolder(view)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.github.farshidroohi

import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.viewbinding.ViewBinding
import io.github.farshidroohi.vh.BaseViewHolder


/**
* Created by Farshid Roohi.
* CustomAdapterRecyclerview | Copyrights 10/8/22.
*/

abstract class DiffUtilAdapterRecyclerView<ViewBindingType : ViewBinding, T>(
diffCallback: DiffUtil.ItemCallback<T>
) : ListAdapter<T, BaseViewHolder<ViewBindingType>>(diffCallback) {

abstract fun initViewBinding(parent: ViewGroup): ViewBindingType
abstract fun bind(binding: ViewBindingType, position: Int, item: T, payloads: MutableList<Any>?)

override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): BaseViewHolder<ViewBindingType> {
val binding = initViewBinding(parent)
return BaseViewHolder(binding)
}

override fun onBindViewHolder(
holder: BaseViewHolder<ViewBindingType>,
position: Int,
payloads: MutableList<Any>
) {
bind(holder.binding, position, currentList[position], payloads)
}

override fun onBindViewHolder(holder: BaseViewHolder<ViewBindingType>, position: Int) {
bind(holder.binding, position, currentList[position], null)
}

override fun getItemCount(): Int = currentList.size

fun changeItem(items: ArrayList<T>, element: T, newElement: T) {
val index = items.indexOf(element)
items[index] = newElement
submitList(items.toMutableList())
}

fun clear() {
submitList(emptyList())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ fun RecyclerView.onLoadMoreListener(extraCount: Int = 0, onLoadMore: () -> Unit)

is StaggeredGridLayoutManager -> {
val staggeredGridLayoutManager =
layoutManager as StaggeredGridLayoutManager
layoutManager as StaggeredGridLayoutManager
val spanCount = staggeredGridLayoutManager.spanCount
val lastPositions =
staggeredGridLayoutManager.findFirstVisibleItemPositions(IntArray(spanCount))
staggeredGridLayoutManager.findFirstVisibleItemPositions(IntArray(spanCount))
min(lastPositions[0], lastPositions[1])
}

Expand All @@ -61,16 +61,29 @@ fun RecyclerView.onLoadMoreListener(extraCount: Int = 0, onLoadMore: () -> Unit)

}

fun RecyclerView.onItemClickListener(onClickItem: (position: Int) -> Unit, onLongClickItem: (position: Int) -> Unit) {
fun RecyclerView.onItemClickListener(
onClickItem: (position: Int) -> Unit,
onLongClickItem: ((position: Int) -> Unit)? = null
) {

this.addOnItemTouchListener(
RecyclerTouchListener(
context,
this,
object : OnItemListenerRecyclerViewListener {
override fun onClick(position: Int) {
if (position != (adapter?.itemCount ?: 0) - 1) {
onClickItem(position)
}
}

this.addOnItemTouchListener(RecyclerTouchListener(context, this, object : OnItemListenerRecyclerViewListener {
override fun onClick(position: Int) {
onClickItem(position)
}
override fun onLongClick(position: Int) {
if (position != (adapter?.itemCount ?: 0) - 1) {
onLongClickItem?.invoke(position)
}

override fun onLongClick(position: Int) {
onLongClickItem(position)
}
}))
}
})
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class RecyclerTouchListener(context: Context?, recyclerView: RecyclerView, priva
override fun onLongPress(e: MotionEvent) {
val child = recyclerView.findChildViewUnder(e.x, e.y)
if (child != null && clickListener != null) {
clickListener.onLongClick(recyclerView.getChildPosition(child))
clickListener.onLongClick(recyclerView.getChildAdapterPosition(child))
}
}
})
Expand All @@ -35,7 +35,7 @@ class RecyclerTouchListener(context: Context?, recyclerView: RecyclerView, priva
override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
val child = rv.findChildViewUnder(e.x, e.y)
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(rv.getChildPosition(child))
clickListener.onClick(rv.getChildAdapterPosition(child))
}
return false
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.github.farshidroohi.vh

import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding

/**
* Created by Farshid Roohi.
* CustomAdapterRecyclerview | Copyrights 10/8/22.
*/

class BaseViewHolder<ViewBindingType : ViewBinding>(val binding: ViewBindingType) :
RecyclerView.ViewHolder(binding.root) {
}
5 changes: 2 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ org.gradle.jvmargs=-Xmx1536m

GROUP=io.github.farshidroohi
POM_ARTIFACT_ID=customAdapterRecycleView
VERSION_NAME=2.0.3
VERSION_NAME=2.1.2

POM_NAME=CustomAdapterRecycleView
POM_DESCRIPTION=Very easy endless scrolled, onClick item and simple use recyclerView adapter
Expand All @@ -19,5 +19,4 @@ POM_LICENCE_URL=https://www.apache.org/licenses/LICENSE-2.0.txt
POM_LICENCE_DIST=repo

POM_DEVELOPER_ID=farshidroohi
POM_DEVELOPER_NAME=Farshid Roohi

POM_DEVELOPER_NAME=Farshid Roohi
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-bin.zip
19 changes: 14 additions & 5 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 30
compileSdkVersion 33
defaultConfig {
minSdkVersion 14
targetSdkVersion 30
targetSdkVersion 33

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
Expand All @@ -19,14 +19,23 @@ android {
}
}

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

buildFeatures {
viewBinding = true
}

}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'androidx.appcompat:appcompat:1.5.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation "androidx.core:core-ktx:1.6.0"
implementation 'androidx.core:core-ktx:1.9.0'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

implementation project(":customAdapterRecycleView")
Expand Down
Loading

0 comments on commit 1810a4c

Please sign in to comment.