120 lines
4.3 KiB
Dart
120 lines
4.3 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:aitrainer_app/model/cache.dart';
|
|
import 'package:aitrainer_app/repository/customer_repository.dart';
|
|
import 'package:bloc/bloc.dart';
|
|
import 'package:equatable/equatable.dart';
|
|
import 'package:meta/meta.dart';
|
|
|
|
part 'customer_change_event.dart';
|
|
part 'customer_change_state.dart';
|
|
|
|
class CustomerChangeBloc extends Bloc<CustomerChangeEvent, CustomerChangeState> {
|
|
final CustomerRepository customerRepository;
|
|
bool visiblePassword = false;
|
|
int year = 1990;
|
|
double weight = 60;
|
|
double height = 170;
|
|
CustomerChangeBloc({this.customerRepository}) : super(CustomerChangeInitial()) {
|
|
year = this.customerRepository.customer.birthYear;
|
|
if (year == 0) {
|
|
year = 1990;
|
|
}
|
|
weight = this.customerRepository.getWeight();
|
|
height = this.customerRepository.getHeight();
|
|
}
|
|
|
|
@override
|
|
Stream<CustomerChangeState> mapEventToState(
|
|
CustomerChangeEvent event,
|
|
) async* {
|
|
try {
|
|
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();
|
|
if (validation()) {
|
|
await customerRepository.saveCustomer();
|
|
Cache().initBadges();
|
|
yield CustomerSaveSuccess();
|
|
} else {
|
|
yield CustomerSaveError(message: "Please provide the necessary information");
|
|
}
|
|
}
|
|
} 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;
|
|
}
|
|
}
|