Start search implementation
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
package gq.kirmanak.mealient.ui.activity
|
||||
|
||||
import android.app.SearchManager
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.view.Menu
|
||||
import android.view.MenuItem
|
||||
import androidx.activity.viewModels
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.appcompat.widget.SearchView
|
||||
import androidx.core.content.getSystemService
|
||||
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
||||
import androidx.core.view.isVisible
|
||||
import androidx.navigation.NavController
|
||||
@@ -46,6 +50,15 @@ class MainActivity : AppCompatActivity(R.layout.main_activity) {
|
||||
binding.navigationView.setNavigationItemSelectedListener(::onNavigationItemSelected)
|
||||
}
|
||||
|
||||
override fun onNewIntent(intent: Intent?) {
|
||||
super.onNewIntent(intent)
|
||||
if (Intent.ACTION_SEARCH == intent?.action) {
|
||||
intent.getStringExtra(SearchManager.QUERY)?.also { query ->
|
||||
viewModel.onSearchQuery(query)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun configureNavGraph() {
|
||||
viewModel.startDestination.observeOnce(this) {
|
||||
logger.d { "configureNavGraph: received destination" }
|
||||
@@ -104,6 +117,15 @@ class MainActivity : AppCompatActivity(R.layout.main_activity) {
|
||||
menuInflater.inflate(R.menu.main_toolbar, menu)
|
||||
menu.findItem(R.id.logout).isVisible = uiState.canShowLogout
|
||||
menu.findItem(R.id.login).isVisible = uiState.canShowLogin
|
||||
val searchItem = menu.findItem(R.id.search_recipe_action)
|
||||
searchItem.isVisible = uiState.searchVisible
|
||||
val searchManager: SearchManager? = getSystemService()
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ data class MainActivityUiState(
|
||||
val titleVisible: Boolean = true,
|
||||
val isAuthorized: Boolean = false,
|
||||
val navigationVisible: Boolean = false,
|
||||
val searchVisible: Boolean = false,
|
||||
) {
|
||||
val canShowLogin: Boolean
|
||||
get() = !isAuthorized && loginButtonVisible
|
||||
|
||||
@@ -52,4 +52,8 @@ class MainActivityViewModel @Inject constructor(
|
||||
logger.v { "logout() called" }
|
||||
viewModelScope.launch { authRepo.logout() }
|
||||
}
|
||||
|
||||
fun onSearchQuery(query: String) {
|
||||
logger.v { "onSearchQuery() called with: query = $query" }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user