WT 1.1.26+3 bloc migration

This commit is contained in:
Tibor Bossanyi (Freelancer)
2022-04-15 08:47:32 +02:00
parent 60df0bcf2d
commit 225172f950
47 changed files with 1527 additions and 1620 deletions
+48 -41
View File
@@ -1,9 +1,6 @@
import 'dart:async';
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/repository/exercise_repository.dart';
import 'package:aitrainer_app/util/enums.dart';
import 'package:bloc/bloc.dart';
import 'package:equatable/equatable.dart';
@@ -17,12 +14,60 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
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;
@@ -57,42 +102,4 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
}
return bodyType;
}
@override
Stream<AccountState> mapEventToState(
AccountEvent event,
) async* {
try {
if (event is AccountChangeCustomer) {
// route to Customer Change page
yield AccountReady();
} else if (event is AccountLogin) {
//route to Login Page
} else if (event is AccountLogInFinished) {
customerRepository.customer = event.customer;
this.loggedIn = true;
yield AccountLoggedIn();
} else if (event is AccountLogout) {
await Cache().logout();
customerRepository.customer = null;
customerRepository.emptyTrainees();
loggedIn = false;
yield AccountLoggedOut();
} else if (event is AccountGetTrainees) {
yield AccountLoading();
await customerRepository.getTrainees();
yield AccountReady();
} else if (event is AccountSelectTrainee) {
yield AccountLoading();
customerRepository.setTrainee(event.traineeId);
Cache().setTrainee(customerRepository.getTraineeById(event.traineeId)!);
ExerciseRepository exerciseRepository = ExerciseRepository();
await exerciseRepository.getExercisesByCustomer(event.traineeId);
this.traineeId = event.traineeId;
yield AccountReady();
}
} on Exception catch (e) {
yield AccountError(message: e.toString());
}
}
}