From a73ab76fac1ec670c3ca6a60981c3d30cbb42629 Mon Sep 17 00:00:00 2001 From: Jared Andrews Date: Sat, 25 Mar 2023 16:20:39 -0400 Subject: [PATCH] Add calorie per 100g calculator to 'Add a new entry' screen --- .../ui/AddFoodFragment.java | 108 ++++++++++++++++++ .../main/res/drawable/ic_calculate_24dp.xml | 5 + app/src/main/res/layout/content_food.xml | 2 + app/src/main/res/values-de/strings.xml | 4 + app/src/main/res/values-fr/strings.xml | 4 + app/src/main/res/values/ids.xml | 4 + app/src/main/res/values/strings.xml | 4 + 7 files changed, 131 insertions(+) create mode 100644 app/src/main/res/drawable/ic_calculate_24dp.xml create mode 100644 app/src/main/res/values/ids.xml diff --git a/app/src/main/java/org/secuso/privacyfriendlyfoodtracker/ui/AddFoodFragment.java b/app/src/main/java/org/secuso/privacyfriendlyfoodtracker/ui/AddFoodFragment.java index 3775364..1e7dc59 100644 --- a/app/src/main/java/org/secuso/privacyfriendlyfoodtracker/ui/AddFoodFragment.java +++ b/app/src/main/java/org/secuso/privacyfriendlyfoodtracker/ui/AddFoodFragment.java @@ -16,15 +16,24 @@ */ package org.secuso.privacyfriendlyfoodtracker.ui; +import android.annotation.SuppressLint; +import android.app.AlertDialog; +import android.content.Context; +import android.content.DialogInterface; import android.os.Bundle; +import android.text.Editable; import android.text.InputFilter; import android.text.InputType; +import android.text.TextWatcher; import android.util.Log; +import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.EditText; +import android.widget.LinearLayout; import android.widget.TextView; +import android.widget.Toast; import androidx.fragment.app.Fragment; import androidx.lifecycle.ViewModelProvider; @@ -52,6 +61,7 @@ public class AddFoodFragment extends Fragment { TextView textView; DatabaseFacade databaseFacade; EditText amountField; + TextInputLayout caloriesFieldLayout; EditText caloriesField; /** * The required empty public constructor @@ -84,6 +94,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, InputFilter[] caloriesFilter = { new InputFilter.LengthFilter(10) }; amountField = parentHolder.findViewById(R.id.input_amount); + caloriesFieldLayout = parentHolder.findViewById(R.id.inputCalories); caloriesField = parentHolder.findViewById(R.id.input_calories); amountField.setFilters(amountFilter); amountField.setInputType(InputType.TYPE_CLASS_NUMBER); @@ -132,6 +143,8 @@ public void setUserVisibleHint(boolean isVisible) { nameField.setFocusable(false); nameField.setClickable(false); nameField.setTextColor(getResources().getColor(R.color.middlegrey)); + caloriesFieldLayout.setEndIconActivated(false); + caloriesFieldLayout.setEndIconMode(TextInputLayout.END_ICON_NONE); caloriesField.setText(String.format(Locale.ENGLISH, "%.2f", referenceActivity.calories)); caloriesField.setFocusable(false); caloriesField.setClickable(false); @@ -144,6 +157,9 @@ public void setUserVisibleHint(boolean isVisible) { nameField.setFocusableInTouchMode(true); nameField.setClickable(true); nameField.setTextColor(getResources().getColor(R.color.black)); + caloriesFieldLayout.setEndIconActivated(true); + caloriesFieldLayout.setEndIconMode(TextInputLayout.END_ICON_CUSTOM); + caloriesFieldLayout.setEndIconOnClickListener(caloriesPer100GCalculatorClickListener); caloriesField.setText(""); caloriesField.setFocusable(true); caloriesField.setFocusableInTouchMode(true); @@ -227,4 +243,96 @@ private void showErrorMessage(View view, int errorMessageId){ } } + private final View.OnClickListener caloriesPer100GCalculatorClickListener = v -> { + final Context context = getContext(); + if (context == null) return; + + final AlertDialog.Builder caloriesPer100GramsCalculatorDialog = new AlertDialog.Builder(context); + + caloriesPer100GramsCalculatorDialog.setTitle(getString(R.string.calculate_kcal_per_100g)); + + LinearLayout container = new LinearLayout(context); + container.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); + container.setOrientation(LinearLayout.VERTICAL); + + final EditText calories = new EditText(context); + InputFilter[] caloriesInputFilter = new InputFilter[1]; + caloriesInputFilter[0] = new InputFilter.LengthFilter(5); + calories.setFilters(caloriesInputFilter); + calories.setInputType(InputType.TYPE_CLASS_NUMBER); + + final TextInputLayout caloriesLayout = new TextInputLayout(context); + caloriesLayout.addView(calories); + caloriesLayout.setHint(getString(R.string.calories)); + + final EditText grams = new EditText(context); + InputFilter[] gramsInputFilter = new InputFilter[1]; + gramsInputFilter[0] = new InputFilter.LengthFilter(5); + grams.setFilters(gramsInputFilter); + grams.setInputType(InputType.TYPE_CLASS_NUMBER); + + final TextInputLayout gramsLayout = new TextInputLayout(context); + gramsLayout.addView(grams); + gramsLayout.setHint(getString(R.string.grams)); + + final String emptyResult = String.format(getString(R.string.x_equals_n), getString(R.string.hint_food_calories), "~"); + + final TextView result = new TextView(context); + result.setGravity(Gravity.CENTER_HORIZONTAL); + result.setText(emptyResult); + + final TextWatcher textWatcher = new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { + + } + + @Override + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { + + } + + @Override + public void afterTextChanged(Editable editable) { + try { + Float caloriesAmount = Float.parseFloat(calories.getText().toString()); + Float gramsAmount = Float.parseFloat(grams.getText().toString()); + + float caloriesPer100G = (caloriesAmount / gramsAmount) * 100; + + if (!Float.isNaN(caloriesPer100G) && !Float.isInfinite(caloriesPer100G)) { + + result.setText(String.format(getString(R.string.x_equals_n), getString(R.string.hint_food_calories), Float.toString(caloriesPer100G))); + result.setTag(R.id.tag_calories_per_calculator_result, caloriesPer100G); + return; + } + } catch (NumberFormatException e) { + // no-op + } + + result.setText(emptyResult); + } + }; + + calories.addTextChangedListener(textWatcher); + grams.addTextChangedListener(textWatcher); + + container.addView(caloriesLayout); + container.addView(gramsLayout); + container.addView(result); + + caloriesPer100GramsCalculatorDialog.setView(container); + + caloriesPer100GramsCalculatorDialog.setPositiveButton(getResources().getString(R.string.save), (dialogInterface, i) -> { + Object tag = result.getTag(R.id.tag_calories_per_calculator_result); + if (tag instanceof Float) { + caloriesField.setText(Float.toString((float) tag)); + } + }); + + caloriesPer100GramsCalculatorDialog.setNegativeButton(getResources().getString(R.string.decline), (dialog, whichButton) -> {}); + + caloriesPer100GramsCalculatorDialog.show(); + }; + } diff --git a/app/src/main/res/drawable/ic_calculate_24dp.xml b/app/src/main/res/drawable/ic_calculate_24dp.xml new file mode 100644 index 0000000..36f0426 --- /dev/null +++ b/app/src/main/res/drawable/ic_calculate_24dp.xml @@ -0,0 +1,5 @@ + + + diff --git a/app/src/main/res/layout/content_food.xml b/app/src/main/res/layout/content_food.xml index c75d5bb..867eea9 100644 --- a/app/src/main/res/layout/content_food.xml +++ b/app/src/main/res/layout/content_food.xml @@ -57,6 +57,8 @@ android:layout_width="match_parent" android:layout_height="wrap_content" app:errorEnabled="true" + app:endIconMode="custom" + app:endIconDrawable="@drawable/ic_calculate_24dp" app:layout_constraintTop_toBottomOf="@+id/inputFoodAmount"> Bestätigen Abbrechen Löschen? + Gramm + Kalorien + %1$s = %2$s + Berechnung kCal/100g Erstelle Key Erstelle Passphrase diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index a6e0694..ae41e84 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -72,4 +72,8 @@ Example Switch Example Switch Summary Example Summary + Grammes + Calories + %1$s = %2$s + Calculer kCal/100g diff --git a/app/src/main/res/values/ids.xml b/app/src/main/res/values/ids.xml new file mode 100644 index 0000000..5483857 --- /dev/null +++ b/app/src/main/res/values/ids.xml @@ -0,0 +1,4 @@ + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c8cd735..0efa219 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -86,6 +86,10 @@ Confirm Cancel Delete? + Grams + Calories + %1$s = %2$s + Calculate kCal/100g Generating Key