@@ -7,6 +7,8 @@ import android.view.ViewGroup
77import android.widget.TextView
88import androidx.recyclerview.widget.RecyclerView
99import androidx.viewbinding.ViewBinding
10+ import com.github.code.gambit.utility.extention.hide
11+ import com.github.code.gambit.utility.extention.show
1012
1113/* *
1214 * Use as a base class for all the adapters in this project
@@ -22,7 +24,8 @@ abstract class BaseAdapter<T, B : ViewBinding, LS : OnItemClickListener<T>>(priv
2224 private val backupData = ArrayList <T >()
2325 private var dataList = ArrayList <T >()
2426 var listener: LS ? = null
25- var counterView: TextView ? = null
27+ private var counterView: TextView ? = null
28+ private var emptyIndicatorView: View ? = null
2629
2730 /* *
2831 * Adds the clickListener to the root view of [BaseViewHolder.binding], which calls the abstract
@@ -77,6 +80,7 @@ abstract class BaseAdapter<T, B : ViewBinding, LS : OnItemClickListener<T>>(priv
7780 dataList.add(data)
7881 notifyDataSetChanged()
7982 updateCounterText()
83+ refreshEmptyIndicatorState()
8084 }
8185
8286 /* *
@@ -91,10 +95,13 @@ abstract class BaseAdapter<T, B : ViewBinding, LS : OnItemClickListener<T>>(priv
9195 dataList.addAll(dataItems)
9296 notifyDataSetChanged()
9397 updateCounterText()
98+ refreshEmptyIndicatorState()
9499 }
95100
96101 fun remove (item : T ) {
97102 dataList.remove(item)
103+ updateCounterText()
104+ refreshEmptyIndicatorState()
98105 notifyDataSetChanged()
99106 }
100107
@@ -105,6 +112,13 @@ abstract class BaseAdapter<T, B : ViewBinding, LS : OnItemClickListener<T>>(priv
105112 counterView = view
106113 }
107114
115+ /* *
116+ * Sets the view which is displayed when list is empty
117+ */
118+ fun bindEmptyListView (view : View ) {
119+ emptyIndicatorView = view
120+ }
121+
108122 /* *
109123 * updates the [counterView] with latest item count from [getItemCount]
110124 */
@@ -113,13 +127,26 @@ abstract class BaseAdapter<T, B : ViewBinding, LS : OnItemClickListener<T>>(priv
113127 counterView?.text = " $itemCount Results"
114128 }
115129
130+ /* *
131+ * updates the [emptyIndicatorView] visibility based on latest item count from [getItemCount]
132+ */
133+ private fun refreshEmptyIndicatorState () {
134+ emptyIndicatorView?.let {
135+ if (itemCount == 0 ) {
136+ it.show()
137+ } else {
138+ it.hide()
139+ }
140+ }
141+ }
142+
116143 /* *
117144 * Returns the item <T> from [dataList] at specific position
118145 * @param position : position of item in [dataList]
119146 *
120147 * @return : element at [position] in [dataList]
121148 */
122- fun getItem (position : Int ) = dataList[position]
149+ private fun getItem (position : Int ) = dataList[position]
123150
124151 /* *
125152 * @return : current size of [dataList]
@@ -148,6 +175,7 @@ abstract class BaseAdapter<T, B : ViewBinding, LS : OnItemClickListener<T>>(priv
148175 backupData.addAll(dataList)
149176 dataList.clear()
150177 updateCounterText()
178+ refreshEmptyIndicatorState()
151179 notifyDataSetChanged()
152180 }
153181
@@ -158,6 +186,8 @@ abstract class BaseAdapter<T, B : ViewBinding, LS : OnItemClickListener<T>>(priv
158186 dataList.clear()
159187 dataList.addAll(backupData)
160188 backupData.clear()
189+ updateCounterText()
190+ refreshEmptyIndicatorState()
161191 notifyDataSetChanged()
162192 }
163193
0 commit comments