Skip to content

Commit

Permalink
fix: query attribute_groups only for the actual UI language.
Browse files Browse the repository at this point in the history
Closes #3857
  • Loading branch information
VaiTon committed Feb 27, 2021
1 parent 0f23183 commit 6b7cb63
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 126 deletions.
@@ -1,7 +1,6 @@
package openfoodfacts.github.scrachx.openfood.features.product.view.serverattributes

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -29,8 +28,6 @@ class ServerAttributesFragment : BaseFragment() {
super.onViewCreated(view, savedInstanceState)

val attributeGroups = productState.product!!.getLocalAttributeGroups(requireContext())
Log.i("ServerAttributes", attributeGroups.toString())

binding.attrsList.setAdapter(AttributeGroupsAdapter(attributeGroups, requireActivity()))
}

Expand Down
Expand Up @@ -13,6 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("MemberVisibilityCanBePrivate")

package openfoodfacts.github.scrachx.openfood.network

import openfoodfacts.github.scrachx.openfood.models.ProductImageField
Expand Down Expand Up @@ -163,6 +165,9 @@ object ApiFields {
ProductImageField.NUTRITION,
ProductImageField.PACKAGING
)
const val LANGUAGES_CODES = "languages_codes"
const val URL = "url"
const val ALLERGENS = "allergens"

@JvmStatic
@Contract(pure = true)
Expand All @@ -171,5 +176,101 @@ object ApiFields {
@JvmStatic
@Contract(pure = true)
fun lcIngredientsKey(lang: String) = "${Prefix.INGREDIENTS_TEXT}$lang"

const val NUTRITION_GRADES_TAGS = "nutrition_grades_tags"

const val OTHER = "other"

val PRODUCT_COMMON_FIELDS = setOf(
PRODUCT_NAME,
GENERIC_NAME,
IMAGE_SMALL_URL,
IMAGE_FRONT_URL,
IMAGE_INGREDIENTS_URL,
IMAGE_NUTRITION_URL,
IMAGE_PACKAGING_URL,
IMAGE_URL,
SELECTED_IMAGES,
LANGUAGES_CODES,
VITAMINS_TAGS,
MINERALS_TAGS,
AMINO_ACIDS_TAGS,
OTHER_NUTRITIONAL_SUBSTANCES_TAGS,
URL,
BARCODE,
TRACES_TAGS,
INGREDIENTS_MAY_PALM_OIL_TAGS,
BRANDS_TAGS,
TRACES,
CATEGORIES_TAGS,
INGREDIENTS_TEXT,
INGREDIENTS_FROM_PALM_OIL_TAGS,
SERVING_SIZE,
ALLERGENS_TAGS,
ALLERGENS,
ORIGINS,
STORES,
NUTRITION_GRADE_FR,
NUTRITION_GRADES_TAGS,
NUTRIENT_LEVELS,
ECOSCORE,
COUNTRIES,
COUNTRIES_TAGS,
BRANDS,
PACKAGING,
LABELS_TAGS,
LABELS_HIERARCHY,
CITIES_TAGS,
QUANTITY,
INGREDIENTS_PALM_OIL_N,
LINK,
EMB_CODES_TAGS,
STATES_TAGS,
CREATOR,
CREATED_DATE_TIME,
LAST_MODIFIED_TIME,
LAST_MODIFIED_BY,
EDITORS_TAGS,
NOVA_GROUPS,
LANG,
PURCHASE_PLACES,
NUTRITION_DATA_PER,
NO_NUTRITION_DATA,
OTHER,
OTHER_INFORMATION,
CONSERVATION_CONDITIONS,
RECYCLING_INSTRUCTIONS_TO_DISCARD,
RECYCLING_INSTRUCTIONS_TO_RECYCLE,
WARNING,
CUSTOMER_SERVICE,
ENVIRONMENT_INFOCARD,
ENVIRONMENT_IMPACT_LEVEL_TAGS,
INGREDIENTS_ANALYSIS_TAGS,
INGREDIENTS,
STATES_TAGS
)

val PRODUCT_LOCAL_FIELDS = setOf(
PRODUCT_NAME to true,
GENERIC_NAME to true,
INGREDIENTS_TEXT to true,
OTHER_INFORMATION to true,
CONSERVATION_CONDITIONS to true,
RECYCLING_INSTRUCTIONS_TO_DISCARD to true,
RECYCLING_INSTRUCTIONS_TO_RECYCLE to true,
WARNING to true,
ATTRIBUTE_GROUPS to false,
CUSTOMER_SERVICE to true,
IMAGE_FRONT_URL to true,
IMAGE_INGREDIENTS_URL to true,
IMAGE_NUTRITION_URL to true,
IMAGE_PACKAGING_URL to true
)

val PRODUCT_IMAGES_FIELDS = setOf(
PRODUCT_NAME, GENERIC_NAME, BARCODE, LANG, IMAGE_SMALL_URL, IMAGES,
IMAGE_FRONT_URL, IMAGE_INGREDIENTS_URL, IMAGE_NUTRITION_URL,
IMAGE_PACKAGING_URL, IMAGE_URL, SELECTED_IMAGES
)
}
}
Expand Up @@ -8,7 +8,6 @@ import android.widget.Toast
import androidx.core.content.edit
import com.afollestad.materialdialogs.MaterialDialog
import com.fasterxml.jackson.databind.JsonNode
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import io.reactivex.Completable
import io.reactivex.Single
import io.reactivex.disposables.CompositeDisposable
Expand All @@ -34,15 +33,9 @@ import openfoodfacts.github.scrachx.openfood.models.entities.OfflineSavedProduct
import openfoodfacts.github.scrachx.openfood.models.entities.ToUploadProduct
import openfoodfacts.github.scrachx.openfood.models.entities.ToUploadProductDao
import openfoodfacts.github.scrachx.openfood.network.CommonApiManager.productsApi
import openfoodfacts.github.scrachx.openfood.network.services.ProductsAPI
import openfoodfacts.github.scrachx.openfood.utils.*
import openfoodfacts.github.scrachx.openfood.utils.LocaleHelper.getLanguage
import openfoodfacts.github.scrachx.openfood.utils.Utils.daoSession
import openfoodfacts.github.scrachx.openfood.utils.Utils.defaultHttpClient
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.jackson.JacksonConverterFactory
import retrofit2.converter.scalars.ScalarsConverterFactory
import java.io.File
import java.io.IOException
import java.util.*
Expand All @@ -60,14 +53,14 @@ class OpenFoodAPIClient(private val context: Context) {
}

private fun getAllFields(): String {
val allFields = context.resources.getStringArray(R.array.product_all_fields_array)
val fieldsToLocalize = context.resources.getStringArray(R.array.fields_array)
val langCode = getLanguage(context)
val allFields = ApiFields.Keys.PRODUCT_COMMON_FIELDS
val fieldsToLocalize = ApiFields.Keys.PRODUCT_LOCAL_FIELDS

val langCode = getLanguage(context)
val fieldsSet = allFields.toMutableSet()
fieldsToLocalize.forEach { fieldToLocalize ->
fieldsSet.add("${fieldToLocalize}_$langCode")
fieldsSet.add("${fieldToLocalize}_en")
fieldsToLocalize.forEach { (field, shouldAddEn) ->
fieldsSet.add("${field}_$langCode")
if (shouldAddEn) fieldsSet.add("${field}_en")
}
return fieldsSet.joinToString(",")
}
Expand Down Expand Up @@ -95,10 +88,9 @@ class OpenFoodAPIClient(private val context: Context) {
* @param barcode product barcode
*/
fun getProductImages(barcode: String): Single<ProductState> {
val fields = setOf(
*context.resources.getStringArray(R.array.product_images_fields_array),
"product_name_${getLanguage(context)}"
).joinToString(",")
val fields = ApiFields.Keys.PRODUCT_IMAGES_FIELDS.toMutableSet().also {
it += ApiFields.Keys.lcProductNameKey(getLanguage(context))
}.joinToString(",")
return productsApi.getProductByBarcode(
barcode,
fields,
Expand Down
106 changes: 0 additions & 106 deletions app/src/main/res/values/strings.xml
Expand Up @@ -310,112 +310,6 @@
</string-array>


<string-array name="product_images_fields_array" translatable="false">
<item>product_name</item>
<item>generic_name</item>
<item>code</item>
<item>lang</item>
<item>image_small_url</item>
<item>images</item>
<item>image_front_url</item>
<item>image_ingredients_url</item>
<item>image_nutrition_url</item>
<item>image_packaging_url</item>
<item>image_url</item>
<item>selected_images</item>
</string-array>


<string-array name="product_all_fields_array" translatable="false">
<item>product_name</item>
<item>generic_name</item>
<item>image_small_url</item>
<item>image_front_url</item>
<item>image_ingredients_url</item>
<item>image_nutrition_url</item>
<item>image_packaging_url</item>
<item>image_url</item>
<item>selected_images</item>
<item>languages_codes</item>
<item>vitamins_tags</item>
<item>minerals_tags</item>
<item>amino_acids_tags</item>
<item>other_nutritional_substances_tags</item>
<item>url</item>
<item>code</item>
<item>traces_tags</item>
<item>ingredients_that_may_be_from_palm_oil_tags</item>
<item>additives_tags</item>
<item>allergens_hierarchy</item>
<item>manufacturing_places</item>
<item>nutriments</item>
<item>ingredients_from_palm_oil_tags</item>
<item>brands_tags</item>
<item>traces</item>
<item>categories_tags</item>
<item>ingredients_text</item>
<item>ingredients_from_or_that_may_be_from_palm_oil_n</item>
<item>serving_size</item>
<item>allergens_tags</item>
<item>allergens</item>
<item>origins</item>
<item>stores</item>
<item>nutrition_grade_fr</item>
<item>nutrition_grades_tags</item>
<item>nutrient_levels</item>
<item>ecoscore_grade</item>
<item>countries</item>
<item>countries_tags</item>
<item>brands</item>
<item>packaging</item>
<item>labels_tags</item>
<item>labels_hierarchy</item>
<item>cities_tags</item>
<item>quantity</item>
<item>ingredients_from_palm_oil_n</item>
<item>link</item>
<item>emb_codes_tags</item>
<item>states_tags</item>
<item>creator</item>
<item>created_t</item>
<item>last_modified_t</item>
<item>last_modified_by</item>
<item>editors_tags</item>
<item>nova_groups</item>
<item>lang</item>
<item>purchase_places</item>
<item>nutrition_data_per</item>
<item>no_nutrition_data</item>
<item>other</item>
<item>other_information</item>
<item>conservation_conditions</item>
<item>recycling_instructions_to_discard</item>
<item>recycling_instructions_to_recycle</item>
<item>warning</item>
<item>customer_service</item>
<item>environment_infocard</item>
<item>environment_impact_level_tags</item>
<item>ingredients_analysis_tags</item>
<item>ingredients</item>
<item>states_tags</item>
</string-array>
<string-array name="fields_array" translatable="false">
<item>product_name</item>
<item>generic_name</item>
<item>ingredients_text</item>
<item>other_information</item>
<item>conservation_conditions</item>
<item>recycling_instructions_to_discard</item>
<item>recycling_instructions_to_recycle</item>
<item>warning</item>
<item>attribute_groups</item>
<item>customer_service</item>
<item>image_front_url</item>
<item>image_ingredients_url</item>
<item>image_nutrition_url</item>
<item>image_packaging_url</item>
</string-array>

<string name="help_translate_ingredients_link" translatable="false">https://world-%1$s.openfoodfacts.org/ingredients?translate=1</string>

<!-- Content Description -->
Expand Down

0 comments on commit 6b7cb63

Please sign in to comment.