/
IngredientsProductFragment.java
183 lines (171 loc) · 8.83 KB
/
IngredientsProductFragment.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package openfoodfacts.github.scrachx.openfood.fragments;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.afollestad.materialdialogs.MaterialDialog;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import butterknife.BindView;
import openfoodfacts.github.scrachx.openfood.R;
import openfoodfacts.github.scrachx.openfood.models.Additive;
import openfoodfacts.github.scrachx.openfood.models.State;
public class IngredientsProductFragment extends BaseFragment {
@BindView(R.id.textIngredientProduct) TextView ingredientsProduct;
@BindView(R.id.textSubstanceProduct) TextView substanceProduct;
@BindView(R.id.textTraceProduct) TextView traceProduct;
@BindView(R.id.textAdditiveProduct) TextView additiveProduct;
@BindView(R.id.textPalmOilProduct) TextView palmOilProduct;
@BindView(R.id.textMayBeFromPalmOilProduct) TextView mayBeFromPalmOilProduct;
@BindView(R.id.ingredientContainer) ViewGroup containerView;
private State mState;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return createView(inflater, container, R.layout.fragment_ingredients_product);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Intent intent = getActivity().getIntent();
mState = (State) intent.getExtras().getSerializable("state");
if(mState.getProduct().getIngredientsText() != null) {
SpannableStringBuilder txtIngredients = new SpannableStringBuilder(Html.fromHtml(mState.getProduct().getIngredientsText().replace("_","")));
txtIngredients = setSpanBoldBetweenTokens(txtIngredients);
if(!txtIngredients.toString().substring(txtIngredients.toString().indexOf(":")).trim().isEmpty()) {
ingredientsProduct.setText(txtIngredients);
} else {
ingredientsProduct.setVisibility(View.GONE);
}
}
if(!cleanAllergensString().trim().isEmpty()) {
substanceProduct.setText(Html.fromHtml("<b>" + getString(R.string.txtSubstances) + "</b>" + ' ' + cleanAllergensString()));
} else {
substanceProduct.setVisibility(View.GONE);
}
String traces;
if (mState.getProduct().getCategories() == null) {
traceProduct.setVisibility(View.GONE);
} else {
traces = mState.getProduct().getTraces().replace(",", ", ");
if(traces.isEmpty()) {
traceProduct.setVisibility(View.GONE);
} else {
traceProduct.setText(Html.fromHtml("<b>" + getString(R.string.txtTraces) + "</b>" + ' ' + traces));
}
}
if(!mState.getProduct().getAdditivesTags().toString().replace("[", "").replace("]", "").isEmpty()) {
additiveProduct.setText(Html.fromHtml("<b>" + getString(R.string.txtAdditives) + "</b>" + ' ' + mState.getProduct().getAdditivesTags().toString().replace("[", "").replace("]", "").replace("en:", " ").replace("fr:", " ")));
} else {
additiveProduct.setVisibility(View.GONE);
}
if (mState.getProduct().getIngredientsFromPalmOilN() == 0 && mState.getProduct().getIngredientsFromOrThatMayBeFromPalmOilN() == 0) {
palmOilProduct.setVisibility(View.VISIBLE);
palmOilProduct.setText(getString(R.string.txtPalm));
} else {
if (!mState.getProduct().getIngredientsFromPalmOilTags().toString().replace("[", "").replace("]", "").isEmpty()) {
palmOilProduct.setVisibility(View.VISIBLE);
palmOilProduct.setText(Html.fromHtml("<b>" + getString(R.string.txtPalmOilProduct) + "</b>" + ' ' + mState.getProduct().getIngredientsFromPalmOilTags().toString().replace("[", "").replace("]", "")));
}
if (!mState.getProduct().getIngredientsThatMayBeFromPalmOilTags().toString().replace("[", "").replace("]", "").isEmpty()) {
mayBeFromPalmOilProduct.setVisibility(View.VISIBLE);
mayBeFromPalmOilProduct.setText(Html.fromHtml("<b>" + getString(R.string.txtMayBeFromPalmOilProduct) + "</b>" + ' ' + mState.getProduct().getIngredientsThatMayBeFromPalmOilTags().toString().replace("[", "").replace("]", "")));
}
}
SpannableStringBuilder txt = new SpannableStringBuilder(Html.fromHtml(mState.getProduct().getAdditivesTags().toString().replace("[", "").replace("]", "").replace("en:", " ").replace("fr:", " ")).toString());
txt = setSpanClickBetweenTokens(txt, containerView);
if(!txt.toString().trim().isEmpty()) {
additiveProduct.setMovementMethod(LinkMovementMethod.getInstance());
additiveProduct.setText(txt, TextView.BufferType.SPANNABLE);
} else {
additiveProduct.setVisibility(View.GONE);
}
}
private SpannableStringBuilder setSpanClickBetweenTokens(CharSequence text, final View view) {
final SpannableStringBuilder ssb = new SpannableStringBuilder(text);
Pattern p = Pattern.compile("[eE][a-zA-Z0-9]+");
Matcher m = p.matcher(ssb);
while (m.find()) {
final String tm = m.group();
final List<Additive> la = Additive.find(Additive.class, "code = ?", tm.toUpperCase());
if (la.size() == 2) {
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View v) {
Additive a;
if (Locale.getDefault().getLanguage().contains("fr")) {
a = la.get(0);
} else {
a = la.get(1);
}
new MaterialDialog.Builder(view.getContext())
.title(a.getCode() + " : " + a.getName())
.content(a.getRisk().toUpperCase())
.positiveText(R.string.txtOk)
.show();
}
};
ssb.setSpan(clickableSpan, m.start(), m.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
ssb.insert(0, Html.fromHtml("<b>" + getString(R.string.txtAdditives) + "</b>" + ' '));
return ssb;
}
private SpannableStringBuilder setSpanBoldBetweenTokens(CharSequence text) {
final SpannableStringBuilder ssb = new SpannableStringBuilder(text);
Pattern p = Pattern.compile("[a-zA-Z0-9(),àâçéèêëîïôûùüÿñæœ.-]+");
Matcher m = p.matcher(ssb);
while (m.find()) {
final String tm = m.group();
for (String l:cleanAllergensMultipleOccurence()) {
if(l.toLowerCase().equals(tm.toLowerCase().replaceAll("[(),.-]+", ""))) {
StyleSpan bold = new StyleSpan(android.graphics.Typeface.BOLD);
if(tm.contains("(")) {
ssb.setSpan(bold, m.start()+1, m.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else if(tm.contains(")")) {
ssb.setSpan(bold, m.start(), m.end()-1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} else {
ssb.setSpan(bold, m.start(), m.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
}
ssb.insert(0, Html.fromHtml("<b>" + getString(R.string.txtIngredients) + "</b>" + ' '));
return ssb;
}
private List<String> cleanAllergensMultipleOccurence() {
List<String> list = new ArrayList<>();
Pattern p = Pattern.compile("[a-zA-Z0-9àâçéèêëîïôûùüÿñæœ]+");
Matcher m = p.matcher(mState.getProduct().getAllergens().replace(",", ""));
while (m.find()) {
final String tma = m.group();
boolean canAdd = true;
if(list.size() == 0) list.add(tma);
for (int i = 0; i < list.size(); i++) {
if(list.get(i).toLowerCase().equals(tma.toLowerCase())) canAdd = false;
}
if(canAdd) {
list.add(tma);
}
}
return list;
}
private String cleanAllergensString() {
StringBuilder allergens = new StringBuilder("");
for (String l:cleanAllergensMultipleOccurence()) {
allergens.append(l + " ");
}
return allergens.toString();
}
}