Use the id to identify a recipe #42

Merged
MicMun merged 2 commits from leafar/nextcloud-cookbook:optimization_search into master 5 months ago
  1. 2
      app/src/main/java/de/micmun/android/nextcloudcookbook/db/DbRecipeRepository.kt
  2. 4
      app/src/main/java/de/micmun/android/nextcloudcookbook/db/RecipeDataDao.kt
  3. 2
      app/src/main/java/de/micmun/android/nextcloudcookbook/ui/recipedetail/RecipeDetailFragment.kt
  4. 4
      app/src/main/java/de/micmun/android/nextcloudcookbook/ui/recipedetail/RecipeViewModel.kt
  5. 5
      app/src/main/java/de/micmun/android/nextcloudcookbook/ui/recipedetail/RecipeViewModelFactory.kt
  6. 4
      app/src/main/java/de/micmun/android/nextcloudcookbook/ui/recipelist/RecipeListAdapter.kt
  7. 6
      app/src/main/java/de/micmun/android/nextcloudcookbook/ui/recipelist/RecipeListViewModel.kt
  8. 2
      app/src/main/java/de/micmun/android/nextcloudcookbook/ui/recipesearch/RecipeSearchFragment.kt
  9. 6
      app/src/main/java/de/micmun/android/nextcloudcookbook/ui/recipesearch/RecipeSearchViewModel.kt
  10. 5
      app/src/main/res/navigation/navigation.xml

2
app/src/main/java/de/micmun/android/nextcloudcookbook/db/DbRecipeRepository.kt

@ -43,7 +43,7 @@ class DbRecipeRepository private constructor(application: Application) {
fun getAllRecipePreviews() = mRecipeDao.getAllRecipePreviews()
fun getRecipe(name: String) = mRecipeDao.getByName(name)
fun getRecipe(id: Long) = mRecipeDao.getById(id)
fun filterCategory(sort: SortValue, category: String, recipeFilter: RecipeFilter? = null): LiveData<List<DbRecipePreview>> {
var select = "SELECT * FROM recipes WHERE recipeCategory = '${category}' "

4
app/src/main/java/de/micmun/android/nextcloudcookbook/db/RecipeDataDao.kt

@ -41,8 +41,8 @@ interface RecipeDataDao {
fun sortByTotalTime(isAsc: Boolean): LiveData<List<DbRecipePreview>>
@Transaction
@Query("SELECT * FROM recipes WHERE name = :n")
fun getByName(n: String): LiveData<DbRecipe?>
@Query("SELECT * FROM recipes WHERE id = :id")
fun getById(id: Long): LiveData<DbRecipe?>
@Transaction
@Query("SELECT * FROM recipes WHERE name = :n")

2
app/src/main/java/de/micmun/android/nextcloudcookbook/ui/recipedetail/RecipeDetailFragment.kt

@ -43,7 +43,7 @@ class RecipeDetailFragment : Fragment() {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_detail, container, false)
val args = RecipeDetailFragmentArgs.fromBundle(requireArguments())
val viewModelFactory = RecipeViewModelFactory(args.recipeName, requireActivity().application)
val viewModelFactory = RecipeViewModelFactory(args.recipeId, requireActivity().application)
viewModel = ViewModelProvider(this, viewModelFactory).get(RecipeViewModel::class.java)
binding.lifecycleOwner = this

4
app/src/main/java/de/micmun/android/nextcloudcookbook/ui/recipedetail/RecipeViewModel.kt

@ -17,8 +17,8 @@ import de.micmun.android.nextcloudcookbook.db.model.DbRecipe
* @author MicMun
* @version 1.1, 07.04.21
*/
class RecipeViewModel(name: String, application: Application) :
class RecipeViewModel(id: Long, application: Application) :
AndroidViewModel(application) {
private val repository = DbRecipeRepository.getInstance(application)
val recipe: LiveData<DbRecipe?> = repository.getRecipe(name)
val recipe: LiveData<DbRecipe?> = repository.getRecipe(id)
}

5
app/src/main/java/de/micmun/android/nextcloudcookbook/ui/recipedetail/RecipeViewModelFactory.kt

@ -8,7 +8,6 @@ package de.micmun.android.nextcloudcookbook.ui.recipedetail
import android.app.Application
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import de.micmun.android.nextcloudcookbook.db.RecipeDataDao
/**
* Factory to create RecipeViewModel.
@ -17,12 +16,12 @@ import de.micmun.android.nextcloudcookbook.db.RecipeDataDao
* @version 1.1, 21.03.21
*/
class RecipeViewModelFactory(private val name: String, private val application: Application)
class RecipeViewModelFactory(private val id: Long, private val application: Application)
: ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(RecipeViewModel::class.java)) {
@Suppress("UNCHECKED_CAST")
return RecipeViewModel(name, application) as T
return RecipeViewModel(id, application) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}

4
app/src/main/java/de/micmun/android/nextcloudcookbook/ui/recipelist/RecipeListAdapter.kt

@ -68,6 +68,6 @@ class RecipeListAdapter(private val clickListener: RecipeListListener) :
}
}
class RecipeListListener(val clickListener: (recipeName: String) -> Unit) {
fun onClick(recipe: DbRecipePreview) = clickListener(recipe.name)
class RecipeListListener(val clickListener: (recipeId: Long) -> Unit) {
fun onClick(recipe: DbRecipePreview) = clickListener(recipe.id)
}

6
app/src/main/java/de/micmun/android/nextcloudcookbook/ui/recipelist/RecipeListViewModel.kt

@ -43,12 +43,12 @@ class RecipeListViewModel(private val app: Application) : AndroidViewModel(app)
private var catFilter: CategoryFilter = CategoryFilter(CategoryFilter.CategoryFilterOption.ALL_CATEGORIES)
// navigate to recipe
private val _navigateToRecipe = MutableLiveData<String>()
private val _navigateToRecipe = MutableLiveData<Long>()
val navigateToRecipe
get() = _navigateToRecipe
fun onRecipeClicked(name: String) {
_navigateToRecipe.value = name
fun onRecipeClicked(id: Long) {
_navigateToRecipe.value = id
}
fun onRecipeNavigated() {

2
app/src/main/java/de/micmun/android/nextcloudcookbook/ui/recipesearch/RecipeSearchFragment.kt

@ -94,7 +94,7 @@ class RecipeSearchFragment : Fragment() {
// data adapter
val adapter =
RecipeListAdapter(RecipeListListener { recipeName -> recipeSearchViewModel.onRecipeClicked(recipeName) })
RecipeListAdapter(RecipeListListener { recipeId -> recipeSearchViewModel.onRecipeClicked(recipeId) })
binding.recipeResultList.adapter = adapter
adapter.stateRestorationPolicy = RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY

6
app/src/main/java/de/micmun/android/nextcloudcookbook/ui/recipesearch/RecipeSearchViewModel.kt

@ -69,12 +69,12 @@ class RecipeSearchViewModel(application: Application) :
applyFilter = filter
}
private val _navigateToRecipe = MutableLiveData<String>()
private val _navigateToRecipe = MutableLiveData<Long>()
val navigateToRecipe
get() = _navigateToRecipe
fun onRecipeClicked(name: String) {
_navigateToRecipe.value = name
fun onRecipeClicked(id: Long) {
_navigateToRecipe.value = id
}
fun onRecipeNavigated() {

5
app/src/main/res/navigation/navigation.xml

@ -25,9 +25,8 @@
android:name="de.micmun.android.nextcloudcookbook.ui.recipedetail.RecipeDetailFragment"
android:label="fragment_detail">
<argument
android:name="recipeName"
android:defaultValue="-"
app:argType="string" />
android:name="recipeId"
app:argType="long" />
</fragment>
<fragment
android:id="@+id/recipeSearchFragment"

Loading…
Cancel
Save