Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add calorie per 100g calculator to 'Add a new entry' screen #103

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,6 +61,7 @@ public class AddFoodFragment extends Fragment {
TextView textView;
DatabaseFacade databaseFacade;
EditText amountField;
TextInputLayout caloriesFieldLayout;
EditText caloriesField;
/**
* The required empty public constructor
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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();
};

}
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_calculate_24dp.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#000000"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="@android:color/white" android:pathData="M19,3H5C3.9,3 3,3.9 3,5v14c0,1.1 0.9,2 2,2h14c1.1,0 2,-0.9 2,-2V5C21,3.9 20.1,3 19,3zM13.03,7.06L14.09,6l1.41,1.41L16.91,6l1.06,1.06l-1.41,1.41l1.41,1.41l-1.06,1.06L15.5,9.54l-1.41,1.41l-1.06,-1.06l1.41,-1.41L13.03,7.06zM6.25,7.72h5v1.5h-5V7.72zM11.5,16h-2v2H8v-2H6v-1.5h2v-2h1.5v2h2V16zM18,17.25h-5v-1.5h5V17.25zM18,14.75h-5v-1.5h5V14.75z"/>
</vector>
2 changes: 2 additions & 0 deletions app/src/main/res/layout/content_food.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<EditText
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@
<string name="action_confirm">Bestätigen</string>
<string name="action_cancel">Abbrechen</string>
<string name="delete_entry_prompt">Löschen?</string>
<string name="grams">Gramm</string>
<string name="calories">Kalorien</string>
<string name="x_equals_n">%1$s = %2$s</string>
<string name="calculate_kcal_per_100g">Berechnung kCal/100g</string>

<string name="setup_step_generating_key">Erstelle Key</string>
<string name="setup_step_generating_passphrase">Erstelle Passphrase</string>
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,8 @@
<string name="pref_example_switch">Example Switch</string>
<string name="pref_example_switch_summary">Example Switch Summary</string>
<string name="pref_example_summary">Example Summary</string>
<string name="grams">Grammes</string>
<string name="calories">Calories</string>
<string name="x_equals_n">%1$s = %2$s</string>
<string name="calculate_kcal_per_100g">Calculer kCal/100g</string>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="tag_calories_per_calculator_result" />
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@
<string name="action_confirm">Confirm</string>
<string name="action_cancel">Cancel</string>
<string name="delete_entry_prompt">Delete?</string>
<string name="grams">Grams</string>
<string name="calories">Calories</string>
<string name="x_equals_n">%1$s = %2$s</string>
<string name="calculate_kcal_per_100g">Calculate kCal/100g</string>


<string name="setup_step_generating_key">Generating Key</string>
Expand Down