Skip to content

Commit

Permalink
Merge pull request #51 from vsukharev1/dev
Browse files Browse the repository at this point in the history
dev -> master 1.0.3
  • Loading branch information
vsukharew authored Mar 7, 2021
2 parents ba276a9 + 15574cf commit 6520092
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 34 deletions.
2 changes: 1 addition & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ext {
buildToolsVersion: "29.0.3",
minSdkVersion : 29,
library : [
versionName: "1.0.2",
versionName: "1.0.3",
versionCode: 1,
],
sample : [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ open class AnyTypeAdapter : RecyclerView.Adapter<AnyTypeViewHolder<Any, ViewBind
protected var anyTypeCollection: AnyTypeCollection = AnyTypeCollection.EMPTY
private var diffJob: Job? = null

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): AnyTypeViewHolder<Any, ViewBinding> {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): AnyTypeViewHolder<Any, ViewBinding> {
val view = LayoutInflater.from(parent.context).inflate(viewType, parent, false)
return with(anyTypeCollection) {
delegateAt(currentItemViewTypePosition).createViewHolder(view)
}
return anyTypeCollection.currentItemViewTypeDelegate.createViewHolder(view)
}

override fun onBindViewHolder(holder: AnyTypeViewHolder<Any, ViewBinding>, position: Int) {
with(anyTypeCollection) {
val delegate = delegateAt(currentItemViewTypePosition)
delegate.bind(items[position], holder)
currentItemViewTypeDelegate.bind(items[position], holder)
}
}

Expand All @@ -37,7 +37,7 @@ open class AnyTypeAdapter : RecyclerView.Adapter<AnyTypeViewHolder<Any, ViewBind
return with(anyTypeCollection) {
findCurrentItemViewTypePosition(this, position)
.also { currentItemViewTypePosition = it }
.let { delegateAt(it).getItemViewType() }
.let { currentItemViewTypeDelegate.getItemViewType() }
}
}

Expand Down Expand Up @@ -80,29 +80,14 @@ open class AnyTypeAdapter : RecyclerView.Adapter<AnyTypeViewHolder<Any, ViewBind
anyTypeCollection: AnyTypeCollection,
adapterPosition: Int
): Int {
return with(anyTypeCollection) {
if (itemsMetaData.size == 1) {
0
} else {
var result = 0
/**
* The following code is looking for the range the [adapterPosition] fits in
* or determines that [adapterPosition] is larger than right border of the last range
*/
loop@ for (i in positionsRanges.indices) {
when {
adapterPosition in positionsRanges[i] -> {
result = i
break@loop
}
adapterPosition > positionsRanges.last().last -> {
result = positionsRanges.size
break@loop
}
}
return with(anyTypeCollection.positionsRanges) {
binarySearch {
when {
adapterPosition in it -> 0
adapterPosition < it.first -> 1
else -> -1
}
result
}
}.takeIf { it >= 0 } ?: size
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ class AnyTypeCollection private constructor(
* Saved position value provided in [RecyclerView.Adapter.getItemViewType]
*/
var currentItemViewTypePosition: Int = 0
val size: Int = items.size

/**
* Returns delegate at the given position in the [itemsMetaData] collection
*/
fun delegateAt(position: Int): AnyTypeDelegate<Any, ViewBinding, AnyTypeViewHolder<Any, ViewBinding>> =
itemsMetaData[position].delegate
val currentItemViewTypeDelegate: AnyTypeDelegate<Any, ViewBinding, AnyTypeViewHolder<Any, ViewBinding>>
get() = itemsMetaData[currentItemViewTypePosition].delegate

val size: Int = items.size

class Builder {
private val items = mutableListOf<AdapterItem<Any>>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ data class AdapterItem<T>(
* @property position position the first [AdapterItem] of the given type is placed at
* @property delegate delegate creating appropriate [AnyTypeViewHolder] and binding data to
*/
data class AdapterItemMetaData<T: Any, V: ViewBinding>(
data class AdapterItemMetaData<T, V: ViewBinding>(
val position: Int,
val delegate: AnyTypeDelegate<T, V, AnyTypeViewHolder<T, V>>
)

0 comments on commit 6520092

Please sign in to comment.