Skip to content

Commit

Permalink
fix: typo in variable usage in EditIngredientsFragment.kt
Browse files Browse the repository at this point in the history
design: moved the "extract ingredients button" to crea

Closes #4329
  • Loading branch information
VaiTon committed Oct 26, 2021
1 parent 43b933f commit 389bf33
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 46 deletions.
Expand Up @@ -24,7 +24,7 @@ import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.core.net.toFile
import androidx.core.view.isGone
import androidx.core.view.isVisible
import androidx.core.widget.doAfterTextChanged
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
Expand Down Expand Up @@ -89,27 +89,42 @@ class EditIngredientsFragment : ProductEditFragment() {
@Inject
lateinit var localeManager: LocaleManager

/**
* Executed when an image is returned from the camera
*/
private val photoReceiverHandler by lazy {
PhotoReceiverHandler(sharedPreferences) {
val uri = it.toURI()
imagePath = uri.path
newImageSelected = true
photoFile = it
val image = ProductImage(code!!, ProductImageField.INGREDIENTS, it, localeManager.getLanguage()).apply {
val image = ProductImage(
code!!,
ProductImageField.INGREDIENTS,
it,
localeManager.getLanguage()
).apply {
filePath = uri.path
}

(activity as? ProductEditActivity)?.savePhoto(image, 1)
matomoAnalytics.trackEvent(AnalyticsEvent.ProductIngredientsPictureEdited(code))

// Change UI state
hideImageProgress(false, getString(R.string.image_uploaded_successfully))

// Analytics
matomoAnalytics.trackEvent(AnalyticsEvent.ProductIngredientsPictureEdited(code))
}
}

private var photoFile: File? = null
private var code: String? = null
private var mOfflineSavedProduct: OfflineSavedProduct? = null

private var offlineProduct: OfflineSavedProduct? = null
private var product: Product? = null
private var productDetails = mutableMapOf<String, String?>()

private var imagePath: String? = null
private var product: Product? = null
private var newImageSelected = false


Expand All @@ -123,10 +138,11 @@ class EditIngredientsFragment : ProductEditFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

val intent = if (activity == null) null else requireActivity().intent
if (intent != null
&& intent.getBooleanExtra(ProductEditActivity.KEY_MODIFY_NUTRITION_PROMPT, false)
&& !intent.getBooleanExtra(ProductEditActivity.KEY_MODIFY_CATEGORY_PROMPT, false)
// FIXME: DO NOT USE INTENTS IN FRAGMENTS
val activityIntent = activity?.intent
if (activityIntent != null
&& activityIntent.getBooleanExtra(ProductEditActivity.KEY_MODIFY_NUTRITION_PROMPT, false)
&& !activityIntent.getBooleanExtra(ProductEditActivity.KEY_MODIFY_CATEGORY_PROMPT, false)
) {
(activity as ProductEditActivity).proceed()
}
Expand All @@ -137,7 +153,9 @@ class EditIngredientsFragment : ProductEditFragment() {
binding.btnLooksGood.setOnClickListener { verifyIngredients() }
binding.btnSkipIngredients.setOnClickListener { skipIngredients() }
binding.btnExtractIngredients.setOnClickListener { extractIngredients() }
binding.ingredientsList.doAfterTextChanged { toggleOCRButtonVisibility() }
binding.ingredientsList.doAfterTextChanged { newText ->
binding.btnExtractIngredients.isVisible = newText.isNullOrEmpty()
}

val bundle = arguments
if (bundle == null) {
Expand All @@ -148,18 +166,20 @@ class EditIngredientsFragment : ProductEditFragment() {
}

product = getProductFromArgs()
offlineProduct = getEditOfflineProductFromArgs()

mOfflineSavedProduct = getEditOfflineProductFromArgs()
if (product != null) {
code = product!!.code
}

if (isEditingFromArgs && product != null) {
code = product!!.code
preFillProductValues(product!!)
} else if (mOfflineSavedProduct != null) {
code = mOfflineSavedProduct!!.barcode
preFillValuesForOffline(mOfflineSavedProduct!!)

} else if (offlineProduct != null) {
code = offlineProduct!!.barcode
preFillValuesForOffline(offlineProduct!!)

} else {
// Fast addition
val enabled = requireContext().isFastAdditionMode()
Expand All @@ -182,6 +202,7 @@ class EditIngredientsFragment : ProductEditFragment() {
binding.btnExtractIngredients.visibility = View.VISIBLE
}

// Allergens autosuggestion
viewModel.allergens.observe(viewLifecycleOwner) { loadAutoSuggestions(it) }

(activity as? ProductEditActivity)?.let { getAllDetails(it) }
Expand All @@ -197,7 +218,9 @@ class EditIngredientsFragment : ProductEditFragment() {
private fun getAddProductActivity() = activity as ProductEditActivity?

private fun extractTracesChipValues(product: Product?): List<String> =
product?.tracesTags?.map { getTracesName(localeManager.getLanguage(), it) } ?: emptyList()
product?.tracesTags
?.map { getTracesName(localeManager.getLanguage(), it) }
?: emptyList()

/**
* Pre fill the fields of the product which are already present on the server.
Expand All @@ -211,8 +234,7 @@ class EditIngredientsFragment : ProductEditFragment() {

product.takeUnless { it.tracesTags.isEmpty() }
?.let {
val chipValues = extractTracesChipValues(it)
binding.traces.setText(chipValues)
binding.traces.setText(extractTracesChipValues(it))
}
}

Expand Down Expand Up @@ -387,10 +409,6 @@ class EditIngredientsFragment : ProductEditFragment() {
binding.btnLooksGood.visibility = View.GONE
}

private fun toggleOCRButtonVisibility() {
binding.ingredientsList.isGone = binding.ingredientsList.isNotEmpty()
}

/**
* adds all the fields to the query map even those which are null or empty.
*/
Expand Down Expand Up @@ -490,15 +508,24 @@ class EditIngredientsFragment : ProductEditFragment() {
}

fun showOCRProgress() {
binding.btnExtractIngredients.visibility = View.GONE
// Disable extract button and ingredients text field
binding.btnExtractIngredients.isEnabled = false
binding.ingredientsList.isEnabled = false

// Delete ingredients text
binding.ingredientsList.text = null

// Show progress spinner and text
binding.ocrProgress.visibility = View.VISIBLE
binding.ocrProgressText.visibility = View.VISIBLE
}

fun hideOCRProgress() {
// Re-enable extract button and ingredients text field
binding.btnExtractIngredients.isEnabled = true
binding.ingredientsList.isEnabled = true

// Hide progress spinner and text
binding.ocrProgress.visibility = View.GONE
binding.ocrProgressText.visibility = View.GONE
}

private val dps50ToPixels by lazy { requireContext().dpsToPixel(50) }
Expand Down
54 changes: 32 additions & 22 deletions app/src/main/res/layout/fragment_add_product_ingredients.xml
Expand Up @@ -102,12 +102,13 @@
android:id="@+id/grey_line1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="@dimen/spacing_small"
android:layout_marginStart="@dimen/spacing_small"
android:layout_marginTop="@dimen/spacing_small"
android:layout_marginRight="@dimen/spacing_small"
android:layout_marginEnd="@dimen/spacing_small"
android:background="@color/grey_400"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/btnAddImageIngredients" />
app:layout_constraintTop_toBottomOf="@+id/btn_extract_ingredients" />

<TextView
android:id="@+id/section_ingredients_list"
Expand All @@ -119,6 +120,7 @@
android:layout_marginTop="@dimen/spacing_small"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:labelFor="@id/ingredients_list"
android:text="@string/ingredients_list"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
Expand Down Expand Up @@ -158,15 +160,16 @@
<Button
android:id="@+id/btn_extract_ingredients"
style="@style/ButtonBorder"
android:layout_width="wrap_content"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:text="@string/extract_ingredients"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="@id/ingredients_list"
app:layout_constraintEnd_toEndOf="@id/ingredients_list"
app:layout_constraintStart_toStartOf="@id/ingredients_list"
app:layout_constraintTop_toBottomOf="@id/traces"
app:layout_constraintTop_toTopOf="@id/ingredients_list"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/btnAddImageIngredients"
tools:visibility="visible" />

<ImageView
Expand All @@ -182,35 +185,39 @@
tools:ignore="ContentDescription" />

<ProgressBar
android:id="@+id/ocr_progress"
android:id="@+id/ocr_progress_spinner"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="@dimen/spacing_small"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="@id/ingredients_list"
app:layout_constraintBottom_toTopOf="@id/ocr_progress_text"
app:layout_constraintEnd_toEndOf="@id/ingredients_list"
app:layout_constraintStart_toStartOf="@id/ingredients_list"
app:layout_constraintTop_toTopOf="@id/ingredients_list"
tools:visibility="visible" />
app:layout_constraintVertical_chainStyle="packed" />

<TextView
android:id="@+id/ocr_progress_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_small"
android:text="@string/extracting_ingredients"
app:layout_constraintBottom_toBottomOf="@+id/ingredients_list"
app:layout_constraintEnd_toEndOf="@id/ocr_progress_spinner"
app:layout_constraintStart_toStartOf="@id/ocr_progress_spinner"
app:layout_constraintTop_toBottomOf="@id/ocr_progress_spinner" />

<androidx.constraintlayout.widget.Group
android:id="@+id/ocr_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="@id/ocr_progress"
app:layout_constraintStart_toStartOf="@id/ocr_progress"
app:layout_constraintTop_toBottomOf="@id/ocr_progress"
tools:visibility="visible" />
app:constraint_referenced_ids="ocr_progress_spinner,ocr_progress_text" />

<Button
android:id="@+id/btn_looks_good"
style="@style/ButtonFlat.Green"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_small"
android:layout_height="0dp"
android:layout_marginTop="@dimen/spacing_small"
android:layout_marginEnd="@dimen/spacing_small"
android:text="@string/looksGood"
android:textColor="@color/white"
android:visibility="gone"
Expand All @@ -225,7 +232,8 @@
style="@style/ButtonFlat.Red"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_small"
android:layout_marginStart="@dimen/spacing_small"
android:layout_marginTop="@dimen/spacing_small"
android:text="@string/skip_ingredients"
android:textColor="@color/white"
android:visibility="gone"
Expand All @@ -251,7 +259,9 @@
android:layout_height="1dp"
android:layout_marginStart="@dimen/spacing_small"
android:layout_marginTop="@dimen/spacing_small"
android:layout_marginEnd="@dimen/spacing_small"
android:background="@color/grey_400"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/btn_barrier" />

Expand Down

0 comments on commit 389bf33

Please sign in to comment.