Skip to content

Commit

Permalink
Merge pull request #4 from iFanie/feature-grouped-position-info
Browse files Browse the repository at this point in the history
Added grouped position info along with parent info
  • Loading branch information
iFanie authored Nov 15, 2018
2 parents 8e4c10a + bbd9376 commit a1bd2f1
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
RecyclerView adapter with nested items & expand/contract functionality
## Installation
```
implementation 'com.izikode.izilib:accordionrecycler:0.1'
implementation 'com.izikode.izilib:accordionrecycler:0.3'
```
## Usage
#### Have your Model Classes implement the ```AccordionRecyclerData``` interface.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,7 @@ abstract class AccordionRecyclerAdapter<ViewHolder, DataType> : AdapterContract.
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder = buildViewHolder(p0, p1)

override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
updateViewHolder(
position,
viewHolder,
presenter.getItem(position),
presenter.getItemEnclosedImmediateSum(position),
presenter.getItemEnclosedTotalSum(position),
presenter.getItemPosition(position),
presenter.getItemEnclosedPosition(position)
)
updateViewHolder(position, viewHolder, presenter.getItem(position), presenter.getItemRecyclingDetails(position))
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.izikode.izilib.accordionrecycler

/**
* Data class containing additional information about an item being recycled.
*/
data class AccordionRecyclerItemDetails(

/**
* The number of immediate children of the current item.
*/
val numberOfImmediateEnclosedItems: Int,

/**
* The number of total children, immediate or not, of the current item.
*/
val numberOfTotalEnclosedItems: Int,

/**
* The overall position info of the current item.
*/
val overallPosition: AccordionRecyclerPosition,

/**
* The position info of the current item, relative to it's parent item.
*/
val enclosedPosition: AccordionRecyclerPosition,

/**
* The recycling details of the item enclosing the current, if existing.
*/
val enclosingDetails: AccordionRecyclerItemDetails?

)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.izikode.izilib.accordionrecycler.adaptercomponents
import android.support.v7.widget.RecyclerView
import android.view.ViewGroup
import com.izikode.izilib.accordionrecycler.AccordionRecyclerData
import com.izikode.izilib.accordionrecycler.AccordionRecyclerItemDetails
import com.izikode.izilib.accordionrecycler.AccordionRecyclerPosition
import com.izikode.izilib.accordionrecycler.AccordionRecyclerViewHolder
import java.util.*
Expand Down Expand Up @@ -71,6 +72,13 @@ interface AdapterContract {
*/
fun getData(index: Int): DataType?

/**
* Gets the index of the data enclosing the data at the provided index.
* @param enclosedIndex The index of the enclosed data.
* @return The index of the enclosing index, if existing.
*/
fun getEnclosingDataIndex(enclosedIndex: Int): Int?

/**
* Gets the viewType of the data stored at the provided index.
*
Expand Down Expand Up @@ -140,12 +148,9 @@ interface AdapterContract {
* @param position The position of the data on the recycler.
* @param viewHolder The ViewHolder to be renewed.
* @param data The data to be used for the renewal of the ViewHolder.
* @param immediateEnclosedItemsSum The number of immediate children of the current item.
* @param totalEnclosedItemsSum The number of total children, immediate or not, of the current item.
* @param overallPosition The overall position info of the current item.
* @param enclosedPosition The position info of the current item, relative to it's parent item.
* @param details Details about the item being recycled.
*/
abstract fun updateViewHolder(position: Int, viewHolder: ViewHolder, data: DataType?, immediateEnclosedItemsSum: Int, totalEnclosedItemsSum: Int, overallPosition: AccordionRecyclerPosition, enclosedPosition: AccordionRecyclerPosition)
abstract fun updateViewHolder(position: Int, viewHolder: ViewHolder, data: DataType?, details: AccordionRecyclerItemDetails)

}

Expand Down Expand Up @@ -295,6 +300,13 @@ interface AdapterContract {
*/
fun getItemEnclosedPosition(position: Int): AccordionRecyclerPosition

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

/**
* Function called when the data set of the adapter changes.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ class AdapterModel<DataType> : AdapterContract.Model<DataType> {

override fun getData(index: Int): DataType? = dataList[index].data

override fun getEnclosingDataIndex(enclosedIndex: Int): Int? = dataList[enclosedIndex].enclosingWrapper?.let { dataList.indexOf(it) }

override fun getDataViewType(index: Int): Int = dataList[index].viewType

override fun getDataEnclosedImmediate(index: Int): SortedMap<Int, DataType?> = containingListMap[dataList[index]]?.let {
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 com.izikode.izilib.accordionrecycler.AccordionRecyclerViewHolder

Expand Down Expand Up @@ -75,6 +76,12 @@ 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 onItemSetChanged() {
view.notifyDataSetChanged()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.izikode.izilib.accordionrecyclerdemo
import android.view.ViewGroup
import com.izikode.izilib.accordionrecycler.AccordionRecyclerAdapter
import com.izikode.izilib.accordionrecycler.AccordionRecyclerData
import com.izikode.izilib.accordionrecycler.AccordionRecyclerPosition
import com.izikode.izilib.accordionrecycler.AccordionRecyclerItemDetails
import com.izikode.izilib.accordionrecyclerdemo.data.*
import com.izikode.izilib.accordionrecyclerdemo.viewholder.*

Expand Down Expand Up @@ -47,11 +47,11 @@ class MainAccordionAdapter : AccordionRecyclerAdapter<ColorViewHolder<out ColorD

}

override fun updateViewHolder(position: Int, viewHolder: ColorViewHolder<out ColorData>, data: ColorData?, immediateEnclosedItemsSum: Int, totalEnclosedItemsSum: Int, overallPosition: AccordionRecyclerPosition, enclosedPosition: AccordionRecyclerPosition) {
override fun updateViewHolder(position: Int, viewHolder: ColorViewHolder<out ColorData>, data: ColorData?, details: AccordionRecyclerItemDetails) {
when (viewHolder) {

is RedViewHolder -> viewHolder.apply {
update(data as RedData?, overallPosition, immediateEnclosedItemsSum)
update(data as RedData?, details.overallPosition, details.numberOfImmediateEnclosedItems)

onClick = { position, enclosedSum ->
if (enclosedSum > 0) {
Expand All @@ -63,7 +63,7 @@ class MainAccordionAdapter : AccordionRecyclerAdapter<ColorViewHolder<out ColorD
}

is PinkViewHolder -> viewHolder.apply {
update(data as PinkData?, overallPosition, enclosedPosition, totalEnclosedItemsSum)
update(data as PinkData?, details.overallPosition, details.enclosedPosition, details.numberOfTotalEnclosedItems)

onClick = { position, enclosedSum ->
if (enclosedSum > 0) {
Expand All @@ -75,18 +75,18 @@ class MainAccordionAdapter : AccordionRecyclerAdapter<ColorViewHolder<out ColorD
}

is EmptyPinkViewHolder -> viewHolder.apply {
update(data as EmptyPinkViewHolder.EmptyPinkData?, overallPosition, enclosedPosition, totalEnclosedItemsSum)
update(data as EmptyPinkViewHolder.EmptyPinkData?, details.overallPosition, details.enclosedPosition, details.numberOfTotalEnclosedItems)
}

is WhiteViewHolder -> viewHolder.apply {
update(data as WhiteData?, overallPosition, enclosedPosition)
update(data as WhiteData?, details.overallPosition, details.enclosedPosition)

onClick = {
removeItem(it)
}
}

else -> (viewHolder as GrayViewHolder).update(data as GrayData?, overallPosition)
else -> (viewHolder as GrayViewHolder).update(data as GrayData?, details.overallPosition)

}
}
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ buildscript {
],

Build : [
CODE : 2,
CODE : 3,
MAJOR : 0,
MINOR : 2
MINOR : 3
]
]
}
Expand Down

0 comments on commit a1bd2f1

Please sign in to comment.