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,7 +91,10 @@ 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,

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,44 +35,47 @@ 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(
listener: (context, state) { create: (context) => Modular.get<EmergencyContactBloc>(),
if (state.status == EmergencyContactStatus.failure) { child: BlocConsumer<EmergencyContactBloc, EmergencyContactState>(
ScaffoldMessenger.of(context).showSnackBar( listener: (context, state) {
SnackBar(content: Text(state.errorMessage ?? 'An error occurred')), 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()); builder: (context, state) {
} if (state.status == EmergencyContactStatus.loading) {
return Column( return const Center(child: CircularProgressIndicator());
children: [ }
Expanded( return Column(
child: SingleChildScrollView( children: [
padding: EdgeInsets.all(UiConstants.space6), Expanded(
child: Column( child: SingleChildScrollView(
children: [ padding: EdgeInsets.all(UiConstants.space6),
const EmergencyContactInfoBanner(), child: Column(
SizedBox(height: UiConstants.space6), children: [
...state.contacts.asMap().entries.map( const EmergencyContactInfoBanner(),
(entry) => EmergencyContactFormItem( SizedBox(height: UiConstants.space6),
index: entry.key, ...state.contacts.asMap().entries.map(
contact: entry.value, (entry) => EmergencyContactFormItem(
totalContacts: state.contacts.length, index: entry.key,
contact: entry.value,
totalContacts: state.contacts.length,
),
), ),
), const EmergencyContactAddButton(),
const EmergencyContactAddButton(), SizedBox(height: UiConstants.space16),
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)), 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, child: state.status == EmergencyContactStatus.saving
style: ElevatedButton.styleFrom( ? SizedBox(
backgroundColor: UiColors.primary, height: 20.0,
foregroundColor: UiColors.primaryForeground, width: 20.0,
disabledBackgroundColor: UiColors.textPlaceholder, child: CircularProgressIndicator(
padding: EdgeInsets.symmetric(vertical: UiConstants.space4), strokeWidth: 2,
shape: RoundedRectangleBorder( valueColor:
borderRadius: UiConstants.radiusFull, AlwaysStoppedAnimation<Color>(UiColors.primaryForeground),
),
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,
), ),
), )
: Text(
'Save & Continue',
),
), ),
), ),
); );

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>(),