refactor: improve code readability and formatting in shifts repository and shift details page

This commit is contained in:
Achintha Isuru
2026-02-01 12:25:45 -05:00
parent 989a1d3f84
commit 5d561ff825
3 changed files with 137 additions and 127 deletions

View File

@@ -37,7 +37,9 @@ class ShiftsRepositoryImpl implements ShiftsRepositoryInterface {
} }
try { try {
final response = await _dataConnect.getStaffByUserId(userId: user.uid).execute(); final response = await _dataConnect
.getStaffByUserId(userId: user.uid)
.execute();
if (response.data.staffs.isNotEmpty) { if (response.data.staffs.isNotEmpty) {
_cachedStaffId = response.data.staffs.first.id; _cachedStaffId = response.data.staffs.first.id;
return _cachedStaffId!; return _cachedStaffId!;
@@ -102,7 +104,9 @@ class ShiftsRepositoryImpl implements ShiftsRepositoryInterface {
.getApplicationsByStaffId(staffId: staffId) .getApplicationsByStaffId(staffId: staffId)
.execute(); .execute();
final apps = response.data.applications.where((app) => app.status.stringValue == status.name); final apps = response.data.applications.where(
(app) => app.status.stringValue == status.name,
);
final List<Shift> shifts = []; final List<Shift> shifts = [];
for (final app in apps) { for (final app in apps) {
@@ -112,7 +116,8 @@ class ShiftsRepositoryImpl implements ShiftsRepositoryInterface {
final shift = await _getShiftDetails(app.shift.id); final shift = await _getShiftDetails(app.shift.id);
if (shift != null) { if (shift != null) {
// Override status to reflect the application state (e.g., CHECKED_OUT, ACCEPTED) // Override status to reflect the application state (e.g., CHECKED_OUT, ACCEPTED)
shifts.add(Shift( shifts.add(
Shift(
id: shift.id, id: shift.id,
title: shift.title, title: shift.title,
clientName: shift.clientName, clientName: shift.clientName,
@@ -129,7 +134,8 @@ class ShiftsRepositoryImpl implements ShiftsRepositoryInterface {
durationDays: shift.durationDays, durationDays: shift.durationDays,
requiredSlots: shift.requiredSlots, requiredSlots: shift.requiredSlots,
filledSlots: shift.filledSlots, filledSlots: shift.filledSlots,
)); ),
);
} }
} }
return shifts; return shifts;
@@ -157,8 +163,7 @@ class ShiftsRepositoryImpl implements ShiftsRepositoryInterface {
@override @override
Future<List<Shift>> getAvailableShifts(String query, String type) async { Future<List<Shift>> getAvailableShifts(String query, String type) async {
try { try {
final String? vendorId = final String? vendorId = dc.StaffSessionStore.instance.session?.ownerId;
dc.StaffSessionStore.instance.session?.ownerId;
if (vendorId == null || vendorId.isEmpty) { if (vendorId == null || vendorId.isEmpty) {
return <Shift>[]; return <Shift>[];
} }
@@ -184,8 +189,9 @@ class ShiftsRepositoryImpl implements ShiftsRepositoryInterface {
location: sr.shift.location ?? '', location: sr.shift.location ?? '',
locationAddress: sr.shift.locationAddress ?? '', locationAddress: sr.shift.locationAddress ?? '',
date: startDt?.toIso8601String() ?? '', date: startDt?.toIso8601String() ?? '',
startTime: startTime: startDt != null
startDt != null ? DateFormat('HH:mm').format(startDt) : '', ? DateFormat('HH:mm').format(startDt)
: '',
endTime: endDt != null ? DateFormat('HH:mm').format(endDt) : '', endTime: endDt != null ? DateFormat('HH:mm').format(endDt) : '',
createdDate: createdDt?.toIso8601String() ?? '', createdDate: createdDt?.toIso8601String() ?? '',
status: sr.shift.status?.stringValue.toLowerCase() ?? 'open', status: sr.shift.status?.stringValue.toLowerCase() ?? 'open',
@@ -235,12 +241,12 @@ class ShiftsRepositoryImpl implements ShiftsRepositoryInterface {
bool hasApplied = false; bool hasApplied = false;
String status = 'open'; String status = 'open';
if (staffId != null) { if (staffId != null) {
final apps = final apps = await _dataConnect
await _dataConnect.getApplicationsByStaffId(staffId: staffId).execute(); .getApplicationsByStaffId(staffId: staffId)
.execute();
final app = apps.data.applications final app = apps.data.applications
.where( .where(
(a) => (a) => a.shiftId == shiftId && a.shiftRole.roleId == roleId,
a.shiftId == shiftId && a.shiftRole.roleId == roleId,
) )
.firstOrNull; .firstOrNull;
if (app != null) { if (app != null) {
@@ -260,8 +266,8 @@ class ShiftsRepositoryImpl implements ShiftsRepositoryInterface {
clientName: sr.shift.order.business.businessName, clientName: sr.shift.order.business.businessName,
logoUrl: sr.shift.order.business.companyLogoUrl, logoUrl: sr.shift.order.business.companyLogoUrl,
hourlyRate: sr.role.costPerHour, hourlyRate: sr.role.costPerHour,
location: sr.shift.order.teamHub.hubName, location: sr.shift.location ?? sr.shift.order.teamHub.hubName,
locationAddress: '', locationAddress: sr.shift.locationAddress ?? '',
date: startDt?.toIso8601String() ?? '', date: startDt?.toIso8601String() ?? '',
startTime: startDt != null ? DateFormat('HH:mm').format(startDt) : '', startTime: startDt != null ? DateFormat('HH:mm').format(startDt) : '',
endTime: endDt != null ? DateFormat('HH:mm').format(endDt) : '', endTime: endDt != null ? DateFormat('HH:mm').format(endDt) : '',
@@ -283,7 +289,9 @@ class ShiftsRepositoryImpl implements ShiftsRepositoryInterface {
int? required; int? required;
int? filled; int? filled;
try { try {
final rolesRes = await _dataConnect.listShiftRolesByShiftId(shiftId: shiftId).execute(); final rolesRes = await _dataConnect
.listShiftRolesByShiftId(shiftId: shiftId)
.execute();
if (rolesRes.data.shiftRoles.isNotEmpty) { if (rolesRes.data.shiftRoles.isNotEmpty) {
required = 0; required = 0;
filled = 0; filled = 0;
@@ -331,8 +339,9 @@ class ShiftsRepositoryImpl implements ShiftsRepositoryInterface {
String targetRoleId = roleId ?? ''; String targetRoleId = roleId ?? '';
if (targetRoleId.isEmpty) { if (targetRoleId.isEmpty) {
final rolesResult = final rolesResult = await _dataConnect
await _dataConnect.listShiftRolesByShiftId(shiftId: shiftId).execute(); .listShiftRolesByShiftId(shiftId: shiftId)
.execute();
if (rolesResult.data.shiftRoles.isEmpty) { if (rolesResult.data.shiftRoles.isEmpty) {
throw Exception('No open roles for this shift'); throw Exception('No open roles for this shift');
} }
@@ -384,10 +393,7 @@ class ShiftsRepositoryImpl implements ShiftsRepositoryInterface {
.execute(); .execute();
updatedRole = true; updatedRole = true;
await _dataConnect await _dataConnect.updateShift(id: shiftId).filled(filled + 1).execute();
.updateShift(id: shiftId)
.filled(filled + 1)
.execute();
updatedShift = true; updatedShift = true;
} catch (e) { } catch (e) {
if (updatedShift) { if (updatedShift) {
@@ -416,7 +422,10 @@ class ShiftsRepositoryImpl implements ShiftsRepositoryInterface {
await _updateApplicationStatus(shiftId, dc.ApplicationStatus.REJECTED); await _updateApplicationStatus(shiftId, dc.ApplicationStatus.REJECTED);
} }
Future<void> _updateApplicationStatus(String shiftId, dc.ApplicationStatus newStatus) async { Future<void> _updateApplicationStatus(
String shiftId,
dc.ApplicationStatus newStatus,
) async {
String? appId = _shiftToAppIdMap[shiftId]; String? appId = _shiftToAppIdMap[shiftId];
String? roleId; String? roleId;
@@ -431,8 +440,12 @@ class ShiftsRepositoryImpl implements ShiftsRepositoryInterface {
} else { } else {
// Fallback fetch // Fallback fetch
final staffId = await _getStaffId(); final staffId = await _getStaffId();
final apps = await _dataConnect.getApplicationsByStaffId(staffId: staffId).execute(); final apps = await _dataConnect
final app = apps.data.applications.where((a) => a.shiftId == shiftId).firstOrNull; .getApplicationsByStaffId(staffId: staffId)
.execute();
final app = apps.data.applications
.where((a) => a.shiftId == shiftId)
.firstOrNull;
if (app != null) { if (app != null) {
appId = app.id; appId = app.id;
roleId = app.shiftRole.id; roleId = app.shiftRole.id;
@@ -442,27 +455,29 @@ class ShiftsRepositoryImpl implements ShiftsRepositoryInterface {
if (appId == null || roleId == null) { if (appId == null || roleId == null) {
// If we are rejecting and can't find an application, create one as rejected (declining an available shift) // If we are rejecting and can't find an application, create one as rejected (declining an available shift)
if (newStatus == dc.ApplicationStatus.REJECTED) { if (newStatus == dc.ApplicationStatus.REJECTED) {
final rolesResult = await _dataConnect.listShiftRolesByShiftId(shiftId: shiftId).execute(); final rolesResult = await _dataConnect
.listShiftRolesByShiftId(shiftId: shiftId)
.execute();
if (rolesResult.data.shiftRoles.isNotEmpty) { if (rolesResult.data.shiftRoles.isNotEmpty) {
final role = rolesResult.data.shiftRoles.first; final role = rolesResult.data.shiftRoles.first;
final staffId = await _getStaffId(); final staffId = await _getStaffId();
await _dataConnect.createApplication( await _dataConnect
.createApplication(
shiftId: shiftId, shiftId: shiftId,
staffId: staffId, staffId: staffId,
roleId: role.id, roleId: role.id,
status: dc.ApplicationStatus.REJECTED, status: dc.ApplicationStatus.REJECTED,
origin: dc.ApplicationOrigin.STAFF, origin: dc.ApplicationOrigin.STAFF,
).execute(); )
.execute();
return; return;
} }
} }
throw Exception("Application not found for shift $shiftId"); throw Exception("Application not found for shift $shiftId");
} }
await _dataConnect.updateApplicationStatus( await _dataConnect
id: appId, .updateApplicationStatus(id: appId, roleId: roleId)
roleId: roleId,
)
.status(newStatus) .status(newStatus)
.execute(); .execute();
} }

View File

@@ -123,12 +123,8 @@ class ShiftDetailsPage extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
return BlocProvider<ShiftDetailsBloc>( return BlocProvider<ShiftDetailsBloc>(
create: (_) => create: (_) =>
Modular.get<ShiftDetailsBloc>()..add( Modular.get<ShiftDetailsBloc>()
LoadShiftDetailsEvent( ..add(LoadShiftDetailsEvent(shiftId, roleId: shift?.roleId)),
shiftId,
roleId: shift?.roleId,
),
),
child: BlocListener<ShiftDetailsBloc, ShiftDetailsState>( child: BlocListener<ShiftDetailsBloc, ShiftDetailsState>(
listener: (context, state) { listener: (context, state) {
if (state is ShiftActionSuccess) { if (state is ShiftActionSuccess) {
@@ -140,6 +136,7 @@ class ShiftDetailsPage extends StatelessWidget {
); );
Modular.to.navigateToShiftsHome(); Modular.to.navigateToShiftsHome();
} else if (state is ShiftDetailsError) { } else if (state is ShiftDetailsError) {
if (shift == null) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar( SnackBar(
content: Text(state.message), content: Text(state.message),
@@ -147,6 +144,7 @@ class ShiftDetailsPage extends StatelessWidget {
), ),
); );
} }
}
}, },
child: BlocBuilder<ShiftDetailsBloc, ShiftDetailsState>( child: BlocBuilder<ShiftDetailsBloc, ShiftDetailsState>(
builder: (context, state) { builder: (context, state) {

View File

@@ -135,10 +135,7 @@ class _MyShiftsTabState extends State<MyShiftsTab> {
final visibleMyShifts = widget.myShifts.where((s) { final visibleMyShifts = widget.myShifts.where((s) {
try { try {
final date = DateTime.parse(s.date); final date = DateTime.parse(s.date);
return date.isAfter( return _isSameDay(date, _selectedDate);
weekStartDate.subtract(const Duration(seconds: 1)),
) &&
date.isBefore(weekEndDate.add(const Duration(days: 1)));
} catch (_) { } catch (_) {
return false; return false;
} }