This commit is contained in:
Dennis Nemec
2026-04-28 13:03:09 +02:00
parent de8668c11a
commit 2470299a10
53 changed files with 2409 additions and 1433 deletions

View File

@ -1,4 +1,7 @@
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/widget/operations/bloc/operation_bloc.dart';
import 'package:hl_lieferservice/widget/operations/bloc/operation_event.dart';
@ -10,8 +13,9 @@ import 'cars_state.dart';
class CarsBloc extends Bloc<CarEvents, CarsState> {
CarsRepository repository;
OperationBloc opBloc;
AuthBloc authBloc;
CarsBloc({required this.repository, required this.opBloc})
CarsBloc({required this.repository, required this.opBloc, required this.authBloc})
: super(CarsInitial()) {
on<CarAdd>(_carAdd);
on<CarEdit>(_carEdit);
@ -19,12 +23,27 @@ class CarsBloc extends Bloc<CarEvents, CarsState> {
on<CarLoad>(_carLoad);
}
void _handleError(Object e, String fallbackMessage) {
if (e is UserUnauthorized) {
authBloc.add(SessionExpiredEvent());
} else {
opBloc.add(FailOperation(message: fallbackMessage));
}
}
Future<void> _carLoad(CarLoad event, Emitter<CarsState> emit) async {
// Skip the API call if cars are already loaded and no force-refresh requested.
if (state is CarsLoaded && !event.force) return;
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());
}
}
@ -33,7 +52,6 @@ class CarsBloc extends Bloc<CarEvents, CarsState> {
final currentState = state;
try {
opBloc.add(LoadOperation());
Car newCar = await repository.add(event.teamId, event.plate);
if (currentState is CarsLoaded) {
@ -46,7 +64,7 @@ class CarsBloc extends Bloc<CarEvents, CarsState> {
opBloc.add(FinishOperation(message: "Auto erfolgreich hinzugefügt"));
} catch (e) {
opBloc.add(FailOperation(message: "Fehler beim Hinzufügen eines Autos"));
_handleError(e, "Fehler beim Hinzufügen eines Autos");
}
}
@ -54,7 +72,6 @@ class CarsBloc extends Bloc<CarEvents, CarsState> {
final currentState = state;
try {
opBloc.add(LoadOperation());
await repository.edit(event.teamId, event.newCar);
if (currentState is CarsLoaded) {
@ -74,7 +91,7 @@ class CarsBloc extends Bloc<CarEvents, CarsState> {
opBloc.add(FinishOperation(message: "Auto erfolgreich editiert"));
} catch (e) {
opBloc.add(FailOperation(message: "Fehler beim Editieren des Autos"));
_handleError(e, "Fehler beim Editieren des Autos");
}
}
@ -82,7 +99,6 @@ class CarsBloc extends Bloc<CarEvents, CarsState> {
final currentState = state;
try {
opBloc.add(LoadOperation());
await repository.delete(event.carId, event.teamId);
if (currentState is CarsLoaded) {
@ -100,7 +116,7 @@ class CarsBloc extends Bloc<CarEvents, CarsState> {
opBloc.add(FinishOperation(message: "Auto erfolgreich gelöscht"));
} catch (e) {
opBloc.add(FailOperation(message: "Fehler beim Löschen des Autos"));
_handleError(e, "Fehler beim Löschen des Autos");
}
}
}

View File

@ -5,7 +5,11 @@ abstract class CarEvents {}
class CarLoad extends CarEvents {
String teamId;
CarLoad({required this.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});
}
class CarEdit extends CarEvents {

View File

@ -0,0 +1,14 @@
/*
Settings for the driver to select a car for the current workday.
*/
class CarSelection {
final DateTime date;
final int? selectedCarId;
final String? selectedCarPlate;
CarSelection({
required this.date,
this.selectedCarId,
this.selectedCarPlate,
});
}

View File

@ -5,6 +5,7 @@ import 'car_dialog.dart';
class CarCard extends StatelessWidget {
final Car car;
final bool isSelected;
final Function(Car car) onDelete;
final Function(Car car, String newName) onEdit;
@ -13,11 +14,20 @@ class CarCard extends StatelessWidget {
required this.car,
required this.onEdit,
required this.onDelete,
this.isSelected = false,
});
@override
Widget build(BuildContext context) {
final primary = Theme.of(context).primaryColor;
return Card(
color: isSelected ? primary.withValues(alpha: 0.08) : null,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
side: isSelected
? BorderSide(color: primary, width: 2)
: BorderSide.none,
),
child: Padding(
padding: const EdgeInsets.all(10),
child: Row(
@ -30,13 +40,30 @@ class CarCard extends StatelessWidget {
child: Icon(
Icons.local_shipping,
size: 32,
color: Theme.of(context).primaryColor,
color: primary,
),
),
Padding(
padding: const EdgeInsets.only(left: 10),
child: Text(car.plate),
child: Text(
car.plate,
style: TextStyle(
fontWeight: isSelected
? FontWeight.bold
: FontWeight.normal,
),
),
),
if (isSelected)
Padding(
padding: const EdgeInsets.only(left: 8),
child: Icon(
Icons.check_circle,
size: 20,
color: primary,
semanticLabel: 'Aktuell ausgewählt',
),
),
],
),

View File

@ -6,9 +6,11 @@ import 'package:flutter/material.dart';
class CarManagementOverview extends StatefulWidget {
final List<Car> cars;
final int? selectedCarId;
final Function(String plate) onAdd;
final Function(String id) onDelete;
final Function(String id, String plate) onEdit;
final Future<void> Function() onRefresh;
const CarManagementOverview({
super.key,
@ -16,6 +18,8 @@ class CarManagementOverview extends StatefulWidget {
required this.onDelete,
required this.onEdit,
required this.onAdd,
required this.onRefresh,
this.selectedCarId,
});
@override
@ -40,30 +44,14 @@ class _CarManagementOverviewState extends State<CarManagementOverview> {
widget.onEdit(car.id.toString(), newName);
}
Widget _buildCarOverview() {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(padding: const EdgeInsets.all(15), child: Text("Fahrzeuge", style: Theme.of(context).textTheme.headlineSmall),),
Expanded(child: Padding(
padding: const EdgeInsets.all(10),
child: widget.cars.isEmpty ? const Center(child: Text("keine Fahrzeuge vorhanden")) : ListView.builder(
itemBuilder:
(context, index) => CarCard(
car: widget.cars[index],
onEdit: _editCar,
onDelete: _removeCar,
),
itemCount: widget.cars.length,
),
))
],
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text("Fahrzeuge"),
backgroundColor: Theme.of(context).primaryColor,
foregroundColor: Theme.of(context).colorScheme.onSecondary,
),
floatingActionButton: FloatingActionButton(
onPressed: _addCar,
backgroundColor: Theme.of(context).primaryColor,
@ -72,7 +60,34 @@ class _CarManagementOverviewState extends State<CarManagementOverview> {
color: Theme.of(context).colorScheme.onSecondary,
),
),
body: _buildCarOverview(),
body: RefreshIndicator(
onRefresh: widget.onRefresh,
child: widget.cars.isEmpty
? ListView(
physics: const AlwaysScrollableScrollPhysics(),
padding: const EdgeInsets.all(10),
children: const [
SizedBox(
height: 200,
child: Center(child: Text("keine Fahrzeuge vorhanden")),
),
],
)
: ListView.builder(
physics: const AlwaysScrollableScrollPhysics(),
padding: const EdgeInsets.all(10),
itemCount: widget.cars.length,
itemBuilder: (context, index) {
final car = widget.cars[index];
return CarCard(
car: car,
isSelected: widget.selectedCarId == car.id,
onEdit: _editCar,
onDelete: _removeCar,
);
},
),
),
);
}
}

View File

@ -2,6 +2,8 @@ import 'package:flutter/material.dart';
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_state.dart';
import 'package:hl_lieferservice/feature/car_selection/bloc/bloc.dart';
import 'package:hl_lieferservice/feature/car_selection/bloc/state.dart';
import 'package:hl_lieferservice/feature/cars/bloc/cars_bloc.dart';
import 'package:hl_lieferservice/feature/cars/bloc/cars_event.dart';
import 'package:hl_lieferservice/feature/cars/bloc/cars_state.dart';
@ -37,7 +39,55 @@ class _CarManagementPageState extends State<CarManagementPage> {
);
}
Future<void> _refresh() async {
context.read<CarsBloc>().add(CarLoad(teamId: _authState.user.number, force: true));
}
void _remove(String id) {
final carId = int.parse(id);
final carSelectState = context.read<CarSelectBloc>().state;
if (carSelectState is CarSelectComplete &&
carSelectState.selectedCar.id == carId) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
"Dieses Fahrzeug ist aktuell ausgewählt und kann nicht gelöscht werden. "
"Bitte wähle zuerst ein anderes Fahrzeug aus.",
),
duration: Duration(seconds: 4),
),
);
return;
}
final tourState = context.read<TourBloc>().state;
if (tourState is! TourLoaded) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
"Die Tourdaten sind noch nicht verfügbar. "
"Bitte versuche es in Kürze erneut.",
),
duration: Duration(seconds: 4),
),
);
return;
}
if (tourState.tour.hasUndeliveredLoadedArticles(carId)) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
"Dieses Fahrzeug hat noch geladene Artikel, die nicht ausgeliefert wurden. "
"Bitte schließe alle offenen Lieferungen ab, bevor du das Fahrzeug löschst.",
),
duration: Duration(seconds: 4),
),
);
return;
}
context.read<CarsBloc>().add(
CarDelete(carId: id, teamId: _authState.user.number),
);
@ -68,11 +118,20 @@ class _CarManagementPageState extends State<CarManagementPage> {
}
if (state is CarsLoaded) {
return CarManagementOverview(
cars: state.cars,
onEdit: _edit,
onAdd: _add,
onDelete: _remove,
return BlocBuilder<CarSelectBloc, CarSelectState>(
builder: (context, selectState) {
final int? selectedCarId = selectState is CarSelectComplete
? selectState.selectedCar.id
: null;
return CarManagementOverview(
cars: state.cars,
selectedCarId: selectedCarId,
onEdit: _edit,
onAdd: _add,
onDelete: _remove,
onRefresh: _refresh,
);
},
);
}