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].
static RelationshipType stringToRelationshipType(String? value) {
if (value != null) {
final strVal = value.toUpperCase();
final String strVal = value.toUpperCase();
switch (strVal) {
case '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,7 +91,10 @@ class EmergencyContactBloc
on<EmergencyContactRemoved>(_onRemoved);
on<EmergencyContactUpdated>(_onUpdated);
on<EmergencyContactsSaved>(_onSaved);
add(EmergencyContactsLoaded());
}
Future<void> _onLoaded(
EmergencyContactsLoaded event,

View File

@@ -17,19 +17,6 @@ import '../widgets/emergency_contact_save_button.dart';
class EmergencyContactScreen extends StatelessWidget {
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
Widget build(BuildContext context) {
return Scaffold(
@@ -48,44 +35,47 @@ class _EmergencyContactView extends StatelessWidget {
child: Container(color: UiColors.border, height: 1.0),
),
),
body: BlocConsumer<EmergencyContactBloc, EmergencyContactState>(
listener: (context, state) {
if (state.status == EmergencyContactStatus.failure) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(state.errorMessage ?? 'An error occurred')),
);
}
},
builder: (context, state) {
if (state.status == EmergencyContactStatus.loading) {
return const Center(child: CircularProgressIndicator());
}
return Column(
children: [
Expanded(
child: SingleChildScrollView(
padding: EdgeInsets.all(UiConstants.space6),
child: Column(
children: [
const EmergencyContactInfoBanner(),
SizedBox(height: UiConstants.space6),
...state.contacts.asMap().entries.map(
(entry) => EmergencyContactFormItem(
index: entry.key,
contact: entry.value,
totalContacts: state.contacts.length,
body: BlocProvider(
create: (context) => Modular.get<EmergencyContactBloc>(),
child: BlocConsumer<EmergencyContactBloc, EmergencyContactState>(
listener: (context, state) {
if (state.status == EmergencyContactStatus.failure) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(state.errorMessage ?? 'An error occurred')),
);
}
},
builder: (context, state) {
if (state.status == EmergencyContactStatus.loading) {
return const Center(child: CircularProgressIndicator());
}
return Column(
children: [
Expanded(
child: SingleChildScrollView(
padding: EdgeInsets.all(UiConstants.space6),
child: Column(
children: [
const EmergencyContactInfoBanner(),
SizedBox(height: UiConstants.space6),
...state.contacts.asMap().entries.map(
(entry) => EmergencyContactFormItem(
index: entry.key,
contact: entry.value,
totalContacts: state.contacts.length,
),
),
),
const EmergencyContactAddButton(),
SizedBox(height: UiConstants.space16),
],
const EmergencyContactAddButton(),
SizedBox(height: UiConstants.space16),
],
),
),
),
),
EmergencyContactSaveButton(state: state),
],
);
},
EmergencyContactSaveButton(state: state),
],
);
},
),
),
);
}

View File

@@ -17,39 +17,26 @@ class EmergencyContactSaveButton extends StatelessWidget {
border: Border(top: BorderSide(color: UiColors.border)),
),
child: SafeArea(
child: SizedBox(
width: double.infinity,
child: ElevatedButton(
onPressed: state.isValid
? () => context
.read<EmergencyContactBloc>()
.add(EmergencyContactsSaved())
: 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
? SizedBox(
height: 20.0,
width: 20.0,
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor:
AlwaysStoppedAnimation<Color>(UiColors.primaryForeground),
),
)
: Text(
'Save & Continue',
style: UiTypography.title2b,
child: UiButton.primary(
fullWidth: true,
onPressed: state.isValid
? () => context
.read<EmergencyContactBloc>()
.add(EmergencyContactsSaved())
: null,
child: state.status == EmergencyContactStatus.saving
? SizedBox(
height: 20.0,
width: 20.0,
child: CircularProgressIndicator(
strokeWidth: 2,
valueColor:
AlwaysStoppedAnimation<Color>(UiColors.primaryForeground),
),
),
)
: Text(
'Save & Continue',
),
),
),
);

View File

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