Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions day21_login_ui_bloc/lib/auth/login/login_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,29 @@ import 'package:social_media_app/auth/login/login_event.dart';
import 'package:social_media_app/auth/login/login_state.dart';

class LoginBloc extends Bloc<LoginEvent, LoginState> {
final AuthRepository authRepo;
final AuthRepository authRepository;

LoginBloc({this.authRepo}) : super(LoginState());

@override
Stream<LoginState> mapEventToState(LoginEvent event) async* {
// Username updated
if (event is LoginUsernameChanged) {
yield state.copyWith(username: event.username);
LoginBloc({required this.authRepository}) : super(LoginState()) {
on<LoginUsernameChanged>(_onLoginUsernameChanged);
on<LoginPasswordChanged>(_onLoginPasswordChanged);
on<LoginSubmitted>(_onLoginSubmitted);
}

// Password updated
} else if (event is LoginPasswordChanged) {
yield state.copyWith(password: event.password);
void _onLoginUsernameChanged(LoginUsernameChanged event, Emitter<LoginState> emit) async {
emit(state.copyWith(username: event.username));
}

// Form submitted
} else if (event is LoginSubmitted) {
yield state.copyWith(formStatus: FormSubmitting());
void _onLoginPasswordChanged(LoginPasswordChanged event, Emitter<LoginState> emit) async {
emit(state.copyWith(password: event.password));
}

try {
await authRepo.login();
yield state.copyWith(formStatus: SubmissionSuccess());
} catch (e) {
yield state.copyWith(formStatus: SubmissionFailed(e));
}
void _onLoginSubmitted(LoginSubmitted event, Emitter<LoginState> emit) async {
emit(state.copyWith(formStatus: FormSubmitting()));
try {
await authRepository.login();
emit(state.copyWith(formStatus: SubmissionSuccess()));
} on Exception catch (e) {
emit(state.copyWith(formStatus: SubmissionFailed(e)));
}
}
}