feat: Refactor code structure and optimize performance across multiple modules
This commit is contained in:
@@ -0,0 +1,217 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:easy_localization/easy_localization.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:krow/core/application/common/validators/email_validator.dart';
|
||||
import 'package:krow/core/application/di/injectable.dart';
|
||||
import 'package:krow/core/data/models/staff/staff.dart';
|
||||
import 'package:krow/core/data/enums/state_status.dart';
|
||||
import 'package:krow/features/profile/email_verification/data/email_verification_service.dart';
|
||||
import 'package:krow/features/profile/personal_info/data/staff_repository.dart';
|
||||
|
||||
part 'personal_info_event.dart';
|
||||
|
||||
part 'personal_info_state.dart';
|
||||
|
||||
class PersonalInfoBloc extends Bloc<PersonalInfoEvent, PersonalInfoState> {
|
||||
PersonalInfoBloc() : super(const PersonalInfoState()) {
|
||||
on<InitializeProfileInfoEvent>(_onInitializeProfileInfoEvent);
|
||||
|
||||
on<UpdateProfileImage>(_onUpdateProfileImage);
|
||||
|
||||
on<UpdateFirstName>(_onUpdateFirstName);
|
||||
|
||||
on<UpdateLastName>(_onUpdateLastName);
|
||||
|
||||
on<UpdateMiddleName>(_onUpdateMiddleName);
|
||||
|
||||
on<UpdateEmail>(_onUpdateEmail);
|
||||
|
||||
on<UpdatePhoneNumber>(_onUpdatePhoneNumber);
|
||||
|
||||
on<UpdateEmailVerificationStatus>(_onUpdateEmailVerification);
|
||||
|
||||
on<SaveProfileChanges>(_onSaveProfileChanges);
|
||||
}
|
||||
|
||||
Future<void> _onInitializeProfileInfoEvent(
|
||||
InitializeProfileInfoEvent event,
|
||||
Emitter<PersonalInfoState> emit,
|
||||
) async {
|
||||
emit(
|
||||
state.copyWith(
|
||||
isInEditMode: event.isInEditMode,
|
||||
status: event.isInEditMode ? StateStatus.loading : StateStatus.idle,
|
||||
),
|
||||
);
|
||||
if (!state.isInEditMode) return;
|
||||
|
||||
await for (final currentStaffData
|
||||
in getIt<StaffPersonalInfoRepository>().getPersonalInfo()) {
|
||||
try {
|
||||
emit(
|
||||
state.copyWith(
|
||||
firstName: currentStaffData.firstName,
|
||||
lastName: currentStaffData.lastName,
|
||||
middleName: currentStaffData.middleName,
|
||||
email: currentStaffData.email,
|
||||
phoneNumber: currentStaffData.phone,
|
||||
profileImageUrl: currentStaffData.avatar,
|
||||
isUpdateReceived: true,
|
||||
status: StateStatus.idle,
|
||||
),
|
||||
);
|
||||
} catch (except) {
|
||||
log(except.toString());
|
||||
} finally {
|
||||
emit(state.copyWith(isUpdateReceived: false));
|
||||
}
|
||||
}
|
||||
|
||||
if (state.status == StateStatus.loading) {
|
||||
emit(state.copyWith(status: StateStatus.idle));
|
||||
}
|
||||
}
|
||||
|
||||
void _onUpdateProfileImage(
|
||||
UpdateProfileImage event,
|
||||
Emitter<PersonalInfoState> emit,
|
||||
) {
|
||||
emit(state.copyWith(profileImagePath: event.imagePath));
|
||||
}
|
||||
|
||||
void _onUpdateFirstName(
|
||||
UpdateFirstName event,
|
||||
Emitter<PersonalInfoState> emit,
|
||||
) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
firstName: event.firstName,
|
||||
validationErrors: Map.from(state.validationErrors)
|
||||
..remove(ProfileRequiredField.firstName),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _onUpdateLastName(
|
||||
UpdateLastName event,
|
||||
Emitter<PersonalInfoState> emit,
|
||||
) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
lastName: event.lastName,
|
||||
validationErrors: Map.from(state.validationErrors)
|
||||
..remove(ProfileRequiredField.lastName),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _onUpdateMiddleName(
|
||||
UpdateMiddleName event,
|
||||
Emitter<PersonalInfoState> emit,
|
||||
) {
|
||||
emit(state.copyWith(middleName: event.middleName));
|
||||
}
|
||||
|
||||
void _onUpdateEmail(
|
||||
UpdateEmail event,
|
||||
Emitter<PersonalInfoState> emit,
|
||||
) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
email: event.email,
|
||||
validationErrors: Map.from(state.validationErrors)
|
||||
..remove(ProfileRequiredField.email),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _onUpdatePhoneNumber(
|
||||
UpdatePhoneNumber event,
|
||||
Emitter<PersonalInfoState> emit,
|
||||
) {
|
||||
emit(state.copyWith(phoneNumber: event.phoneNumber));
|
||||
}
|
||||
|
||||
void _onUpdateEmailVerification(
|
||||
UpdateEmailVerificationStatus event,
|
||||
Emitter<PersonalInfoState> emit,
|
||||
) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
isEmailVerified: event.isEmailVerified,
|
||||
shouldRouteToVerification: false,
|
||||
),
|
||||
);
|
||||
|
||||
if (state.isEmailVerified) {
|
||||
add(const SaveProfileChanges(shouldSkipEmailVerification: true));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _onSaveProfileChanges(
|
||||
SaveProfileChanges event,
|
||||
Emitter<PersonalInfoState> emit,
|
||||
) async {
|
||||
final errorsMap = state.invalidate();
|
||||
if (errorsMap.isNotEmpty) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: StateStatus.error,
|
||||
validationErrors: errorsMap,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
emit(state.copyWith(status: StateStatus.loading, validationErrors: {}));
|
||||
|
||||
if (!event.shouldSkipEmailVerification) {
|
||||
log('Verifying email');
|
||||
bool isEmailVerified = getIt<EmailVerificationService>()
|
||||
.checkEmailForVerification(email: state.email);
|
||||
log('Email verified: $isEmailVerified');
|
||||
emit(
|
||||
state.copyWith(
|
||||
isEmailVerified: isEmailVerified,
|
||||
shouldRouteToVerification: !isEmailVerified,
|
||||
),
|
||||
);
|
||||
|
||||
log('Should route to Verification ${state.shouldRouteToVerification}');
|
||||
if (!isEmailVerified) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: StateStatus.idle,
|
||||
shouldRouteToVerification: false,
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
log('Continuing with profile saving');
|
||||
}
|
||||
|
||||
Staff? response;
|
||||
try {
|
||||
response = await getIt<StaffPersonalInfoRepository>().updatePersonalInfo(
|
||||
firstName: state.firstName,
|
||||
middleName: state.middleName,
|
||||
lastName: state.lastName,
|
||||
email: state.email,
|
||||
avatarPath: state.profileImagePath,
|
||||
);
|
||||
} catch (except) {
|
||||
log(
|
||||
'Error in PersonalInfoBloc. Event SaveProfileChanges',
|
||||
error: except,
|
||||
);
|
||||
} finally {
|
||||
emit(
|
||||
state.copyWith(
|
||||
status: response != null ? StateStatus.success : StateStatus.idle,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
part of 'personal_info_bloc.dart';
|
||||
|
||||
@immutable
|
||||
sealed class PersonalInfoEvent {
|
||||
const PersonalInfoEvent();
|
||||
}
|
||||
|
||||
class InitializeProfileInfoEvent extends PersonalInfoEvent {
|
||||
const InitializeProfileInfoEvent(this.isInEditMode);
|
||||
|
||||
final bool isInEditMode;
|
||||
}
|
||||
|
||||
class UpdateProfileImage extends PersonalInfoEvent {
|
||||
const UpdateProfileImage(this.imagePath);
|
||||
|
||||
final String imagePath;
|
||||
}
|
||||
|
||||
class UpdateFirstName extends PersonalInfoEvent {
|
||||
const UpdateFirstName(this.firstName);
|
||||
|
||||
final String firstName;
|
||||
}
|
||||
|
||||
class UpdateLastName extends PersonalInfoEvent {
|
||||
const UpdateLastName(this.lastName);
|
||||
|
||||
final String lastName;
|
||||
}
|
||||
|
||||
class UpdateMiddleName extends PersonalInfoEvent {
|
||||
const UpdateMiddleName(this.middleName);
|
||||
|
||||
final String middleName;
|
||||
}
|
||||
|
||||
class UpdateEmail extends PersonalInfoEvent {
|
||||
const UpdateEmail(this.email);
|
||||
|
||||
final String email;
|
||||
}
|
||||
|
||||
class UpdatePhoneNumber extends PersonalInfoEvent {
|
||||
const UpdatePhoneNumber(this.phoneNumber);
|
||||
|
||||
final String phoneNumber;
|
||||
}
|
||||
|
||||
class UpdateEmailVerificationStatus extends PersonalInfoEvent {
|
||||
const UpdateEmailVerificationStatus({required this.isEmailVerified});
|
||||
|
||||
final bool isEmailVerified;
|
||||
}
|
||||
|
||||
class SaveProfileChanges extends PersonalInfoEvent {
|
||||
const SaveProfileChanges({this.shouldSkipEmailVerification = false});
|
||||
|
||||
final bool shouldSkipEmailVerification;
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
part of 'personal_info_bloc.dart';
|
||||
|
||||
@immutable
|
||||
class PersonalInfoState {
|
||||
const PersonalInfoState({
|
||||
this.firstName = '',
|
||||
this.lastName = '',
|
||||
this.email = '',
|
||||
this.phoneNumber = '',
|
||||
this.middleName,
|
||||
this.profileImageUrl,
|
||||
this.profileImagePath,
|
||||
this.isInEditMode = true,
|
||||
this.isUpdateReceived = false,
|
||||
this.status = StateStatus.idle,
|
||||
this.validationErrors = const <ProfileRequiredField, String>{},
|
||||
this.isEmailVerified = false,
|
||||
this.shouldRouteToVerification = false,
|
||||
});
|
||||
|
||||
final String firstName;
|
||||
final String lastName;
|
||||
final String email;
|
||||
final String phoneNumber;
|
||||
final String? middleName;
|
||||
final String? profileImageUrl;
|
||||
final String? profileImagePath;
|
||||
final bool isInEditMode;
|
||||
final bool isUpdateReceived;
|
||||
final StateStatus status;
|
||||
final Map<ProfileRequiredField, String> validationErrors;
|
||||
final bool isEmailVerified;
|
||||
final bool shouldRouteToVerification;
|
||||
|
||||
PersonalInfoState copyWith({
|
||||
String? firstName,
|
||||
String? lastName,
|
||||
String? email,
|
||||
String? phoneNumber,
|
||||
String? middleName,
|
||||
String? profileImageUrl,
|
||||
String? profileImagePath,
|
||||
bool? isInEditMode,
|
||||
bool? isUpdateReceived,
|
||||
StateStatus? status,
|
||||
Map<ProfileRequiredField, String>? validationErrors,
|
||||
bool? isEmailVerified,
|
||||
bool? shouldRouteToVerification,
|
||||
}) {
|
||||
return PersonalInfoState(
|
||||
firstName: firstName ?? this.firstName,
|
||||
lastName: lastName ?? this.lastName,
|
||||
email: email ?? this.email,
|
||||
phoneNumber: phoneNumber ?? this.phoneNumber,
|
||||
middleName: middleName ?? this.middleName,
|
||||
profileImagePath: profileImagePath ?? this.profileImagePath,
|
||||
profileImageUrl: profileImageUrl ?? this.profileImageUrl,
|
||||
isInEditMode: isInEditMode ?? this.isInEditMode,
|
||||
isUpdateReceived: isUpdateReceived ?? this.isUpdateReceived,
|
||||
status: status ?? this.status,
|
||||
validationErrors: validationErrors ?? this.validationErrors,
|
||||
isEmailVerified: isEmailVerified ?? this.isEmailVerified,
|
||||
shouldRouteToVerification:
|
||||
shouldRouteToVerification ?? this.shouldRouteToVerification,
|
||||
);
|
||||
}
|
||||
|
||||
Map<ProfileRequiredField, String> invalidate() {
|
||||
final emailError = EmailValidator.validate(email, isRequired: true);
|
||||
|
||||
return {
|
||||
if (firstName.isEmpty) ProfileRequiredField.firstName: 'required_to_fill'.tr(),
|
||||
if (lastName.isEmpty) ProfileRequiredField.lastName: 'required_to_fill'.tr(),
|
||||
if (emailError != null) ProfileRequiredField.email: emailError,
|
||||
if (profileImagePath == null && profileImageUrl == null)
|
||||
ProfileRequiredField.avatar: 'required'.tr(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
enum ProfileRequiredField { firstName, lastName, email, avatar }
|
||||
@@ -0,0 +1,42 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:krow/core/data/models/staff/staff.dart';
|
||||
import 'package:krow/features/profile/personal_info/data/staff_api_source.dart';
|
||||
import 'package:krow/features/profile/personal_info/data/staff_repository.dart';
|
||||
|
||||
@Singleton(as: StaffPersonalInfoRepository)
|
||||
class StaffPersonalInfoRepositoryImpl implements StaffPersonalInfoRepository {
|
||||
StaffPersonalInfoRepositoryImpl({
|
||||
required StaffPersonalInfoApiProvider staffApi,
|
||||
}) : _staffApi = staffApi;
|
||||
|
||||
final StaffPersonalInfoApiProvider _staffApi;
|
||||
|
||||
@override
|
||||
Stream<Staff> getPersonalInfo() {
|
||||
return _staffApi.getMeWithCache();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Staff> updatePersonalInfo({
|
||||
required String firstName,
|
||||
required String? middleName,
|
||||
required String lastName,
|
||||
required String email,
|
||||
String? avatarPath,
|
||||
}) {
|
||||
try {
|
||||
return _staffApi.updateStaffPersonalInfo(
|
||||
firstName: firstName,
|
||||
middleName: middleName,
|
||||
lastName: lastName,
|
||||
email: email,
|
||||
avatarPath: avatarPath,
|
||||
);
|
||||
} catch (exception) {
|
||||
log((exception as Error).stackTrace.toString());
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user