26
26
import android .view .Gravity ;
27
27
import android .view .View ;
28
28
import android .view .ViewGroup ;
29
- import android .widget .ImageView ;
30
- import android .widget .TableRow ;
31
- import android .widget .TextView ;
29
+ import android .widget .*;
32
30
import androidx .annotation .DrawableRes ;
33
31
import androidx .appcompat .content .res .AppCompatResources ;
34
32
41
39
import be .ugent .zeus .hydra .resto .RestoMenu ;
42
40
import com .google .android .material .textview .MaterialTextView ;
43
41
42
+ import static be .ugent .zeus .hydra .common .utils .ViewUtils .convertDpToPixelInt ;
43
+
44
44
/**
45
45
* Helper class to display meals.
46
46
* <p>
@@ -76,7 +76,8 @@ private static ImageView makeImageView(Context context, @DrawableRes int id) {
76
76
ImageView imageView = new ImageView (context );
77
77
imageView .setScaleType (ImageView .ScaleType .FIT_CENTER );
78
78
imageView .setImageDrawable (AppCompatResources .getDrawable (context , id ));
79
- TableRow .LayoutParams params = new TableRow .LayoutParams (TableRow .LayoutParams .WRAP_CONTENT , TableRow .LayoutParams .MATCH_PARENT );
79
+ var size = convertDpToPixelInt (16 , context );
80
+ TableRow .LayoutParams params = new TableRow .LayoutParams (size , size );
80
81
imageView .setLayoutParams (params );
81
82
return imageView ;
82
83
}
@@ -104,27 +105,8 @@ public static int getDrawable(RestoMeal meal) {
104
105
* @param parent The view to which the child views will be added. This will be done by calling {@link
105
106
* ViewGroup#addView(View)}. This is also the view to get a context from.
106
107
*/
107
- void addVegetableViews (ViewGroup parent ) {
108
-
109
- final Context context = parent .getContext ();
110
- final int rowPadding = ViewUtils .convertDpToPixelInt (ROW_PADDING_DP , context );
111
-
112
- for (String vegetable : menu .vegetables ()) {
113
-
114
- TableRow tr = new TableRow (context );
115
- TableRow .LayoutParams lp = new TableRow .LayoutParams (TableRow .LayoutParams .MATCH_PARENT , TableRow .LayoutParams .WRAP_CONTENT );
116
- tr .setLayoutParams (lp );
117
- tr .setPadding (0 , rowPadding , 0 , rowPadding );
118
-
119
- ImageView imageView = makeImageView (context , R .drawable .resto_vegetables );
120
-
121
- TextView tvCenter = makeCenterTextView (context , vegetable , lp );
122
-
123
- tr .addView (imageView );
124
- tr .addView (tvCenter );
125
-
126
- parent .addView (tr );
127
- }
108
+ void addVegetableViews (ViewGroup parent , boolean showAllergens ) {
109
+ addMealViews (parent , menu .vegetables (), showAllergens );
128
110
}
129
111
130
112
/**
@@ -133,8 +115,8 @@ void addVegetableViews(ViewGroup parent) {
133
115
* @param parent The view to which the child views will be added. This will be done by calling {@link
134
116
* ViewGroup#addView(View)}. This is also the view to get a context from.
135
117
*/
136
- void addSoupViews (ViewGroup parent ) {
137
- addMealViews (parent , menu .soups (), false );
118
+ void addSoupViews (ViewGroup parent , boolean showAllergens ) {
119
+ addMealViews (parent , menu .soups (), showAllergens );
138
120
}
139
121
140
122
/**
@@ -190,7 +172,7 @@ boolean hasColdDishes() {
190
172
private TextView makeCenterTextView (Context context , String text , TableRow .LayoutParams lp ) {
191
173
TextView tvCenter = new MaterialTextView (context , null , normalStyle );
192
174
tvCenter .setTextIsSelectable (selectable );
193
- tvCenter .setPadding (ViewUtils . convertDpToPixelInt (16 , context ), 0 , 0 , 0 );
175
+ tvCenter .setPadding (convertDpToPixelInt (16 , context ), 0 , 0 , 0 );
194
176
tvCenter .setLayoutParams (lp );
195
177
tvCenter .setText (text );
196
178
return tvCenter ;
@@ -204,7 +186,7 @@ private TextView makeCenterTextView(Context context, String text, TableRow.Layou
204
186
*/
205
187
private void addMealViews (ViewGroup parent , List <RestoMeal > meals , boolean showAllergens ) {
206
188
final Context context = parent .getContext ();
207
- final int rowPadding = ViewUtils . convertDpToPixelInt (ROW_PADDING_DP , context );
189
+ final int rowPadding = convertDpToPixelInt (ROW_PADDING_DP , context );
208
190
209
191
TableRow .LayoutParams lp = new TableRow .LayoutParams (TableRow .LayoutParams .MATCH_PARENT , TableRow .LayoutParams .WRAP_CONTENT );
210
192
@@ -216,17 +198,22 @@ private void addMealViews(ViewGroup parent, List<RestoMeal> meals, boolean showA
216
198
//Set the correct image.
217
199
@ DrawableRes final int id = getDrawable (meal );
218
200
219
- ImageView imageView = makeImageView (context , id );
220
- String name = meal .name ();
221
- TextView tvCenter = makeCenterTextView (context , name , lp );
222
- TextView tvRight = new MaterialTextView (context , null , normalStyle );
223
- tvRight .setLayoutParams (lp );
224
- tvRight .setText (meal .price ());
225
- tvRight .setGravity (Gravity .END );
226
-
227
- tr .addView (imageView );
228
- tr .addView (tvCenter );
229
- tr .addView (tvRight );
201
+ tr .addView (makeImageView (context , id ));
202
+ var center = makeCenterTextView (context , meal .name (), lp );
203
+ tr .addView (center );
204
+
205
+ if (meal .price () != null ) {
206
+ TextView tvRight = new MaterialTextView (context , null , normalStyle );
207
+ tvRight .setLayoutParams (lp );
208
+ tvRight .setText (meal .price ());
209
+ tvRight .setGravity (Gravity .END );
210
+ tr .addView (tvRight );
211
+ } else {
212
+ // Allow the center to span more columns.
213
+ TableRow .LayoutParams tlp = (TableRow .LayoutParams ) center .getLayoutParams ();
214
+ tlp .span = 2 ;
215
+ center .setLayoutParams (tlp );
216
+ }
230
217
231
218
parent .addView (tr );
232
219
0 commit comments