Skip to content

Commit

Permalink
Fix #35: Use simpler fractions
Browse files Browse the repository at this point in the history
Fix #37: Use better calculation
  • Loading branch information
Tom committed Jul 30, 2023
1 parent 3919d23 commit 31fe40c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
23 changes: 21 additions & 2 deletions lib/components/widgets/recipe_detail_tabbar_widget.dart
@@ -1,3 +1,5 @@
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:untare/cubits/settings_cubit.dart';
Expand Down Expand Up @@ -262,11 +264,28 @@ class RecipeDetailTabBarWidgetState extends State<RecipeDetailTabBarWidget> {
SettingsCubit settingsCubit = context.read<SettingsCubit>();
bool? useFractions = (settingsCubit.state.userServerSetting!.useFractions == true);

double rawAmount = (ingredient.amount * (((newServing/initServing))*100).ceil()/100);
double rawAmount = ingredient.amount * newServing / initServing;
String amount = (ingredient.amount > 0) ? ('${rawAmount.toFormattedString()} ') : '';
if (amount != '' && useFractions == true && (rawAmount % 1) != 0) {
amount = '${rawAmount.toMixedFraction()} ';
// If we have a complex decimal we build a "simple" fraction. Otherwise we do the normal one
if ((((rawAmount - rawAmount.toInt()) * 100) % 5) != 0) {
// Use this crap because we can't change precision programmatically
if (rawAmount.toInt() < 1) {
amount = '${MixedFraction.fromDouble(rawAmount, precision: 1.0e-1).reduce()} ';
} else if (rawAmount.toInt() < 10) {
amount = '${MixedFraction.fromDouble(rawAmount, precision: 1.0e-2).reduce()} ';
} else if (rawAmount.toInt() < 100) {
amount = '${MixedFraction.fromDouble(rawAmount, precision: 1.0e-3).reduce()} ';
} else if(rawAmount.toInt() < 1000) {
amount = '${MixedFraction.fromDouble(rawAmount, precision: 1.0e-4).reduce()} ';
} else {
amount = '${MixedFraction.fromDouble(rawAmount, precision: 1.0e-5).reduce()} ';
}
} else {
amount = '${MixedFraction.fromDouble(rawAmount)} ';
}
}

String unit = (ingredient.amount > 0 && ingredient.unit != null) ? ('${ingredient.unit!.getUnitName(rawAmount)} ') : '';
String food = (ingredient.food != null) ? ('${ingredient.food!.getFoodName(rawAmount)} ') : '';
String note = (ingredient.note != null && ingredient.note != '') ? ('(${ingredient.note!})') : '';
Expand Down
21 changes: 19 additions & 2 deletions lib/components/widgets/recipe_shopping_list_stateful_widget.dart
Expand Up @@ -223,11 +223,28 @@ class RecipeShoppingListWidgetState extends State<RecipeShoppingListWidget> {
SettingsCubit settingsCubit = context.read<SettingsCubit>();
bool? useFractions = (settingsCubit.state.userServerSetting!.useFractions == true);

double rawAmount = (ingredient.amount * (((newServing/initServing))*100).ceil()/100);
double rawAmount = ingredient.amount * newServing / initServing;
String amount = (ingredient.amount > 0) ? ('${rawAmount.toFormattedString()} ') : '';
if (amount != '' && useFractions == true && (rawAmount % 1) != 0) {
amount = '${rawAmount.toMixedFraction()} ';
// If we have a complex decimal we build a "simple" fraction. Otherwise we do the normal one
if ((((rawAmount - rawAmount.toInt()) * 100) % 5) != 0) {
// Use this crap because we can't change precision programmatically
if (rawAmount.toInt() < 1) {
amount = '${MixedFraction.fromDouble(rawAmount, precision: 1.0e-1).reduce()} ';
} else if (rawAmount.toInt() < 10) {
amount = '${MixedFraction.fromDouble(rawAmount, precision: 1.0e-2).reduce()} ';
} else if (rawAmount.toInt() < 100) {
amount = '${MixedFraction.fromDouble(rawAmount, precision: 1.0e-3).reduce()} ';
} else if(rawAmount.toInt() < 1000) {
amount = '${MixedFraction.fromDouble(rawAmount, precision: 1.0e-4).reduce()} ';
} else {
amount = '${MixedFraction.fromDouble(rawAmount, precision: 1.0e-5).reduce()} ';
}
} else {
amount = '${MixedFraction.fromDouble(rawAmount)} ';
}
}

String unit = (ingredient.unit != null && ingredient.amount > 0) ? ('${ingredient.unit!.getUnitName(rawAmount)} ') : '';
String food = (ingredient.food != null) ? ('${ingredient.food!.getFoodName(rawAmount)} ') : '';
bool? checkBoxValue = !(ingredient.food != null && ingredient.food!.onHand!);
Expand Down

0 comments on commit 31fe40c

Please sign in to comment.