WT1.1.2f Purchase, Product, Property, BMR, BMI, Sizes

This commit is contained in:
bossanyit
2020-11-16 15:42:40 +01:00
parent 5fa5382e3f
commit e28f1edf50
48 changed files with 2631 additions and 1441 deletions
@@ -10,29 +10,105 @@ part 'customer_change_state.dart';
class CustomerChangeBloc extends Bloc<CustomerChangeEvent, CustomerChangeState> {
final CustomerRepository customerRepository;
CustomerChangeBloc({this.customerRepository}) : super(CustomerChangeInitial());
bool visiblePassword = false;
int year = 1990;
double weight = 60;
double height = 170;
CustomerChangeBloc({this.customerRepository}) : super(CustomerChangeInitial()) {
year = this.customerRepository.customer.birthYear;
weight = this.customerRepository.getWeight();
height = this.customerRepository.getHeight();
}
@override
Stream<CustomerChangeState> mapEventToState(
CustomerChangeEvent event,
) async* {
try {
if (event is CustomerGoalChange) {
if (event is CustomerLoad) {
yield CustomerChangeLoading();
yield CustomerDataChanged();
} else if (event is CustomerGoalChange) {
customerRepository.setGoal(event.goal);
yield CustomerDataChanged();
} else if (event is CustomerChangePasswordObscure) {
visiblePassword = !visiblePassword;
yield CustomerDataChanged();
} else if (event is CustomerFitnessChange) {
customerRepository.setFitnessLevel(event.fitness);
yield CustomerDataChanged();
} else if (event is CustomerBirthYearChange) {
yield CustomerChangeLoading();
customerRepository.setBirthYear(event.year);
year = event.year;
yield CustomerDataChanged();
} else if (event is CustomerWeightChange) {
yield CustomerChangeLoading();
customerRepository.setWeight(event.weight);
weight = event.weight.toDouble();
yield CustomerDataChanged();
} else if (event is CustomerHeightChange) {
yield CustomerChangeLoading();
customerRepository.setHeight(event.height);
height = event.height.toDouble();
yield CustomerDataChanged();
} else if (event is CustomerBodyTypeChange) {
customerRepository.setBodyType(event.bodyType);
yield CustomerDataChanged();
} else if (event is CustomerEmailChange) {
customerRepository.setEmail(event.email);
yield CustomerDataChanged();
} else if (event is CustomerPasswordChange) {
customerRepository.setPassword(event.password);
yield CustomerDataChanged();
} else if (event is CustomerFirstNameChange) {
customerRepository.setFirstName(event.firstName);
yield CustomerDataChanged();
} else if (event is CustomerNameChange) {
customerRepository.setName(event.name);
yield CustomerDataChanged();
} else if (event is CustomerGenderChange) {
customerRepository.setSex(event.gender == 0 ? "m" : "w");
yield CustomerDataChanged();
} else if (event is CustomerSave) {
yield CustomerSaving();
await customerRepository.saveCustomer();
yield CustomerSaveSuccess();
if (validation()) {
await customerRepository.saveCustomer();
yield CustomerSaveSuccess();
} else {
yield CustomerSaveError(message: "Please provide the necessary information");
}
}
} on Exception catch(e) {
} on Exception catch (e) {
yield CustomerSaveError(message: e.toString());
}
}
bool validation() {
print("f " + customerRepository.customer.firstname);
return (emailValidation(customerRepository.customer.email) == null) &&
(passwordValidation(customerRepository.customer.password) == null) &&
(nameValidation(customerRepository.customer.firstname) == null) &&
(nameValidation(customerRepository.customer.name) == null);
}
String emailValidation(String email) {
bool emailValid = RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+").hasMatch(email);
return emailValid ? null : "Please type an email address";
}
String passwordValidation(String value) {
if (value == null || value.length == 0) {
return null;
}
bool valid = 8 < value.length;
return valid ? null : "Password too short";
}
String nameValidation(String value) {
if (value == null || value.length == 0) {
return "Name too short";
}
return null;
}
}
@@ -31,6 +31,78 @@ class CustomerBodyTypeChange extends CustomerChangeEvent {
List<Object> get props => [bodyType];
}
class CustomerBirthYearChange extends CustomerChangeEvent {
final int year;
const CustomerBirthYearChange({this.year});
@override
List<Object> get props => [year];
}
class CustomerWeightChange extends CustomerChangeEvent {
final int weight;
const CustomerWeightChange({this.weight});
@override
List<Object> get props => [weight];
}
class CustomerHeightChange extends CustomerChangeEvent {
final int height;
const CustomerHeightChange({this.height});
@override
List<Object> get props => [height];
}
class CustomerGenderChange extends CustomerChangeEvent {
final int gender;
const CustomerGenderChange({this.gender});
@override
List<Object> get props => [gender];
}
class CustomerEmailChange extends CustomerChangeEvent {
final String email;
const CustomerEmailChange({this.email});
@override
List<Object> get props => [email];
}
class CustomerFirstNameChange extends CustomerChangeEvent {
final String firstName;
const CustomerFirstNameChange({this.firstName});
@override
List<Object> get props => [firstName];
}
class CustomerNameChange extends CustomerChangeEvent {
final String name;
const CustomerNameChange({this.name});
@override
List<Object> get props => [name];
}
class CustomerPasswordChange extends CustomerChangeEvent {
final String password;
const CustomerPasswordChange({this.password});
@override
List<Object> get props => [password];
}
class CustomerChangePasswordObscure extends CustomerChangeEvent {
const CustomerChangePasswordObscure();
}
class CustomerLoad extends CustomerChangeEvent {
const CustomerLoad();
}
class CustomerSave extends CustomerChangeEvent {
const CustomerSave();
}
@@ -11,6 +11,10 @@ class CustomerChangeInitial extends CustomerChangeState {
const CustomerChangeInitial();
}
class CustomerChangeLoading extends CustomerChangeState {
const CustomerChangeLoading();
}
class CustomerSaving extends CustomerChangeState {
const CustomerSaving();
}
+51 -23
View File
@@ -4,6 +4,9 @@ import 'package:flutter_form_bloc/flutter_form_bloc.dart';
class CustomerChangeFormBloc extends FormBloc<String, String> {
final CustomerRepository customerRepository;
int weight = 60;
int birthYear = 1990;
final emailField = TextFieldBloc(
validators: [
FieldBlocValidators.required,
@@ -22,31 +25,32 @@ class CustomerChangeFormBloc extends FormBloc<String, String> {
//FieldBlocValidators.confirmPassword(passwordField),
],
);
final birthYearField = TextFieldBloc();
final weightField = TextFieldBloc();
final genderField = SelectFieldBloc();
final birthYearField = InputFieldBloc<int, Object>(initialValue: 1990);
final weightField = InputFieldBloc<int, Object>(initialValue: 60);
final genderField = InputFieldBloc<int, Object>(initialValue: 0);
final goalField = TextFieldBloc();
CustomerChangeFormBloc({this.customerRepository}) {
addFieldBlocs(fieldBlocs: [
emailField,
firstNameField,
nameField,
passwordField,
birthYearField,
weightField,
genderField,
goalField,
]);
emailField,
firstNameField,
nameField,
passwordField,
birthYearField,
weightField,
genderField,
goalField,
]);
emailField.updateInitialValue(customerRepository.customer.email);
firstNameField.updateInitialValue(customerRepository.customer.firstname);
nameField.updateInitialValue(customerRepository.customer.name);
birthYearField.updateInitialValue(customerRepository.customer.birthYear.toString());
weightField.updateInitialValue(customerRepository.customer.weight.toString());
genderField.updateInitialValue(customerRepository.getGenderByDBValue(customerRepository.sex));
birthYearField.updateInitialValue(customerRepository.customer.birthYear);
weightField.updateInitialValue(customerRepository.customer.getProperty("weight").toInt());
int initialGender = customerRepository.getGenderByDBValue(customerRepository.sex) == "m" ? 0 : 1;
genderField.updateInitialValue(initialGender);
firstNameField.onValueChanges(onData: (previous, current) async* {
customerRepository.setFirstName(current.value);
@@ -54,7 +58,7 @@ class CustomerChangeFormBloc extends FormBloc<String, String> {
nameField.onValueChanges(onData: (previous, current) async* {
customerRepository.setName(current.value);
});
birthYearField.onValueChanges(onData: (previous, current) async* {
/*birthYearField.onValueChanges(onData: (previous, current) async* {
customerRepository.setBirthYear(current.valueToInt);
});
weightField.onValueChanges(onData: (previous, current) async* {
@@ -63,24 +67,49 @@ class CustomerChangeFormBloc extends FormBloc<String, String> {
customerRepository.genders.forEach((element) {
genderField.addItem(element.name);
});
});*/
genderField.onValueChanges(onData: (previous, current) async* {
String dbValue = customerRepository.getGenderByName(current.value);
String dbValue = customerRepository.getGenderByName(current.value.toString());
customerRepository.setSex(dbValue);
});
}
int getGender() {
return customerRepository.customer.sex == "M" ? 0 : 1;
}
void switchGender(int index) {
String dbValue;
if (index == 0) {
dbValue = customerRepository.getGenderByName("Man");
} else if (index == 1) {
dbValue = customerRepository.getGenderByName("Woman");
}
customerRepository.setSex(dbValue);
genderField.add(UpdateFieldBlocValue(dbValue));
}
void changeWeight(int value) {
customerRepository.setWeight(value);
weight = value;
weightField.updateValue(value);
}
changeBirthYear(int value) {
customerRepository.setBirthYear(value);
birthYear = value;
birthYearField.updateValue(value);
}
@override
void onSubmitting() async {
print("on Submitting Custom form");
print("on Submitting Customer Change form");
try {
emitLoading(progress: 30);
// Emit either Loaded or Error
await customerRepository.saveCustomer();
emitSuccess(canSubmitAgain: false);
emitSuccess(canSubmitAgain: true);
} on Exception catch (ex) {
emitFailure(failureResponse: ex.toString());
}
@@ -98,5 +127,4 @@ class CustomerChangeFormBloc extends FormBloc<String, String> {
goalField.close();
return super.close();
}
}
}
+299 -7
View File
@@ -1,25 +1,212 @@
import 'dart:async';
import 'package:aitrainer_app/bloc/menu/menu_bloc.dart';
import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/model/property.dart';
import 'package:aitrainer_app/model/exercise_type.dart';
import 'package:aitrainer_app/model/fitness_state.dart';
import 'package:aitrainer_app/repository/customer_repository.dart';
import 'package:aitrainer_app/repository/exercise_repository.dart';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter/animation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_form_bloc/flutter_form_bloc.dart';
import 'package:meta/meta.dart';
part 'exercise_new_event.dart';
part 'exercise_new_state.dart';
class ExerciseNewBloc extends Bloc<ExerciseNewEvent, ExerciseNewState> {
final ExerciseRepository exerciseRepository;
final CustomerRepository customerRepository;
final MenuBloc menuBloc;
AnimationController bmiAnimationController;
double quantity = 12;
double unitQuantity = 30;
double bmi = 0;
double bmr = 0;
double goalBMI = 0;
double goalWeight = 0;
double bmiAngle = 0;
double bmiTop = 0;
double bmiLeft = 0;
double weight;
double height;
String fitnessLevel;
bool changedWeight = false;
bool changedSizes = false;
final List<Property> womanSizes = List();
final List<Property> manSizes = List();
double mediaWidth = 0;
double mediaHeight = 0;
@override
ExerciseNewBloc({this.exerciseRepository, this.menuBloc, ExerciseType exerciseType}) : super(ExerciseNewInitial()) {
ExerciseNewBloc({this.exerciseRepository, this.menuBloc, this.customerRepository, ExerciseType exerciseType})
: super(ExerciseNewInitial()) {
exerciseRepository.setUnit(exerciseType.unit);
exerciseRepository.setQuantity(quantity);
exerciseRepository.setUnitQuantity(unitQuantity);
if (Cache().userLoggedIn != null) {
customerRepository.customer = Cache().userLoggedIn;
weight = customerRepository.customer.getProperty("Weight");
height = customerRepository.customer.getProperty("Height");
fitnessLevel = customerRepository.customer.fitnessLevel;
}
}
void setMediaDimensions(double width, double height) {
this.mediaHeight = height;
this.mediaWidth = width;
this.addSizes(customerRepository.sex);
}
void addSizes(String sex) {
List<Property> properties = Cache().getProperties();
if (sex == "Man") {
properties.forEach((element) {
if (element.propertyName == "Shoulder") {
element.top = (mediaHeight * 0.131).toInt();
element.left = (mediaWidth * 0.23).toInt();
element.value = customerRepository.customer.getProperty("Shoulder");
manSizes.add(element);
} else if (element.propertyName == "Neck") {
element.top = (mediaHeight * 0.069).toInt();
element.left = (mediaWidth * 0.23).toInt();
element.value = customerRepository.customer.getProperty("Neck");
manSizes.add(element);
} else if (element.propertyName == "Biceps") {
element.top = (mediaHeight * 0.199).toInt();
element.left = (mediaWidth * 0.44).toInt();
element.value = customerRepository.customer.getProperty("Biceps");
manSizes.add(element);
} else if (element.propertyName == "Chest") {
element.top = (mediaHeight * 0.171).toInt();
element.left = (mediaWidth * 0.23).toInt();
element.value = customerRepository.customer.getProperty("Chest");
manSizes.add(element);
} else if (element.propertyName == "Belly") {
element.top = (mediaHeight * 0.275).toInt();
element.left = (mediaWidth * 0.23).toInt();
element.value = customerRepository.customer.getProperty("Belly");
manSizes.add(element);
} else if (element.propertyName == "Hip") {
element.top = (mediaHeight * 0.355).toInt();
element.left = (mediaWidth * 0.23).toInt();
element.value = customerRepository.customer.getProperty("Hip");
manSizes.add(element);
} else if (element.propertyName == "Thigh Top") {
element.top = (mediaHeight * 0.38).toInt();
element.left = (mediaWidth * 0.32).toInt();
element.value = customerRepository.customer.getProperty("Thigh Top");
manSizes.add(element);
} else if (element.propertyName == "Thigh Middle") {
element.top = (mediaHeight * 0.438).toInt();
element.left = (mediaWidth * 0.14).toInt();
element.value = customerRepository.customer.getProperty("Thigh Middle");
manSizes.add(element);
} else if (element.propertyName == "Knee") {
element.top = (mediaHeight * 0.535).toInt();
element.left = (mediaWidth * 0.14).toInt();
element.value = customerRepository.customer.getProperty("Knee");
manSizes.add(element);
} else if (element.propertyName == "Calf") {
element.top = (mediaHeight * 0.60).toInt();
element.left = (mediaWidth * 0.14).toInt();
element.value = customerRepository.customer.getProperty("Calf");
manSizes.add(element);
} else if (element.propertyName == "Ankle") {
element.top = (mediaHeight * 0.72).toInt();
element.left = (mediaWidth * 0.28).toInt();
element.value = customerRepository.customer.getProperty("Ankle");
manSizes.add(element);
} else if (element.propertyName == "Weight") {
element.top = (mediaHeight * 0.44).toInt();
element.left = (mediaWidth * 0.54).toInt();
element.value = customerRepository.customer.getProperty("Weight");
manSizes.add(element);
}
});
} else {
properties.forEach((element) {
if (element.propertyName == "Shoulder") {
element.top = (mediaHeight * 0.131).toInt();
element.left = (mediaWidth * 0.195).toInt();
element.value = customerRepository.customer.getProperty("Shoulder");
manSizes.add(element);
} else if (element.propertyName == "Neck") {
element.top = (mediaHeight * 0.081).toInt();
element.left = (mediaWidth * 0.195).toInt();
element.value = customerRepository.customer.getProperty("Neck");
manSizes.add(element);
} else if (element.propertyName == "Biceps") {
element.top = (mediaHeight * 0.199).toInt();
element.left = (mediaWidth * 0.36).toInt();
element.value = customerRepository.customer.getProperty("Biceps");
manSizes.add(element);
} else if (element.propertyName == "Chest") {
element.top = (mediaHeight * 0.168).toInt();
element.left = (mediaWidth * 0.195).toInt();
element.value = customerRepository.customer.getProperty("Chest");
manSizes.add(element);
} else if (element.propertyName == "Belly") {
element.top = (mediaHeight * 0.26).toInt();
element.left = (mediaWidth * 0.195).toInt();
element.value = customerRepository.customer.getProperty("Belly");
manSizes.add(element);
} else if (element.propertyName == "Hip") {
element.top = (mediaHeight * 0.335).toInt();
element.left = (mediaWidth * 0.195).toInt();
element.value = customerRepository.customer.getProperty("Hip");
manSizes.add(element);
} else if (element.propertyName == "Thigh Top") {
element.top = (mediaHeight * 0.385).toInt();
element.left = (mediaWidth * 0.28).toInt();
element.value = customerRepository.customer.getProperty("Thigh Top");
manSizes.add(element);
} else if (element.propertyName == "Thigh Middle") {
element.top = (mediaHeight * 0.433).toInt();
element.left = (mediaWidth * 0.12).toInt();
element.value = customerRepository.customer.getProperty("Thigh Middle");
manSizes.add(element);
} else if (element.propertyName == "Knee") {
element.top = (mediaHeight * 0.543).toInt();
element.left = (mediaWidth * 0.14).toInt();
element.value = customerRepository.customer.getProperty("Knee");
manSizes.add(element);
} else if (element.propertyName == "Calf") {
element.top = (mediaHeight * 0.608).toInt();
element.left = (mediaWidth * 0.14).toInt();
element.value = customerRepository.customer.getProperty("Calf");
manSizes.add(element);
} else if (element.propertyName == "Ankle") {
element.top = (mediaHeight * 0.725).toInt();
element.left = (mediaWidth * 0.23).toInt();
element.value = customerRepository.customer.getProperty("Ankle");
manSizes.add(element);
} else if (element.propertyName == "Weight") {
element.top = (mediaHeight * 0.44).toInt();
element.left = (mediaWidth * 0.54).toInt();
element.value = customerRepository.customer.getProperty("Weight");
manSizes.add(element);
}
});
}
}
void updateSizes(String propertyName, double value) {
List<Property> sizes;
if (customerRepository.sex == "Man") {
sizes = this.manSizes;
} else {
sizes = this.womanSizes;
}
sizes.forEach((element) {
if (element.propertyName == propertyName) {
element.value = value;
}
});
}
@override
@@ -28,26 +215,131 @@ class ExerciseNewBloc extends Bloc<ExerciseNewEvent, ExerciseNewState> {
if (event is ExerciseNewLoad) {
yield ExerciseNewLoading();
yield ExerciseNewReady();
} else if ( event is ExerciseNewQuantityChange ) {
} else if (event is ExerciseNewQuantityChange) {
yield ExerciseNewLoading();
exerciseRepository.setQuantity(event.quantity);
quantity = event.quantity;
yield ExerciseNewReady();
} else if ( event is ExerciseNewQuantityUnitChange ) {
} else if (event is ExerciseNewQuantityUnitChange) {
yield ExerciseNewLoading();
exerciseRepository.setUnitQuantity(event.quantity);
unitQuantity = event.quantity;
yield ExerciseNewReady();
} else if ( event is ExerciseNewSubmit ) {
} else if (event is ExerciseNewWeightChange) {
yield ExerciseNewLoading();
customerRepository.setWeight(event.value.toInt());
changedWeight = true;
weight = event.value;
getBMI();
getBMR();
yield ExerciseNewReady();
} else if (event is ExerciseNewFitnessLevelChange) {
yield ExerciseNewLoading();
customerRepository.setFitnessLevel(event.value);
fitnessLevel = event.value;
changedWeight = true;
getBMR();
yield ExerciseNewReady();
} else if (event is ExerciseNewHeightChange) {
yield ExerciseNewLoading();
customerRepository.setHeight(event.value.toInt());
changedWeight = true;
height = event.value;
getBMI();
getBMR();
yield ExerciseNewReady();
} else if (event is ExerciseNewSaveWeight) {
yield ExerciseNewLoading();
customerRepository.saveCustomer();
changedWeight = false;
this.changedSizes = false;
yield ExerciseNewReady();
} else if (event is ExerciseNewSizeChange) {
yield ExerciseNewLoading();
this.changedSizes = true;
this.updateSizes(event.propertyName, event.value);
customerRepository.setCustomerProperty(event.propertyName, event.value);
yield ExerciseNewReady();
} else if (event is ExerciseNewSubmit) {
yield ExerciseNewLoading();
await exerciseRepository.addExercise();
menuBloc.add(MenuTreeDown(parent: 0));
yield ExerciseNewReady();
} else if (event is ExerciseNewBMIAnimate) {
yield ExerciseNewLoading();
yield ExerciseNewReady();
}
} on Exception catch (e) {
yield ExerciseNewError(message: e.toString());
}
}
double getBMI() {
this.bmi = weight / (height * height / 10000);
this.getGoalBMI();
return this.bmi;
}
double getBMR() {
var date = DateTime.now();
int year = int.parse(DateFormat(DateFormat.YEAR).format(date));
if (customerRepository.customer.sex == "m") {
//66.47 + ( 13.75 × tömeg kg-ban ) + ( 5.003 × magasság cm-ben ) ( 6.755 × életkor évben kifejezve )
bmr = 66.47 + (13.75 * weight) + (5.003 * height) - (6.755 * (year - customerRepository.customer.birthYear));
} else {
//BMR = 655.1 + ( 9.563 × ömeg kg-ban ) + ( 1.85 × magasság cm-ben) ( 4.676 × életkor évben kifejezve )
bmr = 655.1 + (9.563 * weight) + (1.85 * height) - (4.676 * (year - customerRepository.customer.birthYear));
}
if (customerRepository.customer.fitnessLevel == FitnessState.beginner) {
bmr *= 1.2;
} else if (customerRepository.customer.fitnessLevel == FitnessState.intermediate) {
bmr *= 1.375;
} else if (customerRepository.customer.fitnessLevel == FitnessState.advanced) {
bmr *= 1.55;
} else if (customerRepository.customer.fitnessLevel == FitnessState.professional) {
bmr *= 1.9;
}
return bmr;
}
double getGoalBMI() {
if (this.bmi == 0) {
getBMI();
}
//bmi = 15;
this.bmiAngle = (bmi * 90 / 25) - 90;
if (bmi < 18.5) {
goalBMI = 19;
this.bmiTop = 99;
this.bmiLeft = 72;
bmiAngle = -62;
} else if (bmi < 25 && 18.5 < bmi) {
goalBMI = bmi;
this.bmiTop = 46;
this.bmiLeft = 130;
bmiAngle = -21;
} else if (bmi < 30 && 24.9 < bmi) {
goalBMI = 24;
this.bmiTop = 38.0;
this.bmiLeft = 186.0;
} else if (bmi < 34.9 && 29.9 < bmi) {
goalBMI = 29;
bmiTop = 48;
bmiLeft = 211;
} else if (bmi > 35) {
goalBMI = 34;
bmiTop = 94;
bmiLeft = 260;
bmiAngle = 59;
}
this.goalWeight = goalBMI * (height * height / 10000);
//print("Angle: " + bmiAngle.toStringAsFixed(1));
return goalBMI;
}
}
+44 -1
View File
@@ -28,7 +28,50 @@ class ExerciseNewQuantityUnitChange extends ExerciseNewEvent {
List<Object> get props => [quantity];
}
class ExerciseNewWeightChange extends ExerciseNewEvent {
final double value;
const ExerciseNewWeightChange({this.value});
@override
List<Object> get props => [value];
}
class ExerciseNewHeightChange extends ExerciseNewEvent {
final double value;
const ExerciseNewHeightChange({this.value});
@override
List<Object> get props => [value];
}
class ExerciseNewFitnessLevelChange extends ExerciseNewEvent {
final String value;
const ExerciseNewFitnessLevelChange({this.value});
@override
List<Object> get props => [value];
}
class ExerciseNewSizeChange extends ExerciseNewEvent {
final String propertyName;
final double value;
const ExerciseNewSizeChange({this.propertyName, this.value});
@override
List<Object> get props => [propertyName, value];
}
class ExerciseNewSaveWeight extends ExerciseNewEvent {
const ExerciseNewSaveWeight();
}
class ExerciseNewBMIAnimate extends ExerciseNewEvent {
final dynamic value;
const ExerciseNewBMIAnimate({this.value});
@override
List<Object> get props => [value];
}
class ExerciseNewSubmit extends ExerciseNewEvent {
const ExerciseNewSubmit();
}