Skip to content

Commit

Permalink
fix: display analysis tags always in the same order in Summary Produc…
Browse files Browse the repository at this point in the history
…t Fragment
  • Loading branch information
VaiTon committed Jul 27, 2021
1 parent ecc5047 commit 7cdea9c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 43 deletions.
Expand Up @@ -16,10 +16,10 @@ import openfoodfacts.github.scrachx.openfood.R
import openfoodfacts.github.scrachx.openfood.models.entities.analysistagconfig.AnalysisTagConfig

class IngredientAnalysisTagsAdapter(
private val context: Context,
private val tags: List<AnalysisTagConfig>,
private val picasso: Picasso,
private val sharedPreferences: SharedPreferences,
private val context: Context,
private val tags: List<AnalysisTagConfig>,
private val picasso: Picasso,
private val sharedPreferences: SharedPreferences,
) : RecyclerView.Adapter<IngredientAnalysisTagsAdapter.IngredientAnalysisTagsViewHolder>() {

private val visibleTags = tags.toMutableList()
Expand All @@ -37,9 +37,9 @@ class IngredientAnalysisTagsAdapter(
picasso.load(tag.iconUrl).into(holder.icon)

holder.background.background = ResourcesCompat.getDrawable(
context.resources,
R.drawable.rounded_button,
context.theme
context.resources,
R.drawable.rounded_button,
context.theme
)?.apply {
colorFilter = createBlendModeColorFilterCompat(Color.parseColor(tag.color), BlendModeCompat.SRC_IN)
}
Expand All @@ -50,8 +50,25 @@ class IngredientAnalysisTagsAdapter(
// total number of rows
override fun getItemCount() = visibleTags.count()

fun filterVisibleTags() {
visibleTags.clear()

tags.filterTo(visibleTags) {
sharedPreferences.getBoolean(it.type, true)
}.sortBy { it.id }

notifyDataSetChanged()
}

// allows clicks events to be caught
fun setOnItemClickListener(listener: (View, Int) -> Unit) {
onClickListener = listener
}

// stores and recycles views as they are scrolled off screen
inner class IngredientAnalysisTagsViewHolder internal constructor(val itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
inner class IngredientAnalysisTagsViewHolder(
val itemView: View
) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
val background = itemView
val icon: ImageView = itemView.findViewById(R.id.icon)

Expand All @@ -64,19 +81,6 @@ class IngredientAnalysisTagsAdapter(
}
}

fun filterVisibleTags() {
visibleTags.clear()
tags.filterTo(visibleTags) {
sharedPreferences.getBoolean(it.type, true)
}
notifyDataSetChanged()
}

// allows clicks events to be caught
fun setOnItemClickListener(listener: (View, Int) -> Unit) {
onClickListener = listener
}

// data is passed into the constructor
init {
filterVisibleTags()
Expand Down
Expand Up @@ -8,21 +8,21 @@
import org.greenrobot.greendao.annotation.JoinProperty;
import org.greenrobot.greendao.annotation.Keep;
import org.greenrobot.greendao.annotation.ToMany;
import org.greenrobot.greendao.annotation.Unique;

import java.util.List;

import openfoodfacts.github.scrachx.openfood.models.DaoSession;

@Entity(indexes = {
@Index(value = "tag", unique = true)
})
@Entity
public class AnalysisTag {
@Id(autoincrement = true)
private Long id;
@Unique
/**
* If the analysis tag is being checked for by the user.
*/
private Boolean enabled = true;
@Index
private String tag;
private Boolean enabled = true; /*If the analysis tag is being checked for by the user.*/
@ToMany(joinProperties = {
@JoinProperty(name = "tag", referencedName = "analysisTag")
})
Expand Down Expand Up @@ -149,7 +149,9 @@ public void setEnabled(Boolean enabled) {
this.enabled = enabled;
}

/** called by internal mechanisms, do not call yourself. */
/**
* called by internal mechanisms, do not call yourself.
*/
@Generated(hash = 250411501)
public void __setDaoSession(DaoSession daoSession) {
this.daoSession = daoSession;
Expand Down
Expand Up @@ -8,9 +8,9 @@ import openfoodfacts.github.scrachx.openfood.models.entities.EntityResponse
* @author Rares
*/
class AnalysisTagResponse(
private val uniqueAnalysisTagID: String,
private val namesMap: Map<String, String>,
private val showIngredientsMap: Map<String, String>
private val uniqueAnalysisTagID: String,
private val namesMap: Map<String, String>,
private val showIngredientsMap: Map<String, String>
) : EntityResponse<AnalysisTag> {
/**
* Converts an AnalysisTagResponse object into a new AnalysisTag object.
Expand All @@ -19,12 +19,10 @@ class AnalysisTagResponse(
*/
override fun map(): AnalysisTag {
val analysisTag = AnalysisTag(uniqueAnalysisTagID, arrayListOf())
namesMap.forEach { (key, value) ->
var showIngredients = showIngredientsMap[key]
if (showIngredients == null) {
showIngredients = showIngredientsMap[DEFAULT_LANGUAGE]
}
analysisTag.names.add(AnalysisTagName(analysisTag.tag, key, value, showIngredients))

analysisTag.names += namesMap.map { (lc, name) ->
val showIngredients = showIngredientsMap[lc] ?: showIngredientsMap[DEFAULT_LANGUAGE]
AnalysisTagName(analysisTag.tag, lc, name, showIngredients)
}
return analysisTag
}
Expand Down
Expand Up @@ -9,12 +9,11 @@

import openfoodfacts.github.scrachx.openfood.models.entities.analysistag.AnalysisTagName;

@Entity(indexes = {
@Index(value = "analysisTag", unique = true)
})
@Entity
public class AnalysisTagConfig {
@Id(autoincrement = true)
private Long id;
@Index
private String analysisTag;
private String type;
@Transient
Expand Down
Expand Up @@ -15,11 +15,11 @@ import openfoodfacts.github.scrachx.openfood.models.entities.store.StoresWrapper
import openfoodfacts.github.scrachx.openfood.models.entities.tag.TagsWrapper
import retrofit2.http.GET

/*
Created by Lobster on 03.03.18.
*/ /**
/**
* API calls for loading static multilingual data
* This calls should be used as rare as possible, because they load Big Data
*
* @author Lobster
*/
interface AnalysisDataAPI {
@GET(LABELS_JSON)
Expand Down

0 comments on commit 7cdea9c

Please sign in to comment.