Skip to content

Commit 9996507

Browse files
authored
empty indicator view support added in base adapter
empty indicator view support added in base adapter
2 parents 79594c6 + 75381d9 commit 9996507

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

app/src/main/java/com/github/code/gambit/ui/BaseAdapter.kt

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import android.view.ViewGroup
77
import android.widget.TextView
88
import androidx.recyclerview.widget.RecyclerView
99
import 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

app/src/main/java/com/github/code/gambit/ui/fragment/home/main/HomeFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,6 @@ class HomeFragment : Fragment(R.layout.fragment_home), FileUrlClickCallback, Bot
137137
binding.linearProgress.hide()
138138
}
139139
adapter.addAll(it.files, true)
140-
binding.noFileIllustrationContainer.hide()
141140
binding.swipeRefresh.isRefreshing = false
142141
return@observe
143142
}
@@ -185,6 +184,7 @@ class HomeFragment : Fragment(R.layout.fragment_home), FileUrlClickCallback, Bot
185184
}
186185

187186
private fun setUpFileRecyclerView() {
187+
adapter.bindEmptyListView(binding.noFileIllustrationContainer)
188188
val layoutManager = LinearLayoutManager(requireContext())
189189
binding.fileList.layoutManager = layoutManager
190190
binding.fileList.setHasFixedSize(false)

0 commit comments

Comments
 (0)