refactor: streamline emergency contact management by removing unused extensions and optimizing BLoC initialization

This commit is contained in:
Achintha Isuru
2026-01-27 12:38:21 -05:00
parent 450683c45c
commit 70f350d216
6 changed files with 62 additions and 112 deletions

View File

@@ -56,7 +56,7 @@ class EmergencyContact extends Equatable {
/// Converts a string value to a [RelationshipType]. /// Converts a string value to a [RelationshipType].
static RelationshipType stringToRelationshipType(String? value) { static RelationshipType stringToRelationshipType(String? value) {
if (value != null) { if (value != null) {
final strVal = value.toUpperCase(); final String strVal = value.toUpperCase();
switch (strVal) { switch (strVal) {
case 'FAMILY': case 'FAMILY':
return RelationshipType.family; return RelationshipType.family;

View File

@@ -1,30 +0,0 @@
import 'package:krow_domain/krow_domain.dart';
/// Extensions for [EmergencyContact] to support UI operations.
extension EmergencyContactExtensions on EmergencyContact {
/// returns a copy of this [EmergencyContact] with the given fields replaced.
EmergencyContact copyWith({
String? id,
String? name,
String? phone,
RelationshipType? relationship,
}) {
return EmergencyContact(
id: id ?? this.id,
name: name ?? this.name,
phone: phone ?? this.phone,
relationship: relationship ?? this.relationship,
);
}
/// Returns an empty [EmergencyContact].
static EmergencyContact empty() {
return const EmergencyContact(
id: '',
name: '',
phone: '',
relationship: RelationshipType.family,
);
}
}

View File

@@ -91,8 +91,11 @@ class EmergencyContactBloc
on<EmergencyContactRemoved>(_onRemoved); on<EmergencyContactRemoved>(_onRemoved);
on<EmergencyContactUpdated>(_onUpdated); on<EmergencyContactUpdated>(_onUpdated);
on<EmergencyContactsSaved>(_onSaved); on<EmergencyContactsSaved>(_onSaved);
add(EmergencyContactsLoaded());
} }
Future<void> _onLoaded( Future<void> _onLoaded(
EmergencyContactsLoaded event, EmergencyContactsLoaded event,
Emitter<EmergencyContactState> emit, Emitter<EmergencyContactState> emit,

View File

@@ -17,19 +17,6 @@ import '../widgets/emergency_contact_save_button.dart';
class EmergencyContactScreen extends StatelessWidget { class EmergencyContactScreen extends StatelessWidget {
const EmergencyContactScreen({super.key}); const EmergencyContactScreen({super.key});
@override
Widget build(BuildContext context) {
return BlocProvider(
create: (_) =>
Modular.get<EmergencyContactBloc>()..add(EmergencyContactsLoaded()),
child: const _EmergencyContactView(),
);
}
}
class _EmergencyContactView extends StatelessWidget {
const _EmergencyContactView();
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
@@ -48,7 +35,9 @@ class _EmergencyContactView extends StatelessWidget {
child: Container(color: UiColors.border, height: 1.0), child: Container(color: UiColors.border, height: 1.0),
), ),
), ),
body: BlocConsumer<EmergencyContactBloc, EmergencyContactState>( body: BlocProvider(
create: (context) => Modular.get<EmergencyContactBloc>(),
child: BlocConsumer<EmergencyContactBloc, EmergencyContactState>(
listener: (context, state) { listener: (context, state) {
if (state.status == EmergencyContactStatus.failure) { if (state.status == EmergencyContactStatus.failure) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
@@ -87,6 +76,7 @@ class _EmergencyContactView extends StatelessWidget {
); );
}, },
), ),
),
); );
} }
} }

View File

@@ -17,24 +17,13 @@ class EmergencyContactSaveButton extends StatelessWidget {
border: Border(top: BorderSide(color: UiColors.border)), border: Border(top: BorderSide(color: UiColors.border)),
), ),
child: SafeArea( child: SafeArea(
child: SizedBox( child: UiButton.primary(
width: double.infinity, fullWidth: true,
child: ElevatedButton(
onPressed: state.isValid onPressed: state.isValid
? () => context ? () => context
.read<EmergencyContactBloc>() .read<EmergencyContactBloc>()
.add(EmergencyContactsSaved()) .add(EmergencyContactsSaved())
: null, : null,
style: ElevatedButton.styleFrom(
backgroundColor: UiColors.primary,
foregroundColor: UiColors.primaryForeground,
disabledBackgroundColor: UiColors.textPlaceholder,
padding: EdgeInsets.symmetric(vertical: UiConstants.space4),
shape: RoundedRectangleBorder(
borderRadius: UiConstants.radiusFull,
),
elevation: 0,
),
child: state.status == EmergencyContactStatus.saving child: state.status == EmergencyContactStatus.saving
? SizedBox( ? SizedBox(
height: 20.0, height: 20.0,
@@ -47,8 +36,6 @@ class EmergencyContactSaveButton extends StatelessWidget {
) )
: Text( : Text(
'Save & Continue', 'Save & Continue',
style: UiTypography.title2b,
),
), ),
), ),
), ),

View File

@@ -28,7 +28,7 @@ class StaffEmergencyContactModule extends Module {
); );
// BLoC // BLoC
i.addLazySingleton<EmergencyContactBloc>( i.add<EmergencyContactBloc>(
() => EmergencyContactBloc( () => EmergencyContactBloc(
getEmergencyContacts: i.get<GetEmergencyContactsUseCase>(), getEmergencyContacts: i.get<GetEmergencyContactsUseCase>(),
saveEmergencyContacts: i.get<SaveEmergencyContactsUseCase>(), saveEmergencyContacts: i.get<SaveEmergencyContactsUseCase>(),