@@ -6,9 +6,11 @@ import android.content.pm.PackageInfo
6
6
import android.content.pm.PackageManager
7
7
import android.content.res.Resources.NotFoundException
8
8
import android.os.Bundle
9
+ import android.view.Menu
9
10
import android.view.View
10
11
import androidx.annotation.StringRes
11
12
import androidx.appcompat.app.AppCompatActivity
13
+ import androidx.appcompat.widget.SearchView
12
14
import androidx.lifecycle.lifecycleScope
13
15
import androidx.recyclerview.widget.LinearLayoutManager
14
16
import androidx.recyclerview.widget.RecyclerView
@@ -35,6 +37,7 @@ import java.util.*
35
37
class FeatureOverviewActivity : AppCompatActivity () {
36
38
private lateinit var recyclerView: RecyclerView
37
39
private var sectionAdapter: FeatureSectionAdapter ? = null
40
+ private var featureAdapter: FeatureAdapter ? = null
38
41
private var features: List <Feature >? = null
39
42
40
43
override fun onCreate (savedInstanceState : Bundle ? ) {
@@ -50,7 +53,7 @@ class FeatureOverviewActivity : AppCompatActivity() {
50
53
override fun onItemClicked (recyclerView : RecyclerView ? , position : Int , view : View ? ) {
51
54
if (sectionAdapter!! .isSectionHeaderPosition(position).not ()) {
52
55
val itemPosition = sectionAdapter!! .getConvertedPosition(position)
53
- val feature = features !! [ itemPosition]
56
+ val feature = featureAdapter !! .getItem( itemPosition)
54
57
startFeature(feature)
55
58
}
56
59
}
@@ -86,6 +89,7 @@ class FeatureOverviewActivity : AppCompatActivity() {
86
89
if (featuresList.isNullOrEmpty()) {
87
90
return
88
91
}
92
+ featureAdapter = FeatureAdapter (features!! )
89
93
val sections: MutableList <FeatureSectionAdapter .Section > = ArrayList ()
90
94
var currentCat = " "
91
95
for (i in features!! .indices) {
@@ -95,12 +99,11 @@ class FeatureOverviewActivity : AppCompatActivity() {
95
99
currentCat = category
96
100
}
97
101
}
98
-
99
102
sectionAdapter = FeatureSectionAdapter (
100
103
this ,
101
104
R .layout.section_main_layout,
102
105
R .id.section_text,
103
- FeatureAdapter (features !! )
106
+ featureAdapter !!
104
107
)
105
108
sectionAdapter!! .setSections(sections.toTypedArray())
106
109
recyclerView.adapter = sectionAdapter
@@ -163,6 +166,56 @@ class FeatureOverviewActivity : AppCompatActivity() {
163
166
}
164
167
}
165
168
169
+ // Add SearchView to the app bar
170
+ override fun onCreateOptionsMenu (menu : Menu ): Boolean {
171
+ menuInflater.inflate(R .menu.menu_feature_overview, menu)
172
+ val searchItem = menu.findItem(R .id.action_search)
173
+ val searchView = searchItem.actionView as SearchView
174
+ searchView.setOnQueryTextListener(object : SearchView .OnQueryTextListener {
175
+ override fun onQueryTextSubmit (query : String? ): Boolean {
176
+ return false // No action on submit
177
+ }
178
+
179
+ override fun onQueryTextChange (newText : String? ): Boolean {
180
+ filterFeatures(newText)
181
+ return true
182
+ }
183
+ })
184
+ return true
185
+ }
186
+
187
+ // Filter the features based on the search query
188
+ private fun filterFeatures (query : String? ) {
189
+ val filteredFeatures = if (query.isNullOrEmpty()) {
190
+ features // Show full list if query is empty
191
+ } else {
192
+ features?.filter { it.getLabel().contains(query, ignoreCase = true ) }
193
+ }
194
+ updateAdapter(filteredFeatures)
195
+ }
196
+
197
+ // Update the adapter with filtered features
198
+ private fun updateAdapter (filteredFeatures : List <Feature >? ) {
199
+ if (filteredFeatures.isNullOrEmpty()) {
200
+ featureAdapter?.update(emptyList())
201
+ sectionAdapter?.setSections(emptyArray())
202
+ sectionAdapter?.notifyDataSetChanged()
203
+ return
204
+ }
205
+ val sections: MutableList <FeatureSectionAdapter .Section > = ArrayList ()
206
+ var currentCat = " "
207
+ for (i in filteredFeatures.indices) {
208
+ val category = filteredFeatures[i].category
209
+ if (currentCat != category) {
210
+ sections.add(FeatureSectionAdapter .Section (i, category))
211
+ currentCat = category
212
+ }
213
+ }
214
+ featureAdapter?.update(filteredFeatures)
215
+ sectionAdapter?.setSections(sections.toTypedArray())
216
+ sectionAdapter?.notifyDataSetChanged()
217
+ }
218
+
166
219
companion object {
167
220
private const val KEY_STATE_FEATURES = " featureList"
168
221
}
0 commit comments