Skip to content

Commit

Permalink
fix: remove runBlocking in ProductListAdapter.kt
Browse files Browse the repository at this point in the history
Closes #4123
  • Loading branch information
VaiTon committed Aug 3, 2021
1 parent 7c195cf commit bc2d218
Showing 1 changed file with 40 additions and 32 deletions.
Expand Up @@ -138,9 +138,16 @@ class ProductEditNutritionFactsFragment : ProductEditFragment() {
}


fun Spinner.setOnItemSelectedListener(block: (parent: AdapterView<*>?, view: View, position: Int, id: Long) -> Unit) {
private fun Spinner.setOnItemSelectedListener(
block: (
parent: AdapterView<*>?,
view: View?,
position: Int,
id: Long
) -> Unit
) {
this.onItemSelectedListener = object : OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>?, view: View, position: Int, id: Long) =
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) =
block(parent, view, position, id)

override fun onNothingSelected(parent: AdapterView<*>?) = Unit // This is not possible
Expand Down Expand Up @@ -334,20 +341,21 @@ class ProductEditNutritionFactsFragment : ProductEditFragment() {
binding.imageProgress.visibility = View.VISIBLE
loadNutritionImage(path)
}

if (productDetails[ApiFields.Keys.NO_NUTRITION_DATA] != null) {
binding.checkboxNoNutritionData.isChecked = true
binding.nutritionFactsLayout.visibility = View.GONE
}
if (productDetails[ApiFields.Keys.NUTRITION_DATA_PER] != null) {
val nutritionDataPer = productDetails[ApiFields.Keys.NUTRITION_DATA_PER]

productDetails[ApiFields.Keys.NUTRITION_DATA_PER]?.let { nutritionDataPer ->
// can be "100g" or "serving"
updateSelectedDataPer(nutritionDataPer!!)
updateSelectedDataPer(nutritionDataPer)
}
val servingSize = productDetails[ApiFields.Keys.SERVING_SIZE]
if (servingSize != null) {
productDetails[ApiFields.Keys.SERVING_SIZE]?.let {
// Splits the serving size into value and unit. Example: "15g" into "15" and "g"
updateServingSizeFrom(servingSize)
updateServingSizeFrom(it)
}

(binding.root as ViewGroup).getViewsByType(CustomValidatingEditTextView::class.java).forEach { view ->
val nutrientShortName = view.entryName
if (nutrientShortName == binding.servingSize.entryName) {
Expand All @@ -361,21 +369,21 @@ class ProductEditNutritionFactsFragment : ProductEditFragment() {
}
}
//set the values of all the other nutrients if defined and create new row in the tableLayout.
PARAMS_OTHER_NUTRIENTS.withIndex().forEach { (i, completeNutrientName) ->
if (productDetails[completeNutrientName] != null) {
var unitIndex = 0
var modIndex = 0
val value = productDetails[completeNutrientName]
if (productDetails[completeNutrientName + ApiFields.Suffix.UNIT] != null) {
unitIndex = getPositionInAllUnitArray(productDetails[completeNutrientName + ApiFields.Suffix.UNIT])
}
if (productDetails[completeNutrientName + ApiFields.Suffix.MODIFIER] != null) {
modIndex = getPositionInAllUnitArray(productDetails[completeNutrientName + ApiFields.Suffix.MODIFIER])
}
usedNutrientsIndexes.add(i)
val nutrients = resources.getStringArray(R.array.nutrients_array)
addNutrientRow(i, nutrients[i], true, value, unitIndex, modIndex)
for ((i, completeNutrientName) in PARAMS_OTHER_NUTRIENTS.withIndex()) {
if (productDetails[completeNutrientName] == null) continue

var unitIndex = 0
var modIndex = 0
val value = productDetails[completeNutrientName]
if (productDetails[completeNutrientName + ApiFields.Suffix.UNIT] != null) {
unitIndex = getPositionInAllUnitArray(productDetails[completeNutrientName + ApiFields.Suffix.UNIT])
}
if (productDetails[completeNutrientName + ApiFields.Suffix.MODIFIER] != null) {
modIndex = getPositionInAllUnitArray(productDetails[completeNutrientName + ApiFields.Suffix.MODIFIER])
}
usedNutrientsIndexes.add(i)
val nutrients = resources.getStringArray(R.array.nutrients_array)
addNutrientRow(i, nutrients[i], true, value, unitIndex, modIndex)
}
}

Expand Down Expand Up @@ -466,15 +474,15 @@ class ProductEditNutritionFactsFragment : ProductEditFragment() {
}

private fun CustomValidatingEditTextView.checkValue(value: Float) = sequenceOf(
this.checkPh(value),
this.checkAlcohol(value),
this.checkEnergyField(value),
this.checkCarbohydrate(value),
this.checkPerServing()
checkPh(value),
checkAlcohol(value),
checkEnergyField(value),
checkCarbohydrate(value),
checkPerServing()
).firstOrNull { it != ValueState.NOT_TESTED } ?: this.checkAsGram(value)

private fun CustomValidatingEditTextView.checkAsGram(value: Float): ValueState {
val valid = Companion.convertToGrams(value, unitSpinner!!.selectedItemPosition) <= referenceValueInGram
val valid = convertToGrams(value, unitSpinner!!.selectedItemPosition) <= referenceValueInGram
return if (!valid) {
this.showError(getString(R.string.max_nutrient_val_msg))
ValueState.NOT_VALID
Expand Down Expand Up @@ -597,7 +605,7 @@ class ProductEditNutritionFactsFragment : ProductEditFragment() {
/**
* Add nutrients to the map by from the text entered into EditText, only if the value has been edited
*
* @param editTextView EditText with spinner for entering the nutients
* @param editTextView EditText with spinner for entering the nutrients
* @param targetMap map to enter the nutrient value recieved from edit texts
*/
private fun addNutrientToMapIfUpdated(
Expand Down Expand Up @@ -825,10 +833,10 @@ class ProductEditNutritionFactsFragment : ProductEditFragment() {

// Check that value of (sugar + starch) is not greater than value of carbohydrates
// Convert all the values to grams
carbsValue = Companion.convertToGrams(carbsValue, binding.carbohydrates.unitSpinner!!.selectedItemPosition)
sugarValue = Companion.convertToGrams(sugarValue, binding.sugars.unitSpinner!!.selectedItemPosition)
carbsValue = convertToGrams(carbsValue, binding.carbohydrates.unitSpinner!!.selectedItemPosition)
sugarValue = convertToGrams(sugarValue, binding.sugars.unitSpinner!!.selectedItemPosition)

val newStarch = Companion.convertToGrams(starchValue, starchUnitSelectedIndex).toDouble()
val newStarch = convertToGrams(starchValue, starchUnitSelectedIndex).toDouble()

return if (sugarValue + newStarch > carbsValue) {
binding.carbohydrates.showError(getString(R.string.error_in_carbohydrate_value))
Expand Down

0 comments on commit bc2d218

Please sign in to comment.