Skip to content

Instantly share code, notes, and snippets.

@jifferon
Last active August 29, 2017 19:23
Show Gist options
  • Save jifferon/a142376d0bf0a560c1b906bd6746843c to your computer and use it in GitHub Desktop.
Save jifferon/a142376d0bf0a560c1b906bd6746843c to your computer and use it in GitHub Desktop.
import android.support.v7.widget.RecyclerView
import java.util.*
abstract class AbstractArrayAdapter<ITEM, VH : RecyclerView.ViewHolder>(dataSet: MutableList<ITEM>?) : RecyclerView.Adapter<VH>() {
//region Data
private val dataSet: MutableList<ITEM>
//endregion
init {
if (dataSet != null) {
this.dataSet = dataSet
} else {
this.dataSet = mutableListOf()
}
}
/**
* Method to get dataset size
*/
override fun getItemCount(): Int {
return dataSet.size
}
/**
* Returns item from data set located at specified position
* @param position position
* *
* @return item
*/
internal fun getItemAtPosition(position: Int): ITEM {
return dataSet[position]
}
/**
* Method to get position of item
* @param item Item
*/
fun getItemPosition(item: ITEM): Int {
return dataSet.indexOf(item)
}
/**
* Adds item to collection and notifies adapter about changes
* @param item item
*/
fun addItem(item: ITEM) {
val position = dataSet.size
dataSet.add(item)
notifyItemInserted(position)
}
/**
* Returns whole dataset
* @return dataset
*/
fun getDataset(): List<ITEM> {
return this.dataSet
}
/**
* Merge existing collection with new one and notifies adapter about changes
* @param items new collection
* *
* @see .replaceCollection
*/
fun addCollection(items: List<ITEM>) {
val positionStart = dataSet.size
val itemCount = items.size
dataSet.addAll(items)
notifyItemRangeInserted(positionStart, itemCount)
}
/**
* Replace existing collection with new one and notifies adapter about changes
* @param items new collection
* *
* @see .setCollection
*/
fun replaceCollection(items: List<ITEM>) {
if (dataSet.size != 0) {
removeAll()
}
addCollection(items)
}
/**
* Method to update all occurances of items in collection
*/
fun updateCollection(items: List<ITEM>) {
if(dataSet.isNotEmpty() && items.isNotEmpty()) {
dataSet.forEachIndexed { i, item ->
updateItem(item)
}
}
}
/**
* Remove item from collection at given position and notifies adapter about changes
* @param position item position
* *
* @see .removeAll
*/
fun removeItem(position: Int) {
dataSet.removeAt(position)
notifyItemRemoved(position)
}
fun updateItem(index: Int, item: ITEM) {
if(dataSet.size-1>=index && dataSet.contains(item)) {
dataSet[index] = item
notifyItemChanged(index)
}
}
fun updateItem(item: ITEM) {
val index: Int? = dataSet.indexOf(item)
if(index != null) {
updateItem(index,item)
}
}
/**
* Remove all items from collection leaving it empty and notifies adapter about changes
*/
fun removeAll() {
val positionStart = 0
val itemCount = dataSet.size
dataSet.clear()
notifyItemRangeRemoved(positionStart, itemCount)
}
/**
* Method to pass new data to adapter, without bothering about its state.
* If dataset is not empty, this will attempt data update
* If dataset is empty, this will assign the new collection to it
*/
fun dropData(dataSet: List<ITEM>) {
if(isEmpty()) {
addCollection(dataSet)
}
else {
updateCollection(dataSet)
}
}
/**
* Method to get state of dataset
*/
fun isEmpty(): Boolean {
return dataSet.isEmpty()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment