feat: Refactor code structure and optimize performance across multiple modules
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:krow/core/application/di/injectable.dart';
|
||||
import 'package:krow/core/data/enums/state_status.dart';
|
||||
import 'package:krow/features/profile/emergency_contacts/data/emergency_contact_repository.dart';
|
||||
import 'package:krow/features/profile/emergency_contacts/data/models/emergency_contact_model.dart';
|
||||
|
||||
part 'emergency_contacts_event.dart';
|
||||
|
||||
part 'emergency_contacts_state.dart';
|
||||
|
||||
class EmergencyContactsBloc
|
||||
extends Bloc<EmergencyContactsEvent, EmergencyContactsState> {
|
||||
EmergencyContactsBloc() : super(const EmergencyContactsState()) {
|
||||
on<InitializeEmergencyContactsEvent>(_handleInitializeEvent);
|
||||
|
||||
on<AddNewContactEvent>(_handleAddNewContactEvent);
|
||||
|
||||
on<UpdateContactEvent>(_handleUpdateContactEvent);
|
||||
|
||||
on<DeleteContactEvent>(_handleDeleteContactEvent);
|
||||
|
||||
on<SaveContactsChanges>(_handleSaveContactsChanges);
|
||||
}
|
||||
|
||||
Future<void> _handleInitializeEvent(
|
||||
InitializeEmergencyContactsEvent event,
|
||||
Emitter<EmergencyContactsState> emit,
|
||||
) async {
|
||||
emit(
|
||||
state.copyWith(
|
||||
isInEditMode: event.isInEditMode,
|
||||
contacts: event.isInEditMode
|
||||
? state.contacts
|
||||
: [const EmergencyContactModel.empty()],
|
||||
status: event.isInEditMode ? StateStatus.loading : StateStatus.idle,
|
||||
),
|
||||
);
|
||||
if (!state.isInEditMode) return;
|
||||
|
||||
try {
|
||||
await for (final emergencyContacts
|
||||
in getIt<EmergencyContactRepository>().getStaffEmergencyContacts()) {
|
||||
if (emergencyContacts.isEmpty) continue;
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
contacts: emergencyContacts,
|
||||
status: StateStatus.idle,
|
||||
isListReducible: emergencyContacts.length > 1,
|
||||
),
|
||||
);
|
||||
}
|
||||
} catch (except) {
|
||||
log(except.toString());
|
||||
}
|
||||
|
||||
if (state.status == StateStatus.loading) {
|
||||
emit(state.copyWith(status: StateStatus.idle));
|
||||
}
|
||||
}
|
||||
|
||||
void _handleAddNewContactEvent(
|
||||
AddNewContactEvent event,
|
||||
Emitter<EmergencyContactsState> emit,
|
||||
) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
contacts: [
|
||||
...state.contacts,
|
||||
const EmergencyContactModel.empty(),
|
||||
],
|
||||
isListReducible: true,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _handleUpdateContactEvent(
|
||||
UpdateContactEvent event,
|
||||
Emitter<EmergencyContactsState> emit,
|
||||
) {
|
||||
final updatedContact = state.contacts[event.index].copyWith(
|
||||
firstName: event.firstName,
|
||||
lastName: event.lastName,
|
||||
phoneNumber: event.phoneNumber,
|
||||
);
|
||||
|
||||
emit(
|
||||
state.copyWith(
|
||||
contacts: List.from(state.contacts)..[event.index] = updatedContact,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
void _handleDeleteContactEvent(
|
||||
DeleteContactEvent event,
|
||||
Emitter<EmergencyContactsState> emit,
|
||||
) {
|
||||
emit(
|
||||
state.copyWith(
|
||||
contacts: List.from(state.contacts)..removeAt(event.index),
|
||||
isListReducible: state.contacts.length > 2,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _handleSaveContactsChanges(
|
||||
SaveContactsChanges event,
|
||||
Emitter<EmergencyContactsState> emit,
|
||||
) async {
|
||||
emit(state.copyWith(status: StateStatus.loading));
|
||||
|
||||
try {
|
||||
await getIt<EmergencyContactRepository>().updateEmergencyContacts(
|
||||
state.contacts,
|
||||
);
|
||||
} catch (except) {
|
||||
log(except.toString());
|
||||
}
|
||||
|
||||
emit(state.copyWith(status: StateStatus.success));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
part of 'emergency_contacts_bloc.dart';
|
||||
|
||||
@immutable
|
||||
sealed class EmergencyContactsEvent {}
|
||||
|
||||
class InitializeEmergencyContactsEvent extends EmergencyContactsEvent {
|
||||
InitializeEmergencyContactsEvent(this.isInEditMode);
|
||||
|
||||
final bool isInEditMode;
|
||||
}
|
||||
|
||||
class AddNewContactEvent extends EmergencyContactsEvent {}
|
||||
|
||||
class UpdateContactEvent extends EmergencyContactsEvent {
|
||||
UpdateContactEvent({
|
||||
required this.index,
|
||||
required this.firstName,
|
||||
required this.lastName,
|
||||
required this.phoneNumber,
|
||||
});
|
||||
|
||||
final int index;
|
||||
final String firstName;
|
||||
final String lastName;
|
||||
final String phoneNumber;
|
||||
}
|
||||
|
||||
class DeleteContactEvent extends EmergencyContactsEvent {
|
||||
DeleteContactEvent({required this.index});
|
||||
|
||||
final int index;
|
||||
}
|
||||
|
||||
class SaveContactsChanges extends EmergencyContactsEvent {}
|
||||
@@ -0,0 +1,32 @@
|
||||
part of 'emergency_contacts_bloc.dart';
|
||||
|
||||
@immutable
|
||||
final class EmergencyContactsState {
|
||||
const EmergencyContactsState({
|
||||
this.contacts = const [],
|
||||
this.status = StateStatus.idle,
|
||||
this.isInEditMode = true,
|
||||
this.isListReducible = false,
|
||||
});
|
||||
|
||||
final List<EmergencyContactModel> contacts;
|
||||
final StateStatus status;
|
||||
final bool isInEditMode;
|
||||
final bool isListReducible;
|
||||
|
||||
bool get isFilled => contacts.indexWhere((contact) => !contact.isFilled) < 0;
|
||||
|
||||
EmergencyContactsState copyWith({
|
||||
List<EmergencyContactModel>? contacts,
|
||||
StateStatus? status,
|
||||
bool? isInEditMode,
|
||||
bool? isListReducible,
|
||||
}) {
|
||||
return EmergencyContactsState(
|
||||
contacts: contacts ?? this.contacts,
|
||||
status: status ?? this.status,
|
||||
isInEditMode: isInEditMode ?? this.isInEditMode,
|
||||
isListReducible: isListReducible ?? this.isListReducible,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
import 'package:injectable/injectable.dart';
|
||||
import 'package:krow/features/profile/emergency_contacts/data/emergency_contact_api_provider.dart';
|
||||
import 'package:krow/features/profile/emergency_contacts/data/emergency_contact_repository.dart';
|
||||
import 'package:krow/features/profile/emergency_contacts/data/models/emergency_contact_model.dart';
|
||||
|
||||
@LazySingleton(as: EmergencyContactRepository)
|
||||
class EmergencyContactRepositoryImpl implements EmergencyContactRepository {
|
||||
EmergencyContactRepositoryImpl({
|
||||
required EmergencyContactApiProvider apiProvider,
|
||||
}) : _apiProvider = apiProvider;
|
||||
|
||||
final EmergencyContactApiProvider _apiProvider;
|
||||
|
||||
@override
|
||||
Stream<List<EmergencyContactModel>> getStaffEmergencyContacts() {
|
||||
return _apiProvider.getStaffEmergencyContacts();
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<EmergencyContactModel>?> updateEmergencyContacts(
|
||||
List<EmergencyContactModel> contacts,
|
||||
) {
|
||||
return _apiProvider.updateEmergencyContacts(contacts);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user