workouttest_app/lib/bloc/account/account_bloc.dart
2021-04-12 00:51:09 +02:00

99 lines
3.5 KiB
Dart

import 'dart:async';
import 'package:aitrainer_app/model/cache.dart';
import 'package:aitrainer_app/model/customer.dart';
import 'package:aitrainer_app/repository/customer_repository.dart';
import 'package:aitrainer_app/repository/exercise_repository.dart';
import 'package:aitrainer_app/util/enums.dart';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
import 'package:meta/meta.dart';
part 'account_event.dart';
part 'account_state.dart';
class AccountBloc extends Bloc<AccountEvent, AccountState> {
final CustomerRepository customerRepository;
bool loggedIn = false;
int traineeId = 0;
AccountBloc({required this.customerRepository}) : super(AccountInitial()) {
if (Cache().userLoggedIn != null) {
customerRepository.customer = Cache().userLoggedIn!;
loggedIn = true;
}
}
String getAccurateBodyType() {
String bodyType = ("Set your body type");
int _ecto = 0;
int _mezo = 0;
int _endo = 0;
_ecto = customerRepository.getCustomerPropertyValue(PropertyEnum.Ectomorph.toStr()).toInt();
_mezo = customerRepository.getCustomerPropertyValue(PropertyEnum.Mesomorph.toStr()).toInt();
_endo = customerRepository.getCustomerPropertyValue(PropertyEnum.Endomorph.toStr()).toInt();
if (_ecto == 0 && _mezo == 0 && _endo == 0) {
return bodyType;
}
int bodyTypeValue;
if (_ecto < 50) {
bodyTypeValue = (50 + ((50 / (_mezo + _endo)) * _endo)).toInt();
} else if (_endo < 50) {
bodyTypeValue = (0 + ((50 / (_mezo + _ecto)) * _mezo)).toInt();
} else {
// random answers probably
bodyTypeValue = (0 + ((100 / (_endo + _ecto))) * _endo).toInt();
}
print("BodyType value " + bodyTypeValue.toString());
if (bodyTypeValue < 20) {
bodyType = ("Ectomorph");
} else if (bodyTypeValue >= 25 && bodyTypeValue < 40) {
bodyType = ("Ecto") + "-" + ("Mesomorph");
} else if (bodyTypeValue >= 40 && bodyTypeValue < 60) {
bodyType = ("Mesomorph");
} else if (bodyTypeValue >= 60 && bodyTypeValue < 80) {
bodyType = ("Meso") + "-" + ("Endomorph");
} else {
bodyType = ("Endomorph");
}
return bodyType;
}
@override
Stream<AccountState> mapEventToState(
AccountEvent event,
) async* {
try {
if (event is AccountChangeCustomer) {
// route to Customer Change page
yield AccountReady();
} else if (event is AccountLogin) {
//route to Login Page
} else if (event is AccountLogInFinished) {
customerRepository.customer = event.customer;
this.loggedIn = true;
yield AccountLoggedIn();
} else if (event is AccountLogout) {
await Cache().logout();
customerRepository.customer = null;
customerRepository.emptyTrainees();
loggedIn = false;
yield AccountLoggedOut();
} else if (event is AccountGetTrainees) {
yield AccountLoading();
await customerRepository.getTrainees();
yield AccountReady();
} else if (event is AccountSelectTrainee) {
yield AccountLoading();
customerRepository.setTrainee(event.traineeId);
Cache().setTrainee(customerRepository.getTraineeById(event.traineeId)!);
ExerciseRepository exerciseRepository = ExerciseRepository();
await exerciseRepository.getExercisesByCustomer(event.traineeId);
this.traineeId = event.traineeId;
yield AccountReady();
}
} on Exception catch (e) {
yield AccountError(message: e.toString());
}
}
}