@@ -7,6 +7,8 @@ import android.view.ViewGroup
7
7
import android.widget.TextView
8
8
import androidx.recyclerview.widget.RecyclerView
9
9
import androidx.viewbinding.ViewBinding
10
+ import com.github.code.gambit.utility.extention.hide
11
+ import com.github.code.gambit.utility.extention.show
10
12
11
13
/* *
12
14
* 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
22
24
private val backupData = ArrayList <T >()
23
25
private var dataList = ArrayList <T >()
24
26
var listener: LS ? = null
25
- var counterView: TextView ? = null
27
+ private var counterView: TextView ? = null
28
+ private var emptyIndicatorView: View ? = null
26
29
27
30
/* *
28
31
* 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
77
80
dataList.add(data)
78
81
notifyDataSetChanged()
79
82
updateCounterText()
83
+ refreshEmptyIndicatorState()
80
84
}
81
85
82
86
/* *
@@ -91,10 +95,13 @@ abstract class BaseAdapter<T, B : ViewBinding, LS : OnItemClickListener<T>>(priv
91
95
dataList.addAll(dataItems)
92
96
notifyDataSetChanged()
93
97
updateCounterText()
98
+ refreshEmptyIndicatorState()
94
99
}
95
100
96
101
fun remove (item : T ) {
97
102
dataList.remove(item)
103
+ updateCounterText()
104
+ refreshEmptyIndicatorState()
98
105
notifyDataSetChanged()
99
106
}
100
107
@@ -105,6 +112,13 @@ abstract class BaseAdapter<T, B : ViewBinding, LS : OnItemClickListener<T>>(priv
105
112
counterView = view
106
113
}
107
114
115
+ /* *
116
+ * Sets the view which is displayed when list is empty
117
+ */
118
+ fun bindEmptyListView (view : View ) {
119
+ emptyIndicatorView = view
120
+ }
121
+
108
122
/* *
109
123
* updates the [counterView] with latest item count from [getItemCount]
110
124
*/
@@ -113,13 +127,26 @@ abstract class BaseAdapter<T, B : ViewBinding, LS : OnItemClickListener<T>>(priv
113
127
counterView?.text = " $itemCount Results"
114
128
}
115
129
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
+
116
143
/* *
117
144
* Returns the item <T> from [dataList] at specific position
118
145
* @param position : position of item in [dataList]
119
146
*
120
147
* @return : element at [position] in [dataList]
121
148
*/
122
- fun getItem (position : Int ) = dataList[position]
149
+ private fun getItem (position : Int ) = dataList[position]
123
150
124
151
/* *
125
152
* @return : current size of [dataList]
@@ -148,6 +175,7 @@ abstract class BaseAdapter<T, B : ViewBinding, LS : OnItemClickListener<T>>(priv
148
175
backupData.addAll(dataList)
149
176
dataList.clear()
150
177
updateCounterText()
178
+ refreshEmptyIndicatorState()
151
179
notifyDataSetChanged()
152
180
}
153
181
@@ -158,6 +186,8 @@ abstract class BaseAdapter<T, B : ViewBinding, LS : OnItemClickListener<T>>(priv
158
186
dataList.clear()
159
187
dataList.addAll(backupData)
160
188
backupData.clear()
189
+ updateCounterText()
190
+ refreshEmptyIndicatorState()
161
191
notifyDataSetChanged()
162
192
}
163
193
0 commit comments