@@ -4,6 +4,7 @@ import 'package:freezed_annotation/freezed_annotation.dart';
44import 'package:get/get.dart' ;
55
66import '../../../data/constants/routes.dart' ;
7+ import '../../../data/constants/strings.dart' ;
78import '../../../domain/models/status.dart' ;
89import '../../../domain/repositories/user_repository.dart' ;
910
@@ -12,7 +13,7 @@ part 'login_event.dart';
1213part 'login_state.dart' ;
1314
1415class LoginBloc extends Bloc <LoginEvent , LoginState > {
15- LoginBloc () : super (LoginState (rememberMe : true )) {
16+ LoginBloc () : super (const LoginState ()) {
1617 on < ToggleDialog > (_toggleDialog);
1718 on < PasswordInput > (_updatePasswordInput);
1819 on < Submit > (_submitForm);
@@ -22,6 +23,8 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
2223 }
2324
2425 void _submitForm (Submit event, Emitter <LoginState > emit) async {
26+ if (! state.isFormFilled ()) return ;
27+
2528 emit (state.copyWith (status: const Status .loading ()));
2629
2730 final result = await UserRepository .login (
@@ -35,50 +38,43 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
3538 } else {
3639 if (result == 'Unverified' ) {
3740 emit (state.copyWith (
38- status: const Status .error (
39- 'Please verify your email before attempting to log in\n '
40- 'Check your email for the verification link' ,
41- ),
41+ status: const Status .error (AppStrings .verifyFirst),
4242 ));
4343 } else if (result == 'Unauthorized' ) {
4444 emit (state.copyWith (
45- status: const Status .error ('Incorrect credentials' ),
45+ status: const Status .error (AppStrings .incorrectCredentials ),
4646 ));
4747 } else {
4848 emit (state.copyWith (
49- status: const Status .error ('Something went wrong' ),
49+ status: const Status .error (AppStrings .genericError ),
5050 ));
5151 }
5252 }
5353 }
5454
5555 void _toggleDialog (ToggleDialog event, Emitter <LoginState > emit) async {
56- if (event.email != null ) {
56+ if (event.email == null ) {
57+ emit (state.copyWith (showDialog: ! state.showDialog));
58+ } else {
59+ if (event.email! .isEmpty) return ;
5760 emit (state.copyWith (
5861 showDialog: ! state.showDialog,
5962 status: const Status .loading (),
6063 ));
61- if (event.email! .isEmpty) return ;
6264 final result = await UserRepository .resetPassword (event.email! );
6365 if (result == null ) {
6466 emit (state.copyWith (
65- status: const Status .error ('Something went wrong' ),
67+ status: const Status .error (AppStrings .genericError ),
6668 ));
6769 } else if (result) {
6870 emit (state.copyWith (
69- status: const Status (
70- 'Success! Please check your email for a password reset link' ,
71- ),
71+ status: const Status (AppStrings .passwordResetSuccess),
7272 ));
7373 } else {
7474 emit (state.copyWith (
75- status: const Status .error (
76- 'No user associated with given email address' ,
77- ),
75+ status: const Status .error (AppStrings .noUserWithEmail),
7876 ));
7977 }
80- } else {
81- emit (state.copyWith (showDialog: ! state.showDialog));
8278 }
8379 }
8480
@@ -91,10 +87,18 @@ class LoginBloc extends Bloc<LoginEvent, LoginState> {
9187 }
9288
9389 void _updatePasswordInput (PasswordInput event, Emitter <LoginState > emit) {
94- emit (state.copyWith (password: event.value));
90+ emit (state.copyWith (
91+ isPasswordFocused: true ,
92+ isUsernameFocused: false ,
93+ password: event.value,
94+ ));
9595 }
9696
9797 void _updateUsernameInput (UsernameInput event, Emitter <LoginState > emit) {
98- emit (state.copyWith (username: event.value));
98+ emit (state.copyWith (
99+ isUsernameFocused: true ,
100+ isPasswordFocused: false ,
101+ username: event.value,
102+ ));
99103 }
100104}
0 commit comments