Skip to content

Commit

Permalink
Merge pull request #7 from iFanie/feature-reusable-item-details
Browse files Browse the repository at this point in the history
Created map of reusable item details
  • Loading branch information
iFanie authored Nov 19, 2018
2 parents 9a71120 + 736ad5f commit 9ec1cba
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 33 deletions.
27 changes: 16 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,23 @@ implementation 'com.izikode.izilib:accordionrecycler:0.3'
```kotlin
abstract class ColorData : AccordionRecyclerData<ColorData>

class RedData(var text: String) : ColorData() {
data class RedData(

var arrayOfPink: Array<PinkData> = arrayOf()

) : ColorData(RedViewHolder.VIEW_TYPE) {

override val mainData: ColorData?
get() = this

override val enclosedDataArray: Array<out AccordionRecyclerData<out ColorData?>>?
get() = arrayOfPink

override val viewType: Int = RedViewHolder.VIEW_TYPE
override var mainData: ColorData? = this
override var enclosedDataArray: Array<out AccordionRecyclerData<out ColorData?>>? = arrayOf()

}

class GrayData( ... ) : ColorData() { ... }
class PinkData( ... ) : ColorData() { ... }
class WhiteData( ... ) : ColorData() { ... }
data class GrayData( ... ) : ColorData() { ... }
data class PinkData( ... ) : ColorData() { ... }
data class WhiteData( ... ) : ColorData() { ... }
```
#### Have your ViewHolders extend the ```AccordionRecyclerViewHolder``` abstract class.
- Provide the ViewHolder constructor with the parent ViewGroup and the layout to be inflated.
Expand Down Expand Up @@ -96,9 +102,8 @@ class MainAccordionAdapter : AccordionRecyclerAdapter<ColorViewHolder<out ColorD

if (it is PinkData && it.enclosedDataArray.isNullOrEmpty()) {
arrayOf(
it.apply {
enclosedDataArray = arrayOf(EmptyPinkViewHolder.EmptyPinkData())
}
it,
EmptyPinkViewHolder.EmptyPinkData()
)
} else {
super.processForAdditionalItems(position, item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,21 @@ interface AdapterContract {
*/
fun getDataEnclosedPosition(index: Int): AccordionRecyclerPosition

/**
* Builds additional info required by an item being recycled.
*
* @param index The item that is being recycled.
* @return The additional info needed for the item being recycled.
*/
fun getDataRecyclingDetails(index: Int): AccordionRecyclerItemDetails

/**
* Clears the reusable additional recycling info for the item at the provided index.
*
* @param index The item whose additional info will be removed.
*/
fun removeDataRecyclingDetails(index: Int)

}

abstract class View<ViewHolder, DataType> : RecyclerView.Adapter<ViewHolder>()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.izikode.izilib.accordionrecycler.adaptercomponents

import com.izikode.izilib.accordionrecycler.AccordionRecyclerData
import com.izikode.izilib.accordionrecycler.AccordionRecyclerItemDetails
import com.izikode.izilib.accordionrecycler.AccordionRecyclerPosition
import java.lang.ref.WeakReference
import java.util.*
Expand All @@ -23,6 +24,11 @@ class AdapterModel<DataType> : AdapterContract.Model<DataType> {
*/
private val containingListMap: WeakHashMap<Wrapper<out DataType?>, MutableList<WeakReference<Wrapper<out DataType?>>>> = WeakHashMap()

/**
* Map of reusable item details.
*/
private val detailsMap: WeakHashMap<Wrapper<out DataType?>, AccordionRecyclerItemDetails> = WeakHashMap()

override val dataCount: Int get() = dataList.size

override fun setData(dataArray: Array<out AccordionRecyclerData<out DataType?>>, silently: Boolean) {
Expand Down Expand Up @@ -300,6 +306,21 @@ class AdapterModel<DataType> : AdapterContract.Model<DataType> {

}

override fun getDataRecyclingDetails(index: Int): AccordionRecyclerItemDetails = dataList[index].let { wrapper ->
detailsMap[wrapper] ?:
AccordionRecyclerItemDetails(
getDataEnclosedImmediate(index).size,
getDataEnclosedTotal(index).size,
getDataPosition(index),
getDataEnclosedPosition(index),
getEnclosingDataIndex(index)?.let { getDataRecyclingDetails(it) }
).also { detailsMap[wrapper] = it }
}

override fun removeDataRecyclingDetails(index: Int) {
detailsMap.remove(dataList[index])
}

/**
* Helper class for the model data items.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,7 @@ class AdapterPresenter<DataType>(

override fun getItemEnclosedPosition(position: Int): AccordionRecyclerPosition = model.getDataEnclosedPosition(position)

override fun getItemRecyclingDetails(position: Int): AccordionRecyclerItemDetails = AccordionRecyclerItemDetails(
getItemEnclosedImmediateSum(position), getItemEnclosedTotalSum(position),
getItemPosition(position), getItemEnclosedPosition(position),
model.getEnclosingDataIndex(position)?.let { getItemRecyclingDetails(it) }
)
override fun getItemRecyclingDetails(position: Int): AccordionRecyclerItemDetails = model.getDataRecyclingDetails(position)

override fun onItemSetChanged() {
view.notifyDataSetChanged()
Expand All @@ -104,8 +100,13 @@ class AdapterPresenter<DataType>(

override fun onItemsChanged(startingPosition: Int, numberOfItemsChanged: Int) {
if (numberOfItemsChanged == 1) {
model.removeDataRecyclingDetails(startingPosition)
view.notifyItemChanged(startingPosition)
} else if (numberOfItemsChanged > 1) {
repeat(numberOfItemsChanged) { iteration ->
model.removeDataRecyclingDetails(startingPosition + iteration)
}

view.notifyItemRangeChanged(startingPosition, numberOfItemsChanged)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,46 @@ import java.util.*

class MainActivity : AppCompatActivity() {

var grayIndex = 0
private val colorData = arrayListOf<ColorData>().apply {
add(GrayData("1 gray"))
addAll(redArray(2))
add(GrayData("2 gray"))
add(GrayData("3 gray"))
addAll(redArray(4))
add(GrayData("4 gray"))
addAll(redArray(2))
repeat(100) {
add(gray())
addAll(redArray(2))
add(gray())
add(gray())
addAll(redArray(8))
add(gray())
add(gray())
add(gray())
addAll(redArray(3))
add(gray())
addAll(redArray(1))
add(gray())
addAll(redArray(4))
}
}

fun getNewPink() = pinkArray(-1, Random().nextInt(4) + 1)

private fun redArray(sum: Int): Array<RedData> = Array(sum) { index ->
RedData("$index red").apply {
arrayOfPink = pinkArray(index, Random().nextInt(8) + 1)
}
private fun gray() = GrayData("$grayIndex gray").also { grayIndex += 1 }

var redIndex = 0
private fun redArray(sum: Int): Array<RedData> = Array(sum) {
RedData("$redIndex red").apply {
arrayOfPink = pinkArray(redIndex, Random().nextInt(8) + 1)
}.also { redIndex += 1 }
}

private fun pinkArray(forRed: Int, sum: Int): Array<PinkData> = Array(sum) { index ->
PinkData("$index pink of $forRed red").apply {
arrayOfWhite = whiteArray(index, Random().nextInt(9))
}
var pinkIndex = 0
private fun pinkArray(forRed: Int, sum: Int): Array<PinkData> = Array(sum) {
PinkData("$pinkIndex pink of $forRed red").apply {
arrayOfWhite = whiteArray(pinkIndex, Random().nextInt(9))
}.also { pinkIndex += 1 }
}

private fun whiteArray(forPink: Int, sum: Int): Array<WhiteData> = Array(sum) { index ->
WhiteData("$index white of $forPink pink")
var whiteIndex = 0
private fun whiteArray(forPink: Int, sum: Int): Array<WhiteData> = Array(sum) {
WhiteData("$whiteIndex white of $forPink pink").also { whiteIndex += 1 }
}

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -50,6 +64,7 @@ class MainActivity : AppCompatActivity() {
}

accordionAdapter.colors = colorData
hello.text = "Hello Accordion Recycler! size: ${accordionAdapter.itemCount}"
}

}

0 comments on commit 9ec1cba

Please sign in to comment.