Skip to content

Commit

Permalink
feat: Add search view to the PIDs list
Browse files Browse the repository at this point in the history
  • Loading branch information
tzebrowski committed Sep 3, 2023
1 parent 4fa3e65 commit 67aaa21
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import android.view.View
import android.view.ViewGroup
import android.view.Window
import android.widget.Button
import androidx.appcompat.widget.SearchView
import androidx.appcompat.widget.Toolbar
import androidx.fragment.app.DialogFragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -20,50 +22,61 @@ import org.obd.graphs.preferences.getStringSet
import org.obd.graphs.preferences.updateStringSet
import org.obd.metrics.pid.PIDsGroup
import org.obd.metrics.pid.PidDefinition
import java.util.*


private const val FILTER_BY_ECU_SUPPORTED_PIDS_PREF = "pref.pids.registry.filter_pids_ecu_supported"
private const val FILTER_BY_STABLE_PIDS_PREF = "pref.pids.registry.filter_pids_stable"


data class PidDefinitionDetails(val source: PidDefinition, var checked: Boolean = false, var supported: Boolean = true)


class PIDsListPreferenceDialog(private val key: String, private val priority: String) : DialogFragment() {

private lateinit var root: View

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
dialog?.let {
it.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
it.window?.requestFeature(Window.FEATURE_NO_TITLE)
}
super.onViewCreated(view, savedInstanceState)
}

private lateinit var listOfItems: List<PidDefinitionDetails>

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {

val root = inflater.inflate(R.layout.dialog_pids, container, false)
root = inflater.inflate(R.layout.dialog_pids, container, false)
val toolbar = root.findViewById<Toolbar>(R.id.custom_dialog_layout_toolbar)
toolbar.inflateMenu(R.menu.pids_dialog_menu)

when (priority) {
"low" -> findPidDefinitionByPriority { pidDefinition -> pidDefinition.priority > 0 }
"high" -> findPidDefinitionByPriority { pidDefinition -> pidDefinition.priority == 0 }
else -> mutableListOf()
}.let {
val pref = Prefs.getStringSet(key).map { s -> s.toLong() }
it.forEach { p ->
if (pref.contains(p.source.id)) {
p.checked = true
}
val searchView = toolbar.menu.findItem(R.id.menu_searchview).actionView as SearchView
searchView.setIconifiedByDefault(false)
searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {

override fun onQueryTextSubmit(query: String): Boolean {
Log.i("PIDsListPreferenceDialog", "OnQueryTextSubmit newText=$query")
filterListOfItems(query)
return false
}

val adapter = PIDsViewAdapter(context, it)
val recyclerView: RecyclerView = root.findViewById(R.id.recycler_view)
recyclerView.layoutManager = GridLayoutManager(context, 1)
recyclerView.adapter = adapter
}
override fun onQueryTextChange(newText: String): Boolean {
Log.i("PIDsListPreferenceDialog", "OnQueryTextChange newText=$newText")
filterListOfItems(newText)
return false
}
})

listOfItems = findPIDs()

val adapter = PIDsViewAdapter(context, listOfItems)
val recyclerView: RecyclerView = getRecyclerView(root)
recyclerView.layoutManager = GridLayoutManager(context, 1)
recyclerView.adapter = adapter

root.findViewById<Button>(R.id.pid_list_close_window).apply {
setOnClickListener {
Expand All @@ -73,21 +86,60 @@ class PIDsListPreferenceDialog(private val key: String, private val priority: St

root.findViewById<Button>(R.id.pid_list_save).apply {
setOnClickListener {
val pidList = getSelectedPIDs(root)
Log.i("PIDsListPreferenceDialog", "Key=$key, selected PIDs=$pidList")
Prefs.updateStringSet(key, pidList)
persistSelection()
dialog?.dismiss()
}
}

return root
}

private fun getSelectedPIDs(root: View): List<String> =
(root.findViewById<RecyclerView>(R.id.recycler_view).adapter as PIDsViewAdapter).data
private fun persistSelection() {
val pidList = getAdapter().data
.filter { it.checked }
.map { it.source.id.toString() }.toList()

Log.i("PIDsListPreferenceDialog", "Key=$key, selected PIDs=$pidList")
Prefs.updateStringSet(key, pidList)
}

private fun filterListOfItems(newText: String) {
val adapter = getAdapter()

adapter.data.forEach { pp ->
listOfItems.find { it.source.id == pp.source.id }?.let {
it.checked = pp.checked
}
}

val ccc = listOfItems.filter { it.source.description.lowercase(Locale.getDefault()).contains(newText) }
adapter.updateData(ccc)
adapter.notifyDataSetChanged()
}

private fun getAdapter() = (getRecyclerView(root).adapter as PIDsViewAdapter)

private fun findPIDs(): List<PidDefinitionDetails> {
val pref = Prefs.getStringSet(key).map { s -> s.toLong() }

val list = when (priority) {
"low" -> findPidDefinitionByPriority { pidDefinition -> pidDefinition.priority > 0 }
"high" -> findPidDefinitionByPriority { pidDefinition -> pidDefinition.priority == 0 }
else -> mutableListOf()
}

list.let {
it.forEach { p ->
if (pref.contains(p.source.id)) {
p.checked = true
}
}
}
return list
}

private fun getRecyclerView(root: View): RecyclerView = root.findViewById(R.id.recycler_view)


private fun findPidDefinitionByPriority(predicate: (PidDefinition) -> Boolean): List<PidDefinitionDetails> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ class PIDsViewAdapter internal constructor(
}
}

fun updateData(data: List<PidDefinitionDetails>){
this.data = data
}

override fun getItemCount(): Int {
return data.size
}
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/res/layout/dialog_pids.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content">



<TableLayout android:background="@color/white"
android:id="@+id/tablelayout"
android:layout_width="fill_parent"
Expand All @@ -13,6 +15,15 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<androidx.appcompat.widget.Toolbar
android:id="@+id/custom_dialog_layout_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />



<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/menu/pids_dialog_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?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/menu_searchview"
android:icon="@android:drawable/ic_menu_search"
android:title="@string/pref_search_view_title"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always|collapseActionView" />
</menu>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@
<string name="virtual_screen_8">8</string>
<string name="dialog_save_action">Save</string>
<string name="pids_supported">Supported</string>
<string name="pref_search_view_title">Search</string>


<string-array name="pref.numbers_1_20">
Expand Down

0 comments on commit 67aaa21

Please sign in to comment.