Add explicit types and improve type safety across codebase
This commit adds explicit type annotations to variables, function parameters, and return types throughout the codebase, particularly in widget trees, Bloc logic, and repository implementations. The changes improve code readability, maintainability, and type safety, and align with Dart best practices. No business logic was changed.
This commit is contained in:
@@ -52,18 +52,18 @@ class ClientCreateOrderRepositoryImpl
|
||||
|
||||
@override
|
||||
Future<void> createOneTimeOrder(domain.OneTimeOrder order) async {
|
||||
final businessId = dc.ClientSessionStore.instance.session?.business?.id;
|
||||
final String? businessId = dc.ClientSessionStore.instance.session?.business?.id;
|
||||
if (businessId == null || businessId.isEmpty) {
|
||||
await _firebaseAuth.signOut();
|
||||
throw Exception('Business is missing. Please sign in again.');
|
||||
}
|
||||
final vendorId = order.vendorId;
|
||||
final String? vendorId = order.vendorId;
|
||||
if (vendorId == null || vendorId.isEmpty) {
|
||||
throw Exception('Vendor is missing.');
|
||||
}
|
||||
|
||||
final orderTimestamp = _toTimestamp(order.date);
|
||||
final orderResult = await _dataConnect
|
||||
final fdc.Timestamp orderTimestamp = _toTimestamp(order.date);
|
||||
final fdc.OperationResult<dc.CreateOrderData, dc.CreateOrderVariables> orderResult = await _dataConnect
|
||||
.createOrder(businessId: businessId, orderType: dc.OrderType.ONE_TIME)
|
||||
.vendorId(vendorId)
|
||||
.location(order.location)
|
||||
@@ -71,19 +71,19 @@ class ClientCreateOrderRepositoryImpl
|
||||
.date(orderTimestamp)
|
||||
.execute();
|
||||
|
||||
final orderId = orderResult.data?.order_insert.id;
|
||||
final String? orderId = orderResult.data?.order_insert.id;
|
||||
if (orderId == null) {
|
||||
throw Exception('Order creation failed.');
|
||||
}
|
||||
|
||||
final workersNeeded = order.positions.fold<int>(
|
||||
final int workersNeeded = order.positions.fold<int>(
|
||||
0,
|
||||
(sum, position) => sum + position.count,
|
||||
(int sum, domain.OneTimeOrderPosition position) => sum + position.count,
|
||||
);
|
||||
final shiftTitle = 'Shift 1 ${_formatDate(order.date)}';
|
||||
final shiftCost = _calculateShiftCost(order);
|
||||
final String shiftTitle = 'Shift 1 ${_formatDate(order.date)}';
|
||||
final double shiftCost = _calculateShiftCost(order);
|
||||
|
||||
final shiftResult = await _dataConnect
|
||||
final fdc.OperationResult<dc.CreateShiftData, dc.CreateShiftVariables> shiftResult = await _dataConnect
|
||||
.createShift(title: shiftTitle, orderId: orderId)
|
||||
.date(orderTimestamp)
|
||||
.location(order.location)
|
||||
@@ -95,19 +95,19 @@ class ClientCreateOrderRepositoryImpl
|
||||
.cost(shiftCost)
|
||||
.execute();
|
||||
|
||||
final shiftId = shiftResult.data?.shift_insert.id;
|
||||
final String? shiftId = shiftResult.data?.shift_insert.id;
|
||||
if (shiftId == null) {
|
||||
throw Exception('Shift creation failed.');
|
||||
}
|
||||
|
||||
for (final position in order.positions) {
|
||||
final start = _parseTime(order.date, position.startTime);
|
||||
final end = _parseTime(order.date, position.endTime);
|
||||
final normalizedEnd =
|
||||
for (final domain.OneTimeOrderPosition position in order.positions) {
|
||||
final DateTime start = _parseTime(order.date, position.startTime);
|
||||
final DateTime end = _parseTime(order.date, position.endTime);
|
||||
final DateTime normalizedEnd =
|
||||
end.isBefore(start) ? end.add(const Duration(days: 1)) : end;
|
||||
final hours = normalizedEnd.difference(start).inMinutes / 60.0;
|
||||
final rate = order.roleRates[position.role] ?? 0;
|
||||
final totalValue = rate * hours * position.count;
|
||||
final double hours = normalizedEnd.difference(start).inMinutes / 60.0;
|
||||
final double rate = order.roleRates[position.role] ?? 0;
|
||||
final double totalValue = rate * hours * position.count;
|
||||
|
||||
await _dataConnect
|
||||
.createShiftRole(
|
||||
@@ -136,13 +136,13 @@ class ClientCreateOrderRepositoryImpl
|
||||
|
||||
double _calculateShiftCost(domain.OneTimeOrder order) {
|
||||
double total = 0;
|
||||
for (final position in order.positions) {
|
||||
final start = _parseTime(order.date, position.startTime);
|
||||
final end = _parseTime(order.date, position.endTime);
|
||||
final normalizedEnd =
|
||||
for (final domain.OneTimeOrderPosition position in order.positions) {
|
||||
final DateTime start = _parseTime(order.date, position.startTime);
|
||||
final DateTime end = _parseTime(order.date, position.endTime);
|
||||
final DateTime normalizedEnd =
|
||||
end.isBefore(start) ? end.add(const Duration(days: 1)) : end;
|
||||
final hours = normalizedEnd.difference(start).inMinutes / 60.0;
|
||||
final rate = order.roleRates[position.role] ?? 0;
|
||||
final double hours = normalizedEnd.difference(start).inMinutes / 60.0;
|
||||
final double rate = order.roleRates[position.role] ?? 0;
|
||||
total += rate * hours * position.count;
|
||||
}
|
||||
return total;
|
||||
@@ -170,16 +170,16 @@ class ClientCreateOrderRepositoryImpl
|
||||
}
|
||||
|
||||
fdc.Timestamp _toTimestamp(DateTime dateTime) {
|
||||
final utc = dateTime.toUtc();
|
||||
final seconds = utc.millisecondsSinceEpoch ~/ 1000;
|
||||
final nanoseconds = (utc.microsecondsSinceEpoch % 1000000) * 1000;
|
||||
final DateTime utc = dateTime.toUtc();
|
||||
final int seconds = utc.millisecondsSinceEpoch ~/ 1000;
|
||||
final int nanoseconds = (utc.microsecondsSinceEpoch % 1000000) * 1000;
|
||||
return fdc.Timestamp(nanoseconds, seconds);
|
||||
}
|
||||
|
||||
String _formatDate(DateTime dateTime) {
|
||||
final year = dateTime.year.toString().padLeft(4, '0');
|
||||
final month = dateTime.month.toString().padLeft(2, '0');
|
||||
final day = dateTime.day.toString().padLeft(2, '0');
|
||||
final String year = dateTime.year.toString().padLeft(4, '0');
|
||||
final String month = dateTime.month.toString().padLeft(2, '0');
|
||||
final String day = dateTime.day.toString().padLeft(2, '0');
|
||||
return '$year-$month-$day';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import 'package:firebase_data_connect/src/core/ref.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:krow_data_connect/krow_data_connect.dart' as dc;
|
||||
import 'package:krow_domain/krow_domain.dart';
|
||||
@@ -26,10 +27,10 @@ class OneTimeOrderBloc extends Bloc<OneTimeOrderEvent, OneTimeOrderState> {
|
||||
|
||||
Future<void> _loadVendors() async {
|
||||
try {
|
||||
final result = await _dataConnect.listVendors().execute();
|
||||
final vendors = result.data.vendors
|
||||
final QueryResult<dc.ListVendorsData, void> result = await _dataConnect.listVendors().execute();
|
||||
final List<Vendor> vendors = result.data.vendors
|
||||
.map(
|
||||
(vendor) => Vendor(
|
||||
(dc.ListVendorsVendors vendor) => Vendor(
|
||||
id: vendor.id,
|
||||
name: vendor.companyName,
|
||||
rates: const <String, double>{},
|
||||
@@ -44,12 +45,12 @@ class OneTimeOrderBloc extends Bloc<OneTimeOrderEvent, OneTimeOrderState> {
|
||||
|
||||
Future<void> _loadRolesForVendor(String vendorId) async {
|
||||
try {
|
||||
final result = await _dataConnect.listRolesByVendorId(
|
||||
final QueryResult<dc.ListRolesByVendorIdData, dc.ListRolesByVendorIdVariables> result = await _dataConnect.listRolesByVendorId(
|
||||
vendorId: vendorId,
|
||||
).execute();
|
||||
final roles = result.data.roles
|
||||
final List<OneTimeOrderRoleOption> roles = result.data.roles
|
||||
.map(
|
||||
(role) => OneTimeOrderRoleOption(
|
||||
(dc.ListRolesByVendorIdRoles role) => OneTimeOrderRoleOption(
|
||||
id: role.id,
|
||||
name: role.name,
|
||||
costPerHour: role.costPerHour,
|
||||
@@ -146,7 +147,7 @@ class OneTimeOrderBloc extends Bloc<OneTimeOrderEvent, OneTimeOrderState> {
|
||||
emit(state.copyWith(status: OneTimeOrderStatus.loading));
|
||||
try {
|
||||
final Map<String, double> roleRates = <String, double>{
|
||||
for (final role in state.roles) role.id: role.costPerHour,
|
||||
for (final OneTimeOrderRoleOption role in state.roles) role.id: role.costPerHour,
|
||||
};
|
||||
final OneTimeOrder order = OneTimeOrder(
|
||||
date: state.date,
|
||||
|
||||
@@ -301,9 +301,9 @@ class OneTimeOrderPositionCard extends StatelessWidget {
|
||||
}
|
||||
|
||||
List<DropdownMenuItem<String>> _buildRoleItems() {
|
||||
final items = roles
|
||||
final List<DropdownMenuItem<String>> items = roles
|
||||
.map(
|
||||
(role) => DropdownMenuItem<String>(
|
||||
(OneTimeOrderRoleOption role) => DropdownMenuItem<String>(
|
||||
value: role.id,
|
||||
child: Text(
|
||||
'${role.name} - \$${role.costPerHour.toStringAsFixed(0)}',
|
||||
@@ -313,7 +313,7 @@ class OneTimeOrderPositionCard extends StatelessWidget {
|
||||
)
|
||||
.toList();
|
||||
|
||||
final hasSelected = roles.any((role) => role.id == position.role);
|
||||
final bool hasSelected = roles.any((OneTimeOrderRoleOption role) => role.id == position.role);
|
||||
if (position.role.isNotEmpty && !hasSelected) {
|
||||
items.add(
|
||||
DropdownMenuItem<String>(
|
||||
|
||||
Reference in New Issue
Block a user