Send null queries to ViewModel

This commit is contained in:
Kirill Kamakin
2022-11-13 10:10:45 +01:00
parent 5a874e4899
commit 0db76155a5
4 changed files with 17 additions and 9 deletions

View File

@@ -5,6 +5,7 @@ import gq.kirmanak.mealient.database.recipe.entity.FullRecipeEntity
import gq.kirmanak.mealient.database.recipe.entity.RecipeSummaryEntity
interface RecipeRepo {
fun createPager(): Pager<Int, RecipeSummaryEntity>
suspend fun clearLocalData()
@@ -13,5 +14,5 @@ interface RecipeRepo {
suspend fun loadRecipeInfo(recipeId: String): FullRecipeEntity?
fun setSearchName(name: String?)
fun updateNameQuery(name: String?)
}

View File

@@ -56,8 +56,8 @@ class RecipeRepoImpl @Inject constructor(
return recipeInfo
}
override fun setSearchName(name: String?) {
logger.v { "setSearchName() called with: name = $name" }
override fun updateNameQuery(name: String?) {
logger.v { "updateNameQuery() called with: name = $name" }
pagingSourceFactory.setQuery(name)
invalidatingPagingSourceFactory.invalidate()
}

View File

@@ -52,14 +52,21 @@ class MainActivity : AppCompatActivity(R.layout.main_activity) {
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
if (Intent.ACTION_SEARCH == intent?.action) {
intent.getStringExtra(SearchManager.QUERY)?.also { query ->
viewModel.onSearchQuery(query)
}
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() {
logger.v { "configureNavGraph() called" }
viewModel.startDestination.observeOnce(this) {
logger.d { "configureNavGraph: received destination" }
val graph = navController.navInflater.inflate(R.navigation.nav_graph)

View File

@@ -55,8 +55,8 @@ class MainActivityViewModel @Inject constructor(
viewModelScope.launch { authRepo.logout() }
}
fun onSearchQuery(query: String) {
fun onSearchQuery(query: String?) {
logger.v { "onSearchQuery() called with: query = $query" }
recipeRepo.setSearchName(query)
recipeRepo.updateNameQuery(query)
}
}