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'; import '../../service/customer_service.dart'; part 'account_event.dart'; part 'account_state.dart'; class AccountBloc extends Bloc { final CustomerRepository customerRepository; bool loggedIn = false; int traineeId = 0; AccountBloc({required this.customerRepository}) : super(AccountInitial()) { _load(); on(_onAccountChange); on(_onLoginChange); on(_onLoginFinished); on(_onLogout); on(_onGetTrainees); on(_onSelectTrainees); on(_onDeleteAccount); } void _load() { if (Cache().userLoggedIn != null) { customerRepository.customer = Cache().userLoggedIn!; loggedIn = true; } } void _onAccountChange(AccountChangeCustomer event, Emitter emit) { emit(AccountLoading()); emit(AccountReady()); } void _onLoginChange(AccountLogin event, Emitter emit) { emit(AccountLoading()); emit(AccountReady()); } void _onLoginFinished(AccountLogInFinished event, Emitter emit) { emit(AccountLoading()); customerRepository.customer = event.customer; this.loggedIn = true; emit(AccountLoggedIn()); } void _onLogout(AccountLogout event, Emitter emit) async { emit(AccountLoading()); await Cache().logout(); customerRepository.customer = null; customerRepository.emptyTrainees(); loggedIn = false; emit(AccountReady()); } void _onDeleteAccount(DeleteAccount event, Emitter emit) async { emit(AccountLoading()); await Cache().logout(); customerRepository.customer = event.customer; customerRepository.emptyTrainees(); loggedIn = false; //delete local store int customerId = customerRepository.customer!.customerId!; await Cache().deleteCustomerId(customerId); customerRepository.customer = null; // deactivate user await CustomerApi().deactivateCustomer(customerId); emit(AccountReady()); } void _onGetTrainees(AccountGetTrainees event, Emitter emit) async { emit(AccountLoading()); await customerRepository.getTrainees(); emit(AccountReady()); } void _onSelectTrainees(AccountSelectTrainee event, Emitter 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; } }