Skip to content

Commit

Permalink
add search to equipment
Browse files Browse the repository at this point in the history
  • Loading branch information
phillipthelen committed Nov 22, 2024
1 parent 2095542 commit 19b5166
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 6 deletions.
13 changes: 13 additions & 0 deletions Habitica/res/menu/menu_searchable.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<item
android:id="@+id/action_search"
android:icon="@android:drawable/ic_menu_search"
android:title="@string/search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="always" />
</menu>
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ abstract class BaseMainFragment<VB : ViewBinding> : BaseFragment<VB>() {
mainActivity?.supportActionBar?.setDisplayHomeAsUpEnabled(true)
}

@Deprecated("Use onCreateOptionsMenu(Menu, MenuInflater) instead")
override fun onCreateOptionsMenu(
menu: Menu,
inflater: MenuInflater,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,11 @@ class NavigationDrawerFragment : DialogFragment() {
"spring" -> R.string.spring
"summer" -> R.string.summer
"fall" -> R.string.fall
"nye" -> R.string.nye
"nye" -> R.string.winter
"birthday" -> R.string.winter
"valentines" -> R.string.valentines
"habitoween" -> R.string.habitoween
"thanksgiving" -> R.string.turkey_day
"valentines" -> R.string.winter
"habitoween" -> R.string.fall
"thanksgiving" -> R.string.fall
else -> R.string.open
}
seasonalItem.pillText = requireContext().getString(seasonID)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package com.habitrpg.android.habitica.ui.fragments.inventory.equipment

import android.app.SearchManager
import android.database.MatrixCursor
import android.os.Bundle
import android.provider.BaseColumns
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.AutoCompleteTextView
import androidx.appcompat.widget.SearchView
import androidx.core.view.MenuProvider
import androidx.cursoradapter.widget.SimpleCursorAdapter
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
Expand All @@ -22,14 +32,17 @@ import com.habitrpg.common.habitica.helpers.ExceptionHandler
import com.habitrpg.common.habitica.helpers.MainNavigationController
import com.habitrpg.common.habitica.helpers.launchCatching
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject


@AndroidEntryPoint
class EquipmentDetailFragment :
BaseMainFragment<FragmentRefreshRecyclerviewBinding>(),
SwipeRefreshLayout.OnRefreshListener {
SwipeRefreshLayout.OnRefreshListener, MenuProvider {
@Inject
lateinit var inventoryRepository: InventoryRepository

Expand All @@ -52,6 +65,8 @@ class EquipmentDetailFragment :
var equippedGear: String? = null
var isCostume: Boolean? = null

private var searchedText = MutableStateFlow<String?>(null)

private var adapter: EquipmentRecyclerViewAdapter = EquipmentRecyclerViewAdapter()

override fun onCreateView(
Expand All @@ -74,6 +89,7 @@ class EquipmentDetailFragment :
}
}
}
activity?.addMenuProvider(this, viewLifecycleOwner)
return super.onCreateView(inflater, container, savedInstanceState)
}

Expand Down Expand Up @@ -118,6 +134,12 @@ class EquipmentDetailFragment :
type?.let { type ->
lifecycleScope.launchCatching {
inventoryRepository.getOwnedEquipment(type)
.combine(searchedText) { equipment, query ->
if (query.isNullOrBlank()) {
return@combine equipment
}
equipment.filter { it.text.contains(query, true) || it.notes.contains(query, true) }
}
.map { it.sortedBy { equipment -> equipment.text } }
.collect { adapter.data = it }
}
Expand All @@ -135,4 +157,57 @@ class EquipmentDetailFragment :
binding?.refreshLayout?.isRefreshing = false
}
}

override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
menuInflater.inflate(R.menu.menu_searchable, menu)

val searchItem = menu.findItem(R.id.action_search)
val searchView = searchItem.actionView as SearchView
val suggestions = arrayOf("Spring Gear", "Summer Gear", "Fall Gear", "Winter Gear")
val from = arrayOf(SearchManager.SUGGEST_COLUMN_TEXT_1)
val to = intArrayOf(android.R.id.text1)
val suggestionAdapter = SimpleCursorAdapter(requireContext(), R.layout.support_simple_spinner_dropdown_item, null, from, to, 0)
val cursor = MatrixCursor(arrayOf(BaseColumns._ID, SearchManager.SUGGEST_COLUMN_TEXT_1))
for ((index, suggestion) in suggestions.withIndex()) {
cursor.addRow(arrayOf(index, suggestion))
}
suggestionAdapter.changeCursor(cursor)
searchView.suggestionsAdapter = suggestionAdapter
searchView.findViewById<AutoCompleteTextView>(R.id.search_src_text).threshold = 0

searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
override fun onQueryTextSubmit(query: String): Boolean {
searchedText.value = query
return false
}

override fun onQueryTextChange(newText: String): Boolean {
searchedText.value = newText
val filteredCursor = MatrixCursor(arrayOf(BaseColumns._ID, SearchManager.SUGGEST_COLUMN_TEXT_1))
for ((index, suggestion) in suggestions.withIndex()) {
if (suggestion.contains(newText, true)) {
filteredCursor.addRow(arrayOf(index, suggestion))
}
}
suggestionAdapter.changeCursor(filteredCursor)
return false
}
})

searchView.setOnSuggestionListener(object : SearchView.OnSuggestionListener {
override fun onSuggestionSelect(position: Int): Boolean {
val text = suggestionAdapter.getItem(position)
searchedText.value = text.toString()
return false
}

override fun onSuggestionClick(position: Int): Boolean {
return false
}
})
}

override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
TODO("Not yet implemented")
}
}
2 changes: 1 addition & 1 deletion version.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
NAME=4.5.0
CODE=9011
CODE=9231

0 comments on commit 19b5166

Please sign in to comment.