47 lines
1.4 KiB
Dart
47 lines
1.4 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:aitrainer_app/model/auth.dart';
|
|
import 'package:aitrainer_app/model/customer.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 'account_event.dart';
|
|
part 'account_state.dart';
|
|
|
|
class AccountBloc extends Bloc<AccountEvent, AccountState> {
|
|
final CustomerRepository customerRepository;
|
|
bool loggedIn = false;
|
|
AccountBloc({this.customerRepository}) : super(AccountInitial()) {
|
|
if ( Auth().userLoggedIn != null ) {
|
|
customerRepository.customer = Auth().userLoggedIn;
|
|
loggedIn = true;
|
|
}
|
|
}
|
|
|
|
@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;
|
|
yield AccountLoggedIn();
|
|
} else if (event is AccountLogout) {
|
|
await Auth().logout();
|
|
customerRepository.customer = null;
|
|
loggedIn = false;
|
|
yield AccountLoggedOut();
|
|
}
|
|
} on Exception catch(e) {
|
|
yield AccountError(message: e.toString());
|
|
}
|
|
}
|
|
}
|