Skip to content

Commit

Permalink
Optimized addition and removal functions on the adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
rahulchowdhury committed May 30, 2017
1 parent 7341834 commit b41e54b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 23 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/co/upcurve/mystiquesample/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import co.upcurve.mystique.MystiqueAdapter
import co.upcurve.mystique.MystiqueItemPresenter
import co.upcurve.mystique.removeItems
import co.upcurve.mystique.addItems
import co.upcurve.mystique.toMystifiedList
import co.upcurve.mystiquesample.items.PostItem
import co.upcurve.mystiquesample.models.BannerModel
Expand Down Expand Up @@ -50,7 +50,7 @@ class MainActivity : AppCompatActivity(), PostItem.OnItemClickListener {
recyclerView.adapter = mystiqueAdapter

Handler().postDelayed({
mystiqueAdapter.removeItems(newList)
mystiqueAdapter.addItems(newList.toMystifiedList(this), 2)
}, 3000)
}
}
58 changes: 37 additions & 21 deletions mystique/src/main/java/co/upcurve/mystique/MystiqueExtensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,17 @@ fun ViewGroup.inflate(layoutId: Int, attachToRoot: Boolean = false): View {
* An extension function to [MystiqueAdapter] to add a list of items
* to the adapter and notifying the adapter of the same
*
* An optional starting position from where the items will be inserted
* can be specified. Otherwise, the new items will be added to the end
* of the list
*
* @param items A list of [T] items to be added
* @param startPosition The starting position from where the items will be added
*/
fun <T : MystiqueItemPresenter> MystiqueAdapter<T>.addItems(items: List<T>) {
fun <T : MystiqueItemPresenter> MystiqueAdapter<T>.addItems(items: List<T>, startPosition: Int = mystiqueItems.size) {
items.isNotEmpty().let {
mystiqueItems.addAll(items)
notifyDataSetChanged()
notifyItemRangeInserted(startPosition, items.size)
}
}

Expand All @@ -55,23 +60,8 @@ fun <T : MystiqueItemPresenter> MystiqueAdapter<T>.addItems(items: List<T>) {
fun <T : MystiqueItemPresenter> MystiqueAdapter<T>.removeItems(items: List<Any>) {
items.isNotEmpty().let {
items.forEach {
val itemModel = it
var itemToRemove: MystiqueItemPresenter? = null

mystiqueItems.forEach innerLoop@ {
val mystiqueItemModel = it.getModel()

if (mystiqueItemModel != null && itemModel == mystiqueItemModel) {
itemToRemove = it
return@innerLoop
}
}

itemToRemove?.let {
mystiqueItems.remove(it)
}
removeItem(it)
}
notifyDataSetChanged()
}
}

Expand All @@ -83,9 +73,11 @@ fun <T : MystiqueItemPresenter> MystiqueAdapter<T>.removeItems(items: List<Any>)
* @param item A [T] item to be added
* @param index Position where the item is to be added (if required)
*/
fun <T : MystiqueItemPresenter> MystiqueAdapter<T>.addItem(item: T, index: Int = mystiqueItems.size) {
mystiqueItems.add(index, item)
notifyItemInserted(index)
fun <T : MystiqueItemPresenter> MystiqueAdapter<T>.addItem(item: T?, index: Int = mystiqueItems.size) {
item?.let {
mystiqueItems.add(index, item)
notifyItemInserted(index)
}
}

/**
Expand All @@ -100,6 +92,30 @@ fun <T : MystiqueItemPresenter> MystiqueAdapter<T>.removeItem(index: Int = mysti
notifyItemRemoved(index)
}

/**
* An extension function to [MystiqueAdapter] to remove a single item from
* the adapter and notify the adapter of the same
*
* @param model The item model to remove from the adapter list
*/
fun <T : MystiqueItemPresenter> MystiqueAdapter<T>.removeItem(model: Any) {
var itemToRemove: MystiqueItemPresenter? = null

mystiqueItems.forEach {
val mystiqueItemModel = it.getModel()

if (mystiqueItemModel != null && model == mystiqueItemModel) {
itemToRemove = it
}
}

itemToRemove?.let {
val indexRemoved = mystiqueItems.indexOf(it)
mystiqueItems.remove(it)
notifyItemRemoved(indexRemoved)
}
}

/**
* This function is the core of mystifying a model. It takes in a
* regular model and magically turns it into a [MystiqueItemPresenter]
Expand Down

0 comments on commit b41e54b

Please sign in to comment.