Skip to content

Commit b75d6a2

Browse files
authored
Merge pull request #883 from ZeusWPI/enhance/better-resto-icons
Improve resto menu display
2 parents dd6daac + c47c2ac commit b75d6a2

17 files changed

+292
-196
lines changed

app/src/main/java/be/ugent/zeus/hydra/common/ui/widgets/DisplayableMenu.java

+27-40
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
import android.view.Gravity;
2727
import android.view.View;
2828
import android.view.ViewGroup;
29-
import android.widget.ImageView;
30-
import android.widget.TableRow;
31-
import android.widget.TextView;
29+
import android.widget.*;
3230
import androidx.annotation.DrawableRes;
3331
import androidx.appcompat.content.res.AppCompatResources;
3432

@@ -41,6 +39,8 @@
4139
import be.ugent.zeus.hydra.resto.RestoMenu;
4240
import com.google.android.material.textview.MaterialTextView;
4341

42+
import static be.ugent.zeus.hydra.common.utils.ViewUtils.convertDpToPixelInt;
43+
4444
/**
4545
* Helper class to display meals.
4646
* <p>
@@ -76,7 +76,8 @@ private static ImageView makeImageView(Context context, @DrawableRes int id) {
7676
ImageView imageView = new ImageView(context);
7777
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
7878
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);
8081
imageView.setLayoutParams(params);
8182
return imageView;
8283
}
@@ -104,27 +105,8 @@ public static int getDrawable(RestoMeal meal) {
104105
* @param parent The view to which the child views will be added. This will be done by calling {@link
105106
* ViewGroup#addView(View)}. This is also the view to get a context from.
106107
*/
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);
128110
}
129111

130112
/**
@@ -133,8 +115,8 @@ void addVegetableViews(ViewGroup parent) {
133115
* @param parent The view to which the child views will be added. This will be done by calling {@link
134116
* ViewGroup#addView(View)}. This is also the view to get a context from.
135117
*/
136-
void addSoupViews(ViewGroup parent) {
137-
addMealViews(parent, menu.soups(), false);
118+
void addSoupViews(ViewGroup parent, boolean showAllergens) {
119+
addMealViews(parent, menu.soups(), showAllergens);
138120
}
139121

140122
/**
@@ -190,7 +172,7 @@ boolean hasColdDishes() {
190172
private TextView makeCenterTextView(Context context, String text, TableRow.LayoutParams lp) {
191173
TextView tvCenter = new MaterialTextView(context, null, normalStyle);
192174
tvCenter.setTextIsSelectable(selectable);
193-
tvCenter.setPadding(ViewUtils.convertDpToPixelInt(16, context), 0, 0, 0);
175+
tvCenter.setPadding(convertDpToPixelInt(16, context), 0, 0, 0);
194176
tvCenter.setLayoutParams(lp);
195177
tvCenter.setText(text);
196178
return tvCenter;
@@ -204,7 +186,7 @@ private TextView makeCenterTextView(Context context, String text, TableRow.Layou
204186
*/
205187
private void addMealViews(ViewGroup parent, List<RestoMeal> meals, boolean showAllergens) {
206188
final Context context = parent.getContext();
207-
final int rowPadding = ViewUtils.convertDpToPixelInt(ROW_PADDING_DP, context);
189+
final int rowPadding = convertDpToPixelInt(ROW_PADDING_DP, context);
208190

209191
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
210192

@@ -216,17 +198,22 @@ private void addMealViews(ViewGroup parent, List<RestoMeal> meals, boolean showA
216198
//Set the correct image.
217199
@DrawableRes final int id = getDrawable(meal);
218200

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+
}
230217

231218
parent.addView(tr);
232219

app/src/main/java/be/ugent/zeus/hydra/common/ui/widgets/MenuTable.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -218,19 +218,21 @@ private void populate() {
218218
if (showTitles) {
219219
createTitle(getContext().getString(R.string.resto_menu_soup));
220220
}
221-
menu.addSoupViews(this);
221+
menu.addSoupViews(this, showAllergens);
222222
}
223223

224224
if (isSetIn(displayedKinds, DisplayKind.VEGETABLES) && menu.hasVegetables()) {
225225
if (showTitles) {
226226
createTitle(getContext().getString(R.string.resto_menu_vegetables));
227227
}
228-
menu.addVegetableViews(this);
228+
menu.addVegetableViews(this, showAllergens);
229229
}
230230
}
231231

232232
/**
233233
* Flags to indicate what should be displayed by the menu.
234+
* <p>
235+
* Keep in sync with the showKind XML attribute.
234236
*/
235237
@IntDef(flag = true, value = {DisplayKind.HOT, DisplayKind.COLD, DisplayKind.SOUP, DisplayKind.VEGETABLES, DisplayKind.ALL})
236238
@Retention(RetentionPolicy.SOURCE)

app/src/main/java/be/ugent/zeus/hydra/feed/cards/resto/RestoCardViewHolder.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,12 @@ public void onCreateMenu(Menu menu) {
108108
} else {
109109
menu.add(NONE, KindMenu.SHOW_HOT, NONE, R.string.feed_pref_resto_show_hot);
110110
}
111-
112-
if ((kind & MenuTable.DisplayKind.COLD) == MenuTable.DisplayKind.COLD) {
113-
if (displayed > 1) {
114-
menu.add(NONE, KindMenu.HIDE_COLD, NONE, R.string.feed_pref_resto_hide_cold);
115-
}
116-
} else {
117-
menu.add(NONE, KindMenu.SHOW_COLD, NONE, R.string.feed_pref_resto_show_cold);
118-
}
119111
}
120112

121113
@Override
122114
public boolean onMenuItemClick(MenuItem item) {
123115
return switch (item.getItemId()) {
124-
case KindMenu.HIDE_HOT, KindMenu.HIDE_SOUP, KindMenu.SHOW_HOT, KindMenu.SHOW_SOUP, KindMenu.SHOW_COLD, KindMenu.HIDE_COLD -> {
116+
case KindMenu.HIDE_HOT, KindMenu.HIDE_SOUP, KindMenu.SHOW_HOT, KindMenu.SHOW_SOUP -> {
125117
adapter.companion().executeCommand(new RestoKindCommand(item.getItemId()));
126118
yield true;
127119
}

app/src/main/java/be/ugent/zeus/hydra/feed/preferences/HomeFragment.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class HomeFragment extends PreferenceFragment {
4949

5050
public static final String PREF_DATA_SAVER = "pref_home_feed_save_data";
5151
public static final boolean PREF_DATA_SAVER_DEFAULT = false;
52-
private static final String PREF_RESTO_KINDS = "pref_feed_resto_kinds_v2";
52+
private static final String PREF_RESTO_KINDS = "pref_feed_resto_kinds_v3";
5353
@MenuTable.DisplayKind
5454
private static final int PREF_RESTO_KINDS_DEFAULT = MenuTable.DisplayKind.HOT | MenuTable.DisplayKind.SOUP;
5555
private DeleteViewModel viewModel;

app/src/main/java/be/ugent/zeus/hydra/resto/RestoMeal.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import android.os.Parcel;
2626
import android.os.Parcelable;
2727

28+
import androidx.annotation.Nullable;
29+
2830
import java.util.List;
2931

3032
/**
@@ -33,7 +35,7 @@
3335
* @author Niko Strijbol
3436
* @author Mitch
3537
*/
36-
public record RestoMeal(String name, String price, String type, String kind,
38+
public record RestoMeal(String name, @Nullable String price, @Nullable String type, String kind,
3739
List<String> allergens) implements Parcelable {
3840
public static final String MENU_TYPE_COLD = "cold";
3941

@@ -55,6 +57,13 @@ public void writeToParcel(Parcel dest, int flags) {
5557
dest.writeStringList(this.allergens);
5658
}
5759

60+
public RestoMeal withPrice(String newPrice) {
61+
return new RestoMeal(name, newPrice, type, kind, allergens);
62+
}
63+
64+
public RestoMeal withName(String newName) {
65+
return new RestoMeal(newName, price, type, kind, allergens);
66+
}
5867

5968
public static final Parcelable.Creator<RestoMeal> CREATOR = new Parcelable.Creator<>() {
6069
@Override

app/src/main/java/be/ugent/zeus/hydra/resto/RestoMenu.java

+38-15
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import android.os.Parcel;
2626
import android.os.Parcelable;
2727
import androidx.annotation.NonNull;
28-
import androidx.annotation.VisibleForTesting;
2928
import androidx.core.os.ParcelCompat;
3029

3130
import java.time.LocalDate;
@@ -44,36 +43,37 @@ public final class RestoMenu implements Parcelable {
4443
private final boolean open;
4544
private final LocalDate date;
4645
private final List<RestoMeal> meals;
47-
private final List<String> vegetables;
46+
private final List<RestoMeal> vegetables2;
4847
private final String message;
4948

5049
private transient CategorizedMeals categorized;
5150

52-
public RestoMenu(boolean open, LocalDate date, List<RestoMeal> meals, List<String> vegetables, String message) {
51+
public RestoMenu(boolean open, LocalDate date, List<RestoMeal> meals, List<RestoMeal> vegetables2, String message) {
5352
this.open = open;
5453
this.date = date;
5554
this.meals = meals;
56-
this.vegetables = vegetables;
55+
this.vegetables2 = vegetables2;
5756
this.message = message;
5857
}
5958

6059
private RestoMenu(Parcel in) {
6160
this.open = in.readByte() != 0;
6261
this.date = ParcelCompat.readSerializable(in, LocalDate.class.getClassLoader(), LocalDate.class);
6362
this.meals = in.createTypedArrayList(RestoMeal.CREATOR);
64-
this.vegetables = in.createStringArrayList();
63+
this.vegetables2 = in.createTypedArrayList(RestoMeal.CREATOR);
6564
this.message = in.readString();
6665
}
6766

6867
/**
6968
* Sort the meals available in the menu.
69+
* This will also fix the soups.
7070
*/
7171
private void fillCategoriesIfNeeded() {
7272
if (categorized != null) {
7373
return;
7474
}
7575

76-
var soups = new ArrayList<RestoMeal>();
76+
List<RestoMeal> soups = new ArrayList<>();
7777
var mainDishes = new ArrayList<RestoMeal>();
7878
var coldDishes = new ArrayList<RestoMeal>();
7979

@@ -89,12 +89,35 @@ private void fillCategoriesIfNeeded() {
8989
}
9090
}
9191

92+
soups = fixSoups(soups);
93+
9294
this.categorized = new CategorizedMeals(mainDishes, coldDishes, soups);
9395
}
9496

95-
@VisibleForTesting
96-
public RestoMenu withDate(LocalDate date) {
97-
return new RestoMenu(open, date, meals, vegetables, message);
97+
private static String removeSuffix(String word, String suffix) {
98+
if (word.endsWith(suffix)) {
99+
return word.substring(0, word.length() - suffix.length());
100+
} else {
101+
return word;
102+
}
103+
}
104+
105+
private List<RestoMeal> fixSoups(List<RestoMeal> soups) {
106+
// TODO: this is rather ugly...
107+
List<RestoMeal> finalSoups = new ArrayList<>();
108+
var priceBig = "";
109+
for (RestoMeal soup: soups) {
110+
if (soup.name().endsWith("big") || soup.name().endsWith("groot")) {
111+
priceBig = soup.price();
112+
} else {
113+
var name = removeSuffix(soup.name(), " small");
114+
name = removeSuffix(name, " klein");
115+
finalSoups.add(soup.withName(name));
116+
}
117+
}
118+
final String suffix = priceBig;
119+
120+
return finalSoups.stream().map(s -> s.withPrice(s.price() + " / " + suffix)).toList();
98121
}
99122

100123
/**
@@ -104,8 +127,8 @@ public boolean isClosed() {
104127
return !open;
105128
}
106129

107-
public List<String> vegetables() {
108-
return vegetables;
130+
public List<RestoMeal> vegetables() {
131+
return vegetables2;
109132
}
110133

111134
public LocalDate date() {
@@ -141,13 +164,13 @@ public boolean equals(Object o) {
141164
return open == restoMenu.open &&
142165
Objects.equals(date, restoMenu.date) &&
143166
Objects.equals(meals, restoMenu.meals) &&
144-
Objects.equals(vegetables, restoMenu.vegetables) &&
167+
Objects.equals(vegetables2, restoMenu.vegetables2) &&
145168
Objects.equals(message, restoMenu.message);
146169
}
147170

148171
@Override
149172
public int hashCode() {
150-
return Objects.hash(open, date, meals, vegetables, message);
173+
return Objects.hash(open, date, meals, vegetables2, message);
151174
}
152175

153176
@Override
@@ -156,11 +179,11 @@ public int describeContents() {
156179
}
157180

158181
@Override
159-
public void writeToParcel(Parcel dest, int flags) {
182+
public void writeToParcel(@NonNull Parcel dest, int flags) {
160183
ParcelCompat.writeBoolean(dest, this.open);
161184
dest.writeSerializable(this.date);
162185
dest.writeTypedList(this.meals);
163-
dest.writeStringList(this.vegetables);
186+
dest.writeTypedList(this.vegetables2);
164187
dest.writeString(this.message);
165188
}
166189

app/src/main/java/be/ugent/zeus/hydra/resto/RestoPreferenceFragment.java

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public class RestoPreferenceFragment extends PreferenceFragment {
6161
public static final String DEFAULT_CLOSING_TIME = "21:00";
6262
public static final String PREF_RESTO_CLOSING_HOUR = "pref_resto_closing_hour";
6363

64+
public static final String PREF_SHOW_ALLERGENS = "key_show_allergens";
65+
6466
public static String getDefaultRestoEndpoint(Context context) {
6567
return context.getString(R.string.value_resto_default_endpoint);
6668
}

app/src/main/java/be/ugent/zeus/hydra/resto/SingleDayFragment.java

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public void onDestroyView() {
8585
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
8686
super.onViewCreated(view, savedInstanceState);
8787
binding.menuTable.setMenu(data, showAllergens);
88+
binding.allergenWarningText.setVisibility(showAllergens ? View.VISIBLE : View.GONE);
8889
}
8990

9091
public RestoMenu getData() {

0 commit comments

Comments
 (0)