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

Develop #31

Merged
merged 4 commits into from
Nov 11, 2023
Merged
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
2 changes: 1 addition & 1 deletion lib/core/utils/locator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ Future<void> initLocator() async {
locator.registerLazySingleton(() => CalendarDayBloc(
locator(), locator(), locator(), locator(), locator(), locator()));
locator.registerLazySingleton<ProfileBloc>(
() => ProfileBloc(locator(), locator()));
() => ProfileBloc(locator(), locator(), locator(), locator()));
locator.registerLazySingleton(() => SettingsBloc(locator(), locator()));

locator.registerFactory<ActivitiesBloc>(() => ActivitiesBloc(locator()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,12 @@ class ActivityDetailBloc
return METCalc.getTotalBurnedKcal(user, physicalActivity, duration);
}

void persistActivity(BuildContext context, String durationText,
double totalKcalBurned, PhysicalActivityEntity activityEntity, DateTime day) async {
void persistActivity(
BuildContext context,
String durationText,
double totalKcalBurned,
PhysicalActivityEntity activityEntity,
DateTime day) async {
final duration = double.parse(durationText);

final userActivityEntity = UserActivityEntity(IdGenerator.getUniqueID(),
Expand All @@ -54,7 +58,8 @@ class ActivityDetailBloc

void _updateTrackedDay(DateTime dateTime, double caloriesBurned) async {
final userEntity = await _getUserUsecase.getUserData();
final totalKcalGoal = CalorieGoalCalc.getTdee(userEntity);
final totalKcalGoal =
CalorieGoalCalc.getTotalKcalGoal(userEntity, caloriesBurned);

final hasTrackedDay =
await _addTrackedDayUsecase.hasTrackedDay(DateTime.now());
Expand Down
18 changes: 8 additions & 10 deletions lib/features/meal_detail/presentation/bloc/meal_detail_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,15 @@ class MealDetailBloc {
_updateTrackedDay(intakeEntity, day);
}

Future<void> _updateTrackedDay(IntakeEntity intakeEntity, DateTime day) async {
final userEntity = await _getUserUsecase.getUserData();
final totalKcalGoal = CalorieGoalCalc.getTdee(userEntity);

final hasTrackedDay =
await _addTrackedDayUsecase.hasTrackedDay(day);
Future<void> _updateTrackedDay(
IntakeEntity intakeEntity, DateTime day) async {
final hasTrackedDay = await _addTrackedDayUsecase.hasTrackedDay(day);
if (!hasTrackedDay) {
await _addTrackedDayUsecase.addNewTrackedDay(
day, totalKcalGoal);
final userEntity = await _getUserUsecase.getUserData();
final totalKcalGoal = CalorieGoalCalc.getTotalKcalGoal(userEntity, 0);

await _addTrackedDayUsecase.addNewTrackedDay(day, totalKcalGoal);
}
_addTrackedDayUsecase.addDayCaloriesTracked(
day, intakeEntity.totalKcal);
_addTrackedDayUsecase.addDayCaloriesTracked(day, intakeEntity.totalKcal);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:opennutritracker/core/domain/entity/user_entity.dart';
import 'package:opennutritracker/core/domain/usecase/add_config_usecase.dart';
import 'package:opennutritracker/core/domain/usecase/add_user_usecase.dart';
import 'package:opennutritracker/core/utils/calc/calorie_goal_calc.dart';
import 'package:opennutritracker/core/utils/calc/macro_calc.dart';
import 'package:opennutritracker/core/utils/calc/tdee_calc.dart';
import 'package:opennutritracker/features/onboarding/domain/entity/user_data_mask_entity.dart';

part 'onboarding_event.dart';
Expand Down Expand Up @@ -37,7 +37,7 @@ class OnboardingBloc extends Bloc<OnboardingEvent, OnboardingState> {
final userEntity = userSelection.toUserEntity();
double? calorieGoal;
if (userEntity != null) {
calorieGoal = TDEECalc.getTDEEKcalIOM2005(userEntity);
calorieGoal = CalorieGoalCalc.getTotalKcalGoal(userEntity, 0);
}
return calorieGoal;
}
Expand Down
31 changes: 28 additions & 3 deletions lib/features/profile/presentation/bloc/profile_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import 'package:collection/collection.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:opennutritracker/core/domain/entity/user_bmi_entity.dart';
import 'package:opennutritracker/core/domain/entity/user_entity.dart';
import 'package:opennutritracker/core/domain/usecase/add_tracked_day_usecase.dart';
import 'package:opennutritracker/core/domain/usecase/add_user_usecase.dart';
import 'package:opennutritracker/core/domain/usecase/get_user_activity_usecase.dart';
import 'package:opennutritracker/core/domain/usecase/get_user_usecase.dart';
import 'package:opennutritracker/core/utils/calc/bmi_calc.dart';
import 'package:opennutritracker/core/utils/calc/calorie_goal_calc.dart';
import 'package:opennutritracker/core/utils/locator.dart';
import 'package:opennutritracker/features/diary/presentation/bloc/calendar_day_bloc.dart';
import 'package:opennutritracker/features/diary/presentation/bloc/diary_bloc.dart';
Expand All @@ -17,8 +21,11 @@ part 'profile_state.dart';
class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
final GetUserUsecase _getUserUsecase;
final AddUserUsecase _addUserUsecase;
final AddTrackedDayUsecase _addTrackedDayUsecase;
final GetUserActivityUsecase _getUserActivityUsecase;

ProfileBloc(this._getUserUsecase, this._addUserUsecase)
ProfileBloc(this._getUserUsecase, this._addUserUsecase,
this._addTrackedDayUsecase, this._getUserActivityUsecase)
: super(ProfileInitial()) {
on<LoadProfileEvent>((event, emit) async {
emit(ProfileLoadingState());
Expand All @@ -33,8 +40,12 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
});
}

void updateUser(UserEntity userEntity) {
_addUserUsecase.addUser(userEntity);
void updateUser(UserEntity userEntity) async {
// Update user in DB
await _addUserUsecase.addUser(userEntity);

// Update Tracked Day
await _updateTrackedDayCalorieGoal(userEntity, DateTime.now());

// Refresh Profile
add(LoadProfileEvent());
Expand All @@ -44,4 +55,18 @@ class ProfileBloc extends Bloc<ProfileEvent, ProfileState> {
locator<DiaryBloc>().add(const LoadDiaryYearEvent());
locator<CalendarDayBloc>().add(LoadCalendarDayEvent(DateTime.now()));
}

Future<void> _updateTrackedDayCalorieGoal(
UserEntity user, DateTime day) async {
final hasTrackedDay = await _addTrackedDayUsecase.hasTrackedDay(day);
if (hasTrackedDay) {
final activityDayList =
await _getUserActivityUsecase.getTodayUserActivity();
final totalActivityKcal =
activityDayList.map((activity) => activity.burnedKcal).sum;
final totalKcalGoal = CalorieGoalCalc.getTotalKcalGoal(user, totalActivityKcal);

await _addTrackedDayUsecase.updateDayCalorieGoal(day, totalKcalGoal);
}
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# In iOS, build-name is used as CFBundleShortVersionString while build-number is used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 0.6.7+13
version: 0.6.8+14

environment:
sdk: '>=2.18.1 <3.0.0'
Expand Down
Loading