feat: Simplify HomeRepositoryImpl and ClientHomeModule by using constructor shorthand

This commit is contained in:
Achintha Isuru
2026-02-16 17:43:20 -05:00
parent 789fe24f2b
commit a7e8704e4f
2 changed files with 49 additions and 107 deletions

View File

@@ -23,11 +23,7 @@ class ClientHomeModule extends Module {
@override @override
void binds(Injector i) { void binds(Injector i) {
// Repositories // Repositories
i.addLazySingleton<HomeRepositoryInterface>( i.addLazySingleton<HomeRepositoryInterface>(HomeRepositoryImpl.new);
() => HomeRepositoryImpl(
ExampleConnector.instance,
),
);
// UseCases // UseCases
i.addLazySingleton(GetDashboardDataUseCase.new); i.addLazySingleton(GetDashboardDataUseCase.new);
@@ -35,13 +31,7 @@ class ClientHomeModule extends Module {
i.addLazySingleton(GetUserSessionDataUseCase.new); i.addLazySingleton(GetUserSessionDataUseCase.new);
// BLoCs // BLoCs
i.add<ClientHomeBloc>( i.add<ClientHomeBloc>(ClientHomeBloc.new);
() => ClientHomeBloc(
getDashboardDataUseCase: i.get<GetDashboardDataUseCase>(),
getRecentReordersUseCase: i.get<GetRecentReordersUseCase>(),
getUserSessionDataUseCase: i.get<GetUserSessionDataUseCase>(),
),
);
} }
@override @override

View File

@@ -8,25 +8,14 @@ import '../../domain/repositories/home_repository_interface.dart';
/// This implementation resides in the data layer and acts as a bridge between the /// This implementation resides in the data layer and acts as a bridge between the
/// domain layer and the data source (in this case, a mock from data_connect). /// domain layer and the data source (in this case, a mock from data_connect).
class HomeRepositoryImpl implements HomeRepositoryInterface { class HomeRepositoryImpl implements HomeRepositoryInterface {
/// Creates a [HomeRepositoryImpl]. /// Creates a [HomeRepositoryImpl].
HomeRepositoryImpl(this._dataConnect); HomeRepositoryImpl(this._service);
final dc.ExampleConnector _dataConnect; final dc.DataConnectService _service;
@override @override
Future<HomeDashboardData> getDashboardData() async { Future<HomeDashboardData> getDashboardData() async {
try { return _service.run(() async {
final String? businessId = dc.ClientSessionStore.instance.session?.business?.id; final String businessId = await _service.getBusinessId();
if (businessId == null || businessId.isEmpty) {
return const HomeDashboardData(
weeklySpending: 0,
next7DaysSpending: 0,
weeklyShifts: 0,
next7DaysScheduled: 0,
totalNeeded: 0,
totalFilled: 0,
);
}
final DateTime now = DateTime.now(); final DateTime now = DateTime.now();
final int daysFromMonday = now.weekday - DateTime.monday; final int daysFromMonday = now.weekday - DateTime.monday;
@@ -35,14 +24,13 @@ class HomeRepositoryImpl implements HomeRepositoryInterface {
final DateTime weekRangeStart = DateTime(monday.year, monday.month, monday.day); final DateTime weekRangeStart = DateTime(monday.year, monday.month, monday.day);
final DateTime weekRangeEnd = final DateTime weekRangeEnd =
DateTime(monday.year, monday.month, monday.day + 13, 23, 59, 59, 999); DateTime(monday.year, monday.month, monday.day + 13, 23, 59, 59, 999);
final fdc.QueryResult< final fdc.QueryResult<dc.GetCompletedShiftsByBusinessIdData,
dc.GetCompletedShiftsByBusinessIdData, dc.GetCompletedShiftsByBusinessIdVariables> completedResult =
dc.GetCompletedShiftsByBusinessIdVariables> completedResult = await _service.connector
await _dataConnect
.getCompletedShiftsByBusinessId( .getCompletedShiftsByBusinessId(
businessId: businessId, businessId: businessId,
dateFrom: _toTimestamp(weekRangeStart), dateFrom: _service.toTimestamp(weekRangeStart),
dateTo: _toTimestamp(weekRangeEnd), dateTo: _service.toTimestamp(weekRangeEnd),
) )
.execute(); .execute();
@@ -50,8 +38,7 @@ class HomeRepositoryImpl implements HomeRepositoryInterface {
double next7DaysSpending = 0.0; double next7DaysSpending = 0.0;
int weeklyShifts = 0; int weeklyShifts = 0;
int next7DaysScheduled = 0; int next7DaysScheduled = 0;
for (final dc.GetCompletedShiftsByBusinessIdShifts shift for (final dc.GetCompletedShiftsByBusinessIdShifts shift in completedResult.data.shifts) {
in completedResult.data.shifts) {
final DateTime? shiftDate = shift.date?.toDateTime(); final DateTime? shiftDate = shift.date?.toDateTime();
if (shiftDate == null) { if (shiftDate == null) {
continue; continue;
@@ -73,14 +60,13 @@ class HomeRepositoryImpl implements HomeRepositoryInterface {
final DateTime start = DateTime(now.year, now.month, now.day); final DateTime start = DateTime(now.year, now.month, now.day);
final DateTime end = DateTime(now.year, now.month, now.day, 23, 59, 59, 999); final DateTime end = DateTime(now.year, now.month, now.day, 23, 59, 59, 999);
final fdc.QueryResult< final fdc.QueryResult<dc.ListShiftRolesByBusinessAndDateRangeData,
dc.ListShiftRolesByBusinessAndDateRangeData, dc.ListShiftRolesByBusinessAndDateRangeVariables> result =
dc.ListShiftRolesByBusinessAndDateRangeVariables> result = await _service.connector
await _dataConnect
.listShiftRolesByBusinessAndDateRange( .listShiftRolesByBusinessAndDateRange(
businessId: businessId, businessId: businessId,
start: _toTimestamp(start), start: _service.toTimestamp(start),
end: _toTimestamp(end), end: _service.toTimestamp(end),
) )
.execute(); .execute();
@@ -100,18 +86,7 @@ class HomeRepositoryImpl implements HomeRepositoryInterface {
totalNeeded: totalNeeded, totalNeeded: totalNeeded,
totalFilled: totalFilled, totalFilled: totalFilled,
); );
} catch (e) { });
final String error = e.toString().toLowerCase();
if (error.contains('network') ||
error.contains('connection') ||
error.contains('unavailable') ||
error.contains('offline') ||
error.contains('socket') ||
error.contains('failed host lookup')) {
throw NetworkException(technicalMessage: 'Home dashboard fetch failed: $e');
}
throw ServerException(technicalMessage: 'Home dashboard fetch failed: $e');
}
} }
@override @override
@@ -125,64 +100,41 @@ class HomeRepositoryImpl implements HomeRepositoryInterface {
@override @override
Future<List<ReorderItem>> getRecentReorders() async { Future<List<ReorderItem>> getRecentReorders() async {
try { return _service.run(() async {
final String? businessId = dc.ClientSessionStore.instance.session?.business?.id; final String businessId = await _service.getBusinessId();
if (businessId == null || businessId.isEmpty) {
return const <ReorderItem>[];
}
final DateTime now = DateTime.now(); final DateTime now = DateTime.now();
final DateTime start = now.subtract(const Duration(days: 30)); final DateTime start = now.subtract(const Duration(days: 30));
final fdc.Timestamp startTimestamp = _toTimestamp(start); final fdc.Timestamp startTimestamp = _service.toTimestamp(start);
final fdc.Timestamp endTimestamp = _toTimestamp(now); final fdc.Timestamp endTimestamp = _service.toTimestamp(now);
final fdc.QueryResult< final fdc.QueryResult<dc.ListShiftRolesByBusinessDateRangeCompletedOrdersData,
dc.ListShiftRolesByBusinessDateRangeCompletedOrdersData, dc.ListShiftRolesByBusinessDateRangeCompletedOrdersVariables> result =
dc.ListShiftRolesByBusinessDateRangeCompletedOrdersVariables> result = await _service.connector
await _dataConnect.listShiftRolesByBusinessDateRangeCompletedOrders( .listShiftRolesByBusinessDateRangeCompletedOrders(
businessId: businessId, businessId: businessId,
start: startTimestamp, start: startTimestamp,
end: endTimestamp, end: endTimestamp,
).execute(); )
.execute();
return result.data.shiftRoles.map(( return result.data.shiftRoles
dc.ListShiftRolesByBusinessDateRangeCompletedOrdersShiftRoles shiftRole, .map((
) { dc.ListShiftRolesByBusinessDateRangeCompletedOrdersShiftRoles shiftRole,
) {
final String location = final String location = shiftRole.shift.location ?? shiftRole.shift.locationAddress ?? '';
shiftRole.shift.location ?? final String type = shiftRole.shift.order.orderType.stringValue;
shiftRole.shift.locationAddress ?? return ReorderItem(
''; orderId: shiftRole.shift.order.id,
final String type = shiftRole.shift.order.orderType.stringValue; title: '${shiftRole.role.name} - ${shiftRole.shift.title}',
return ReorderItem( location: location,
orderId: shiftRole.shift.order.id, hourlyRate: shiftRole.role.costPerHour,
title: '${shiftRole.role.name} - ${shiftRole.shift.title}', hours: shiftRole.hours ?? 0,
location: location, workers: shiftRole.count,
hourlyRate: shiftRole.role.costPerHour, type: type,
hours: shiftRole.hours ?? 0, );
workers: shiftRole.count, })
type: type, .toList();
); });
}).toList();
} catch (e) {
final String error = e.toString().toLowerCase();
if (error.contains('network') ||
error.contains('connection') ||
error.contains('unavailable') ||
error.contains('offline') ||
error.contains('socket') ||
error.contains('failed host lookup')) {
throw NetworkException(technicalMessage: 'Home reorders fetch failed: $e');
}
throw ServerException(technicalMessage: 'Home reorders fetch failed: $e');
}
}
fdc.Timestamp _toTimestamp(DateTime date) {
final DateTime utc = date.toUtc();
final int millis = utc.millisecondsSinceEpoch;
final int seconds = millis ~/ 1000;
final int nanos = (millis % 1000) * 1000000;
return fdc.Timestamp(nanos, seconds);
} }
} }