workouttest_app/lib/bloc/account/account_bloc.dart
Tibor Bossanyi (Freelancer) 225172f950 WT 1.1.26+3 bloc migration
2022-04-15 08:47:32 +02:00

106 lines
3.4 KiB
Dart

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/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()) {
_load();
on<AccountChangeCustomer>(_onAccountChange);
on<AccountLogin>(_onLoginChange);
on<AccountLogInFinished>(_onLoginFinished);
on<AccountLogout>(_onLogout);
on<AccountGetTrainees>(_onGetTrainees);
on<AccountSelectTrainee>(_onSelectTrainees);
}
void _load() {
if (Cache().userLoggedIn != null) {
customerRepository.customer = Cache().userLoggedIn!;
loggedIn = true;
}
}
void _onAccountChange(AccountChangeCustomer event, Emitter<AccountState> emit) {
emit(AccountLoading());
emit(AccountReady());
}
void _onLoginChange(AccountLogin event, Emitter<AccountState> emit) {
emit(AccountLoading());
emit(AccountReady());
}
void _onLoginFinished(AccountLogInFinished event, Emitter<AccountState> emit) {
emit(AccountLoading());
customerRepository.customer = event.customer;
this.loggedIn = true;
emit(AccountLoggedIn());
}
void _onLogout(AccountLogout event, Emitter<AccountState> emit) async {
emit(AccountLoading());
await Cache().logout();
customerRepository.customer = null;
customerRepository.emptyTrainees();
loggedIn = false;
emit(AccountReady());
}
void _onGetTrainees(AccountGetTrainees event, Emitter<AccountState> emit) async {
emit(AccountLoading());
await customerRepository.getTrainees();
emit(AccountReady());
}
void _onSelectTrainees(AccountSelectTrainee event, Emitter<AccountState> emit) async {
emit(AccountLoading());
await customerRepository.getTrainees();
emit(AccountReady());
}
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;
}
}