Skip to content

Commit 2bcfd5c

Browse files
Jim ClermontsJim Clermonts
authored andcommitted
-= 1.1.3. =- (42)
1 parent 1810666 commit 2bcfd5c

6 files changed

Lines changed: 28 additions & 12 deletions

File tree

app/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ android {
4343
applicationId "eu.theappfactory.dailyrecipes"
4444
minSdkVersion 23
4545
targetSdkVersion 33
46-
versionCode 41
46+
versionCode 42
4747
versionName "1.1.3"
4848
resConfigs "nl"
4949

app/src/main/java/eu/theappfactory/dailyrecipes/data/ingredients/FirestoreIngredientsDataSource.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class FirestoreIngredientsDataSource(private val firestore: FirebaseFirestore) :
6666
if (it.isSuccessful.not()) {
6767
Timber.d("Did not add ingredient item.")
6868
}
69+
items.add(item)
6970
}.addOnFailureListener { it.printStackTrace() }
7071
}
7172

app/src/main/java/eu/theappfactory/dailyrecipes/data/ingredients/IngredientsRepository.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,9 @@ class DefaultIngredientsRepository @Inject constructor(
3939
// Show quick cached version
4040
val localIngredients = getLocalIngredients(ingredientsUrls!!)
4141
Timber.i("emitting cached")
42-
emit(localIngredients)
42+
if (localIngredients.isNotEmpty()) {
43+
emit(localIngredients)
44+
}
4345

4446
// First get the actual ones from the remote.
4547
if (ingredientsUrls != null) {

app/src/main/java/eu/theappfactory/dailyrecipes/ui/ingredients/AddIngredientsActivity.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,17 @@ class AddIngredientsActivity : BaseActivity() {
6262
}
6363
}
6464

65-
override fun onPause() {
66-
super.onPause()
65+
override fun onDestroy() {
66+
super.onDestroy()
6767
viewModel.resetSelectedStates()
6868
}
6969

7070
private fun initViewModel() {
7171
viewModel.apply {
7272
load(intent.getStringExtra(EXTRA_ID).orEmpty())
7373

74-
onIngredientsUpdate.observe(this@AddIngredientsActivity) {
75-
updateView(it)
74+
onIngredientsUpdate.observe(this@AddIngredientsActivity) { ingredients ->
75+
updateView(ingredients)
7676
}
7777

7878
onSaveSuccess.observe(this@AddIngredientsActivity) {

app/src/main/java/eu/theappfactory/dailyrecipes/ui/ingredients/AddIngredientsViewModel.kt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import eu.theappfactory.dailyrecipes.networking.successOr
2020
import kotlinx.coroutines.ExperimentalCoroutinesApi
2121
import kotlinx.coroutines.InternalCoroutinesApi
2222
import kotlinx.coroutines.flow.first
23+
import kotlinx.coroutines.flow.toList
2324
import kotlinx.coroutines.launch
2425
import timber.log.Timber
2526
import javax.inject.Inject
@@ -50,7 +51,7 @@ class AddIngredientsViewModel @Inject constructor(
5051
private val _onShowSaveButton = MutableLiveData<Boolean>()
5152
val onShowSaveButton: LiveData<Boolean> = _onShowSaveButton
5253

53-
private val _onIngredientsUpdate = MutableLiveData<ArrayList<IngredientItemData>>()
54+
private val _onIngredientsUpdate = MutableLiveData<List<IngredientItemData>>()
5455
val onIngredientsUpdate = _onIngredientsUpdate.map {
5556
val sortedList = it.orEmpty().sortedBy { it.title }
5657
sortedList
@@ -72,8 +73,14 @@ class AddIngredientsViewModel @Inject constructor(
7273

7374
when (result) {
7475
is Result.Success -> {
75-
ingredientsForRecipe = result.data.first() as ArrayList<IngredientItemData>
76-
_onIngredientsUpdate.postValue(ingredientsForRecipe)
76+
if (result.data.toList().isNotEmpty()) {
77+
ingredientsForRecipe = result.data.first() as ArrayList<IngredientItemData>
78+
// In the add ingredient screen, the items are selected by default
79+
addCheckmark()
80+
_onIngredientsUpdate.postValue(ingredientsForRecipe)
81+
} else {
82+
_onIngredientsUpdate.postValue(arrayListOf())
83+
}
7784
}
7885
is Result.Error -> {
7986
Timber.e("Something went wrong.")
@@ -83,6 +90,12 @@ class AddIngredientsViewModel @Inject constructor(
8390
}
8491
}
8592

93+
private fun addCheckmark() {
94+
for (ingredient in ingredientsForRecipe) {
95+
ingredient.isSelected = true
96+
}
97+
}
98+
8699
fun searchIngredients(searchQuery: String) = viewModelScope.launch {
87100
currentSearch = searchQuery
88101

app/src/main/java/eu/theappfactory/dailyrecipes/ui/ingredients/IngredientsActivity.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@ class IngredientsActivity : BaseActivity() {
117117
}
118118

119119
private fun updateView(ingredients: List<IngredientItemData>, index: Int) {
120-
if (ingredients.any { it.isSelected }) {
121-
binding.insertIngredientsButton.visibility = View.VISIBLE
122-
} else {
120+
if (ingredients.all { it.optional == true }) {
123121
binding.insertIngredientsButton.visibility = View.GONE
122+
} else {
123+
binding.insertIngredientsButton.visibility = View.VISIBLE
124124
}
125125

126126
adapter.items = ingredients

0 commit comments

Comments
 (0)