forked from eatvio/eatvio_app
239 lines
7.0 KiB
Dart
239 lines
7.0 KiB
Dart
import 'dart:developer';
|
|
|
|
import 'package:dio/dio.dart';
|
|
import 'package:eatvio_app/core/token_storage.dart';
|
|
import 'package:eatvio_app/core/utils/date_time_utils.dart';
|
|
import 'package:eatvio_app/features/registration_v2/cubit/registration_state.dart';
|
|
import 'package:eatvio_app/features/registration_v2/models/registration_model.dart';
|
|
import 'package:eatvio_app/features/registration_v2/repositories/registration_repository_v2.dart';
|
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
import 'package:gpassword/gpassword.dart';
|
|
|
|
class RegistrationCubitV2 extends Cubit<RegistrationStateV2> {
|
|
final RegistrationRepositoryV2 registrationRepository;
|
|
RegistrationCubitV2({required this.registrationRepository}) : super(RegistrationStateV2());
|
|
|
|
void setFirstName(String firstName) {
|
|
emit(state.copyWith(firstName: firstName));
|
|
validateFirstName();
|
|
}
|
|
|
|
void setLastName(String lastName) {
|
|
emit(state.copyWith(lastName: lastName));
|
|
validateLastName();
|
|
}
|
|
|
|
void setEmail(String email) {
|
|
emit(state.copyWith(email: email));
|
|
validateEmail();
|
|
}
|
|
|
|
void setPhone(String phone) {
|
|
emit(state.copyWith(phone: phone));
|
|
validatePhone();
|
|
}
|
|
|
|
void setBirthday(DateTime birthday) {
|
|
emit(state.copyWith(birthday: birthday));
|
|
validateBirthday();
|
|
}
|
|
|
|
void setGender(String gender) {
|
|
emit(state.copyWith(gender: gender));
|
|
validateGender();
|
|
}
|
|
|
|
void setWeight(int weight) {
|
|
emit(state.copyWith(weight: weight));
|
|
validateWeight();
|
|
}
|
|
|
|
void setHeight(int height) {
|
|
emit(state.copyWith(height: height));
|
|
validateHeight();
|
|
}
|
|
|
|
void setActivityCoefficient(double activityCoefficient) {
|
|
emit(state.copyWith(activityCoefficient: activityCoefficient));
|
|
validateActivityCoefficient();
|
|
}
|
|
|
|
void setPassword(String password) {
|
|
emit(state.copyWith(password: password));
|
|
}
|
|
|
|
bool validateFirstName() {
|
|
if (state.firstName.isEmpty) {
|
|
emit(state.copyWith(isFirstNameValid: false));
|
|
return false;
|
|
}
|
|
emit(state.copyWith(isFirstNameValid: true));
|
|
return true;
|
|
}
|
|
|
|
bool validateLastName() {
|
|
if (state.lastName.isEmpty) {
|
|
emit(state.copyWith(isLastNameValid: false));
|
|
return false;
|
|
}
|
|
emit(state.copyWith(isLastNameValid: true));
|
|
return true;
|
|
}
|
|
|
|
bool validateEmail() {
|
|
if (state.email.isEmpty) {
|
|
emit(state.copyWith(isEmailValid: false));
|
|
return false;
|
|
}
|
|
emit(state.copyWith(isEmailValid: true));
|
|
return true;
|
|
}
|
|
|
|
bool validatePhone() {
|
|
if (state.phone.isEmpty) {
|
|
emit(state.copyWith(isPhoneValid: false));
|
|
return false;
|
|
}
|
|
emit(state.copyWith(isPhoneValid: true));
|
|
return true;
|
|
}
|
|
|
|
bool validateGender() {
|
|
if (state.gender.isEmpty) {
|
|
emit(state.copyWith(isGenderValid: false));
|
|
return false;
|
|
}
|
|
emit(state.copyWith(isGenderValid: true));
|
|
return true;
|
|
}
|
|
|
|
bool validateWeight() {
|
|
if (state.weight == 0) {
|
|
emit(state.copyWith(isWeightValid: false));
|
|
return false;
|
|
}
|
|
emit(state.copyWith(isWeightValid: true));
|
|
return true;
|
|
}
|
|
|
|
bool validateHeight() {
|
|
if (state.height == 0) {
|
|
emit(state.copyWith(isHeightValid: false));
|
|
return false;
|
|
}
|
|
emit(state.copyWith(isHeightValid: true));
|
|
return true;
|
|
}
|
|
|
|
bool validateActivityCoefficient() {
|
|
if (state.activityCoefficient == 0) {
|
|
emit(state.copyWith(isActivityCoefficientValid: false));
|
|
return false;
|
|
}
|
|
emit(state.copyWith(isActivityCoefficientValid: true));
|
|
return true;
|
|
}
|
|
|
|
bool validateBirthday() {
|
|
if (state.birthday == null) {
|
|
emit(state.copyWith(isBirthdayValid: false));
|
|
return false;
|
|
}
|
|
emit(state.copyWith(isBirthdayValid: true));
|
|
return true;
|
|
}
|
|
|
|
void nextStep() {
|
|
emit(state.copyWith(step: 1));
|
|
}
|
|
|
|
void prevStep() {
|
|
emit(state.copyWith(step: 0));
|
|
}
|
|
|
|
bool validateAllFields() {
|
|
if (!validateFirstName() ||
|
|
!validateLastName() ||
|
|
!validateEmail() ||
|
|
!validatePhone() ||
|
|
!validateGender() ||
|
|
!validateWeight() ||
|
|
!validateHeight() ||
|
|
!validateActivityCoefficient() ||
|
|
!validateBirthday()) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
Future<(bool, bool)> register() async {
|
|
if (!validateAllFields()) {
|
|
return (false, true);
|
|
}
|
|
final birthday = state.birthday?.format('yyyy-MM-dd');
|
|
if (birthday == null) {
|
|
validateBirthday();
|
|
return (false, true);
|
|
}
|
|
final password = await _generatePassword();
|
|
final registrationModel = RegistrationModel(
|
|
firstName: state.firstName,
|
|
lastName: state.lastName,
|
|
email: state.email,
|
|
phone: state.phone,
|
|
gender: state.gender,
|
|
password: password,
|
|
confirmPassword: password,
|
|
weight: state.weight,
|
|
height: state.height,
|
|
activityCoefficient: state.activityCoefficient,
|
|
birthday: birthday,
|
|
);
|
|
bool didApiComplainOnValidation = false;
|
|
try {
|
|
final token = await registrationRepository.register(registrationModel: registrationModel);
|
|
if (token != null) {
|
|
TokenStorage.saveTokenInStorage(token);
|
|
return (true, false);
|
|
}
|
|
} on DioException catch (e) {
|
|
final errors = e.response?.data['errors'] as Map<String, dynamic>?;
|
|
final emailErrors = errors?['email'] as List<dynamic>?;
|
|
final phoneErrors = errors?['phone'] as List<dynamic>?;
|
|
final dateOfBirth = errors?['date_of_birth'] as List<dynamic>?;
|
|
if (emailErrors != null && emailErrors.isNotEmpty) {
|
|
if (emailErrors.contains('Некорректный формат email')) {
|
|
emit(state.copyWith(isEmailValid: false, emailDisplayError: 'Некорректный формат email'));
|
|
didApiComplainOnValidation = true;
|
|
}
|
|
if (emailErrors.contains('Такой Эл. почта уже существует.')) {
|
|
emit(state.copyWith(isEmailValid: false, emailDisplayError: 'Такая Эл. почта уже существует.'));
|
|
didApiComplainOnValidation = true;
|
|
}
|
|
}
|
|
if (phoneErrors != null && phoneErrors.isNotEmpty) {
|
|
if (phoneErrors.contains('Такой phone уже существует.')) {
|
|
emit(state.copyWith(isPhoneValid: false, phoneDisplayError: 'Такой номер телефона уже существует.'));
|
|
didApiComplainOnValidation = true;
|
|
}
|
|
}
|
|
if (dateOfBirth != null && dateOfBirth.isNotEmpty) {
|
|
if (dateOfBirth.contains('Некорректный формат даты рождения')) {
|
|
emit(state.copyWith(isBirthdayValid: false, emailDisplayError: 'Некорректный формат даты рождения'));
|
|
didApiComplainOnValidation = true;
|
|
}
|
|
}
|
|
} catch (e) {
|
|
log(e.toString());
|
|
}
|
|
return (false, didApiComplainOnValidation);
|
|
}
|
|
|
|
Future<String> _generatePassword() async {
|
|
final generator = GPassword();
|
|
final password = generator.generate(passwordLength: 10);
|
|
emit(state.copyWith(password: password));
|
|
return password;
|
|
}
|
|
}
|