1 |
|
package co.harlequinmettle.healthfoodconcepts; |
2 |
|
|
3 |
|
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT; |
4 |
|
|
5 |
|
import java.util.ArrayList; |
6 |
|
import java.util.Arrays; |
7 |
|
import java.util.TreeMap; |
8 |
|
import java.util.TreeSet; |
9 |
|
|
10 |
|
import android.content.Context; |
11 |
|
import android.view.Gravity; |
12 |
|
import android.view.View; |
13 |
|
import android.view.ViewGroup; |
14 |
|
import android.view.ViewTreeObserver; |
15 |
|
import android.view.ViewTreeObserver.OnGlobalLayoutListener; |
16 |
|
import android.widget.Button; |
17 |
|
import android.widget.CheckBox; |
18 |
|
import android.widget.LinearLayout; |
19 |
|
import android.widget.ScrollView; |
20 |
|
import android.widget.TextView; |
21 |
|
|
|
|
| 82,9% |
Uncovered Elements: 48 (281) |
Complexity: 48 |
Complexity Density: 0,22 |
|
22 |
|
public class NutrientSearchView extends LinearLayout { |
23 |
|
CheckBox[] highs = new CheckBox[130]; |
24 |
|
CheckBox[] lows = new CheckBox[130]; |
25 |
|
static final int HIGH_COLOR = 0xFF22FF22; |
26 |
|
static final int LOW_COLOR = 0xffff2222; |
27 |
|
|
28 |
|
static final int SEARCH_BUTTON = 111111111; |
29 |
|
static final int RESET_BUTTON = 22222222; |
30 |
|
View.OnClickListener nutrientSearchListener = new View.OnClickListener() { |
31 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (33) |
Complexity: 8 |
Complexity Density: 0,42 |
|
32 |
8
|
public void onClick(View view) {... |
33 |
8
|
int id = view.getId(); |
34 |
8
|
WhatYouEat.appAccess.removeView(WhatYouEat.appAccess |
35 |
|
.findViewById(SubScroll.FOOD_RESULTS_ID)); |
36 |
8
|
WhatYouEat.appAccess.removeView(WhatYouEat.appAccess |
37 |
|
.findViewById(SubScroll.FOOD_NUTRIENT_ID)); |
38 |
8
|
SubScroll.lastFoodsId = 2; |
39 |
8
|
if (id == RESET_BUTTON) { |
40 |
|
|
41 |
655
|
for (int i = 0; i < 130; i++) { |
42 |
|
|
43 |
650
|
highs[i].setChecked(false); |
44 |
650
|
lows[i].setChecked(false); |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
} |
49 |
|
|
50 |
|
} |
51 |
8
|
if (id == SEARCH_BUTTON) { |
52 |
3
|
ArrayList<Integer> highSearch = new ArrayList<Integer>(); |
53 |
3
|
ArrayList<Integer> lowSearch = new ArrayList<Integer>(); |
54 |
393
|
for (int i = 0; i < 130; i++) { |
55 |
|
|
56 |
390
|
if (!WhatYouEat.MY_NUTRIENTS[i]) |
57 |
147
|
continue; |
58 |
243
|
if (highs[i].isChecked()) |
59 |
9
|
highSearch.add(i); |
60 |
243
|
if (lows[i].isChecked()) |
61 |
7
|
lowSearch.add(i); |
62 |
|
|
63 |
|
} |
64 |
3
|
doNutrientSearch(highSearch, lowSearch); |
65 |
|
} |
66 |
|
} |
67 |
|
}; |
68 |
|
|
|
|
| 97,9% |
Uncovered Elements: 2 (95) |
Complexity: 15 |
Complexity Density: 0,19 |
|
69 |
2
|
public NutrientSearchView(Context context) {... |
70 |
2
|
super(context); |
71 |
|
|
72 |
|
|
73 |
2
|
this.setOrientation(VERTICAL); |
74 |
|
|
75 |
|
|
76 |
2
|
LinearLayout titleBar = new LinearLayout(context); |
77 |
2
|
titleBar.setMinimumWidth(400); |
78 |
|
|
79 |
2
|
TextView tvHigh = new TextView(context); |
80 |
2
|
TextView tvLow = new TextView(context); |
81 |
2
|
tvHigh.setBackgroundColor(HIGH_COLOR); |
82 |
2
|
tvHigh.setTextColor(0xff000000); |
83 |
2
|
tvHigh.setText("High In"); |
84 |
2
|
tvHigh.setWidth(200); |
85 |
2
|
tvHigh.setHeight(50); |
86 |
|
|
87 |
2
|
tvLow.setBackgroundColor(LOW_COLOR); |
88 |
2
|
tvLow.setTextColor(0xff000000); |
89 |
2
|
tvLow.setText("Low In"); |
90 |
2
|
tvLow.setWidth(200); |
91 |
2
|
tvLow.setHeight(50); |
92 |
|
|
93 |
2
|
titleBar.addView(tvHigh); |
94 |
2
|
titleBar.addView(tvLow); |
95 |
|
|
96 |
2
|
this.addView(titleBar); |
97 |
|
|
98 |
|
|
99 |
2
|
ScrollView nutrientScroll = new ScrollView(context); |
100 |
2
|
LinearLayout iChild = basicLinearLayout(context); |
101 |
2
|
nutrientScroll.addView(iChild); |
102 |
|
|
103 |
2
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( |
104 |
|
WRAP_CONTENT, WRAP_CONTENT, 1.0f); |
105 |
2
|
nutrientScroll.setLayoutParams(params); |
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
262
|
for (int i = 0; i < 130; i++) { |
111 |
|
|
112 |
260
|
if (!WhatYouEat.MY_NUTRIENTS[i]) { |
113 |
102
|
highs[i] = new CheckBox(context); |
114 |
102
|
lows[i] = new CheckBox(context); |
115 |
102
|
continue; |
116 |
|
} |
117 |
158
|
LinearLayout searchParam = new LinearLayout(context); |
118 |
|
|
119 |
|
|
120 |
158
|
searchParam.setMinimumWidth(400); |
121 |
158
|
searchParam.setGravity(Gravity.CENTER_VERTICAL); |
122 |
158
|
CheckBox highIn = new CheckBox(context); |
123 |
158
|
CheckBox lowIn = new CheckBox(context); |
124 |
158
|
highs[i] = highIn; |
125 |
158
|
lows[i] = lowIn; |
126 |
158
|
TextView nutrientT = new TextView(context); |
127 |
158
|
nutrientT.setHeight(50); |
128 |
158
|
nutrientT.setWidth(300); |
129 |
158
|
nutrientT.setGravity(Gravity.CENTER); |
130 |
|
|
131 |
158
|
highIn.setWidth(50); |
132 |
|
|
133 |
158
|
lowIn.setWidth(50); |
134 |
|
|
135 |
|
|
136 |
|
|
137 |
|
|
138 |
158
|
highIn.setBackgroundColor(HIGH_COLOR); |
139 |
158
|
lowIn.setBackgroundColor(LOW_COLOR); |
140 |
|
|
141 |
|
|
142 |
|
|
143 |
158
|
int bgColor = SubScroll.GENERAL; |
144 |
158
|
if (i >= 8 && i <= 14) { |
145 |
14
|
bgColor = SubScroll.INTERESTING; |
146 |
|
} |
147 |
158
|
if (i >= 15 && i <= 24) { |
148 |
20
|
bgColor = SubScroll.CARB; |
149 |
|
|
150 |
|
} |
151 |
158
|
if (i >= 25 && i <= 35) { |
152 |
22
|
bgColor = SubScroll.MINERAL; |
153 |
|
|
154 |
|
} |
155 |
158
|
if (i >= 36 && i <= 65) { |
156 |
60
|
bgColor = SubScroll.VITAMIN; |
157 |
|
|
158 |
|
} |
159 |
158
|
if (i >= 66 && i <= 84) { |
160 |
0
|
bgColor = SubScroll.PROTEIN; |
161 |
|
|
162 |
|
} |
163 |
158
|
if (i >= 85 && i <= 129) { |
164 |
30
|
bgColor = SubScroll.FAT; |
165 |
|
|
166 |
|
} |
167 |
158
|
int alpha = 150; |
168 |
|
|
169 |
158
|
int colorCombo = bgColor | (alpha << 24); |
170 |
158
|
nutrientT.setBackgroundColor(colorCombo); |
171 |
158
|
nutrientT.setText(WhatYouEat.nutrients[i]); |
172 |
|
|
173 |
158
|
searchParam.addView(highIn); |
174 |
158
|
searchParam.addView(nutrientT); |
175 |
158
|
searchParam.addView(lowIn); |
176 |
158
|
iChild.addView(searchParam); |
177 |
|
} |
178 |
|
|
179 |
2
|
this.addView(nutrientScroll); |
180 |
|
|
181 |
|
|
182 |
2
|
LinearLayout bottomButtons = new LinearLayout(context); |
183 |
2
|
bottomButtons.setGravity(Gravity.CENTER); |
184 |
2
|
Button search = simpleButton(context); |
185 |
2
|
Button clear = simpleButton(context); |
186 |
2
|
search.setText("Search"); |
187 |
2
|
clear.setText("Clear"); |
188 |
2
|
search.setId(SEARCH_BUTTON); |
189 |
2
|
clear.setId(RESET_BUTTON); |
190 |
2
|
search.setOnClickListener(nutrientSearchListener); |
191 |
2
|
clear.setOnClickListener(nutrientSearchListener); |
192 |
2
|
bottomButtons.addView(search); |
193 |
2
|
bottomButtons.addView(clear); |
194 |
|
|
195 |
2
|
this.addView(bottomButtons); |
196 |
|
} |
197 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0,17 |
|
198 |
4
|
public Button simpleButton(Context context) {... |
199 |
4
|
Button button = new Button(context); |
200 |
4
|
button.setHeight(WRAP_CONTENT); |
201 |
4
|
button.setMaxWidth(400); |
202 |
4
|
button.setMaxLines(5); |
203 |
4
|
button.setTextSize(24); |
204 |
4
|
return button; |
205 |
|
} |
206 |
|
|
207 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0,2 |
|
208 |
2
|
public LinearLayout basicLinearLayout(Context context) {... |
209 |
2
|
LinearLayout basic = new LinearLayout(context); |
210 |
2
|
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams( |
211 |
|
WRAP_CONTENT, WRAP_CONTENT); |
212 |
2
|
basic.setLayoutParams(params); |
213 |
2
|
basic.setOrientation(VERTICAL); |
214 |
2
|
return basic; |
215 |
|
} |
216 |
|
|
|
|
| 65,7% |
Uncovered Elements: 46 (134) |
Complexity: 22 |
Complexity Density: 0,2 |
|
217 |
3
|
public static void doNutrientSearch(ArrayList<Integer> h, ArrayList<Integer> l) {... |
218 |
|
|
219 |
3
|
long time = System.currentTimeMillis(); |
220 |
3
|
TreeMap<Float, Integer> foodRanking = new TreeMap<Float, Integer>(); |
221 |
3
|
int[] foodsToSearch = WhatYouEat.getFoodSearchIds(); |
222 |
|
|
223 |
3
|
float[] rankHigh = new float[WhatYouEat.FOOD_COUNT]; |
224 |
3
|
float[] rankLow = new float[WhatYouEat.FOOD_COUNT]; |
225 |
3
|
if (false) |
226 |
0
|
for (int i = 0; i < WhatYouEat.FOOD_COUNT; i++) { |
227 |
0
|
rankHigh[i] = -100; |
228 |
0
|
rankLow[i] = 100; |
229 |
|
} |
230 |
3
|
for (int i : foodsToSearch) { |
231 |
18855
|
rankHigh[i] = 1; |
232 |
18855
|
rankLow[i] = 1; |
233 |
|
} |
234 |
3
|
float null_rank_constant = 0; |
235 |
3
|
float measuredFactor = 1.0f; |
236 |
|
|
237 |
3
|
for (int nutrientID : h) { |
238 |
|
|
239 |
9
|
float[] nstat = WhatYouEat.highlightFactors.get(nutrientID); |
240 |
9
|
float min = nstat[0]; |
241 |
9
|
float mean = nstat[1]; |
242 |
9
|
float max = nstat[2]; |
243 |
|
|
244 |
9
|
for (int i : foodsToSearch) { |
245 |
|
|
246 |
56565
|
float dataPoint = WhatYouEat.db[nutrientID][i]; |
247 |
|
|
248 |
56565
|
if (dataPoint < 0) { |
249 |
0
|
rankHigh[i] += null_rank_constant; |
250 |
0
|
continue; |
251 |
|
} |
252 |
|
|
253 |
56565
|
switch (WhatYouEat.Nutrient_Measure) { |
254 |
0
|
case WhatYouEat.USING_KCAL: |
255 |
0
|
dataPoint = WhatYouEat.getPer100KcalDataPoint(i, dataPoint); |
256 |
|
|
257 |
0
|
break; |
258 |
0
|
case WhatYouEat.USING_SERVING: |
259 |
0
|
dataPoint = WhatYouEat.getPerServingDataPoint(i, dataPoint); |
260 |
0
|
if (dataPoint < 0) { |
261 |
|
|
262 |
0
|
dataPoint = WhatYouEat.db[nutrientID][i]; |
263 |
|
} |
264 |
0
|
break; |
265 |
56565
|
case WhatYouEat.USING_GRAMS: |
266 |
|
|
267 |
56565
|
break; |
268 |
|
|
269 |
0
|
default: |
270 |
|
|
271 |
0
|
break; |
272 |
|
} |
273 |
|
|
274 |
56565
|
float relativeData = dataPoint / max; |
275 |
|
|
276 |
56565
|
if (relativeData > 1) |
277 |
7329
|
relativeData = (float) (1 + Math.atan(relativeData - 1) / 2); |
278 |
|
|
279 |
|
|
280 |
56565
|
switch (WhatYouEat.Search_Type) { |
281 |
0
|
case WhatYouEat.SEARCH_TYPE_SUM: |
282 |
0
|
rankHigh[i] += (measuredFactor + relativeData); |
283 |
0
|
break; |
284 |
|
|
285 |
56565
|
case WhatYouEat.SEARCH_TYPE_PRODUCT: |
286 |
56565
|
rankHigh[i] *= (measuredFactor + relativeData); |
287 |
|
|
288 |
56565
|
break; |
289 |
0
|
default: |
290 |
0
|
break; |
291 |
|
|
292 |
|
} |
293 |
|
|
294 |
56565
|
rankHigh[i] -= (Math.random() / 100000000000.0); |
295 |
|
} |
296 |
|
} |
297 |
|
|
298 |
3
|
for (int nutrientID : l) { |
299 |
|
|
300 |
7
|
float[] nstat = WhatYouEat.highlightFactors.get(nutrientID); |
301 |
7
|
float min = nstat[0]; |
302 |
7
|
float mean = nstat[1]; |
303 |
7
|
float max = nstat[2]; |
304 |
|
|
305 |
7
|
for (int i : foodsToSearch) { |
306 |
|
|
307 |
43995
|
float dataPoint = WhatYouEat.db[nutrientID][i]; |
308 |
43995
|
if (dataPoint < 0) { |
309 |
1038
|
rankLow[i] += null_rank_constant; |
310 |
1038
|
continue; |
311 |
|
|
312 |
|
} |
313 |
|
|
314 |
42957
|
switch (WhatYouEat.Nutrient_Measure) { |
315 |
0
|
case WhatYouEat.USING_KCAL: |
316 |
0
|
dataPoint = WhatYouEat.getPer100KcalDataPoint(i, dataPoint); |
317 |
|
|
318 |
0
|
break; |
319 |
0
|
case WhatYouEat.USING_SERVING: |
320 |
0
|
dataPoint = WhatYouEat.getPerServingDataPoint(i, dataPoint); |
321 |
0
|
if (dataPoint < 0) { |
322 |
|
|
323 |
0
|
dataPoint = WhatYouEat.db[nutrientID][i]; |
324 |
|
} |
325 |
0
|
break; |
326 |
42957
|
case WhatYouEat.USING_GRAMS: |
327 |
|
|
328 |
42957
|
break; |
329 |
|
|
330 |
0
|
default: |
331 |
|
|
332 |
0
|
break; |
333 |
|
} |
334 |
42957
|
float relativeData = dataPoint / max; |
335 |
|
|
336 |
42957
|
if (relativeData > 1) |
337 |
5910
|
relativeData = (float) (1 + Math.atan(relativeData - 1)); |
338 |
|
|
339 |
|
|
340 |
42957
|
switch (WhatYouEat.Search_Type) { |
341 |
0
|
case WhatYouEat.SEARCH_TYPE_SUM: |
342 |
0
|
rankLow[i] += (measuredFactor + relativeData); |
343 |
0
|
break; |
344 |
|
|
345 |
42957
|
case WhatYouEat.SEARCH_TYPE_PRODUCT: |
346 |
42957
|
rankLow[i] *= (measuredFactor + relativeData); |
347 |
42957
|
break; |
348 |
0
|
default: |
349 |
0
|
break; |
350 |
|
|
351 |
|
} |
352 |
|
|
353 |
42957
|
rankLow[i] -= (Math.random() / 100000000000.0); |
354 |
|
} |
355 |
|
} |
356 |
3
|
int ctr = 0; |
357 |
3
|
for (int i : foodsToSearch) { |
358 |
18855
|
float highRank = -(rankHigh[i]); |
359 |
18855
|
float lowRank = (rankLow[i]); |
360 |
|
|
361 |
|
|
362 |
18855
|
foodRanking.put(lowRank + highRank, i); |
363 |
|
} |
364 |
3
|
int counter = 0; |
365 |
3
|
int MAX = foodRanking.size() - 1; |
366 |
3
|
if (MAX > 300) |
367 |
3
|
MAX = 300; |
368 |
3
|
if (MAX < 1) |
369 |
0
|
MAX = 2; |
370 |
3
|
String[] priorityResults = new String[MAX]; |
371 |
3
|
int[] pResIDs = new int[MAX]; |
372 |
3
|
for (int rank : foodRanking.values()) { |
373 |
|
|
374 |
900
|
priorityResults[counter] = WhatYouEat.foods[rank]; |
375 |
900
|
pResIDs[counter] = rank; |
376 |
|
|
377 |
900
|
if (counter++ == MAX - 1) |
378 |
3
|
break; |
379 |
|
} |
380 |
|
|
381 |
3
|
System.out.println("---::>DONE SEARCH: " |
382 |
|
+ (System.currentTimeMillis() - time)); |
383 |
3
|
WhatYouEat.searchResults = priorityResults; |
384 |
3
|
WhatYouEat.foodCodeResults = pResIDs; |
385 |
|
|
386 |
3
|
FoodDescriptionsScroller fds = new FoodDescriptionsScroller( |
387 |
|
WhatYouEat.ctx,true); |
388 |
3
|
WhatYouEat.appAccess.addView(fds, 2); |
389 |
|
|
390 |
|
|
391 |
3
|
final ViewTreeObserver viewTreeObserver4 = WhatYouEat.application |
392 |
|
.getViewTreeObserver(); |
393 |
3
|
if (viewTreeObserver4.isAlive()) { |
394 |
3
|
viewTreeObserver4 |
395 |
|
.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { |
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
396 |
3
|
@Override... |
397 |
|
public void onGlobalLayout() { |
398 |
|
|
399 |
3
|
WhatYouEat.application.scrollBy( |
400 |
|
(int) (0.9 * SubScroll.MAX_BUTTON_WIDTH), 0); |
401 |
3
|
viewTreeObserver4 |
402 |
|
.removeGlobalOnLayoutListener(this); |
403 |
|
} |
404 |
|
}); |
405 |
|
} |
406 |
|
|
407 |
|
|
408 |
|
|
409 |
|
|
410 |
|
} |
411 |
|
|
412 |
|
} |