Skip to content

Commit

Permalink
rfac: improves BLoC structure || feat: adds showPassword functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
am-casper committed Dec 22, 2023
1 parent cc5df7e commit e576d0d
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 22 deletions.
35 changes: 31 additions & 4 deletions lib/presentation/reset_password/bloc/reset_password_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,48 @@ part 'reset_password_state.dart';
class ResetPasswordBloc extends Bloc<ResetPasswordEvent, ResetPasswordState> {
final UserRepository userRepository;
ResetPasswordBloc({required this.userRepository})
: super(const ResetPasswordInitial()) {
: super(const ResetPassword(
showOldPassword: false,
showNewPassword: false,
showConfirmPassword: false,
)) {
on<ResetPasswordPressed>(_onResetPasswordPressed);
on<ToggleObscureResetPassword>(_onToggleObscureResetPassword);
}

FutureOr<void> _onResetPasswordPressed(event, emit) async {
if (event.newPassword.length < 8) {
emit(
const ResetPasswordInitial(
ResetPassword(
error: 'Password must be at least 8 characters long',
showOldPassword: (state as ResetPassword).showOldPassword,
showNewPassword: (state as ResetPassword).showNewPassword,
showConfirmPassword: (state as ResetPassword).showConfirmPassword,
),
);
emit(ResetPassword(
error: null,
showOldPassword: (state as ResetPassword).showOldPassword,
showNewPassword: (state as ResetPassword).showNewPassword,
showConfirmPassword: (state as ResetPassword).showConfirmPassword,
));
return;
}
if (event.newPassword != event.confirmPassword) {
emit(
const ResetPasswordInitial(
ResetPassword(
error: 'Passwords do not match',
showOldPassword: (state as ResetPassword).showOldPassword,
showNewPassword: (state as ResetPassword).showNewPassword,
showConfirmPassword: (state as ResetPassword).showConfirmPassword,
),
);
emit(ResetPassword(
error: null,
showOldPassword: (state as ResetPassword).showOldPassword,
showNewPassword: (state as ResetPassword).showNewPassword,
showConfirmPassword: (state as ResetPassword).showConfirmPassword,
));
return;
}
emit(Loading());
Expand All @@ -41,7 +63,12 @@ class ResetPasswordBloc extends Bloc<ResetPasswordEvent, ResetPasswordState> {
);
emit(const ResetPasswordSuccess());
} catch (e) {
emit(const ResetPasswordInitial(error: AppConstants.GENERIC_FAILURE));
emit(const ResetPassword(
error: AppConstants.GENERIC_FAILURE,
showOldPassword: false,
showNewPassword: false,
showConfirmPassword: false,
));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ class ToggleObscureResetPassword extends ResetPasswordEvent {
required this.showConfirmPassword,
});
}


15 changes: 5 additions & 10 deletions lib/presentation/reset_password/bloc/reset_password_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ abstract class ResetPasswordState extends Equatable {
List<Object?> get props => [];
}

class ResetPasswordInitial extends ResetPasswordState {
const ResetPasswordInitial({this.error});

final String? error;

@override
List<Object?> get props => [error];
}

class Loading extends ResetPasswordState {}

Expand All @@ -24,21 +16,24 @@ class ResetPasswordSuccess extends ResetPasswordState {

class ResetPassword extends ResetPasswordState {
const ResetPassword({
this.error,
required this.showOldPassword,
required this.showNewPassword,
required this.showConfirmPassword,
});

final String? error;
final bool showOldPassword;
final bool showNewPassword;
final bool showConfirmPassword;

ResetPassword copyWith({
String? error,
bool? showOldPassword,
bool? showNewPassword,
bool? showConfirmPassword,
}) {
return ResetPassword(
error: error ?? this.error,
showOldPassword: showOldPassword ?? this.showOldPassword,
showNewPassword: showNewPassword ?? this.showNewPassword,
showConfirmPassword: showConfirmPassword ?? this.showConfirmPassword,
Expand All @@ -47,5 +42,5 @@ class ResetPassword extends ResetPasswordState {

@override
List<Object?> get props =>
[showOldPassword, showNewPassword, showConfirmPassword];
[error, showOldPassword, showNewPassword, showConfirmPassword];
}
65 changes: 57 additions & 8 deletions lib/presentation/reset_password/reset_password_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ class ResetPasswordScreen extends StatelessWidget {
backgroundColor: Colors.white,
body: BlocConsumer<ResetPasswordBloc, ResetPasswordState>(
listener: (context, state) {
if (state is ResetPasswordInitial && state.error != null) {
_confirmPasswordController.clear();
_newPasswordController.clear();
_oldPasswordController.clear();
if (state is ResetPassword && state.error != null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(state.error!),
Expand All @@ -44,7 +41,7 @@ class ResetPasswordScreen extends StatelessWidget {
}
},
builder: (context, state) {
if (state is ResetPasswordInitial) {
if (state is ResetPassword) {
return Column(
children: [
const ResetPasswordBanner(),
Expand All @@ -65,7 +62,7 @@ class ResetPasswordScreen extends StatelessWidget {
20.toVerticalSizedBox,
TextField(
controller: _oldPasswordController,
obscureText: true,
obscureText: !state.showOldPassword,
decoration: InputDecoration(
hintText: 'Old Password',
hintStyle: GoogleFonts.lato(
Expand All @@ -81,6 +78,23 @@ class ResetPasswordScreen extends StatelessWidget {
),
contentPadding: EdgeInsets.symmetric(
horizontal: 20.toAutoScaledWidth),
suffixIcon: IconButton(
onPressed: () {
BlocProvider.of<ResetPasswordBloc>(context)
.add(
ToggleObscureResetPassword(
showOldPassword: !state.showOldPassword,
showNewPassword: state.showNewPassword,
showConfirmPassword:
state.showConfirmPassword),
);
},
icon: state.showOldPassword
? const Icon(Icons.visibility,
color: Color(0xFF757575))
: const Icon(Icons.visibility_off,
color: Color(0xFF757575)),
),
),
),
20.toVerticalSizedBox,
Expand All @@ -94,7 +108,7 @@ class ResetPasswordScreen extends StatelessWidget {
20.toVerticalSizedBox,
TextField(
controller: _newPasswordController,
obscureText: true,
obscureText: !state.showNewPassword,
decoration: InputDecoration(
hintText: 'New Password',
hintStyle: GoogleFonts.lato(
Expand All @@ -110,6 +124,23 @@ class ResetPasswordScreen extends StatelessWidget {
),
contentPadding: EdgeInsets.symmetric(
horizontal: 20.toAutoScaledWidth),
suffixIcon: IconButton(
onPressed: () {
BlocProvider.of<ResetPasswordBloc>(context)
.add(
ToggleObscureResetPassword(
showNewPassword: !state.showNewPassword,
showOldPassword: state.showOldPassword,
showConfirmPassword:
state.showConfirmPassword),
);
},
icon: state.showNewPassword
? const Icon(Icons.visibility,
color: Color(0xFF757575))
: const Icon(Icons.visibility_off,
color: Color(0xFF757575)),
),
),
),
20.toVerticalSizedBox,
Expand All @@ -123,7 +154,7 @@ class ResetPasswordScreen extends StatelessWidget {
20.toVerticalSizedBox,
TextField(
controller: _confirmPasswordController,
obscureText: true,
obscureText: !state.showConfirmPassword,
decoration: InputDecoration(
hintText: 'Confirm Password',
hintStyle: GoogleFonts.lato(
Expand All @@ -139,6 +170,23 @@ class ResetPasswordScreen extends StatelessWidget {
),
contentPadding: EdgeInsets.symmetric(
horizontal: 20.toAutoScaledWidth),
suffixIcon: IconButton(
onPressed: () {
BlocProvider.of<ResetPasswordBloc>(context)
.add(
ToggleObscureResetPassword(
showConfirmPassword:
!state.showConfirmPassword,
showOldPassword: state.showOldPassword,
showNewPassword: state.showNewPassword),
);
},
icon: state.showConfirmPassword
? const Icon(Icons.visibility,
color: Color(0xFF757575))
: const Icon(Icons.visibility_off,
color: Color(0xFF757575)),
),
),
),
20.toVerticalSizedBox,
Expand All @@ -151,6 +199,7 @@ class ResetPasswordScreen extends StatelessWidget {
_oldPasswordController.text,
_newPasswordController.text,
_confirmPasswordController.text));

},
),
),
Expand Down

0 comments on commit e576d0d

Please sign in to comment.