fix: gespeicherte Fahrzeugauswahl gegen Server-Fahrzeuge prüfen
Die Tagesauswahl liegt lokal pro Fahrer und wurde ungeprüft wiederhergestellt. Durch den alten Session-Fehler hatte 414 auf dem Gerät das Fahrzeug von 423 gespeichert — das erschien weiter, obwohl „Fahrzeug verwalten" (frisch vom Server) leer war. CarSelectBloc lädt beim Wiederherstellen die Fahrzeuge des Accounts vom Server und übernimmt die Auswahl nur, wenn das Fahrzeug dazugehört und aktiv ist; sonst wird sie verworfen und neu abgefragt. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -2,7 +2,7 @@ import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:hl_lieferservice/feature/car_selection/repository/car_selection_repository.dart';
|
||||
import 'package:hl_lieferservice/feature/cars/model/selection.dart';
|
||||
import 'package:hl_lieferservice/domain/entity/car.dart';
|
||||
import 'package:hl_lieferservice/domain/repository/cars_repository.dart';
|
||||
|
||||
import 'events.dart';
|
||||
import 'state.dart';
|
||||
@ -10,7 +10,12 @@ import 'state.dart';
|
||||
class CarSelectBloc extends Bloc<CarSelectEvent, CarSelectState> {
|
||||
final CarSelectionRepository repository;
|
||||
|
||||
CarSelectBloc({required this.repository}) : super(CarSelectInitial()) {
|
||||
/// Fahrzeuge des angemeldeten Accounts vom Server — gegen sie wird eine
|
||||
/// gespeicherte Tagesauswahl geprüft.
|
||||
final CarsRepository carsRepository;
|
||||
|
||||
CarSelectBloc({required this.repository, required this.carsRepository})
|
||||
: super(CarSelectInitial()) {
|
||||
on<CarSelectLoad>(_load);
|
||||
on<CarSelectConfirm>(_confirm);
|
||||
on<CarSelectChange>(_change);
|
||||
@ -36,23 +41,21 @@ class CarSelectBloc extends Bloc<CarSelectEvent, CarSelectState> {
|
||||
stored.date.day == today.day;
|
||||
|
||||
if (validForToday) {
|
||||
emit(
|
||||
CarSelectComplete(
|
||||
selectedCar: Car(
|
||||
id: stored.selectedCarId!,
|
||||
// accountId/active fließen aus der lokalen Selection
|
||||
// nicht durch — wir persistieren nur (id, plate) als
|
||||
// UI-Pointer. Die Tour-Logik holt die vollständigen
|
||||
// Car-Felder weiterhin aus dem CarsBloc-Listing.
|
||||
accountId: 0,
|
||||
plate: stored.selectedCarPlate!,
|
||||
active: true,
|
||||
),
|
||||
),
|
||||
);
|
||||
} else {
|
||||
emit(CarSelectRequired());
|
||||
// Die lokale Auswahl ist nur ein Zeiger. Sie gilt nur, wenn das
|
||||
// Fahrzeug laut Server (frisch geladen) zu diesem Account gehört
|
||||
// und aktiv ist — sonst z. B. ein Fahrzeug eines anderen Fahrers,
|
||||
// das auf diesem Gerät gewählt wurde.
|
||||
final ownCars = await carsRepository.listMine();
|
||||
final car = ownCars
|
||||
.where((c) => c.id == stored.selectedCarId && c.active)
|
||||
.firstOrNull;
|
||||
if (car != null) {
|
||||
emit(CarSelectComplete(selectedCar: car));
|
||||
return;
|
||||
}
|
||||
await repository.clearSelection(event.userId);
|
||||
}
|
||||
emit(CarSelectRequired());
|
||||
} catch (e, st) {
|
||||
debugPrint('CarSelectBloc._load failed: $e');
|
||||
debugPrint('Stacktrace: $st');
|
||||
|
||||
Reference in New Issue
Block a user