Skip to content

Commit

Permalink
v21.2 (#207)
Browse files Browse the repository at this point in the history
* unified imperial, metric strings (removes need for %s) (#204)

see #201

* Implement change to read full serving size strings instead of building them

* russian update, escaped quotes, %s simplification  (#205)

* russian update

* xml fixes: `\\'` to `\'`, `\ N` to `\n\n`

* escaped double quotes `\"`

* fixed "about_text_lines" mapping. also some misc update (fractions)

* some Italian updates. French: l\\'application --> l\'application

* Update versionCode and versionName

* Remove redundant label

* Add try/catch for exception that has been observed

* Remove unpopular translations

* Update strings

* Change createWeightsIfDoesNotExist to return boolean (indicating weights saved) instead of the unused Weights object

* Remove unused code

* Revert "Remove unpopular translations"

This reverts commit a6b9c9c.

* Update strings

* Update versionCode

* Update Spanish translation

* Update versionCode and versionName

* Update Spanish (#206)

Co-authored-by: --marc <[email protected]>
  • Loading branch information
slavick and marc-medley authored Aug 14, 2022
1 parent 6f0edc8 commit c89d673
Show file tree
Hide file tree
Showing 22 changed files with 1,820 additions and 3,079 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ android {
minSdkVersion 16
targetSdkVersion 30
vectorDrawables.useSupportLibrary = true
versionCode 69
versionName "21"
versionCode 72
versionName "21.2"
}
buildTypes {
debug {
Expand Down
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@
android:name=".activity.MainActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
android:configChanges="orientation|keyboardHidden">

<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.List;

import hugo.weaving.DebugLog;
import timber.log.Timber;

@Table(name = "foods")
public class Food extends Model implements RDA {
Expand Down Expand Up @@ -111,7 +112,11 @@ private static void createFoodIfDoesNotExist(final String foodName,
}

if (needToSave) {
food.save();
try {
food.save();
} catch (java.lang.SecurityException e) {
Timber.e(e, "Caught SecurityException in createFoodIfDoesNotExist");
}
}
}

Expand Down
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
48 changes: 7 additions & 41 deletions app/src/main/java/org/nutritionfacts/dailydozen/model/Weights.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import com.activeandroid.annotation.Table;
import com.activeandroid.query.Select;

import java.util.List;

@Table(name = "weights")
public class Weights extends TruncatableModel {
@Column(name = "date_id")
Expand Down Expand Up @@ -61,19 +59,21 @@ public Float getAverageWeight() {
return null;
}

public static Weights createWeightsIfDoesNotExist(final Day day, float morningWeight, float eveningWeight) {
public static boolean createWeightsIfDoesNotExist(final Day day, float morningWeight, float eveningWeight) {
Weights weights = getWeightsOnDay(day);

if (weights == null) {
weights = new Weights(day, morningWeight, eveningWeight);
} else {
}

if (weights.morningWeight != morningWeight || weights.eveningWeight != eveningWeight) {
weights.setMorningWeight(morningWeight);
weights.setEveningWeight(eveningWeight);
weights.save();
return true;
}

weights.save();

return weights;
return false;
}

public static Weights getWeightsOnDay(final Day day) {
Expand All @@ -89,38 +89,4 @@ public static Weights getWeightsOnDay(final Day day) {
public static boolean isEmpty() {
return new Select().from(Weights.class).count() == 0;
}

public static float getAverageWeightInMonth(final int year, final int monthOneBased) {
return getAverageWeightForDays(Day.getDaysInYearAndMonth(year, monthOneBased));
}

public static float getAverageWeightInYear(final int year) {
return getAverageWeightForDays(Day.getDaysInYear(year));
}

private static float getAverageWeightForDays(final List<Day> days) {
if (days == null || days.isEmpty()) {
return 0;
}

int totalWeight = 0;
int daysWithWeights = 0;

for (Day day : days) {
Weights weights = getWeightsOnDay(day);
if (weights != null) {
Float averageWeight = weights.getAverageWeight();
if (averageWeight != null) {
totalWeight += averageWeight;
daysWithWeights++;
}
}
}

if (daysWithWeights == 0) {
return 0;
} else {
return (float) totalWeight / daysWithWeights;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,10 @@ public void onTextChanged(CharSequence s, int start, int before, int count) {

if (morningWeight > 0 || eveningWeight > 0) {
day = Day.createDayIfDoesNotExist(day);
Weights.createWeightsIfDoesNotExist(day, morningWeight, eveningWeight);
Timber.d("Saving morning weight [%s] and evening weight [%s]", morningWeight, eveningWeight);
boolean weightSaved = Weights.createWeightsIfDoesNotExist(day, morningWeight, eveningWeight);
if (weightSaved) {
Timber.d("Saving morning weight [%s] and evening weight [%s]", morningWeight, eveningWeight);
}
}
} catch (NumberFormatException e) {
Timber.e(e);
Expand Down
Loading

0 comments on commit c89d673

Please sign in to comment.