wt1.1a flutter_bloc
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
part of 'account_bloc.dart';
|
||||
|
||||
@immutable
|
||||
abstract class AccountEvent extends Equatable {
|
||||
const AccountEvent();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class AccountChangeCustomer extends AccountEvent {
|
||||
final Customer customer;
|
||||
|
||||
const AccountChangeCustomer({this.customer});
|
||||
|
||||
@override
|
||||
List<Object> get props => [customer];
|
||||
}
|
||||
|
||||
class AccountLogout extends AccountEvent {
|
||||
final Customer customer;
|
||||
|
||||
const AccountLogout({this.customer});
|
||||
|
||||
@override
|
||||
List<Object> get props => [customer];
|
||||
|
||||
}
|
||||
|
||||
class AccountLogin extends AccountEvent {
|
||||
final Customer customer;
|
||||
|
||||
const AccountLogin({this.customer});
|
||||
|
||||
@override
|
||||
List<Object> get props => [customer];
|
||||
}
|
||||
|
||||
class AccountLogInFinished extends AccountEvent {
|
||||
final Customer customer;
|
||||
|
||||
const AccountLogInFinished({this.customer});
|
||||
|
||||
@override
|
||||
List<Object> get props => [customer];
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
part of 'account_bloc.dart';
|
||||
|
||||
@immutable
|
||||
abstract class AccountState extends Equatable {
|
||||
const AccountState();
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class AccountInitial extends AccountState {
|
||||
const AccountInitial();
|
||||
}
|
||||
|
||||
class AccountLoading extends AccountState {
|
||||
const AccountLoading();
|
||||
}
|
||||
|
||||
class AccountReady extends AccountState {
|
||||
const AccountReady();
|
||||
}
|
||||
|
||||
class AccountLoggedOut extends AccountState {
|
||||
const AccountLoggedOut();
|
||||
}
|
||||
|
||||
class AccountLoggedIn extends AccountState {
|
||||
const AccountLoggedIn();
|
||||
}
|
||||
|
||||
class AccountError extends AccountState {
|
||||
final String message;
|
||||
const AccountError({this.message});
|
||||
|
||||
@override
|
||||
List<Object> get props => [message];
|
||||
}
|
||||
Reference in New Issue
Block a user