Skip to content

Commit

Permalink
Implement change to read full serving size strings instead of buildin…
Browse files Browse the repository at this point in the history
…g them
  • Loading branch information
slavick committed May 9, 2022
1 parent c0ce7f7 commit 830eff3
Showing 1 changed file with 7 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class FoodInfo {
Expand Down Expand Up @@ -329,20 +328,14 @@ public static List<String> getServingSizes(final String foodName,
}

private static int getServingSizesResourceId(final Context context,
final String idSuffix) {
final String foodName,
@Units.Interface final int unitType) {
return context.getResources().getIdentifier(
"food_info_serving_sizes_" + idSuffix.toLowerCase(),
"food_info_serving_sizes_" + (foodName + (unitType == Units.IMPERIAL ? "_imperial" : "_metric")).toLowerCase(),
"array",
context.getApplicationInfo().packageName);
}

private static int getServingSizesResourceId(final Context context,
final String foodName,
@Units.Interface final int unitType) {
return getServingSizesResourceId(context,
foodName + (unitType == Units.IMPERIAL ? "_imperial" : "_metric"));
}

private static void initServingSizes(Context context) {
servingSizesImperial = new ArrayMap<>();
servingSizesMetric = new ArrayMap<>();
Expand All @@ -354,31 +347,19 @@ private static void initServingSizes(Context context) {

private static void initServingSizesForFood(final Context context, final String foodIdName) {
final Resources res = context.getResources();
final Locale locale = Locale.getDefault();

// Convert food name to resource id pattern ("Other Vegetables" becomes "other_vegetables")
final String formattedFoodIdName = foodIdName.toLowerCase().replace(" ", "_");

try {
// Dynamically load the string-arrays for food.
// The naming convention below must be followed:
// food_info_serving_sizes_<formattedFoodIdName>
// food_info_serving_sizes_<formattedFoodIdName>_imperial
// food_info_serving_sizes_<formattedFoodIdName>_metric
String[] servingSizeTexts = res.getStringArray(getServingSizesResourceId(context, formattedFoodIdName));
String[] imperialServingSizes = res.getStringArray(getServingSizesResourceId(context, formattedFoodIdName, Units.IMPERIAL));
String[] metricServingSizes = res.getStringArray(getServingSizesResourceId(context, formattedFoodIdName, Units.METRIC));

final List<String> imperial = new ArrayList<>();
final List<String> metric = new ArrayList<>();

for (int i = 0; i < servingSizeTexts.length; i++) {
imperial.add(String.format(locale, servingSizeTexts[i], imperialServingSizes[i]));
metric.add(String.format(locale, servingSizeTexts[i], metricServingSizes[i]));
}

servingSizesImperial.put(foodIdName, imperial);
servingSizesMetric.put(foodIdName, metric);
servingSizesImperial.put(foodIdName,
Arrays.asList(res.getStringArray(getServingSizesResourceId(context, formattedFoodIdName, Units.IMPERIAL))));
servingSizesMetric.put(foodIdName,
Arrays.asList(res.getStringArray(getServingSizesResourceId(context, formattedFoodIdName, Units.METRIC))));
} catch (Resources.NotFoundException e) {
// Vitamin B12 doesn't need the above functionality and therefore doesn't have the
// required resource ids for the above code to function correctly
Expand Down

0 comments on commit 830eff3

Please sign in to comment.