180 lines
6.7 KiB
Dart
180 lines
6.7 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:aitrainer_app/model/cache.dart';
|
|
import 'package:aitrainer_app/model/sport.dart';
|
|
import 'package:aitrainer_app/repository/customer_repository.dart';
|
|
import 'package:aitrainer_app/repository/mautic_repository.dart';
|
|
import 'package:aitrainer_app/util/common.dart';
|
|
import 'package:bloc/bloc.dart';
|
|
import 'package:equatable/equatable.dart';
|
|
import 'package:flutter/material.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({required this.customerRepository}) : super(CustomerChangeInitial()) {
|
|
if (this.customerRepository.customer == null) {
|
|
this.customerRepository.createNew();
|
|
}
|
|
year = this.customerRepository.customer!.birthYear;
|
|
if (year == null || year == 0) {
|
|
year = 1990;
|
|
}
|
|
weight = this.customerRepository.getWeight() == 0 ? 60 : this.customerRepository.getWeight();
|
|
height = this.customerRepository.getHeight() == 0 ? 170 : this.customerRepository.getHeight();
|
|
|
|
selectedFitnessItem = customerRepository.fitnessLevel;
|
|
selectedSport = customerRepository.getSport();
|
|
}
|
|
|
|
Sport? selectedSport;
|
|
String? selectedFitnessItem;
|
|
|
|
@override
|
|
Stream<CustomerChangeState> mapEventToState(
|
|
CustomerChangeEvent event,
|
|
) async* {
|
|
try {
|
|
print("Event: $event");
|
|
if (event is CustomerLoad) {
|
|
yield CustomerChangeLoading();
|
|
yield CustomerDataChanged();
|
|
} else if (event is CustomerGoalChange) {
|
|
yield CustomerChangeLoading();
|
|
customerRepository.setGoal(event.goal);
|
|
yield CustomerDataChanged();
|
|
} else if (event is CustomerChangePasswordObscure) {
|
|
yield CustomerChangeLoading();
|
|
visiblePassword = !visiblePassword;
|
|
yield CustomerDataChanged();
|
|
} else if (event is CustomerFitnessChange) {
|
|
yield CustomerChangeLoading();
|
|
customerRepository.setFitnessLevel(event.fitness);
|
|
selectedFitnessItem = 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) {
|
|
yield CustomerChangeLoading();
|
|
customerRepository.setSex(event.gender == 0 ? "m" : "w");
|
|
yield CustomerDataChanged();
|
|
} else if (event is CustomerSportChange) {
|
|
yield CustomerChangeLoading();
|
|
selectedSport = event.sport;
|
|
print("Selected Sport $selectedSport");
|
|
yield CustomerDataChanged();
|
|
} else if (event is CustomerSaveFitness) {
|
|
yield CustomerChangeLoading();
|
|
if (customerRepository.customer!.fitnessLevel == null) {
|
|
throw Exception("Please select your fitness level");
|
|
}
|
|
yield CustomerSaveSuccess();
|
|
} else if (event is CustomerSaveGoal) {
|
|
yield CustomerChangeLoading();
|
|
if (customerRepository.customer!.goal == null) {
|
|
throw Exception("Please select your goal");
|
|
}
|
|
yield CustomerSaveSuccess();
|
|
} else if (event is CustomerSaveSex) {
|
|
yield CustomerChangeLoading();
|
|
if (customerRepository.customer!.sex == null) {
|
|
throw Exception("Please select your biologial gender");
|
|
}
|
|
yield CustomerSaveSuccess();
|
|
} else if (event is CustomerSaveWeight) {
|
|
yield CustomerChangeLoading();
|
|
if (customerRepository.customer!.getProperty("Weight") == null) {
|
|
throw Exception("Please select your weight");
|
|
}
|
|
yield CustomerSaveSuccess();
|
|
} else if (event is CustomerSaveHeight) {
|
|
yield CustomerChangeLoading();
|
|
if (customerRepository.customer!.getProperty("Height") == null) {
|
|
throw Exception("Please select your height");
|
|
}
|
|
yield CustomerSaveSuccess();
|
|
} else if (event is CustomerSave) {
|
|
yield CustomerSaving();
|
|
if (validation()) {
|
|
if (selectedFitnessItem != null) {
|
|
customerRepository.setFitnessLevel(selectedFitnessItem!);
|
|
}
|
|
if (selectedSport != null) {
|
|
customerRepository.customer!.sportId = selectedSport!.sportId;
|
|
}
|
|
|
|
await customerRepository.saveCustomer();
|
|
MauticRepository mauticRepository = MauticRepository(customerRepository: customerRepository);
|
|
await mauticRepository.sendMauticDataChange();
|
|
Cache().initBadges();
|
|
yield CustomerSaveSuccess();
|
|
} else {
|
|
yield CustomerSaveError(message: "Please provide the necessary information");
|
|
}
|
|
}
|
|
} on Exception catch (e) {
|
|
yield CustomerSaveError(message: e.toString());
|
|
}
|
|
}
|
|
|
|
bool validation() {
|
|
if (customerRepository.customer == null) throw Exception("Customer object not defined");
|
|
return true;
|
|
}
|
|
|
|
String? emailValidation(String? email) {
|
|
String? message = Common.emailValidation(email);
|
|
return message;
|
|
}
|
|
|
|
String? passwordValidation(String? value) {
|
|
String? message = Common.passwordValidation(value);
|
|
return message;
|
|
}
|
|
|
|
String? nameValidation(String? value) {
|
|
if (value == null || value.length == 0) {
|
|
return "Name too short";
|
|
}
|
|
return null;
|
|
}
|
|
|
|
Sport? get getSelectedSport => this.selectedSport;
|
|
set setSelectedSport(Sport? selectedSport) => this.selectedSport = selectedSport;
|
|
}
|