Use SearchView's callbacks instead of Intents
This commit is contained in:
@@ -19,21 +19,12 @@
|
|||||||
tools:ignore="UnusedAttribute">
|
tools:ignore="UnusedAttribute">
|
||||||
<activity
|
<activity
|
||||||
android:name=".ui.activity.MainActivity"
|
android:name=".ui.activity.MainActivity"
|
||||||
android:launchMode="singleTop"
|
|
||||||
android:exported="true">
|
android:exported="true">
|
||||||
<intent-filter>
|
<intent-filter>
|
||||||
<action android:name="android.intent.action.MAIN" />
|
<action android:name="android.intent.action.MAIN" />
|
||||||
|
|
||||||
<category android:name="android.intent.category.LAUNCHER" />
|
<category android:name="android.intent.category.LAUNCHER" />
|
||||||
</intent-filter>
|
</intent-filter>
|
||||||
|
|
||||||
<intent-filter>
|
|
||||||
<action android:name="android.intent.action.SEARCH" />
|
|
||||||
</intent-filter>
|
|
||||||
|
|
||||||
<meta-data
|
|
||||||
android:name="android.app.searchable"
|
|
||||||
android:resource="@xml/searchable_recipe_main" />
|
|
||||||
</activity>
|
</activity>
|
||||||
</application>
|
</application>
|
||||||
|
|
||||||
|
|||||||
@@ -1,14 +1,12 @@
|
|||||||
package gq.kirmanak.mealient.ui.activity
|
package gq.kirmanak.mealient.ui.activity
|
||||||
|
|
||||||
import android.app.SearchManager
|
|
||||||
import android.content.Intent
|
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
import androidx.activity.viewModels
|
import androidx.activity.viewModels
|
||||||
import androidx.appcompat.app.AppCompatActivity
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
import androidx.appcompat.widget.SearchView
|
import androidx.appcompat.widget.SearchView
|
||||||
import androidx.core.content.getSystemService
|
import androidx.appcompat.widget.SearchView.OnQueryTextListener
|
||||||
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
||||||
import androidx.core.view.isVisible
|
import androidx.core.view.isVisible
|
||||||
import androidx.navigation.NavController
|
import androidx.navigation.NavController
|
||||||
@@ -50,21 +48,6 @@ class MainActivity : AppCompatActivity(R.layout.main_activity) {
|
|||||||
binding.navigationView.setNavigationItemSelectedListener(::onNavigationItemSelected)
|
binding.navigationView.setNavigationItemSelectedListener(::onNavigationItemSelected)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onNewIntent(intent: Intent?) {
|
|
||||||
super.onNewIntent(intent)
|
|
||||||
logger.v { "onNewIntent() called with: intent = $intent" }
|
|
||||||
when (intent?.action) {
|
|
||||||
Intent.ACTION_SEARCH -> onNewSearch(intent)
|
|
||||||
else -> logger.w { "Unexpected intent!" }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun onNewSearch(intent: Intent) {
|
|
||||||
logger.v { "onNewSearch() called with: intent = $intent" }
|
|
||||||
val query = intent.getStringExtra(SearchManager.QUERY)
|
|
||||||
viewModel.onSearchQuery(query)
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun configureNavGraph() {
|
private fun configureNavGraph() {
|
||||||
logger.v { "configureNavGraph() called" }
|
logger.v { "configureNavGraph() called" }
|
||||||
viewModel.startDestination.observeOnce(this) {
|
viewModel.startDestination.observeOnce(this) {
|
||||||
@@ -126,16 +109,35 @@ class MainActivity : AppCompatActivity(R.layout.main_activity) {
|
|||||||
menu.findItem(R.id.login).isVisible = uiState.canShowLogin
|
menu.findItem(R.id.login).isVisible = uiState.canShowLogin
|
||||||
val searchItem = menu.findItem(R.id.search_recipe_action)
|
val searchItem = menu.findItem(R.id.search_recipe_action)
|
||||||
searchItem.isVisible = uiState.searchVisible
|
searchItem.isVisible = uiState.searchVisible
|
||||||
val searchManager: SearchManager? = getSystemService()
|
setupSearchItem(searchItem)
|
||||||
val searchView = searchItem.actionView as? SearchView
|
|
||||||
if (searchManager != null && searchView != null) {
|
|
||||||
searchView.setSearchableInfo(searchManager.getSearchableInfo(componentName))
|
|
||||||
} else {
|
|
||||||
logger.e { "onCreateOptionsMenu: either search manager or search view is null" }
|
|
||||||
}
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun setupSearchItem(searchItem: MenuItem) {
|
||||||
|
logger.v { "setupSearchItem() called with: searchItem = $searchItem" }
|
||||||
|
val searchView = searchItem.actionView as? SearchView
|
||||||
|
if (searchView == null) {
|
||||||
|
logger.e { "onCreateOptionsMenu: search item's actionView is null or not SearchView" }
|
||||||
|
return
|
||||||
|
}
|
||||||
|
searchView.queryHint = getString(R.string.searchable_recipe_main_hint)
|
||||||
|
searchView.setOnCloseListener {
|
||||||
|
logger.v { "onClose() called" }
|
||||||
|
viewModel.onSearchQuery(null)
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
searchView.setOnQueryTextListener(object : OnQueryTextListener {
|
||||||
|
override fun onQueryTextSubmit(query: String?): Boolean = true
|
||||||
|
|
||||||
|
override fun onQueryTextChange(newText: String?): Boolean {
|
||||||
|
logger.v { "onQueryTextChange() called with: newText = $newText" }
|
||||||
|
viewModel.onSearchQuery(newText?.trim()?.takeUnless { it.isEmpty() })
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||||
logger.v { "onOptionsItemSelected() called with: item = $item" }
|
logger.v { "onOptionsItemSelected() called with: item = $item" }
|
||||||
val result = when (item.itemId) {
|
val result = when (item.itemId) {
|
||||||
|
|||||||
Reference in New Issue
Block a user