Clean-Arch-Schichten für Cars: - lib/domain/entity/car.dart: UUID-id, accountId (Personalnummer), plate, active. Pendant zum Backend-Schema. - lib/domain/repository/cars_repository.dart: Port — listMine, create, update. Keine teamId/personalnummer-Parameter, der Account fließt serverseitig aus dem JWT. - lib/data/mapper/car_mapper.dart: API-DTO (built_value) → Domain. - lib/data/repository/cars_repository_impl.dart: konkrete Impl via generierter CarsApi (dio), mit DioException → CarsRepositoryException- Übersetzung. Feature-Cars-Refactoring: - CarsBloc nimmt jetzt die Domain-Repository-Schnittstelle. Events: CarLoad/CarAdd/CarEdit/CarDeactivate (statt CarDelete). Keine teamId-Parameter mehr. Kein authBloc-Bezug, Session-Expiry läuft über den globalen Provider-Stream. - CarsState sealed mit CarsInitial/Loading/LoadingFailed/Loaded. - Pages: car_management_page, car_management, car_card, car_fail_page, car_selection_page komplett auf die neue Entity und Event-Signaturen. - Alte lib/feature/cars/service/cars_service.dart und lib/feature/cars/repository/cars_repository.dart gelöscht. CarSelectBloc + Storage: - CarSelection.selectedCarId von int? auf String? umgestellt. - CarSelectionRepository persistiert die UUID jetzt als String; defensive Migration für noch vorhandene int-Werte (alte Pre-Migration-Installations) verwirft den Wert leise und erzwingt Neuauswahl. Konsequenz-Cleanup im Tour-Code (Phase-D-Vorbereitung): - Delivery.carId String? statt int?. - Tour.hasUndeliveredLoadedArticles / getFinishedDeliveries auf String carId. - _selectedCarId / int? carId / int selectedCarId in DeliveryOverview, LoadingCustomerPage/OverviewPage, Home, DeliverySelection/SortPage, DeliveryInfo/List, CustomSortDialog, SortableDeliveryList auf String umgestellt. - TourRepository ersetzt int.parse(carId)/int.tryParse-Zuweisungen direkt durch String. - lib/model/car.dart wird zum Re-Export der neuen Domain-Entity, damit Legacy-Imports während Phase-D-Übergang weiter compilieren. DI: - app.dart: CarsBloc bekommt CarsRepositoryImpl(locator<HolzleitnerApi>()) statt der alten CarsRepository(service: CarService()). Build (flutter build apk --debug) durch, flutter analyze ohne errors.
126 lines
3.8 KiB
Dart
126 lines
3.8 KiB
Dart
import 'package:flutter_bloc/flutter_bloc.dart';
|
|
|
|
import 'package:hl_lieferservice/domain/entity/car.dart';
|
|
import 'package:hl_lieferservice/domain/repository/cars_repository.dart';
|
|
import 'package:hl_lieferservice/widget/operations/bloc/operation_bloc.dart';
|
|
import 'package:hl_lieferservice/widget/operations/bloc/operation_event.dart';
|
|
|
|
import 'cars_event.dart';
|
|
import 'cars_state.dart';
|
|
|
|
/// Bloc für die Fahrzeugverwaltung des angemeldeten Fahrers.
|
|
///
|
|
/// `personalnummer`/`teamId` wird **nicht mehr** in den Events
|
|
/// übergeben — der Account kommt serverseitig aus dem JWT.
|
|
/// Session-Expiry geht über den globalen Provider-Stream
|
|
/// (KeycloakOidcTokenProvider → AuthSessionExpired), nicht über
|
|
/// dieses Bloc.
|
|
class CarsBloc extends Bloc<CarEvents, CarsState> {
|
|
CarsBloc({required this.repository, required this.opBloc})
|
|
: super(const CarsInitial()) {
|
|
on<CarLoad>(_handleLoad);
|
|
on<CarAdd>(_handleAdd);
|
|
on<CarEdit>(_handleEdit);
|
|
on<CarDeactivate>(_handleDeactivate);
|
|
}
|
|
|
|
final CarsRepository repository;
|
|
final OperationBloc opBloc;
|
|
|
|
Future<void> _handleLoad(CarLoad event, Emitter<CarsState> emit) async {
|
|
if (state is CarsLoaded && !event.force) return;
|
|
|
|
emit(const CarsLoading());
|
|
try {
|
|
final cars = await repository.listMine();
|
|
emit(CarsLoaded(cars: cars));
|
|
} on CarsRepositoryException catch (e) {
|
|
emit(CarsLoadingFailed(message: e.message));
|
|
} catch (_) {
|
|
emit(const CarsLoadingFailed());
|
|
}
|
|
}
|
|
|
|
Future<void> _handleAdd(CarAdd event, Emitter<CarsState> emit) async {
|
|
final current = state;
|
|
opBloc.add(StartOperation());
|
|
try {
|
|
final created = await repository.create(plate: event.plate);
|
|
if (current is CarsLoaded) {
|
|
emit(current.copyWith(cars: [...current.cars, created]));
|
|
}
|
|
opBloc.add(FinishOperation(message: "Fahrzeug hinzugefügt"));
|
|
} on CarsRepositoryException catch (e) {
|
|
opBloc.add(FailOperation(message: e.message));
|
|
} catch (_) {
|
|
opBloc.add(
|
|
FailOperation(message: "Fehler beim Anlegen eines Fahrzeugs"),
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> _handleEdit(CarEdit event, Emitter<CarsState> emit) async {
|
|
final current = state;
|
|
opBloc.add(StartOperation());
|
|
try {
|
|
final updated = await repository.update(
|
|
carId: event.carId,
|
|
plate: event.plate,
|
|
active: event.active,
|
|
);
|
|
_emitReplaced(current, updated, emit);
|
|
opBloc.add(FinishOperation(message: "Fahrzeug aktualisiert"));
|
|
} on CarsRepositoryException catch (e) {
|
|
opBloc.add(FailOperation(message: e.message));
|
|
} catch (_) {
|
|
opBloc.add(
|
|
FailOperation(message: "Fehler beim Aktualisieren des Fahrzeugs"),
|
|
);
|
|
}
|
|
}
|
|
|
|
Future<void> _handleDeactivate(
|
|
CarDeactivate event,
|
|
Emitter<CarsState> emit,
|
|
) async {
|
|
final current = state;
|
|
opBloc.add(StartOperation());
|
|
try {
|
|
await repository.update(carId: event.carId, active: false);
|
|
if (current is CarsLoaded) {
|
|
// Inaktive Fahrzeuge raus aus der UI-Liste — die Liste zeigt
|
|
// nur aktive (Default).
|
|
emit(
|
|
current.copyWith(
|
|
cars: current.cars
|
|
.where((c) => c.id != event.carId)
|
|
.toList(growable: false),
|
|
),
|
|
);
|
|
}
|
|
opBloc.add(FinishOperation(message: "Fahrzeug deaktiviert"));
|
|
} on CarsRepositoryException catch (e) {
|
|
opBloc.add(FailOperation(message: e.message));
|
|
} catch (_) {
|
|
opBloc.add(
|
|
FailOperation(message: "Fehler beim Deaktivieren des Fahrzeugs"),
|
|
);
|
|
}
|
|
}
|
|
|
|
void _emitReplaced(
|
|
CarsState current,
|
|
Car updated,
|
|
Emitter<CarsState> emit,
|
|
) {
|
|
if (current is! CarsLoaded) return;
|
|
emit(
|
|
current.copyWith(
|
|
cars: current.cars
|
|
.map((c) => c.id == updated.id ? updated : c)
|
|
.toList(growable: false),
|
|
),
|
|
);
|
|
}
|
|
}
|