From 9ce08e660ed780ca50803e9ea6b39fcb23bc7aca Mon Sep 17 00:00:00 2001 From: inad9300 Date: Sat, 22 Sep 2018 09:42:16 +0200 Subject: [PATCH] Add negative streak (#110) --- .../nutritionfacts/dailydozen/model/Servings.java | 8 ++++++-- .../dailydozen/widget/FoodServings.java | 2 +- .../dailydozen/widget/StreakWidget.java | 12 +++++++++++- .../main/res/drawable/rounded_rectangle_amber.xml | 8 ++++++++ .../main/res/drawable/rounded_rectangle_orange.xml | 8 ++++++++ app/src/main/res/drawable/rounded_rectangle_red.xml | 8 ++++++++ app/src/main/res/values/colors.xml | 3 +++ 7 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 app/src/main/res/drawable/rounded_rectangle_amber.xml create mode 100644 app/src/main/res/drawable/rounded_rectangle_orange.xml create mode 100644 app/src/main/res/drawable/rounded_rectangle_red.xml diff --git a/app/src/main/java/org/nutritionfacts/dailydozen/model/Servings.java b/app/src/main/java/org/nutritionfacts/dailydozen/model/Servings.java index 5916097d..af0c0811 100644 --- a/app/src/main/java/org/nutritionfacts/dailydozen/model/Servings.java +++ b/app/src/main/java/org/nutritionfacts/dailydozen/model/Servings.java @@ -58,9 +58,13 @@ public void setServings(int servings) { } public void recalculateStreak() { + int priorStreak = getStreakFromDayBefore(); + if (servings == food.getRecommendedServings()) { - streak = getStreakFromDayBefore() + 1; - } else if (servings < food.getRecommendedServings()) { + streak = priorStreak >= 0 ? priorStreak + 1 : 1; + } else if (servings == 0) { + streak = priorStreak <= 0 ? priorStreak - 1 : 0; + } else { streak = 0; } } diff --git a/app/src/main/java/org/nutritionfacts/dailydozen/widget/FoodServings.java b/app/src/main/java/org/nutritionfacts/dailydozen/widget/FoodServings.java index 92ea239a..73bb6367 100644 --- a/app/src/main/java/org/nutritionfacts/dailydozen/widget/FoodServings.java +++ b/app/src/main/java/org/nutritionfacts/dailydozen/widget/FoodServings.java @@ -87,7 +87,7 @@ private void initFoodName() { private void initFoodStreak(Servings servings) { final int streak = servings != null ? servings.getStreak() : 0; - if (streak > 0) { + if (streak != 0) { tvStreak.setVisibility(VISIBLE); tvStreak.setStreak(streak); } else { diff --git a/app/src/main/java/org/nutritionfacts/dailydozen/widget/StreakWidget.java b/app/src/main/java/org/nutritionfacts/dailydozen/widget/StreakWidget.java index b19f8e96..87954866 100644 --- a/app/src/main/java/org/nutritionfacts/dailydozen/widget/StreakWidget.java +++ b/app/src/main/java/org/nutritionfacts/dailydozen/widget/StreakWidget.java @@ -31,7 +31,6 @@ public StreakWidget(Context context, AttributeSet attrs, int defStyle) { public void setStreak(int streak) { if (streak > ONE_DAY) { setVisibility(View.VISIBLE); - setText(getContext().getString(R.string.format_num_days, streak)); if (streak < ONE_WEEK) { @@ -41,6 +40,17 @@ public void setStreak(int streak) { } else if (streak >= TWO_WEEKS) { setBackgroundAndTextColor(R.drawable.rounded_rectangle_gold, android.R.color.black); } + } else if (streak < -ONE_DAY) { + setVisibility(View.VISIBLE); + setText(getContext().getString(R.string.format_num_days, streak)); + + if (streak > -ONE_WEEK) { + setBackgroundAndTextColor(R.drawable.rounded_rectangle_amber, android.R.color.black); + } else if (streak <= -ONE_WEEK && streak > -TWO_WEEKS) { + setBackgroundAndTextColor(R.drawable.rounded_rectangle_orange, android.R.color.black); + } else if (streak <= -TWO_WEEKS) { + setBackgroundAndTextColor(R.drawable.rounded_rectangle_red, android.R.color.white); + } } else { setVisibility(View.GONE); } diff --git a/app/src/main/res/drawable/rounded_rectangle_amber.xml b/app/src/main/res/drawable/rounded_rectangle_amber.xml new file mode 100644 index 00000000..be66d5fc --- /dev/null +++ b/app/src/main/res/drawable/rounded_rectangle_amber.xml @@ -0,0 +1,8 @@ + + + + + + + diff --git a/app/src/main/res/drawable/rounded_rectangle_orange.xml b/app/src/main/res/drawable/rounded_rectangle_orange.xml new file mode 100644 index 00000000..9d7e6599 --- /dev/null +++ b/app/src/main/res/drawable/rounded_rectangle_orange.xml @@ -0,0 +1,8 @@ + + + + + + + diff --git a/app/src/main/res/drawable/rounded_rectangle_red.xml b/app/src/main/res/drawable/rounded_rectangle_red.xml new file mode 100644 index 00000000..0dcdc4aa --- /dev/null +++ b/app/src/main/res/drawable/rounded_rectangle_red.xml @@ -0,0 +1,8 @@ + + + + + + + diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index a9cfe147..fa9ae196 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -18,5 +18,8 @@ #FFD700 #C0C0C0 #8C7853 + #F6B31B + #EA7A24 + #EA3325 #A52A2A