1 |
|
package co.harlequinmettle.healthfoodconcepts; |
2 |
|
|
3 |
|
import java.util.Arrays; |
4 |
|
|
5 |
|
import android.content.Context; |
6 |
|
import android.graphics.PorterDuff; |
7 |
|
import android.view.View; |
8 |
|
import android.view.ViewTreeObserver.OnGlobalLayoutListener; |
9 |
|
import android.widget.Button; |
10 |
|
import android.widget.EditText; |
11 |
|
import android.widget.ScrollView; |
12 |
|
|
|
|
| 77,6% |
Uncovered Elements: 32 (143) |
Complexity: 38 |
Complexity Density: 0,38 |
|
13 |
|
public class NutritionScroller extends SubScroll { |
14 |
|
|
15 |
|
private View.OnClickListener nutrientListener = new View.OnClickListener() { |
|
|
| 84% |
Uncovered Elements: 4 (25) |
Complexity: 4 |
Complexity Density: 0,21 |
|
16 |
10
|
public void onClick(View view) {... |
17 |
10
|
int id = view.getId(); |
18 |
10
|
currentNutrientID = id; |
19 |
10
|
WhatYouEat.appAccess.removeView(WhatYouEat.appAccess |
20 |
|
.findViewById(STATVIEW_ID)); |
21 |
10
|
showingStats = false; |
22 |
|
|
23 |
|
|
24 |
10
|
StatTool nutrStat = null; |
25 |
|
|
26 |
|
|
27 |
10
|
if (WhatYouEat.nutritionStats.containsKey(id)) { |
28 |
0
|
nutrStat = WhatYouEat.nutritionStats.get(id); |
29 |
|
} else { |
30 |
10
|
nutrStat = new StatTool(WhatYouEat.getFoodSearchIds(), id); |
31 |
10
|
WhatYouEat.nutritionStats.put(id, nutrStat); |
32 |
|
} |
33 |
|
|
34 |
10
|
HistogramView nutrStats = new HistogramView(context, nutrStat, |
35 |
|
(int) WhatYouEat.sw, (int) WhatYouEat.sh,NutritionScroller.getColorForNutrient( id ) ); |
36 |
|
|
37 |
10
|
nutrStats.setId(STATVIEW_ID); |
38 |
10
|
int statId = lastFoodsId + 2; |
39 |
10
|
if(SubScroll.fromKeywordSearch){ |
40 |
2
|
statId-=1; |
41 |
|
} |
42 |
10
|
if(SubScroll.fromFoodSuggestions){ |
43 |
0
|
statId+=2; |
44 |
|
} |
45 |
|
|
46 |
|
|
47 |
10
|
WhatYouEat.appAccess.addView(nutrStats, statId); |
48 |
|
|
49 |
10
|
WhatYouEat.application.scrollBy((int) (0.9 * MAX_BUTTON_WIDTH), 0); |
50 |
10
|
showingStats = true; |
51 |
|
} |
52 |
|
}; |
53 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
54 |
5
|
NutritionScroller(Context c, int foodCode ) {... |
55 |
5
|
super(c); |
56 |
5
|
addNutritionButtons(foodCode); |
57 |
|
|
58 |
|
} |
|
|
| 84,6% |
Uncovered Elements: 4 (26) |
Complexity: 13 |
Complexity Density: 0,93 |
|
59 |
127
|
public static int getColorForNutrient(int i){... |
60 |
127
|
int rColor = GENERAL; |
61 |
127
|
if (i >= 8 && i <= 14) { |
62 |
0
|
rColor = INTERESTING; |
63 |
|
} |
64 |
127
|
if (i >= 15 && i <= 24) { |
65 |
6
|
rColor = CARB; |
66 |
|
|
67 |
|
} |
68 |
127
|
if (i >= 25 && i <= 35) { |
69 |
28
|
rColor = MINERAL; |
70 |
|
|
71 |
|
} |
72 |
127
|
if (i >= 36 && i <= 65) { |
73 |
27
|
rColor = VITAMIN; |
74 |
|
|
75 |
|
} |
76 |
127
|
if (i >= 66 && i <= 84) { |
77 |
0
|
rColor = PROTEIN; |
78 |
|
|
79 |
|
} |
80 |
127
|
if (i >= 85 && i <= 129) { |
81 |
34
|
rColor = FAT; |
82 |
|
|
83 |
|
} |
84 |
127
|
return rColor; |
85 |
|
} |
|
|
| 72,1% |
Uncovered Elements: 24 (86) |
Complexity: 20 |
Complexity Density: 0,3 |
|
86 |
5
|
private void addNutritionButtons(int nID) {... |
87 |
5
|
buttons = new Button[130]; |
88 |
5
|
int actualI = 0; |
89 |
655
|
for (int i = 0; i < 130; i++) { |
90 |
650
|
float dataPoint = WhatYouEat.db[i][nID]; |
91 |
650
|
if (!WhatYouEat.MY_NUTRIENTS[i] || dataPoint <= 0) |
92 |
|
|
93 |
533
|
continue; |
94 |
117
|
float n = 1; |
95 |
|
|
96 |
117
|
boolean useServingOK = true; |
97 |
117
|
switch (WhatYouEat.Nutrient_Measure) { |
98 |
117
|
case WhatYouEat.USING_GRAMS: |
99 |
|
|
100 |
117
|
break; |
101 |
0
|
case WhatYouEat.USING_KCAL: |
102 |
0
|
dataPoint = WhatYouEat.getPer100KcalDataPoint(nID, dataPoint); |
103 |
|
|
104 |
0
|
break; |
105 |
|
|
106 |
0
|
case WhatYouEat.USING_SERVING: |
107 |
|
|
108 |
0
|
dataPoint = WhatYouEat.getPerServingDataPoint(nID, dataPoint); |
109 |
0
|
if (dataPoint < 0) { |
110 |
0
|
dataPoint = WhatYouEat.db[i][nID]; |
111 |
0
|
useServingOK = false; |
112 |
|
} |
113 |
0
|
default: |
114 |
|
|
115 |
0
|
break; |
116 |
|
} |
117 |
|
|
118 |
|
|
119 |
117
|
buttons[actualI] = simpleButton(); |
120 |
|
|
121 |
117
|
String nutritionDescription = WhatYouEat.nutrients[i] + " " |
122 |
|
+ dataPoint + " " + WhatYouEat.units[i]; |
123 |
|
|
124 |
117
|
switch (WhatYouEat.Nutrient_Measure) { |
125 |
0
|
case WhatYouEat.USING_KCAL: |
126 |
0
|
nutritionDescription += " per 100 kilocalories"; |
127 |
0
|
break; |
128 |
117
|
case WhatYouEat.USING_GRAMS: |
129 |
117
|
nutritionDescription += " per 100 grams"; |
130 |
117
|
break; |
131 |
0
|
case WhatYouEat.USING_SERVING: |
132 |
|
|
133 |
0
|
if (useServingOK) { |
134 |
0
|
nutritionDescription += " in " |
135 |
|
+ WhatYouEat.quantityFactor[SubScroll.currentFoodID][WhatYouEat.optimalServingId[SubScroll.currentFoodID]] |
136 |
|
+ " " |
137 |
|
+ WhatYouEat.oddUnits[SubScroll.currentFoodID][WhatYouEat.optimalServingId[SubScroll.currentFoodID]] |
138 |
|
+ " of " |
139 |
|
+ WhatYouEat.foods[SubScroll.currentFoodID]; |
140 |
|
} else { |
141 |
0
|
nutritionDescription += " per 100 grams"; |
142 |
|
} |
143 |
0
|
break; |
144 |
|
|
145 |
0
|
default: |
146 |
|
|
147 |
0
|
break; |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
|
|
152 |
117
|
float[] nstat = WhatYouEat.highlightFactors.get(i); |
153 |
117
|
float min = nstat[0]; |
154 |
117
|
float mean = nstat[1]; |
155 |
117
|
float max = nstat[2]; |
156 |
|
|
157 |
117
|
if (dataPoint > mean) { |
158 |
|
|
159 |
48
|
n = dataPoint - mean; |
160 |
|
|
161 |
48
|
n /= (max - mean); |
162 |
|
|
163 |
|
} else { |
164 |
|
|
165 |
69
|
n = -(mean - dataPoint) / (mean - min); |
166 |
|
} |
167 |
117
|
int sim = (int) (n * 100); |
168 |
|
|
169 |
117
|
if (sim > 100) { |
170 |
12
|
sim = 110; |
171 |
|
} |
172 |
|
|
173 |
|
|
174 |
117
|
buttons[actualI].setText(nutritionDescription); |
175 |
117
|
buttons[actualI].setId(i); |
176 |
|
|
177 |
117
|
int bgColor = getColorForNutrient(i); |
178 |
117
|
if (i == 5 || i == 6) { |
179 |
10
|
sim = 0; |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
117
|
if (sim > 100) |
184 |
4
|
sim = 100; |
185 |
|
|
186 |
117
|
int alpha = 150 + sim; |
187 |
|
|
188 |
117
|
int colorCombo = bgColor | (alpha << 24); |
189 |
117
|
if (sim < 25 && sim > -25) { |
190 |
56
|
buttons[actualI].setTextColor(0xffaaaaaa); |
191 |
61
|
} else if (sim < -25) { |
192 |
|
|
193 |
50
|
buttons[actualI].setTextColor(0xff777777); |
194 |
|
} else { |
195 |
|
|
196 |
11
|
buttons[actualI].setTextColor(0xffffffff); |
197 |
|
} |
198 |
117
|
buttons[actualI].setOnClickListener(nutrientListener); |
199 |
117
|
buttons[actualI].getBackground().setColorFilter(colorCombo, |
200 |
|
PorterDuff.Mode.SRC_IN); |
201 |
|
|
202 |
117
|
instanceChild.addView(buttons[actualI]); |
203 |
117
|
actualI++; |
204 |
|
} |
205 |
|
|
206 |
|
} |
207 |
|
} |