Phase C+D-1: Cars-Domain auf Rust-Backend umgestellt

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.
This commit is contained in:
Dennis Nemec
2026-05-15 11:55:24 +02:00
parent e369d1ceb2
commit 3ecbc82885
34 changed files with 477 additions and 456 deletions

View File

@ -1,125 +1,125 @@
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:hl_lieferservice/feature/authentication/bloc/auth_bloc.dart';
import 'package:hl_lieferservice/feature/authentication/bloc/auth_event.dart';
import 'package:hl_lieferservice/feature/authentication/exceptions.dart';
import 'package:hl_lieferservice/feature/cars/repository/cars_repository.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 '../../../model/car.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> {
CarsRepository repository;
OperationBloc opBloc;
AuthBloc authBloc;
CarsBloc({required this.repository, required this.opBloc, required this.authBloc})
: super(CarsInitial()) {
on<CarAdd>(_carAdd);
on<CarEdit>(_carEdit);
on<CarDelete>(_carDelete);
on<CarLoad>(_carLoad);
CarsBloc({required this.repository, required this.opBloc})
: super(const CarsInitial()) {
on<CarLoad>(_handleLoad);
on<CarAdd>(_handleAdd);
on<CarEdit>(_handleEdit);
on<CarDeactivate>(_handleDeactivate);
}
void _handleError(Object e, String fallbackMessage) {
if (e is UserUnauthorized) {
authBloc.add(SessionExpiredEvent());
} else {
opBloc.add(FailOperation(message: fallbackMessage));
}
}
final CarsRepository repository;
final OperationBloc opBloc;
Future<void> _carLoad(CarLoad event, Emitter<CarsState> emit) async {
// Skip the API call if cars are already loaded and no force-refresh requested.
Future<void> _handleLoad(CarLoad event, Emitter<CarsState> emit) async {
if (state is CarsLoaded && !event.force) return;
emit(const CarsLoading());
try {
emit(CarsLoading());
List<Car> cars = await repository.getAll(event.teamId);
emit(CarsLoaded(cars: cars, teamId: event.teamId));
} catch (e) {
if (e is UserUnauthorized) {
authBloc.add(SessionExpiredEvent());
return;
}
emit(CarsLoadingFailed());
final cars = await repository.listMine();
emit(CarsLoaded(cars: cars));
} on CarsRepositoryException catch (e) {
emit(CarsLoadingFailed(message: e.message));
} catch (_) {
emit(const CarsLoadingFailed());
}
}
Future<void> _carAdd(CarAdd event, Emitter<CarsState> emit) async {
final currentState = state;
Future<void> _handleAdd(CarAdd event, Emitter<CarsState> emit) async {
final current = state;
opBloc.add(StartOperation());
try {
Car newCar = await repository.add(event.teamId, event.plate);
if (currentState is CarsLoaded) {
emit(
currentState.copyWith(
cars: List<Car>.from(currentState.cars)..add(newCar),
),
);
final created = await repository.create(plate: event.plate);
if (current is CarsLoaded) {
emit(current.copyWith(cars: [...current.cars, created]));
}
opBloc.add(FinishOperation(message: "Auto erfolgreich hinzugefügt"));
} catch (e) {
_handleError(e, "Fehler beim Hinzufügen eines Autos");
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> _carEdit(CarEdit event, Emitter<CarsState> emit) async {
final currentState = state;
Future<void> _handleEdit(CarEdit event, Emitter<CarsState> emit) async {
final current = state;
opBloc.add(StartOperation());
try {
await repository.edit(event.teamId, event.newCar);
if (currentState is CarsLoaded) {
emit(
currentState.copyWith(
cars:
List<Car>.from(currentState.cars).map((car) {
if (car.id == event.newCar.id) {
return event.newCar;
}
return car;
}).toList(),
),
);
}
opBloc.add(FinishOperation(message: "Auto erfolgreich editiert"));
} catch (e) {
_handleError(e, "Fehler beim Editieren des Autos");
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> _carDelete(CarDelete event, Emitter<CarsState> emit) async {
final currentState = state;
Future<void> _handleDeactivate(
CarDeactivate event,
Emitter<CarsState> emit,
) async {
final current = state;
opBloc.add(StartOperation());
try {
await repository.delete(event.carId, event.teamId);
if (currentState is CarsLoaded) {
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(
CarsLoaded(
cars: [
...currentState.cars.where(
(car) => car.id != int.parse(event.carId),
),
],
teamId: currentState.teamId,
current.copyWith(
cars: current.cars
.where((c) => c.id != event.carId)
.toList(growable: false),
),
);
}
opBloc.add(FinishOperation(message: "Auto erfolgreich gelöscht"));
} catch (e) {
_handleError(e, "Fehler beim Löschen des Autos");
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),
),
);
}
}

View File

@ -1,34 +1,35 @@
import '../../../model/car.dart';
abstract class CarEvents {}
class CarLoad extends CarEvents {
String teamId;
/// If [force] is true the API is always called, bypassing the cache.
/// Use this for pull-to-refresh. Defaults to false.
bool force;
CarLoad({required this.teamId, this.force = false});
/// Events des CarsBloc nach der Backend-Migration.
///
/// Anders als zuvor brauchen die Events **keinen** `teamId`/
/// `personalnummer`-Parameter — der Server leitet den Account aus dem
/// JWT ab. Statt `CarDelete` gibt's nur noch `CarDeactivate` (Soft-
/// Delete via `active=false`).
sealed class CarEvents {
const CarEvents();
}
class CarEdit extends CarEvents {
Car newCar;
String teamId;
CarEdit({required this.newCar, required this.teamId});
class CarLoad extends CarEvents {
/// Pull-to-Refresh setzt `force=true` und umgeht den Cache.
final bool force;
const CarLoad({this.force = false});
}
class CarAdd extends CarEvents {
String teamId;
String plate;
CarAdd({required this.teamId, required this.plate});
final String plate;
const CarAdd({required this.plate});
}
class CarDelete extends CarEvents {
String carId;
String teamId;
/// PATCH: Kennzeichen ändern und/oder aktivieren-Status setzen.
class CarEdit extends CarEvents {
final String carId;
final String? plate;
final bool? active;
const CarEdit({required this.carId, this.plate, this.active});
}
CarDelete({required this.carId, required this.teamId});
}
/// Soft-Delete (setzt `active=false`). Hartes Löschen ist nicht
/// vorgesehen — Fahrzeuge bleiben als FK-Anker für Audit-Einträge.
class CarDeactivate extends CarEvents {
final String carId;
const CarDeactivate({required this.carId});
}

View File

@ -1,38 +1,26 @@
import 'package:hl_lieferservice/model/car.dart';
import 'package:hl_lieferservice/domain/entity/car.dart';
abstract class CarsState {}
class CarsInitial extends CarsState {}
class CarsLoading extends CarsState {}
class CarsLoadingFailed extends CarsState {}
class CarAdded extends CarsState {
Car car;
CarAdded({required this.car});
sealed class CarsState {
const CarsState();
}
class CarDeleted extends CarsState {
String plate;
CarDeleted({required this.plate});
class CarsInitial extends CarsState {
const CarsInitial();
}
class CarEdited extends CarsState {
Car car;
class CarsLoading extends CarsState {
const CarsLoading();
}
CarEdited({required this.car});
class CarsLoadingFailed extends CarsState {
const CarsLoadingFailed({this.message});
final String? message;
}
class CarsLoaded extends CarsState {
List<Car> cars;
String teamId;
const CarsLoaded({required this.cars});
final List<Car> cars;
CarsLoaded({required this.cars, required this.teamId});
CarsLoaded copyWith({List<Car>? cars, String? teamId}) {
return CarsLoaded(cars: cars ?? this.cars, teamId: teamId ?? this.teamId);
}
CarsLoaded copyWith({List<Car>? cars}) =>
CarsLoaded(cars: cars ?? this.cars);
}