Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add activity search to Android test app #3275

Merged
merged 3 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.content.res.Resources.NotFoundException
import android.os.Bundle
import android.view.Menu
import android.view.View
import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.SearchView
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -35,6 +37,7 @@ import java.util.*
class FeatureOverviewActivity : AppCompatActivity() {
private lateinit var recyclerView: RecyclerView
private var sectionAdapter: FeatureSectionAdapter? = null
private var featureAdapter: FeatureAdapter? = null
private var features: List<Feature>? = null

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -50,7 +53,7 @@ class FeatureOverviewActivity : AppCompatActivity() {
override fun onItemClicked(recyclerView: RecyclerView?, position: Int, view: View?) {
if (sectionAdapter!!.isSectionHeaderPosition(position).not()) {
val itemPosition = sectionAdapter!!.getConvertedPosition(position)
val feature = features!![itemPosition]
val feature = featureAdapter!!.getItem(itemPosition)
startFeature(feature)
}
}
Expand Down Expand Up @@ -86,6 +89,7 @@ class FeatureOverviewActivity : AppCompatActivity() {
if (featuresList.isNullOrEmpty()) {
return
}
featureAdapter = FeatureAdapter(features!!)
val sections: MutableList<FeatureSectionAdapter.Section> = ArrayList()
var currentCat = ""
for (i in features!!.indices) {
Expand All @@ -95,12 +99,11 @@ class FeatureOverviewActivity : AppCompatActivity() {
currentCat = category
}
}

sectionAdapter = FeatureSectionAdapter(
this,
R.layout.section_main_layout,
R.id.section_text,
FeatureAdapter(features!!)
featureAdapter!!
)
sectionAdapter!!.setSections(sections.toTypedArray())
recyclerView.adapter = sectionAdapter
Expand Down Expand Up @@ -163,6 +166,56 @@ class FeatureOverviewActivity : AppCompatActivity() {
}
}

// Add SearchView to the app bar
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.menu_feature_overview, menu)
val searchItem = menu.findItem(R.id.action_search)
val searchView = searchItem.actionView as SearchView
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String?): Boolean {
return false // No action on submit
}

override fun onQueryTextChange(newText: String?): Boolean {
filterFeatures(newText)
return true
}
})
return true
}

// Filter the features based on the search query
private fun filterFeatures(query: String?) {
val filteredFeatures = if (query.isNullOrEmpty()) {
features // Show full list if query is empty
} else {
features?.filter { it.getLabel().contains(query, ignoreCase = true) }
}
updateAdapter(filteredFeatures)
}

// Update the adapter with filtered features
private fun updateAdapter(filteredFeatures: List<Feature>?) {
if (filteredFeatures.isNullOrEmpty()) {
featureAdapter?.update(emptyList())
sectionAdapter?.setSections(emptyArray())
sectionAdapter?.notifyDataSetChanged()
return
}
val sections: MutableList<FeatureSectionAdapter.Section> = ArrayList()
var currentCat = ""
for (i in filteredFeatures.indices) {
val category = filteredFeatures[i].category
if (currentCat != category) {
sections.add(FeatureSectionAdapter.Section(i, category))
currentCat = category
}
}
featureAdapter?.update(filteredFeatures)
sectionAdapter?.setSections(sections.toTypedArray())
sectionAdapter?.notifyDataSetChanged()
}

companion object {
private const val KEY_STATE_FEATURES = "featureList"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@ import org.maplibre.android.testapp.utils.FontCache
/**
* Adapter used for FeatureOverviewActivity.
*
*
* Adapts a Feature to a visual representation to be shown in a RecyclerView.
*
*/
class FeatureAdapter(private val features: List<Feature>) :
class FeatureAdapter(private var features: List<Feature>) :
RecyclerView.Adapter<FeatureAdapter.ViewHolder>() {

class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var labelView: TextView
var descriptionView: TextView
Expand All @@ -32,8 +31,7 @@ class FeatureAdapter(private val features: List<Feature>) :
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view =
LayoutInflater.from(parent.context).inflate(R.layout.item_main_feature, parent, false)
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_main_feature, parent, false)
return ViewHolder(view)
}

Expand All @@ -45,4 +43,13 @@ class FeatureAdapter(private val features: List<Feature>) :
override fun getItemCount(): Int {
return features.size
}

fun update(newFeatures: List<Feature>) {
features = newFeatures
notifyDataSetChanged()
}

fun getItem(position: Int): Feature {
return features[position]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="#333333"
android:alpha="0.6">
<path
android:fillColor="@android:color/white"
android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z"/>
</vector>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_search"
android:title="@string/search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always|collapseActionView" />
</menu>
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
<string name="category_benchmark">Benchmark</string>
<string name="activity_benchmark_world_tour">World Tour Benchmark</string>
<string name="description_world_tour_benchmark">Writes out avg. fps and 1% fps after world tour with .flyTo()</string>
<string name="search">Search</string>
</resources>
Loading