feat: Simplify HomeRepositoryImpl and ClientHomeModule by using constructor shorthand
This commit is contained in:
@@ -23,11 +23,7 @@ class ClientHomeModule extends Module {
|
||||
@override
|
||||
void binds(Injector i) {
|
||||
// Repositories
|
||||
i.addLazySingleton<HomeRepositoryInterface>(
|
||||
() => HomeRepositoryImpl(
|
||||
ExampleConnector.instance,
|
||||
),
|
||||
);
|
||||
i.addLazySingleton<HomeRepositoryInterface>(HomeRepositoryImpl.new);
|
||||
|
||||
// UseCases
|
||||
i.addLazySingleton(GetDashboardDataUseCase.new);
|
||||
@@ -35,13 +31,7 @@ class ClientHomeModule extends Module {
|
||||
i.addLazySingleton(GetUserSessionDataUseCase.new);
|
||||
|
||||
// BLoCs
|
||||
i.add<ClientHomeBloc>(
|
||||
() => ClientHomeBloc(
|
||||
getDashboardDataUseCase: i.get<GetDashboardDataUseCase>(),
|
||||
getRecentReordersUseCase: i.get<GetRecentReordersUseCase>(),
|
||||
getUserSessionDataUseCase: i.get<GetUserSessionDataUseCase>(),
|
||||
),
|
||||
);
|
||||
i.add<ClientHomeBloc>(ClientHomeBloc.new);
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -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
|
||||
/// domain layer and the data source (in this case, a mock from data_connect).
|
||||
class HomeRepositoryImpl implements HomeRepositoryInterface {
|
||||
|
||||
/// Creates a [HomeRepositoryImpl].
|
||||
HomeRepositoryImpl(this._dataConnect);
|
||||
final dc.ExampleConnector _dataConnect;
|
||||
HomeRepositoryImpl(this._service);
|
||||
final dc.DataConnectService _service;
|
||||
|
||||
@override
|
||||
Future<HomeDashboardData> getDashboardData() async {
|
||||
try {
|
||||
final String? businessId = dc.ClientSessionStore.instance.session?.business?.id;
|
||||
if (businessId == null || businessId.isEmpty) {
|
||||
return const HomeDashboardData(
|
||||
weeklySpending: 0,
|
||||
next7DaysSpending: 0,
|
||||
weeklyShifts: 0,
|
||||
next7DaysScheduled: 0,
|
||||
totalNeeded: 0,
|
||||
totalFilled: 0,
|
||||
);
|
||||
}
|
||||
return _service.run(() async {
|
||||
final String businessId = await _service.getBusinessId();
|
||||
|
||||
final DateTime now = DateTime.now();
|
||||
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 weekRangeEnd =
|
||||
DateTime(monday.year, monday.month, monday.day + 13, 23, 59, 59, 999);
|
||||
final fdc.QueryResult<
|
||||
dc.GetCompletedShiftsByBusinessIdData,
|
||||
dc.GetCompletedShiftsByBusinessIdVariables> completedResult =
|
||||
await _dataConnect
|
||||
final fdc.QueryResult<dc.GetCompletedShiftsByBusinessIdData,
|
||||
dc.GetCompletedShiftsByBusinessIdVariables> completedResult =
|
||||
await _service.connector
|
||||
.getCompletedShiftsByBusinessId(
|
||||
businessId: businessId,
|
||||
dateFrom: _toTimestamp(weekRangeStart),
|
||||
dateTo: _toTimestamp(weekRangeEnd),
|
||||
dateFrom: _service.toTimestamp(weekRangeStart),
|
||||
dateTo: _service.toTimestamp(weekRangeEnd),
|
||||
)
|
||||
.execute();
|
||||
|
||||
@@ -50,8 +38,7 @@ class HomeRepositoryImpl implements HomeRepositoryInterface {
|
||||
double next7DaysSpending = 0.0;
|
||||
int weeklyShifts = 0;
|
||||
int next7DaysScheduled = 0;
|
||||
for (final dc.GetCompletedShiftsByBusinessIdShifts shift
|
||||
in completedResult.data.shifts) {
|
||||
for (final dc.GetCompletedShiftsByBusinessIdShifts shift in completedResult.data.shifts) {
|
||||
final DateTime? shiftDate = shift.date?.toDateTime();
|
||||
if (shiftDate == null) {
|
||||
continue;
|
||||
@@ -73,14 +60,13 @@ class HomeRepositoryImpl implements HomeRepositoryInterface {
|
||||
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 fdc.QueryResult<
|
||||
dc.ListShiftRolesByBusinessAndDateRangeData,
|
||||
dc.ListShiftRolesByBusinessAndDateRangeVariables> result =
|
||||
await _dataConnect
|
||||
final fdc.QueryResult<dc.ListShiftRolesByBusinessAndDateRangeData,
|
||||
dc.ListShiftRolesByBusinessAndDateRangeVariables> result =
|
||||
await _service.connector
|
||||
.listShiftRolesByBusinessAndDateRange(
|
||||
businessId: businessId,
|
||||
start: _toTimestamp(start),
|
||||
end: _toTimestamp(end),
|
||||
start: _service.toTimestamp(start),
|
||||
end: _service.toTimestamp(end),
|
||||
)
|
||||
.execute();
|
||||
|
||||
@@ -100,18 +86,7 @@ class HomeRepositoryImpl implements HomeRepositoryInterface {
|
||||
totalNeeded: totalNeeded,
|
||||
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
|
||||
@@ -125,64 +100,41 @@ class HomeRepositoryImpl implements HomeRepositoryInterface {
|
||||
|
||||
@override
|
||||
Future<List<ReorderItem>> getRecentReorders() async {
|
||||
try {
|
||||
final String? businessId = dc.ClientSessionStore.instance.session?.business?.id;
|
||||
if (businessId == null || businessId.isEmpty) {
|
||||
return const <ReorderItem>[];
|
||||
}
|
||||
return _service.run(() async {
|
||||
final String businessId = await _service.getBusinessId();
|
||||
|
||||
final DateTime now = DateTime.now();
|
||||
final DateTime start = now.subtract(const Duration(days: 30));
|
||||
final fdc.Timestamp startTimestamp = _toTimestamp(start);
|
||||
final fdc.Timestamp endTimestamp = _toTimestamp(now);
|
||||
final fdc.Timestamp startTimestamp = _service.toTimestamp(start);
|
||||
final fdc.Timestamp endTimestamp = _service.toTimestamp(now);
|
||||
|
||||
final fdc.QueryResult<
|
||||
dc.ListShiftRolesByBusinessDateRangeCompletedOrdersData,
|
||||
dc.ListShiftRolesByBusinessDateRangeCompletedOrdersVariables> result =
|
||||
await _dataConnect.listShiftRolesByBusinessDateRangeCompletedOrders(
|
||||
businessId: businessId,
|
||||
start: startTimestamp,
|
||||
end: endTimestamp,
|
||||
).execute();
|
||||
final fdc.QueryResult<dc.ListShiftRolesByBusinessDateRangeCompletedOrdersData,
|
||||
dc.ListShiftRolesByBusinessDateRangeCompletedOrdersVariables> result =
|
||||
await _service.connector
|
||||
.listShiftRolesByBusinessDateRangeCompletedOrders(
|
||||
businessId: businessId,
|
||||
start: startTimestamp,
|
||||
end: endTimestamp,
|
||||
)
|
||||
.execute();
|
||||
|
||||
return result.data.shiftRoles.map((
|
||||
dc.ListShiftRolesByBusinessDateRangeCompletedOrdersShiftRoles shiftRole,
|
||||
) {
|
||||
|
||||
final String location =
|
||||
shiftRole.shift.location ??
|
||||
shiftRole.shift.locationAddress ??
|
||||
'';
|
||||
final String type = shiftRole.shift.order.orderType.stringValue;
|
||||
return ReorderItem(
|
||||
orderId: shiftRole.shift.order.id,
|
||||
title: '${shiftRole.role.name} - ${shiftRole.shift.title}',
|
||||
location: location,
|
||||
hourlyRate: shiftRole.role.costPerHour,
|
||||
hours: shiftRole.hours ?? 0,
|
||||
workers: shiftRole.count,
|
||||
type: type,
|
||||
);
|
||||
}).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);
|
||||
return result.data.shiftRoles
|
||||
.map((
|
||||
dc.ListShiftRolesByBusinessDateRangeCompletedOrdersShiftRoles shiftRole,
|
||||
) {
|
||||
final String location = shiftRole.shift.location ?? shiftRole.shift.locationAddress ?? '';
|
||||
final String type = shiftRole.shift.order.orderType.stringValue;
|
||||
return ReorderItem(
|
||||
orderId: shiftRole.shift.order.id,
|
||||
title: '${shiftRole.role.name} - ${shiftRole.shift.title}',
|
||||
location: location,
|
||||
hourlyRate: shiftRole.role.costPerHour,
|
||||
hours: shiftRole.hours ?? 0,
|
||||
workers: shiftRole.count,
|
||||
type: type,
|
||||
);
|
||||
})
|
||||
.toList();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user