Skip to content
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
5 changes: 4 additions & 1 deletion lib/core/data/data_source/user_data_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ class UserDataSource {
weightKG: 80,
gender: UserGenderDBO.male,
goal: UserWeightGoalDBO.maintainWeight,
pal: UserPALDBO.active);
pal: UserPALDBO.active,
carbsPercentageGoal: 0.6,
fatsPercentageGoal: 0.25,
proteinsPercentageGoal: 0.15);
}
}
18 changes: 16 additions & 2 deletions lib/core/data/dbo/user_dbo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,24 @@ class UserDBO extends HiveObject {
UserWeightGoalDBO goal;
@HiveField(5)
UserPALDBO pal;
@HiveField(6)
double carbsPercentageGoal;
@HiveField(7)
double fatsPercentageGoal;
@HiveField(8)
double proteinsPercentageGoal;

UserDBO(
{required this.birthday,
required this.heightCM,
required this.weightKG,
required this.gender,
required this.goal,
required this.pal});
required this.pal,
required this.carbsPercentageGoal,
required this.fatsPercentageGoal,
required this.proteinsPercentageGoal
});

factory UserDBO.fromUserEntity(UserEntity entity) {
return UserDBO(
Expand All @@ -36,6 +46,10 @@ class UserDBO extends HiveObject {
weightKG: entity.weightKG,
gender: UserGenderDBO.fromUserGenderEntity(entity.gender),
goal: UserWeightGoalDBO.fromUserWeightGoalEntity(entity.goal),
pal: UserPALDBO.fromUserPALEntity(entity.pal));
pal: UserPALDBO.fromUserPALEntity(entity.pal),
carbsPercentageGoal: entity.carbsPercentageGoal,
fatsPercentageGoal: entity.fatsPercentageGoal,
proteinsPercentageGoal: entity.proteinsPercentageGoal
);
}
}
13 changes: 11 additions & 2 deletions lib/core/data/dbo/user_dbo.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions lib/core/domain/entity/user_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@ class UserEntity {
UserGenderEntity gender;
UserWeightGoalEntity goal;
UserPALEntity pal;
double carbsPercentageGoal;
double fatsPercentageGoal;
double proteinsPercentageGoal;

UserEntity(
{required this.birthday,
required this.heightCM,
required this.weightKG,
required this.gender,
required this.goal,
required this.pal});
required this.pal,
required this.carbsPercentageGoal,
required this.fatsPercentageGoal,
required this.proteinsPercentageGoal});

factory UserEntity.fromUserDBO(UserDBO userDBO) {
return UserEntity(
Expand All @@ -26,7 +32,10 @@ class UserEntity {
weightKG: userDBO.weightKG,
gender: UserGenderEntity.fromUserGenderDBO(userDBO.gender),
goal: UserWeightGoalEntity.fromUserWeightGoalDBO(userDBO.goal),
pal: UserPALEntity.fromUserPALDBO(userDBO.pal));
pal: UserPALEntity.fromUserPALDBO(userDBO.pal),
carbsPercentageGoal: userDBO.carbsPercentageGoal,
fatsPercentageGoal: userDBO.fatsPercentageGoal,
proteinsPercentageGoal: userDBO.proteinsPercentageGoal);
}

int get age => DateTime.now().difference(birthday).inDays~/365;
Expand Down
18 changes: 8 additions & 10 deletions lib/core/utils/calc/macro_calc.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:opennutritracker/core/domain/entity/user_entity.dart';

class MacroCalc {
/// Information provided by
/// 'OBESITY: PREVENTING AND MANAGING
Expand All @@ -8,16 +10,12 @@ class MacroCalc {
static const _fatKcalPerGram = 9.0;
static const _proteinKcalPerGram = 4.0;

static const _defaultCarbsPercentageGoal = 0.6;
static const _defaultFatsPercentageGoal = 0.25;
static const _defaultProteinsPercentageGoal = 0.15;

static double getTotalCarbsGoal(double totalCalorieGoal) =>
(totalCalorieGoal * _defaultCarbsPercentageGoal) / _carbsKcalPerGram;
static double getTotalCarbsGoal(UserEntity userEntity, double totalCalorieGoal) =>
(totalCalorieGoal * userEntity.carbsPercentageGoal) / _carbsKcalPerGram;

static double getTotalFatsGoal(double totalCalorieGoal) =>
(totalCalorieGoal * _defaultFatsPercentageGoal) / _fatKcalPerGram;
static double getTotalFatsGoal(UserEntity userEntity, double totalCalorieGoal) =>
(totalCalorieGoal * userEntity.fatsPercentageGoal) / _fatKcalPerGram;

static double getTotalProteinsGoal(double totalCalorieGoal) =>
(totalCalorieGoal * _defaultProteinsPercentageGoal) / _proteinKcalPerGram;
static double getTotalProteinsGoal(UserEntity userEntity, double totalCalorieGoal) =>
(totalCalorieGoal * userEntity.proteinsPercentageGoal) / _proteinKcalPerGram;
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,19 @@ class ActivityDetailBloc
final userEntity = await _getUserUsecase.getUserData();
final totalKcalGoal =
CalorieGoalCalc.getTotalKcalGoal(userEntity, caloriesBurned);
final totalCarbsGoal = MacroCalc.getTotalCarbsGoal(totalKcalGoal);
final totalFatGoal = MacroCalc.getTotalFatsGoal(totalKcalGoal);
final totalProteinGoal = MacroCalc.getTotalProteinsGoal(totalKcalGoal);
final totalCarbsGoal = MacroCalc.getTotalCarbsGoal(userEntity, totalKcalGoal);
final totalFatGoal = MacroCalc.getTotalFatsGoal(userEntity, totalKcalGoal);
final totalProteinGoal = MacroCalc.getTotalProteinsGoal(userEntity, totalKcalGoal);

final hasTrackedDay =
await _addTrackedDayUsecase.hasTrackedDay(DateTime.now());
if (!hasTrackedDay) {
await _addTrackedDayUsecase.addNewTrackedDay(dateTime, totalKcalGoal,
totalCarbsGoal, totalFatGoal, totalProteinGoal);
}
final carbsIncrease = MacroCalc.getTotalCarbsGoal(caloriesBurned);
final fatIncrease = MacroCalc.getTotalFatsGoal(caloriesBurned);
final proteinIncrease = MacroCalc.getTotalProteinsGoal(caloriesBurned);
final carbsIncrease = MacroCalc.getTotalCarbsGoal(userEntity, caloriesBurned);
final fatIncrease = MacroCalc.getTotalFatsGoal(userEntity, caloriesBurned);
final proteinIncrease = MacroCalc.getTotalProteinsGoal(userEntity, caloriesBurned);

_addTrackedDayUsecase.increaseDayCalorieGoal(dateTime, caloriesBurned);
_addTrackedDayUsecase.increaseDayMacroGoals(dateTime,
Expand Down
10 changes: 5 additions & 5 deletions lib/features/diary/presentation/bloc/calendar_day_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'package:opennutritracker/core/domain/usecase/delete_user_activity_usecas
import 'package:opennutritracker/core/domain/usecase/get_intake_usecase.dart';
import 'package:opennutritracker/core/domain/usecase/get_tracked_day_usecase.dart';
import 'package:opennutritracker/core/domain/usecase/get_user_activity_usecase.dart';
import 'package:opennutritracker/core/utils/calc/macro_calc.dart';
import 'package:opennutritracker/core/utils/locator.dart';
import 'package:opennutritracker/features/diary/presentation/bloc/diary_bloc.dart';

Expand Down Expand Up @@ -76,12 +75,13 @@ class CalendarDayBloc extends Bloc<CalendarDayEvent, CalendarDayState> {
Future<void> deleteUserActivityItem(BuildContext context,
UserActivityEntity activityEntity, DateTime day) async {
await _deleteUserActivityUsecase.deleteUserActivity(activityEntity);

_addTrackedDayUsecase.reduceDayCalorieGoal(day, activityEntity.burnedKcal);

final carbsAmount = MacroCalc.getTotalCarbsGoal(activityEntity.burnedKcal);
final fatAmount = MacroCalc.getTotalFatsGoal(activityEntity.burnedKcal);
final proteinAmount =
MacroCalc.getTotalProteinsGoal(activityEntity.burnedKcal);
const carbsAmount = 3.3; //MacroCalc.getTotalCarbsGoal(userEntity, activityEntity.burnedKcal);
final fatAmount = 3.3; //MacroCalc.getTotalFatsGoal(userEntity, activityEntity.burnedKcal);
final proteinAmount = 3.3;
//MacroCalc.getTotalProteinsGoal(userEntity, activityEntity.burnedKcal);

_addTrackedDayUsecase.reduceDayMacroGoals(day,
carbsAmount: carbsAmount,
Expand Down
13 changes: 7 additions & 6 deletions lib/features/home/presentation/bloc/home_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {
final user = await _getUserUsecase.getUserData();
final totalKcalGoal =
CalorieGoalCalc.getTotalKcalGoal(user, totalKcalActivities);
final totalCarbsGoal = MacroCalc.getTotalCarbsGoal(totalKcalGoal);
final totalFatsGoal = MacroCalc.getTotalFatsGoal(totalKcalGoal);
final totalProteinsGoal = MacroCalc.getTotalProteinsGoal(totalKcalGoal);
final totalCarbsGoal = MacroCalc.getTotalCarbsGoal(user, totalKcalGoal);
final totalFatsGoal = MacroCalc.getTotalFatsGoal(user, totalKcalGoal);
final totalProteinsGoal = MacroCalc.getTotalProteinsGoal(user, totalKcalGoal);

final totalKcalLeft =
CalorieGoalCalc.getDailyKcalLeft(totalKcalGoal, totalKcalIntake);
Expand Down Expand Up @@ -190,14 +190,15 @@ class HomeBloc extends Bloc<HomeEvent, HomeState> {

Future<void> deleteUserActivityItem(UserActivityEntity activityEntity) async {
final dateTime = DateTime.now();
final user = await _getUserUsecase.getUserData();
await _deleteUserActivityUsecase.deleteUserActivity(activityEntity);
_addTrackedDayUseCase.reduceDayCalorieGoal(
dateTime, activityEntity.burnedKcal);

final carbsAmount = MacroCalc.getTotalCarbsGoal(activityEntity.burnedKcal);
final fatAmount = MacroCalc.getTotalFatsGoal(activityEntity.burnedKcal);
final carbsAmount = MacroCalc.getTotalCarbsGoal(user, activityEntity.burnedKcal);
final fatAmount = MacroCalc.getTotalFatsGoal(user, activityEntity.burnedKcal);
final proteinAmount =
MacroCalc.getTotalProteinsGoal(activityEntity.burnedKcal);
MacroCalc.getTotalProteinsGoal(user, activityEntity.burnedKcal);

_addTrackedDayUseCase.reduceDayMacroGoals(dateTime,
carbsAmount: carbsAmount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ class MealDetailBloc {
if (!hasTrackedDay) {
final userEntity = await _getUserUsecase.getUserData();
final totalKcalGoal = CalorieGoalCalc.getTotalKcalGoal(userEntity, 0);
final totalCarbsGoal = MacroCalc.getTotalCarbsGoal(totalKcalGoal);
final totalFatGoal = MacroCalc.getTotalFatsGoal(totalKcalGoal);
final totalProteinGoal = MacroCalc.getTotalProteinsGoal(totalKcalGoal);
final totalCarbsGoal = MacroCalc.getTotalCarbsGoal(userEntity, totalKcalGoal);
final totalFatGoal = MacroCalc.getTotalFatsGoal(userEntity, totalKcalGoal);
final totalProteinGoal = MacroCalc.getTotalProteinsGoal(userEntity, totalKcalGoal);

await _addTrackedDayUsecase.addNewTrackedDay(
day, totalKcalGoal, totalCarbsGoal, totalFatGoal, totalProteinGoal);
Expand Down
18 changes: 15 additions & 3 deletions lib/features/onboarding/domain/entity/user_data_mask_entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class UserDataMaskEntity {
double? weight;
UserActivitySelectionEntity? activity;
UserGoalSelectionEntity? goal;
double? carbsPercentageGoal;
double? fatsPercentageGoal;
double? proteinsPercentageGoal;

bool acceptDataCollection = false;

Expand All @@ -22,15 +25,18 @@ class UserDataMaskEntity {
this.height,
this.weight,
this.activity,
this.goal});
this.goal,
this.carbsPercentageGoal,
this.fatsPercentageGoal,
this.proteinsPercentageGoal});

bool checkDataProvided() {
if (gender != null &&
birthday != null &&
height != null &&
weight != null &&
activity != null &&
goal != null) {
goal != null){
return true;
} else {
return false;
Expand All @@ -44,6 +50,9 @@ class UserDataMaskEntity {
final userBirthday = birthday ?? DateTime.now(); // TODO
final userHeight = height ?? 180;
final userWeight = weight ?? 70;
final userCarbsPercentageGoal = carbsPercentageGoal ?? 0.6;
final userFatsPercentageGoal = fatsPercentageGoal ?? 0.25;
final userProteinsPercentageGoal = proteinsPercentageGoal ?? 0.15;
UserGenderEntity userGender;
if (gender == UserGenderSelectionEntity.genderMale) {
userGender = UserGenderEntity.male;
Expand Down Expand Up @@ -88,6 +97,9 @@ class UserDataMaskEntity {
weightKG: userWeight,
gender: userGender,
goal: userGoal,
pal: userPal);
pal: userPal,
carbsPercentageGoal: userCarbsPercentageGoal,
fatsPercentageGoal: userFatsPercentageGoal,
proteinsPercentageGoal: userProteinsPercentageGoal);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class OnboardingBloc extends Bloc<OnboardingEvent, OnboardingState> {
final calorieGoal = getOverviewCalorieGoal();
double? carbsGoal;
if (userEntity != null && calorieGoal != null) {
carbsGoal = MacroCalc.getTotalCarbsGoal(calorieGoal);
carbsGoal = MacroCalc.getTotalCarbsGoal(userEntity, calorieGoal);
}
return carbsGoal;
}
Expand All @@ -57,7 +57,7 @@ class OnboardingBloc extends Bloc<OnboardingEvent, OnboardingState> {
final calorieGoal = getOverviewCalorieGoal();
double? fatGoal;
if (userEntity != null && calorieGoal != null) {
fatGoal = MacroCalc.getTotalFatsGoal(calorieGoal);
fatGoal = MacroCalc.getTotalFatsGoal(userEntity, calorieGoal);
}
return fatGoal;
}
Expand All @@ -67,7 +67,7 @@ class OnboardingBloc extends Bloc<OnboardingEvent, OnboardingState> {
final calorieGoal = getOverviewCalorieGoal();
double? proteinGoal;
if (userEntity != null && calorieGoal != null) {
proteinGoal = MacroCalc.getTotalProteinsGoal(calorieGoal);
proteinGoal = MacroCalc.getTotalProteinsGoal(userEntity, calorieGoal);
}
return proteinGoal;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'package:flutter/material.dart';
import 'package:horizontal_picker/horizontal_picker.dart';
import 'package:opennutritracker/generated/l10n.dart';

class SetPercentageDialog extends StatelessWidget {
static const _rangePercent = 100.0;

final double userPercentage;

const SetPercentageDialog({super.key, required this.userPercentage});

@override
Widget build(BuildContext context) {
double selectedPercentage = userPercentage*100;
return AlertDialog(
title: Text(S.of(context).selectPercentageDialogLabel),
content: Wrap(
children: [
Column(
children: [
HorizontalPicker(
height: 100,
backgroundColor: Colors.transparent,
minValue: selectedPercentage - _rangePercent,
maxValue: selectedPercentage + _rangePercent,
divisions: 400,
suffix: ' ${S.of(context).cmLabel}',
onChanged: (value) {
selectedPercentage = value;
})
],
)
],
),
actions: <Widget>[
TextButton(
onPressed: () {
Navigator.of(context).pop();
},
child: Text(S.of(context).dialogCancelLabel)),
TextButton(
onPressed: () {
// TODO validate selected height
Navigator.pop(context, selectedPercentage / 100);
},
child: Text(S.of(context).dialogOKLabel))
],
);
}
}
Loading